home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MDBS.ZIP / COLLECTI.CLS < prev    next >
Text File  |  1990-01-15  |  6KB  |  238 lines

  1. /*
  2. **  (C) Copyright 1989, 1990
  3. **  Micro Data Base Systems Inc.
  4. **  Lafayette, Indiana
  5. */
  6. /* Construct a new instance of a kind of Collection. */
  7. method CollectionClass::new(self, aSize)
  8. {
  9.         return indexedNew(self, aSize);
  10. }
  11.  
  12. /* Add the initialization message on to the basic indexedNew message. */
  13. method CollectionClass::indexedNew(self, aSize)
  14. {
  15.         return init(basicIndexedNew(self, aSize));
  16. }
  17.  
  18. /* Basic initialization message for collection classes. */
  19. method Collection::init(self)
  20. {
  21.         return self;
  22. }
  23.  
  24. /* Answer the size of the collection in elements. */
  25. method Collection::sizeOf(self)
  26. {
  27.     return basicSizeOf(self);
  28. }
  29.  
  30. /* Add all the members of one collection to another. */
  31. method Collection::addAll(self, aCollection)
  32. {
  33.         do(aCollection, ::(anElement){
  34.                  add(self, anElement);
  35.         });
  36.         return aCollection;
  37. }
  38.  
  39. /* Remove all the members of one collection from another. */
  40. method Collection::removeAll(self, aCollection)
  41. {
  42.         reverseDo(aCollection, ::(anElement){
  43.                 remove(self, anElement);
  44.         });
  45.         return aCollection;
  46. }
  47.  
  48. /* Determine if the collection is empty or not. */
  49. method Collection::isEmpty(self)
  50. {
  51.         do(self, ::( aMember ){
  52.                 return false;
  53.         });
  54.         return true;
  55. }
  56.  
  57. /* Determine if the collection contains the argument as a member. */
  58. method Collection::includes(self, anObject)
  59. {
  60.         do(self, ::(aMember){
  61.                 if (anObject == aMember) {
  62.                         return true;
  63.                 }
  64.         });
  65.         return false;
  66. }
  67.  
  68. /* Count the occurences of an object in the collection. */
  69. method Collection::occurrencesOf(self, anObject)
  70. {
  71.         local  tally ;
  72.  
  73.         tally = 0;
  74.         do(self, ::(aMember){
  75.                 if (anObject == aMember) {
  76.                         tally = tally + 1;
  77.                 }
  78.         });
  79.         return tally;
  80. }
  81.  
  82. /* Collect the members of one collection into another. */
  83. method Collection::collect(self, aBlock)
  84. {
  85.         local  newCollection ;
  86.  
  87.         newCollection = new(speciesOf(self), sizeOf(self));
  88.         do(self, ::(aMember){
  89.         add(newCollection, valueOf(aBlock, aMember));
  90.         });
  91.         return newCollection;
  92. }
  93.  
  94. /* Select a subset of a collection to add to another. */
  95. method Collection::select(self, aBlock)
  96. {
  97.         local  newCollection ;
  98.  
  99.         newCollection = new(speciesOf(self), sizeOf(self));
  100.         do(self, ::(aMember){
  101.         if (valueOf(aBlock, aMember)) {
  102.                         add(newCollection, aMember);
  103.                 }
  104.         });
  105.         return newCollection;
  106. }
  107.  
  108. /* Reject unwanted members from a collection. */
  109. method Collection::reject(self, aBlock)
  110. {
  111.         return select(self, ::(aMember){
  112.         !valueOf(aBlock, aMember);
  113.         });
  114. }
  115.  
  116. /* Answer the index of the member in the collection. */
  117. method Collection::find(self, anItem)
  118. {
  119.         local  index ;
  120.  
  121.         index = 0;
  122.         do(self, ::( aMember ){
  123.                 if (aMember == anItem) {
  124.                         return index;
  125.                 }
  126.                 index = index + 1;
  127.         });
  128.         return nil;
  129. }
  130.  
  131. /* Convert the collection into a Set of the elements. */
  132. method Collection::asSet(self)
  133. {
  134.         local  aSet ;
  135.  
  136.     aSet = new(Set, sizeOf(self) + 1);
  137.         addAll(aSet, self);
  138.         return aSet;
  139. }
  140.  
  141. /* Convert the collection into an OrderedCollection of the elements. */
  142. method Collection::asOrderedCollection(self)
  143. {
  144.         local  aCollection ;
  145.  
  146.         aCollection = new(OrderedCollection, sizeOf(self));
  147.         addAll(aCollection, self);
  148.         return aCollection;
  149. }
  150.  
  151. /* Convert the collection into a SortedCollection of the elements. */
  152. method Collection::asSortedCollection(self)
  153. {
  154.         local  aCollection ;
  155.  
  156.     aCollection = new(SortedCollection, sizeOf(self));
  157.         addAll(aCollection, self);
  158.         return aCollection;
  159. }
  160.  
  161. /* Convert the collection into a Array of the elements. */
  162. method Collection::asArray(self)
  163. {
  164.         local  array, i ;
  165.  
  166.     array = new(Array, sizeOf(self));
  167.         i     = 0;
  168.         do(self, ::(member){
  169.                 array[i] = member;
  170.                 i        = i + 1;
  171.         });
  172.         return array;
  173. }
  174.  
  175. /* Convert the collection into a compact string form. */
  176. method Collection::asString(self)
  177. {
  178.         local  aString, tooMany, i ;
  179.  
  180.         aString = classNameOf(classOf(self)) + "(";
  181.         tooMany = maxPrint(self);
  182.         if(isNil(tooMany))
  183.                 return EmptyString;
  184.  
  185.         i       = 0;
  186.         do(self, ::(aMember){
  187.                 if ((i = i + 1) > tooMany) {
  188.                         return aString + " ...etc...)";
  189.                 }
  190.                 aString = aString + " " + asString(aMember);
  191.         });
  192.         return aString + ")";
  193. }
  194.  
  195. /* Return the number of members to include in 'asString' */
  196. method Collection::maxPrint(self)
  197. {
  198.         return 16;
  199. }
  200.         
  201. /* Create a Stream over a Collection. */
  202. method Collection::streamOver(self)
  203. {
  204.         return init(new(Stream), self, 0);
  205. }
  206.  
  207.  
  208. /* add two collections together to produce a third. */
  209. method Collection::+(self, aCollection)
  210. {
  211.     local newCollection;
  212.  
  213.     newCollection = new(speciesOf(self),sizeOf(self)+sizeOf(aCollection));
  214.     addAll(newCollection,self);
  215.     addAll(newCollection,aCollection);
  216.     return newCollection;
  217. }
  218.  
  219.  
  220.  
  221. /* subtract the second collection from the first to produce a third. */
  222. method Collection::-(self, aCollection)
  223. {
  224.     local newCollection;
  225.  
  226.     newCollection = new(speciesOf(self),sizeOf(self));
  227.     addAll(newCollection,self);
  228.     removeAll(newCollection,aCollection);
  229.     return newCollection;
  230. }
  231.  
  232.  
  233. /* Return the class of the receiver. */
  234. method Collection::speciesOf(self)
  235. {
  236.         return classOf(self);
  237. }
  238.