Set.st
changeset 2 6526dde5f3ac
parent 1 a27a279701f8
child 3 24d81bf47225
equal deleted inserted replaced
1:a27a279701f8 2:6526dde5f3ac
    43     ^ self basicNew setTally:anInteger
    43     ^ self basicNew setTally:anInteger
    44 ! !
    44 ! !
    45 
    45 
    46 !Set methodsFor:'private'!
    46 !Set methodsFor:'private'!
    47 
    47 
       
    48 fullCheck
       
    49     "check if dictionary is full, grow if so.
       
    50      Definition of full is currently:'filled more than 70%'"
       
    51 
       
    52     "grow if filled more than 70% "
       
    53     tally > (contentsArray basicSize * 7 // 10) ifTrue:[
       
    54        self grow
       
    55     ]
       
    56 !
       
    57 
    48 goodSizeFor:arg
    58 goodSizeFor:arg
    49     "return a good array size for the given argument.
    59     "return a good array size for the given argument.
    50      Returns the next prime after arg"
    60      Returns the next prime after arg"
    51 
    61 
    52     arg <= 7 ifTrue:[^ 7].
    62     arg <= 7 ifTrue:[^ 7].
    53     arg <= 16384 ifTrue:[
    63     arg <= 131072 ifTrue:[
    54            "2 4 8  16 32 64 128 256 512 1024 2048 4096 8192 16384"
    64            "2 4 8  16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072"
    55         ^ #(7 7 11 17 37 67 131 257 521 1031 2053 4099 8209 16411) at:(arg highBit)
    65         ^ #(7 7 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101) at:(arg highBit)
    56     ].
    66     ].
    57     ^ arg bitOr:1
    67     ^ arg bitOr:1
    58 !
    68 !
    59 
    69 
    60 setTally:count
    70 setTally:count
   159     |oldElements oldSize
   169     |oldElements oldSize
   160      srcIndex "{ Class:SmallInteger }"|
   170      srcIndex "{ Class:SmallInteger }"|
   161 
   171 
   162     oldElements := contentsArray.
   172     oldElements := contentsArray.
   163     oldSize := tally.
   173     oldSize := tally.
   164     self setTally:newSize.
   174 
       
   175     contentsArray := Array new:(self goodSizeFor:newSize). 
   165 
   176 
   166     srcIndex := 1.
   177     srcIndex := 1.
   167     oldElements do:[:elem |
   178     oldElements do:[:elem |
   168         elem notNil ifTrue:[
   179         elem notNil ifTrue:[
   169             "cannot be already there"
   180             "cannot be already there"
   175 !
   186 !
   176 
   187 
   177 rehash
   188 rehash
   178     "rehash is done by re-adding all elements to a new empty set."
   189     "rehash is done by re-adding all elements to a new empty set."
   179 
   190 
   180     | oldArray |
   191     | oldArray element 
       
   192       n "{ Class:SmallInteger }" |
   181 
   193 
   182     oldArray := contentsArray.
   194     oldArray := contentsArray.
   183     contentsArray := Array new:(contentsArray size).
   195     n := oldArray size.
   184     oldArray do:[:element |
   196     contentsArray := Array new:n.
       
   197     1 to:n do:[:index |
       
   198         element := oldArray at:index.
   185         element notNil ifTrue:[
   199         element notNil ifTrue:[
   186             "cannot be already there"
   200             "cannot be already there"
   187             contentsArray basicAt:(self findNil:element) put:element
   201             contentsArray basicAt:(self findNil:element) put:element
   188         ].
   202         ].
   189     ]
   203     ]
   274         index := self findElementOrNil:anObject.
   288         index := self findElementOrNil:anObject.
   275         (contentsArray basicAt:index) isNil ifTrue:[
   289         (contentsArray basicAt:index) isNil ifTrue:[
   276             contentsArray basicAt:index put:anObject.
   290             contentsArray basicAt:index put:anObject.
   277             tally := tally + 1.
   291             tally := tally + 1.
   278 
   292 
   279             "grow if filled more than 70% "
   293             self fullCheck.
   280             tally > (contentsArray basicSize * 7 // 10) ifTrue:[
       
   281                 self grow
       
   282             ]
       
   283         ]
   294         ]
   284     ].
   295     ].
   285     ^ anObject
   296     ^ anObject
   286 !
   297 !
   287 
   298 
   322         each notNil ifTrue:[
   333         each notNil ifTrue:[
   323             aBlock value:each
   334             aBlock value:each
   324         ]
   335         ]
   325     ]
   336     ]
   326 ! !
   337 ! !
       
   338 
       
   339 !Set methodsFor: 'binary storage'!
       
   340 
       
   341 readBinaryContentsFrom: stream manager: manager
       
   342     "must rehash after reload"
       
   343 
       
   344     super readBinaryContentsFrom: stream manager: manager.
       
   345     self rehash
       
   346 ! !