home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / bag.st < prev    next >
Text File  |  1992-02-15  |  4KB  |  175 lines

  1. "======================================================================
  2. |
  3. |   Bag Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         28 Jul 91      #= now checks args better.
  34. |
  35. | sbb         16 Mar 91      Class creation now separate statement.
  36. |
  37. | sbb         21 Sep 90      Removed printOn: method; the one from Collection does
  38. |              the right thing.
  39. |
  40. | sbyrne     25 Apr 89      created.
  41. |
  42. "
  43.  
  44. Collection subclass: #Bag
  45.        instanceVariableNames: 'contents'
  46.        classVariableNames: ''
  47.        poolDictionaries: ''
  48.        category: nil
  49. !
  50.  
  51. Bag comment:
  52. 'My instances are unordered collections of objects.  You can think
  53. of me as a set with a memory; that is, if the same object is added to me
  54. twice, then I will report that that element has been stored twice.'!
  55.  
  56.  
  57. !Bag class methodsFor: 'basic'!
  58.  
  59. new
  60.     ^super new initContents
  61. !!
  62.  
  63.  
  64.  
  65. !Bag methodsFor: 'Adding to a collection'!
  66.  
  67. add: newObject withOccurrences: anInteger
  68.     contents at: newObject
  69.          put: (self occurrencesOf: newObject) + anInteger.
  70.     ^newObject
  71. !
  72.  
  73. add: newObject
  74.     self add: newObject withOccurrences: 1.
  75.     ^newObject 
  76. !
  77.  
  78. at: index
  79.     self error: 'at: is not allowed for a Bag'
  80. !
  81.  
  82. at: index put: value
  83.     self error: 'at:put: is not allowed for a Bag'
  84. !!
  85.  
  86.  
  87.  
  88. !Bag methodsFor: 'Removing from a collection'!
  89.  
  90. remove: oldObject ifAbsent: anExceptionBlock
  91.     | count |
  92.     "Remove oldObject from the collection and return it.  Since we're using
  93.     a dictionary, we need decrement the value until it's zero, in which case
  94.     we can then remove the object from the dictionary"
  95.     count _ self occurrencesOf: oldObject.
  96.     count = 0 ifTrue: [ ^anExceptionBlock value ].
  97.     count = 1 ifTrue: [ contents removeKey: oldObject ]
  98.               ifFalse: [ contents at: oldObject
  99.                                 put: count - 1 ].
  100.     ^oldObject
  101. !!
  102.  
  103.  
  104.  
  105. !Bag methodsFor: 'testing collections'!
  106.  
  107. occurrencesOf: anObject
  108.     ^contents at: anObject ifAbsent: [ ^0 ]
  109. !
  110.  
  111. size
  112.     | count |
  113.     count _ 0.
  114.     contents do: [ :element | count _ count + element ].
  115.     ^count
  116. !
  117.  
  118. hash
  119.     ^contents hash
  120. !
  121.  
  122. = aBag
  123.     self class == aBag class
  124.     ifFalse: [ ^false ].
  125.     ^contents = aBag contents
  126. !!
  127.  
  128.  
  129.  
  130. !Bag methodsFor: 'enumerating the elements of a collection'!
  131.  
  132. do: aBlock
  133.     "Perform the block for all members in the collection.  For Bags, we need
  134.     to go through the contents dictionary, and perform the block for as many
  135.     occurrences of the objects as there are."
  136.     contents associationsDo:
  137.       [ :assoc |  assoc value timesRepeat: [ aBlock value: assoc key ] ]
  138. !!
  139.  
  140.  
  141.  
  142. !Bag methodsFor: 'storing'!
  143.  
  144. storeOn: aStream
  145.     | noElements |
  146.     aStream nextPut: $(;
  147.     nextPutAll: self classNameString;
  148.     nextPutAll: ' new'.
  149.     noElements _ true.
  150.     contents associationsDo:
  151.       [ :assoc | aStream nextPutAll: ' add: ';
  152.              store: assoc key;
  153.              nextPutAll: ' withOccurrences: ';
  154.              store: assoc value;
  155.              nextPut: $;.
  156.          noElements _ false ].
  157.     noElements ifFalse: [ aStream nextPutAll: '; yourself' ].
  158.     aStream nextPut: $)
  159. !!
  160.  
  161.  
  162.  
  163. !Bag methodsFor: 'private'!
  164.  
  165. initContents
  166.     contents _ Dictionary new
  167. !
  168.  
  169. contents
  170.     ^contents
  171. !!
  172.  
  173.