SmallDictionary.st
changeset 24499 5b64d790d22b
parent 24179 f7ae805614ff
child 24500 696cb888b451
equal deleted inserted replaced
24498:0faa7c05e6bb 24499:5b64d790d22b
       
     1 "{ Encoding: utf8 }"
       
     2 
     1 "
     3 "
     2  COPYRIGHT (c) 2018 by eXept Software AG
     4  COPYRIGHT (c) 2018 by eXept Software AG
     3               All Rights Reserved
     5               All Rights Reserved
     4 
     6 
     5  This software is furnished under a license and may be used
     7  This software is furnished under a license and may be used
    78 ! !
    80 ! !
    79 
    81 
    80 !SmallDictionary methodsFor:'accessing'!
    82 !SmallDictionary methodsFor:'accessing'!
    81 
    83 
    82 at:key ifAbsent:aBlock 
    84 at:key ifAbsent:aBlock 
       
    85     "return the element indexed by aKey -
       
    86      return result of exceptionBlock if no element is stored under aKey"
       
    87 
    83     |keyIndex "{Class: SmallInteger}"|
    88     |keyIndex "{Class: SmallInteger}"|
    84 
    89 
    85     keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
    90     keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
    86     ^ keyIndex == 0 ifTrue:[aBlock value] ifFalse:[keysAndValues basicAt:keyIndex+1]
    91     ^ keyIndex == 0 ifTrue:[aBlock value] ifFalse:[keysAndValues basicAt:keyIndex+1]
    87 
    92 
    88     "Modified: / 18-09-2018 / 14:08:21 / Stefan Vogel"
    93     "Modified: / 18-09-2018 / 14:08:21 / Stefan Vogel"
    89 !
    94 !
    90 
    95 
    91 at:key ifAbsentPut:aBlock 
    96 at:key ifAbsentPut:aBlock 
       
    97     "return the element indexed by aKey if present,
       
    98      if not present, store the result of evaluating valueBlock
       
    99      under aKey and return it.
       
   100      WARNING: do not add elements while iterating over the receiver.
       
   101               Iterate over a copy to do this."
       
   102 
    92     |keyIndex|
   103     |keyIndex|
    93 
   104 
    94     keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
   105     keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
    95     ^ keyIndex == 0 ifTrue:[
   106     keyIndex == 0 ifTrue:[
    96         self privateAt:key put:aBlock value
   107         ^ self privateAt:key put:aBlock value
    97     ] ifFalse:[
   108     ] ifFalse:[
    98         keysAndValues basicAt:keyIndex+1.
   109         ^ keysAndValues basicAt:keyIndex+1.
    99     ]
   110     ]
   100 
   111 
   101     "Modified: / 18-09-2018 / 14:08:26 / Stefan Vogel"
   112     "Modified: / 18-09-2018 / 14:08:26 / Stefan Vogel"
   102 !
   113 !
   103 
   114 
   129 
   140 
   130     "Created: / 14-09-2018 / 17:30:44 / Stefan Vogel"
   141     "Created: / 14-09-2018 / 17:30:44 / Stefan Vogel"
   131 !
   142 !
   132 
   143 
   133 size
   144 size
       
   145     "return the number of elements in the receiver."
       
   146 
   134     ^ tally
   147     ^ tally
   135 
   148 
   136     "Modified (format): / 14-09-2018 / 15:44:11 / Stefan Vogel"
   149     "Modified (format): / 14-09-2018 / 15:44:11 / Stefan Vogel"
   137 ! !
   150 ! !
   138 
   151 
   150 
   163 
   151     keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
   164     keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
   152     keyIndex == 0 ifTrue:[
   165     keyIndex == 0 ifTrue:[
   153         self privateAt:key put:value
   166         self privateAt:key put:value
   154     ] ifFalse:[
   167     ] ifFalse:[
   155         keysAndValues basicAt:keyIndex+1 put:value
   168         keysAndValues basicAt:keyIndex+1 put:value.
   156     ].
   169     ].
   157     ^ value
   170     ^ value
   158 
   171 
   159     "Modified: / 18-09-2018 / 14:12:13 / Stefan Vogel"
   172     "Modified: / 18-09-2018 / 14:12:13 / Stefan Vogel"
   160 ! !
   173 ! !
   167     "Modified: / 14-09-2018 / 15:38:50 / Stefan Vogel"
   180     "Modified: / 14-09-2018 / 15:38:50 / Stefan Vogel"
   168 ! !
   181 ! !
   169 
   182 
   170 !SmallDictionary methodsFor:'enumerating'!
   183 !SmallDictionary methodsFor:'enumerating'!
   171 
   184 
   172 do:aBlock 
   185 do:aBlock
       
   186     "enumerate the values"
       
   187 
   173     |sz "{Class: SmallInteger}"|
   188     |sz "{Class: SmallInteger}"|
   174 
   189 
   175     sz := tally.
   190     sz := tally * 2.
   176     2 to:sz by:2 do:[:i | aBlock value:(keysAndValues at:i)]
   191     2 to:sz by:2 do:[:i | aBlock value:(keysAndValues at:i)]
   177 
   192 
   178     "Modified: / 14-09-2018 / 16:30:18 / Stefan Vogel"
   193     "Modified: / 14-09-2018 / 16:30:18 / Stefan Vogel"
   179 !
   194 !
   180 
   195 
   181 keysAndValuesDo:aBlock 
   196 keysAndValuesDo:aBlock 
   182     |sz "{Class: SmallInteger}"|
   197     |sz "{Class: SmallInteger}"|
   183 
   198 
   184     sz := tally * 2.
   199     sz := tally * 2.
   185     1 to:sz-1 by:2 do:[:i | aBlock value:(keysAndValues at:i) value:(keysAndValues at:i+1)]
   200     1 to:sz-1 by:2 do:[:i | 
       
   201         aBlock 
       
   202             value:(keysAndValues at:i) 
       
   203             value:(keysAndValues at:i+1)]
   186 
   204 
   187     "Modified: / 14-09-2018 / 16:31:18 / Stefan Vogel"
   205     "Modified: / 14-09-2018 / 16:31:18 / Stefan Vogel"
   188 !
   206 !
   189 
   207 
   190 keysDo:aBlock 
   208 keysDo:aBlock 
   267     self removeAll
   285     self removeAll
   268 
   286 
   269     "Modified: / 14-09-2018 / 15:59:57 / Stefan Vogel"
   287     "Modified: / 14-09-2018 / 15:59:57 / Stefan Vogel"
   270 !
   288 !
   271 
   289 
   272 remove: oldObject ifAbsent: anExceptionBlock 
   290 remove:anAssociation ifAbsent:anExceptionBlock 
   273     self removeKey: oldObject key ifAbsent: anExceptionBlock.
   291     self removeKey:(anAssociation key) ifAbsent:anExceptionBlock.
   274     ^ oldObject
   292     ^ anAssociation
   275 
   293 
   276     "Modified (format): / 14-09-2018 / 15:40:44 / Stefan Vogel"
   294     "Modified (format): / 14-09-2018 / 15:40:44 / Stefan Vogel"
   277 !
   295 !
   278 
   296 
   279 removeAll
   297 removeAll
   280     tally := 0.
   298     tally := 0.
   281     keysAndValues from:1 to:keysAndValues size put:nil.
   299     keysAndValues atAllPut:nil.
   282 
   300 
   283     "Modified: / 14-09-2018 / 16:02:28 / Stefan Vogel"
   301     "Modified: / 14-09-2018 / 16:02:28 / Stefan Vogel"
   284 !
   302 !
   285 
   303 
   286 removeKey:key ifAbsent:aBlock 
   304 removeKey:key ifAbsent:aBlock 
   293         ^ aBlock value
   311         ^ aBlock value
   294     ].
   312     ].
   295     value := keysAndValues at:keyIndex+1.
   313     value := keysAndValues at:keyIndex+1.
   296     sz := tally*2.
   314     sz := tally*2.
   297     keyIndex to:sz-2 do:[:i | 
   315     keyIndex to:sz-2 do:[:i | 
   298         keysAndValues at:i put:(keysAndValues at:i + 1).
   316         keysAndValues at:i put:(keysAndValues at:i + 2).
   299     ].
   317     ].
   300     keysAndValues at:sz-1 put:nil.
   318     keysAndValues at:sz-1 put:nil.
   301     keysAndValues at:sz put:nil.
   319     keysAndValues at:sz put:nil.
   302     tally := tally - 1.
   320     tally := tally - 1.
   303     ^ value
   321     ^ value
   306 ! !
   324 ! !
   307 
   325 
   308 !SmallDictionary methodsFor:'testing'!
   326 !SmallDictionary methodsFor:'testing'!
   309 
   327 
   310 isDictionary
   328 isDictionary
       
   329     "return true, if the receiver is some kind of dictionary;
       
   330      true returned here - the method is redefined from Object."
       
   331 
   311     ^ true
   332     ^ true
   312 
   333 
   313     "Created: / 14-09-2018 / 18:31:24 / Stefan Vogel"
   334     "Created: / 14-09-2018 / 18:31:24 / Stefan Vogel"
   314 !
   335 !
   315 
   336