some fixes in collections (Bag and Set)
[k8lst.git] / modules / collections / set.st
blobe9ccff1f6f961afb7002ac095c8918ffea5bd694
2  Little Smalltalk, Version 5
4  Copyright (C) 1987-2005 by Timothy A. Budd
5  Copyright (C) 2007 by Charles R. Childers
6  Copyright (C) 2005-2007 by Danny Reinhold
7  Copyright (C) 2010 by Ketmar // Vampire Avalon
9  ============================================================================
10  This license applies to the virtual machine and to the initial image of
11  the Little Smalltalk system and to all files in the Little Smalltalk
12  packages except the files explicitly licensed with another license(s).
13  ============================================================================
14  Permission is hereby granted, free of charge, to any person obtaining a copy
15  of this software and associated documentation files (the 'Software'), to deal
16  in the Software without restriction, including without limitation the rights
17  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18  copies of the Software, and to permit persons to whom the Software is
19  furnished to do so, subject to the following conditions:
21  The above copyright notice and this permission notice shall be included in
22  all copies or substantial portions of the Software.
24  THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30  DEALINGS IN THE SOFTWARE.
32 Package [
33   System
37 Collection subclass: Set [
38   | members growth |
40   ^new: aSize [
41     | ret |
42     ret := super new.
43     self in: ret at: 1 put: (Array new: aSize).
44     self in: ret at: 2 put: (aSize < 1 ifTrue: [16] ifFalse: [aSize]).
45     ^ret
46   ]
48   ^new [
49     ^self new: 19
50   ]
52   ^with: objA [
53     | ret |
54     ret := self new.
55     ret add: objA.
56     ^ret
57   ]
59   ^with: objA with: objB [
60     | ret |
61     ret := self new.
62     ret add: objA. ret add: objB.
63     ^ret
64   ]
66   ^with: objA with: objB with: objC [
67     | ret |
68     ret := self new.
69     ret add: objA. ret add: objB. ret add: objC.
70     ^ret
71   ]
73   removeAll [
74     members := Array new: growth.
75   ]
77   size [
78     | tally |
79     tally := 0.
80     members do: [:elem | elem ifNotNil: [ tally := tally + 1 ]].
81     ^tally
82   ]
84   grow [
85     "recreate ourselves in place with a new, bigger storage"
86     | old |
87     old := members.
88     members := Array new: (old size + growth).
89     "reinsert each existing Set member"
90     old do: [:elem | self add: elem]
91   ]
93   compare: t and: e [
94     ^t = e
95   ]
97   location: elem [
98     | pos start t |
99     start := pos := (elem hash % members size) + 1.
100     [ true ] whileTrue: [
101       "return this position if we match, or have reached a nil slot"
102       t := members at: pos.
103       ((t isNil) or: [self compare: t and: elem]) ifTrue: [
104         ^pos
105       ].
106       "advance to next slot, circularly"
107       pos := pos + 1.
108       (pos > members size) ifTrue: [
109         pos := 1
110       ].
111       "return nil if we have scanned the whole Set"
112       (pos = start) ifTrue: [ ^nil ]
113     ]
114   ]
116   add: elem [
117     | pos |
118     "find the appropriate slot... if none, need to grow the Set"
119     pos := self location: elem.
120     pos ifNil: [
121       self grow.
122       ^self add: elem
123     ].
124     "if the slot is nil, this is a new entry which we put in place now.
125      if it wasn't nil, we still restore it so that if it's an association, the value portion will be updated"
126     members at: pos put: elem.
127     ^elem
128   ]
130   << elem [
131     ^self add: elem
132   ]
134   rehash: start [
135     | pos elem |
136     pos := start.
137     [ true ] whileTrue: [
138       "advance to next slot, ceasing when we reach our start"
139       pos := pos + 1.
140       (pos > members size) ifTrue: [ pos := 1 ].
141       (pos = start) ifTrue: [ ^self ].
142       "if we reach a nil slot, there are no further rehash worries"
143       elem := members at: pos.
144       elem ifNil: [ ^self ].
145       "nil out the slot, and then reinsert the element"
146       members at: pos put: nil.
147       self add: elem
148     ]
149   ]
151   remove: elem ifAbsent: aBlock [
152     | pos |
153     "if not found, return error"
154     (members at: ((pos := self location: elem) ifNil: [ ^aBlock value ])) ifNil: [ ^aBlock value ].
155     "remove our element from the Set"
156     members at: pos put: nil.
157     "rehash all that follow"
158     self rehash: pos.
159     ^elem
160   ]
162   remove: elem [
163     ^self remove: elem ifAbsent: [ self noElement ]
164   ]
166   do: aBlock [
167     members do: [:elem | elem ifNotNil: [ aBlock value: elem ]]
168   ]
170   indexOf: elem [
171     ^self indexOf: elem ifAbsent: [ nil ]
172   ]
174   indexOf: elem ifAbsent: aBlock [
175     | pos |
176     (members at: ((pos := self location: elem) ifNil: [ ^aBlock value ])) ifNil: [ ^aBlock value ].
177     ^pos
178   ]
180   at: elem ifAbsent: aBlock [
181     ^(members at: ((self location: elem) ifNil: [ ^aBlock value ])) ifNil: [ ^aBlock value ].
182   ]
187 Set subclass: IdentitySet [
188   compare: t and: e [
189     ^t == e
190   ]