home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / readme / fileins / weaklut.st < prev   
Encoding:
Text File  |  1995-10-24  |  2.2 KB  |  70 lines

  1.  
  2.  
  3. !AbtWeakValueLookupTable privateMethods !
  4.  
  5. abtRenameKey: aKey to: newKey ifPresent: aPresentBlock ifAbsent: anAbsentBlock
  6.     "Public - Examine the receiver to see if newKey is present.  If it is,
  7.         then answer (aPresentBlock value).  If newKey is not present,
  8.         then examine the receiver to see if aKey is present.  If it is not,
  9.         then answer (anAbsentBlock value).  If aKey is present, then
  10.         remove aKey and answer the result of adding the index at aKey 
  11.         to the receiver at newKey."
  12.      "05.29.92 svd - EZE* initial"
  13.  
  14.     | value oldKeyIndex newKeyIndex hashIndex index element elementsSize |
  15.  
  16.     hashIndex := aKey hash \\ (elementsSize := keys size) + 1.
  17.     
  18.     index := hashIndex.
  19.     [oldKeyIndex isNil and: [index <= elementsSize]] whileTrue: 
  20.         [(element := keys at: index) == nil 
  21.             ifTrue: [^anAbsentBlock value]
  22.             ifFalse: 
  23.                 [element = aKey 
  24.                     ifTrue: [oldKeyIndex := index]
  25.                     ifFalse: [index := index + 1]].
  26.         ].
  27.  
  28.     oldKeyIndex isNil
  29.         ifTrue:
  30.             [index := 1.
  31.             [(element := keys at: index) == nil 
  32.                 ifTrue: [^anAbsentBlock value]
  33.                 ifFalse: 
  34.                     [element = aKey ifTrue: [oldKeyIndex := index].
  35.                     index := index + 1].
  36.             oldKeyIndex isNil and: [index < hashIndex]] whileTrue: [].
  37.             oldKeyIndex isNil ifTrue: [^anAbsentBlock value]].
  38.  
  39.     hashIndex := newKey hash \\ elementsSize + 1.
  40.     index := hashIndex.
  41.  
  42.     [newKeyIndex isNil and: [index <= elementsSize]] whileTrue: 
  43.         [(element := keys at: index) == nil 
  44.             ifTrue: [newKeyIndex := index]
  45.             ifFalse: 
  46.                 [element = newKey ifTrue: [^aPresentBlock value: (values at: oldKeyIndex)].
  47.                 index := index + 1].
  48.         ].
  49.  
  50.     newKeyIndex isNil
  51.         ifTrue:
  52.             [index := 1.
  53.             [(element := keys at: index) == nil 
  54.                 ifTrue: [newKeyIndex := index]
  55.                 ifFalse: 
  56.                     [element = newKey ifTrue: [^aPresentBlock value: (values at: oldKeyIndex)].
  57.                     index := index + 1].
  58.             newKeyIndex isNil and: [index < hashIndex]] whileTrue: []].
  59.  
  60.     newKeyIndex isNil
  61.         ifTrue:
  62.             [self expand at: newKey put: (value := values at: oldKeyIndex).
  63.             self removeKey: aKey]
  64.         ifFalse:
  65.             [keys at: newKeyIndex put: newKey.
  66.             values at: newKeyIndex put: (value := values at: oldKeyIndex).
  67.             super rehashTo: oldKeyIndex].
  68.     ^value
  69. ! !
  70.