Dictionary.st
changeset 3 24d81bf47225
parent 2 6526dde5f3ac
child 5 67342904af11
equal deleted inserted replaced
2:6526dde5f3ac 3:24d81bf47225
    24 
    24 
    25 a Dictionary is (conceptionally) a collection of Associations storing key-value pairs.
    25 a Dictionary is (conceptionally) a collection of Associations storing key-value pairs.
    26 (The implementation uses two array to store the keys and values separately.)
    26 (The implementation uses two array to store the keys and values separately.)
    27 Searching for an element is done using a hash into the key arrlay.
    27 Searching for an element is done using a hash into the key arrlay.
    28 
    28 
    29 %W% %E%
    29 $Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.3 1993-10-13 00:15:31 claus Exp $
    30 
    30 
    31 written jun 91 by claus
    31 written jun 91 by claus
    32 rewritten 92 to use hash scheme
    32 rewritten 92 to use hash scheme
    33 '!
    33 '!
    34 
    34 
   161 
   161 
   162 removeKey:aKey
   162 removeKey:aKey
   163     "remove the association under aKey from the collection.
   163     "remove the association under aKey from the collection.
   164      If it was not in the collection report an error"
   164      If it was not in the collection report an error"
   165 
   165 
   166     |index 
   166     |index "{ Class:SmallInteger }"
   167      next  "{ Class:SmallInteger }" |
   167      next  "{ Class:SmallInteger }" |
   168 
   168 
   169     aKey isNil ifTrue:[
   169     aKey isNil ifTrue:[
   170         self error:'nil is not allowed as key'
   170         self error:'nil is not allowed as key'
   171     ] ifFalse:[
   171     ] ifFalse:[
   192 
   192 
   193 removeKey:aKey ifAbsent:aBlock
   193 removeKey:aKey ifAbsent:aBlock
   194     "remove the association under aKey from the collection.
   194     "remove the association under aKey from the collection.
   195      If it was not in the collection return result from evaluating aBlock"
   195      If it was not in the collection return result from evaluating aBlock"
   196 
   196 
   197     |index 
   197     |index "{ Class:SmallInteger }"
   198      next  "{ Class:SmallInteger }" |
   198      next  "{ Class:SmallInteger }" |
   199 
   199 
   200     aKey isNil ifTrue:[
   200     aKey isNil ifTrue:[
   201         self error:'nil is not allowed as key'
   201         self error:'nil is not allowed as key'
   202     ] ifFalse:[
   202     ] ifFalse:[
   256         ].
   256         ].
   257         index := index + 1
   257         index := index + 1
   258     ]
   258     ]
   259 !
   259 !
   260 
   260 
       
   261 keysAndValuesDo:aTwoArgBlock
       
   262     "evaluate the argument, aBlock for every element in the collection,
       
   263      passing both key and element as arguments."
       
   264 
       
   265     |index "{ Class:SmallInteger }" |
       
   266 
       
   267     tally == 0 ifTrue:[^ self].
       
   268     index := 1.
       
   269     keyArray do:[:key |
       
   270         key notNil ifTrue:[
       
   271             aTwoArgBlock value:key value:(valueArray basicAt:index)
       
   272         ].
       
   273         index := index + 1
       
   274     ]
       
   275 !
       
   276 
   261 collect:aBlock
   277 collect:aBlock
   262     "for each element in the receiver, evaluate the argument, aBlock
   278     "for each element in the receiver, evaluate the argument, aBlock
   263      and return a Bag with the results"
   279      and return a Bag with the results"
   264 
   280 
   265     |newCollection|
   281     |newCollection|
   328      the index of the association containing the key, otherwise
   344      the index of the association containing the key, otherwise
   329      return the index of the first unused slot. Grow the receiver,
   345      return the index of the first unused slot. Grow the receiver,
   330      if key was not found, and no unused slots where present"
   346      if key was not found, and no unused slots where present"
   331 
   347 
   332     |index  "{ Class:SmallInteger }"
   348     |index  "{ Class:SmallInteger }"
   333      length "{ Class:SmallInteger }"
   349      length startIndex probe|
   334      startIndex
       
   335      probe|
       
   336 
   350 
   337     length := keyArray basicSize.
   351     length := keyArray basicSize.
   338     startIndex := key hash \\ length + 1.
   352     startIndex := key hash \\ length + 1.
   339 
   353 
   340     index := startIndex.
   354     index := startIndex.