489 ^ anObject |
489 ^ anObject |
490 |
490 |
491 "Modified: 30.1.1997 / 14:59:10 / cg" |
491 "Modified: 30.1.1997 / 14:59:10 / cg" |
492 ! |
492 ! |
493 |
493 |
494 at:aKey put:aValue ifPresent:aBlock |
494 at:aKey put:anObject ifPresent:aBlock |
495 "if the receiver contains an element stored under aKey, |
495 "if the receiver contains an element stored under aKey, |
496 retrieve it and evaluate aBlock passing the element as argument, |
496 retrieve it and evaluate aBlock passing the element as argument, |
497 return the blocks value. |
497 return the blocks value. |
498 If not, store aValue under the key. |
498 If not, store aValue under the key. |
499 Use this with an error-reporting block, to ensure that no keys are reused" |
499 Use this with an error-reporting block, to ensure that no keys are reused" |
500 |
500 |
501 (self includesKey:aKey) ifTrue: [ ^ aBlock value ]. |
501 |k index "{ Class: SmallInteger }"| |
502 ^ self at:aKey put:aValue. |
502 |
|
503 (k := aKey) isNil ifTrue:[ |
|
504 k := NilEntry |
|
505 ]. |
|
506 |
|
507 index := self findKeyOrNil:k. |
|
508 (keyArray basicAt:index) notNil ifTrue:[ |
|
509 "/ key already present |
|
510 ^ aBlock value:(valueArray basicAt:index). |
|
511 ]. |
|
512 "/ a new key |
|
513 keyArray basicAt:index put:k. |
|
514 valueArray basicAt:index put:anObject. |
|
515 tally := tally + 1. |
|
516 |
|
517 self possiblyGrow. |
|
518 ^ anObject |
503 |
519 |
504 " |
520 " |
505 |d| |
521 |d| |
506 |
522 |
507 d := Dictionary new. |
523 d := Dictionary new. |
508 d at:'foo' put:1234 ifPresent:[ self error: 'duplicate' ]. |
524 d at:'foo' put:1234 ifPresent:[:v| self error: 'duplicate: ', v printString ]. |
509 d at:'foo' put:2345 ifPresent:[ self error: 'duplicate' ]. |
525 d at:'foo' put:1234 ifPresent:[:v| self halt:'duplicate: ', v printString. 5555 ]. |
510 " |
526 " |
511 ! |
527 ! |
512 |
528 |
513 at:aKey update:aBlock |
529 at:aKey update:aBlock |
514 "update the element stored under aKey with the result from |
530 "update the element stored under aKey with the result from |