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) |