3 Set Example

ORIGIN '~beta/containers/sets';
--- program:descriptor ---
(* This demo program illustrates the usage of the set pattern.  The first part
 * of the demo illustrates inserting elements from the sets, and
 * the last part illustrates the union and symDiff operations.
 * 
 * At the end of this file, a copy of the output of this program is given
 *)
(# intSet: set
     (* intSet is a set containing integerObjects *)
     (# element::< integerObject #);
   
   intSet1, intSet2, intSet3: @intSet;
   i: [10]^integerObject;
   
do (* initializing the integerObjects *)
   (for int:10 repeat &integerObject[]->i[int][]; int->i[int] for);
   
   (* initializating intSet1 and inserting integerObjects into it *)
   intSet1.init;
   'intSet1.capacity: '->puttext; intSet1.capacity->putInt; newline;
   (for int:4 repeat '*'->put;
      i[int][]->intSet1.insert
   for);
   (* printing the size of intSet1 *) '+'->put;
   'intSet1.size: '->puttext; intSet1.size->putInt; newline;
   (* printing intSet1 *)
   'intSet1.elements: '->puttext;
   intSet1.scan(# do current->putint; ','->put #); newline;

   (* initializating intSet2 and inserting integerObjects into it *)
   intSet2.init;
   (for int:4 repeat 
      i[int+3][]->intSet2.insert
   for);
   (* printing intSet2 *)
   'intSet2.elements: '->puttext;
   intSet2.scan(# do current->putint; ','->put #); newline;
   
   (* illustrating the use of the union, diff, sect and symDiff operations *)
   'intSet2->intSet3; intSet1->intSet3.union: '->puttext;
   intSet2->intSet3; intSet1->intSet3.union;
   intSet3.scan(# do current->putint; ','->put #); newline;

   'intSet2->intSet3; intSet1->intSet3.symDiff: '->puttext;
   intSet2->intSet3; intSet1->intSet3.symDiff;
   intSet3.scan(# do current->putint; ','->put #); newline;

   (* illustrating finding some integerObject in insSets *)
   'intSet3.find(# predicate::< (# "elm=7" #) #): '->puttext;
   intSet3.find(# predicate::< (# do (current=7)->value #)
               do current->putint
               #); newline;
   '7->intSet3.has: '->puttext;
   (if i[7][]->intSet3.has
    //true then 'yes'->putline
    else 'no'->putline
   if);
   '10->intSet3.has: '->puttext;
   (if i[10][]->intSet3.has
    //true then 'yes'->putline
    else 'no'->putline
   if);
   
   (*********** OUTPUT ***************
    * intSet1.capacity: -1
    * intSet1.size: 4
    * intSet1.elements: 4,3,2,1,
    * intSet2.elements: 7,6,5,4,
    * intSet2->intSet3; intSet1->intSet3.union: 1,2,3,7,6,5,4,
    * intSet2->intSet3; intSet1->intSet3.symDiff: 1,2,3,7,6,5,
    * intSet3.find(# predicate::< (# "elm=7" #) #): 7
    * 7->intSet3.has: yes
    * 10->intSet3.has: no
    **********************************)
#)


Container Libraries - Reference Manual
© 1992-2002 Mjølner Informatics
[Modified: Thursday October 19th 2000 at 12:51]