Set.st
branchjv
changeset 19559 d35a89d5c0ec
parent 19103 71257a47eba2
parent 19542 0cff447dfffa
child 19882 8a3f4071dfec
equal deleted inserted replaced
19533:1c9224a6ec00 19559:d35a89d5c0ec
   811 
   811 
   812     "Modified: 1.3.1996 / 21:41:13 / cg"
   812     "Modified: 1.3.1996 / 21:41:13 / cg"
   813 ! !
   813 ! !
   814 
   814 
   815 
   815 
       
   816 
   816 !Set methodsFor:'obsolete set operations'!
   817 !Set methodsFor:'obsolete set operations'!
   817 
   818 
   818 + aCollection
   819 + aCollection
   819     "Kept for backward compatibility. 
   820     "Kept for backward compatibility. 
   820      Use #union: instead, to isolate arithmethic and set operations"
   821      Use #union: instead, to isolate arithmethic and set operations"
   855 "/  ].
   856 "/  ].
   856 
   857 
   857     startIndex := index := self initialIndexForKey:key.
   858     startIndex := index := self initialIndexForKey:key.
   858 
   859 
   859     [
   860     [
   860         probe := (keyArray basicAt:index).
   861         probe := keyArray basicAt:index.
   861         (probe notNil and:[probe ~~ DeletedEntry and:[key = probe]]) ifTrue:[^ index].
   862         probe isNil ifTrue:[^ aBlock value].
   862         (self slotIsEmpty:probe) ifTrue:[^ aBlock value].
   863         (probe ~~ DeletedEntry and:[key = probe]) ifTrue:[^ index].
   863 
   864 
   864         index == length ifTrue:[
   865         index == length ifTrue:[
   865             index := 1
   866             index := 1
   866         ] ifFalse:[
   867         ] ifFalse:[
   867             index := index + 1
   868             index := index + 1
   881 !
   882 !
   882 
   883 
   883 findKeyOrNil:key
   884 findKeyOrNil:key
   884     "Look for the key in the receiver.  
   885     "Look for the key in the receiver.  
   885      If it is found, return the index of the first unused slot. 
   886      If it is found, return the index of the first unused slot. 
   886      Grow the receiver, if key was not found, and no unused slots were present"
   887      Grow the receiver, if key was not found, and no unused slots were present
       
   888 
       
   889      Warning: an empty slot MUST be filled by the sender - it is only to be sent
       
   890               by at:put: / add: - like methods."
   887 
   891 
   888     |index  "{ Class:SmallInteger }"
   892     |index  "{ Class:SmallInteger }"
   889      length "{ Class:SmallInteger }"
   893      length "{ Class:SmallInteger }"
   890      startIndex probe 
   894      startIndex probe 
   891      delIndex|
   895      delIndex "{ Class:SmallInteger }"|
       
   896 
       
   897     delIndex := 0.
   892 
   898 
   893     length := keyArray basicSize.
   899     length := keyArray basicSize.
   894     startIndex := index := self initialIndexForKey:key.
   900     startIndex := index := self initialIndexForKey:key.
   895 
   901 
   896     [
   902     [
   897         probe := keyArray basicAt:index.
   903         probe := keyArray basicAt:index.
   898         (probe notNil and:[probe ~~ DeletedEntry and:[key = probe]]) ifTrue:[^ index].
   904         probe isNil ifTrue:[
   899         (self slotIsEmpty:probe) ifTrue:[
   905             delIndex == 0 ifTrue:[^ index].
   900             delIndex isNil ifTrue:[^ index].
       
   901             keyArray basicAt:delIndex put:nil.
   906             keyArray basicAt:delIndex put:nil.
   902             ^ delIndex
   907             ^ delIndex
   903         ].
   908         ].
   904 
       
   905         probe == DeletedEntry ifTrue:[
   909         probe == DeletedEntry ifTrue:[
   906             delIndex isNil ifTrue:[
   910             delIndex == 0 ifTrue:[
   907                 delIndex := index
   911                 delIndex := index
   908             ]
   912             ]
       
   913         ] ifFalse:[
       
   914             key = probe ifTrue:[^ index]
   909         ].
   915         ].
   910 
   916 
   911         index == length ifTrue:[
   917         index == length ifTrue:[
   912             index := 1
   918             index := 1
   913         ] ifFalse:[
   919         ] ifFalse:[
   914             index := index + 1
   920             index := index + 1
   915         ].
   921         ].
   916         index == startIndex ifTrue:[
   922         index == startIndex ifTrue:[
   917             delIndex notNil ifTrue:[
   923             delIndex ~~ 0 ifTrue:[
   918                 keyArray basicAt:delIndex put:nil.
   924                 keyArray basicAt:delIndex put:nil.
   919                 ^ delIndex
   925                 ^ delIndex
   920             ].
   926             ].
   921             ^ self grow findKeyOrNil:key
   927             ^ self grow findKeyOrNil:key
   922         ].
   928         ].
  1084 	] ifFalse:[
  1090 	] ifFalse:[
  1085 	    index := index + 1.
  1091 	    index := index + 1.
  1086 	].
  1092 	].
  1087 	element := keyArray basicAt:index.
  1093 	element := keyArray basicAt:index.
  1088     ]
  1094     ]
  1089 !
       
  1090 
       
  1091 slotIsEmpty:aSlotValue
       
  1092     "only redefined in weak subclasses, since they treat a 0-value
       
  1093      as being empty"
       
  1094 
       
  1095     ^ aSlotValue isNil
       
  1096 ! !
  1095 ! !
  1097 
  1096 
  1098 !Set methodsFor:'private-grow & shrink'!
  1097 !Set methodsFor:'private-grow & shrink'!
  1099 
  1098 
  1100 grow
  1099 grow
  1202 
  1201 
  1203     count := 0.
  1202     count := 0.
  1204     [
  1203     [
  1205         probe := keyArray basicAt:index.
  1204         probe := keyArray basicAt:index.
  1206         (probe notNil and:[key = probe]) ifTrue:[^ count].
  1205         (probe notNil and:[key = probe]) ifTrue:[^ count].
  1207         (self slotIsEmpty:probe) ifTrue:[self error:'non existing key'].
  1206         probe isNil ifTrue:[self error:'non existing key'].
  1208 
  1207 
  1209         index == length ifTrue:[
  1208         index == length ifTrue:[
  1210             index := 1.
  1209             index := 1.
  1211         ] ifFalse:[
  1210         ] ifFalse:[
  1212             index := index + 1.
  1211             index := index + 1.
  1238     "return the number of set elements"
  1237     "return the number of set elements"
  1239 
  1238 
  1240     ^ tally
  1239     ^ tally
  1241 ! !
  1240 ! !
  1242 
  1241 
       
  1242 
  1243 !Set methodsFor:'searching'!
  1243 !Set methodsFor:'searching'!
  1244 
  1244 
  1245 findFirst:aBlock ifNone:exceptionValue
  1245 findFirst:aBlock ifNone:exceptionValue
  1246     "find the index of the first element, for which evaluation of the argument, aBlock returns true; 
  1246     "find the index of the first element, for which evaluation of the argument, aBlock returns true; 
  1247      return its index or the value from exceptionValue if none detected.
  1247      return its index or the value from exceptionValue if none detected.