home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 232_01 / bag.st < prev    next >
Text File  |  1987-06-17  |  1KB  |  41 lines

  1. Class Bag :Collection
  2. | dict count |
  3. [
  4.         new
  5.                 dict <- Dictionary new
  6.  
  7. |       add: newElement
  8.                 dict at: newElement
  9.                      put: (1 + (dict at: newElement ifAbsent: [0]))
  10.  
  11. |       add: newObj withOccurrences: anInteger
  12.                 anInteger timesRepeat: [ self add: newObj ].
  13.                 ^ newObj
  14.  
  15. |       remove: oldElement ifAbsent: exceptionBlock   | i |
  16.                 i <- dict at: oldElement
  17.                           ifAbsent: [ ^ exceptionBlock value].
  18.                 (1 = i) ifTrue:  [dict removeKey: oldElement]
  19.                         ifFalse: [dict at: oldElement put: i - 1 ]
  20.  
  21. |       size
  22.                 ^ dict inject: 0 into: [:x :y | x + y]
  23.  
  24. |       occurrencesOf: anElement
  25.                 ^ dict at: anElement ifAbsent: [0]
  26.  
  27. |       first
  28.         (count <- dict first) isNil ifTrue: [^ nil].
  29.         count <- count - 1.
  30.         ^ dict currentKey
  31.  
  32. |       next
  33.         [count notNil] whileTrue:
  34.            [ (count > 0)
  35.                 ifTrue: [count <- count - 1. ^ dict currentKey]
  36.             ifFalse: [(count <- dict next) isNil
  37.                     ifTrue: [^ nil] ]].
  38.         ^ nil
  39.  
  40. ]
  41.