home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 018.lha / prelude / kcollection.st < prev    next >
Text File  |  1986-10-19  |  3KB  |  86 lines

  1. Class KeyedCollection :Collection
  2. [
  3.     add: anElement
  4.         ^ self error: 'Must add with explicit key'
  5. |
  6.     addAll: aCollection
  7.                 aCollection binaryDo: [:x :y | self at: x put: y].
  8.                 ^ aCollection
  9. |
  10.     asDictionary            | newCollection |
  11.         newCollection <- Dictionary new.
  12.         self binaryDo: 
  13.             [:key :val | newCollection at: key put: val].
  14.         ^ newCollection
  15. |
  16.     at: key
  17.                 ^ self at: key ifAbsent:
  18.                    [self error:
  19.                          (key printString , ': association not found').
  20.                     ^ key]
  21. |
  22.     atAll: aCollection put: anObject
  23.         aCollection do: [:x | self at: x put: anObject]
  24. |
  25.     binaryDo: aBlock                | item |
  26.         self do: [:x | aBlock value: self currentKey
  27.                     value: x ].
  28.                 ^ nil
  29. |
  30.     collect: aBlock 
  31.         ^ self coerce:
  32.              (self inject: Dictionary new
  33.                            into: [:x :y | x at: self currentKey
  34.                         put: (aBlock value: y) . x ] )
  35. |
  36.     includesKey: key
  37.                 self at: key ifAbsent: [^ false].
  38.                 ^ true
  39. |
  40.     indexOf: anElement
  41.         ^ self indexOf: anElement
  42.         ifAbsent: [self error: 'indexOf element not found']
  43. |
  44.     indexOf: anElement ifAbsent: exceptionBlock
  45.                 self do: [:x | (x = anElement) 
  46.                     ifTrue: [ ^ self currentKey ]].
  47.                  ^ exceptionBlock value
  48. |
  49.     keys                             | newset |
  50.                 newset <- Set new.
  51.                 self keysDo: [:x | newset add: x].
  52.                 ^ newset
  53. |
  54.     keysDo: aBlock
  55.                 ^ self do: [ :x | aBlock value: self currentKey ]
  56. |
  57.     keysSelect: aBlock          
  58.         ^ self coerce:
  59.              (self inject: Dictionary new
  60.                            into: [:x :y | (aBlock value: y currentKey)
  61.                                            ifTrue: [x at: self currentKey
  62.                                                       put: y]. x ] )
  63. |
  64.     remove: anElement
  65.         ^ self error: 'object must be removed with explicit key'
  66. |
  67.     removeKey: key
  68.                 ^ self removeKey: key ifAbsent:
  69.                    [self error: 'no element associated with key'. ^ key]
  70. |
  71.     removeKey: key ifAbsent: exceptionBlock
  72.         ^ self error: 'subclass should implement RemoveKey:ifAbsent:'
  73. |
  74.     select: aBlock          
  75.         ^ self coerce:
  76.              (self inject: Dictionary new
  77.                            into: [:x :y | (aBlock value: y)
  78.                                            ifTrue: [x at: self currentKey
  79.                                                       put: y]. x ] )
  80. |
  81.     values                           | newbag |
  82.                 newbag <- Bag new.
  83.                 self do: [:x | newbag add: x].
  84.                 ^ newbag
  85. ]
  86.