cache control slightly changed
[k8lst.git] / imgsrc / defs / base / bags / set.st
blobe743119280e777ad79502546a97c5335d96a156a
1 COMMENTS
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.
31 ENDCOMMENTS
34 COMMENT ----------Set------------
35 METHODS FOR Set
36 ^new: aSize [
37   | ret |
38   ret := super new.
39   self in: ret at: 1 put: (Array new: aSize).
40   self in: ret at: 2 put: aSize.
41   ^ret
44 ^with: objA [
45   | ret |
46   ret := self new.
47   ret add: objA.
48   ^ret
51 ^with: objA with: objB [
52   | ret |
53   ret := self new.
54   ret add: objA. ret add: objB.
55   ^ret
58 ^with: objA with: objB with: objC [
59   | ret |
60   ret := self new.
61   ret add: objA. ret add: objB. ret add: objC.
62   ^ret
65 ^new [
66   ^self new: 10
69 size [
70   | tally |
71   tally := 0.
72   members do: [:elem | elem ifNotNil: [ tally := tally + 1 ] ].
73   ^tally
76 grow [
77   "Re-create ourselves in place with a new, bigger storage"
78   | old |
79   old := members.
80   members := Array new: (old size + growth).
81   "Re-insert each existing Set member"
82   old do: [:elem | self add: elem]
85 compare: t and: e [
86   ^t = e
89 location: elem [
90   | pos start t |
91   start := pos := (elem hash mod: members size) + 1.
92   [ true ] whileTrue: [
93     "Return this position if we match, or have reached a nil slot"
94     t := members at: pos.
95     ((t isNil) or: [self compare: t and: elem]) ifTrue: [
96       ^pos
97     ].
98     "Advance to next slot, circularly"
99     pos := pos + 1.
100     (pos > members size) ifTrue: [
101       pos := 1
102     ].
103     "Return nil if we have scanned the whole Set"
104     (pos = start) ifTrue: [ ^nil ]
105   ]
108 add: elem [
109   | pos |
110   "Find the appropriate slot... if none, need to grow the Set"
111   pos := self location: elem.
112   pos ifNil: [
113     self grow.
114     ^self add: elem
115   ].
116   "If the slot is nil, this is a new entry which we put in place now.
117    If it wasn't nil, we still re-store it so that if it's an
118    Association, the value portion will be updated"
119   members at: pos put: elem.
120   ^elem
123 rehash: start [
124   | pos elem |
125   pos := start.
126   [ true ] whileTrue: [
127     "Advance to next slot, ceasing when we reach our start"
128     pos := pos + 1.
129     (pos > members size) ifTrue: [ pos := 1 ].
130     (pos = start) ifTrue: [ ^self ].
131     "If we reach a nil slot, there are no further rehash worries"
132     elem := members at: pos.
133     elem ifNil: [ ^self ].
134     "Nil out the slot, and then re-insert the element"
135     members at: pos put: nil.
136     self add: elem
137   ]
140 remove: elem ifAbsent: aBlock [
141   | pos |
142   "If not found, return error"
143   pos := self location: elem.
144   ((pos isNil) or: [(members at: pos) isNil]) ifTrue: [ ^aBlock value ].  "k8: was without ^"
145   "Remove our element from the Set"
146   members at: pos put: nil.
147   "Re-hash all that follow"
148   self rehash: pos.
149   ^elem
152 remove: elem [
153   ^self remove: elem ifAbsent: [ self noElement ]
156 do: aBlock [
157   members do: [:elem | elem ifNotNil: [ aBlock value: elem ]]
160 at: value ifAbsent: aBlock [
161   | pos |
162   pos := self location: value.
163   ((pos isNil) or: [ (members at: pos) isNil ]) ifTrue: [ ^aBlock value ].
164   ^value
167 indexOf: value [
168   ^self at: value ifAbsent: [ nil ]
173 COMMENT ----------IdentitySet------------
174 METHOD FOR IdentitySet
175 compare: t and: e [
176   ^t == e