Dictionary.st
changeset 16902 869a0975a957
parent 16808 793e0523328e
child 17264 548ea4dc5aa4
equal deleted inserted replaced
16901:8394ec484888 16902:869a0975a957
   227 !Dictionary methodsFor:'Compatibility-Dolphin'!
   227 !Dictionary methodsFor:'Compatibility-Dolphin'!
   228 
   228 
   229 equals:aDictionary
   229 equals:aDictionary
   230     ^ self = aDictionary
   230     ^ self = aDictionary
   231 ! !
   231 ! !
   232 
       
   233 
   232 
   234 !Dictionary methodsFor:'accessing'!
   233 !Dictionary methodsFor:'accessing'!
   235 
   234 
   236 associationAt:aKey
   235 associationAt:aKey
   237     "return an association consisting of aKey and the element indexed
   236     "return an association consisting of aKey and the element indexed
   303 	^ valueArray basicAt:index
   302 	^ valueArray basicAt:index
   304     ].
   303     ].
   305     ^ exceptionBlock value.
   304     ^ exceptionBlock value.
   306 !
   305 !
   307 
   306 
       
   307 at:aKey ifAbsent:default update:aBlock 
       
   308     "update the element stored under aKey with the result from 
       
   309      evaluating aBlock with the previous stored value as argument, or with default,
       
   310      if there was no such key initially.
       
   311      I.e. this is the same as self at:aKey put:(aBlock value:(self at:aKey ifAbsent:default)).
       
   312      Return the new value stored.
       
   313      This is an optimized accessor, which only computes the hash value once."
       
   314 
       
   315     |k index "{ Class: SmallInteger }"
       
   316      newValue|
       
   317 
       
   318     (k := aKey) isNil ifTrue:[
       
   319         k := NilEntry
       
   320     ].
       
   321 
       
   322     index := self findKeyOrNil:k.
       
   323     (keyArray basicAt:index) notNil ifTrue:[
       
   324         "/ key present
       
   325         valueArray basicAt:index put:(newValue := aBlock value:(valueArray basicAt:index)).
       
   326         ^ newValue
       
   327     ].
       
   328     "/ a new key
       
   329     keyArray basicAt:index put:k.
       
   330     valueArray basicAt:index put:(newValue := aBlock value:default).
       
   331     tally := tally + 1.
       
   332 
       
   333     self fullCheck.
       
   334     ^ newValue
       
   335 
       
   336     "
       
   337      |d|
       
   338 
       
   339      d := Dictionary new.
       
   340      d at:'one'  ifAbsent:0 update:[:val | val + 1].
       
   341      d at:'two'  ifAbsent:0 update:[:val | val + 1].
       
   342      d at:'three' ifAbsent:0  update:[:val | val + 1].
       
   343      d at:'one' ifAbsent:0  update:[:val | val + 1].
       
   344      d at:'two' ifAbsent:0  update:[:val | val + 1].
       
   345      d
       
   346     "
       
   347 !
       
   348 
   308 at:aKey ifAbsentPut:valueBlock
   349 at:aKey ifAbsentPut:valueBlock
   309     "return the element indexed by aKey if present,
   350     "return the element indexed by aKey if present,
   310      if not present, store the result of evaluating valueBlock
   351      if not present, store the result of evaluating valueBlock
   311      under aKey and return it.
   352      under aKey and return it.
   312      WARNING: do not add elements while iterating over the receiver.
   353      WARNING: do not add elements while iterating over the receiver.
   401      |d|
   442      |d|
   402 
   443 
   403      d := Dictionary new.
   444      d := Dictionary new.
   404      d at:'foo' put:1234 ifPresent:[ self error: 'duplicate' ].
   445      d at:'foo' put:1234 ifPresent:[ self error: 'duplicate' ].
   405      d at:'foo' put:2345 ifPresent:[ self error: 'duplicate' ].
   446      d at:'foo' put:2345 ifPresent:[ self error: 'duplicate' ].
       
   447     "
       
   448 !
       
   449 
       
   450 at:aKey update:aBlock 
       
   451     "update the element stored under aKey with the result from 
       
   452      evaluating aBlock with the previous stored value as argument.
       
   453      Report an error if there was no such key initially.
       
   454      I.e. this is the same as self at:aKey put:(aBlock value:(self at:aKey)).
       
   455      Return the new value stored.
       
   456      This is an optimized accessor, which only computes the hash value once."
       
   457 
       
   458     |k index "{ Class: SmallInteger }"
       
   459      newValue|
       
   460 
       
   461     (k := aKey) isNil ifTrue:[
       
   462         k := NilEntry
       
   463     ].
       
   464 
       
   465     index := self findKeyOrNil:k.
       
   466     (keyArray basicAt:index) notNil ifTrue:[
       
   467         "/ key present
       
   468         valueArray basicAt:index put:(newValue := aBlock value:(valueArray basicAt:index)).
       
   469         ^ newValue
       
   470     ].
       
   471     "/ a new key
       
   472     ^ self errorKeyNotFound:k.
       
   473 
       
   474     "
       
   475      |d|
       
   476 
       
   477      d := Dictionary new.
       
   478      d at:'one'  update:[:val | val + 1].
       
   479     "
       
   480 
       
   481     "
       
   482      |d|
       
   483      d := Dictionary new.
       
   484      d at:'one' put:0.
       
   485      d at:'two' put:0.
       
   486      d at:'three' put:0.
       
   487 
       
   488      d at:'one'    update:[:val | val + 1].
       
   489      d at:'two'    update:[:val | val + 1].
       
   490      d at:'three'  update:[:val | val + 1].
       
   491      d at:'one'    update:[:val | val + 1].
       
   492      d at:'two'    update:[:val | val + 1].
       
   493      d
   406     "
   494     "
   407 !
   495 !
   408 
   496 
   409 keyAtEqualValue:aValue
   497 keyAtEqualValue:aValue
   410     "return the key whose value is equal (i.e. using #= for compare)
   498     "return the key whose value is equal (i.e. using #= for compare)
  2155 ! !
  2243 ! !
  2156 
  2244 
  2157 !Dictionary class methodsFor:'documentation'!
  2245 !Dictionary class methodsFor:'documentation'!
  2158 
  2246 
  2159 version
  2247 version
  2160     ^ '$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.121 2014-08-01 12:52:05 stefan Exp $'
  2248     ^ '$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.122 2014-10-20 13:45:24 cg Exp $'
  2161 !
  2249 !
  2162 
  2250 
  2163 version_CVS
  2251 version_CVS
  2164     ^ '$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.121 2014-08-01 12:52:05 stefan Exp $'
  2252     ^ '$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.122 2014-10-20 13:45:24 cg Exp $'
  2165 ! !
  2253 ! !
  2166 
  2254