author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 31 Oct 2011 22:19:21 +0000 | |
branch | jv |
changeset 17892 | d86c8bd5ece3 |
parent 17869 | 9610c6c94e71 |
child 17910 | 8d796ca8bd1d |
permissions | -rw-r--r-- |
1 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
250 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5559 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
Set subclass:#IdentitySet |
1290 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Collections-Unordered' |
|
1 | 19 |
! |
20 |
||
88 | 21 |
!IdentitySet class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
250 | 26 |
All Rights Reserved |
1 | 27 |
|
88 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
1 | 36 |
|
88 | 37 |
documentation |
38 |
" |
|
39 |
same as a Set but compares elements using == |
|
40 |
(i.e. they must be identical - not just equal in structure). |
|
41 |
Since compare is on identity (using ==), hashing is also done via |
|
42 |
#identityHash instead of #hash. |
|
1290 | 43 |
|
44 |
[author:] |
|
45 |
Claus Gittinger |
|
88 | 46 |
" |
47 |
! ! |
|
1 | 48 |
|
7310
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
49 |
!IdentitySet methodsFor:'Compatibility-Squeak'! |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
50 |
|
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
51 |
copyWithout:anElement |
11051 | 52 |
"return a new collection consisting of a copy of the receiver, with |
53 |
ALL elements equal to elementToSkip are left out. |
|
54 |
No error is reported, if elementToSkip is not in the collection." |
|
55 |
||
7310
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
56 |
^ self select:[:each | each ~~ anElement] |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
57 |
|
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
58 |
" |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
59 |
#(1 2 3 4 5 6 7) asSet copyWithout:5 |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
60 |
" |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
61 |
! |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
62 |
|
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
63 |
copyWithoutAll:aCollection |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
64 |
^ self select:[:each | (aCollection includesIdentical:each)] |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
65 |
|
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
66 |
" |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
67 |
#(1 2 3 4 5 6 7) asSet copyWithoutAll:#(3 5 7 9) |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
68 |
" |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
69 |
! ! |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
70 |
|
4625 | 71 |
!IdentitySet methodsFor:'adding & removing'! |
72 |
||
73 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
74 |
^ super remove:anObject ifAbsent:exceptionBlock |
|
75 |
! ! |
|
76 |
||
1 | 77 |
!IdentitySet methodsFor:'private'! |
78 |
||
79 |
find:key ifAbsent:aBlock |
|
80 |
"Look for the key in the receiver. If it is found, return |
|
81 |
the index of the slot containing the key, otherwise |
|
82 |
return the value of evaluating aBlock. |
|
83 |
Redefined to compare for identity instead of equality" |
|
84 |
||
6233 | 85 |
|index "{ Class:SmallInteger }" |
86 |
length "{ Class:SmallInteger }" |
|
87 |
startIndex probe | |
|
1 | 88 |
|
13 | 89 |
length := keyArray basicSize. |
1126
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
90 |
|
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
91 |
"/ |
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
92 |
"/ length < 10 ifTrue:[ |
6233 | 93 |
"/ "assuming, that for small sets the overhead of hashing |
94 |
"/ is large ..." |
|
95 |
"/ ^ keyArray identityIndexOf:key ifAbsent:aBlock. |
|
1126
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
96 |
"/ ]. |
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
97 |
"/ |
10 | 98 |
|
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
99 |
startIndex := index := self initialIndexForKey:key. |
1 | 100 |
|
101 |
[true] whileTrue:[ |
|
6233 | 102 |
probe := keyArray basicAt:index. |
103 |
probe == key ifTrue:[^ index]. "<<<< == is different from inherited" |
|
104 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value]. |
|
1 | 105 |
|
6233 | 106 |
index == length ifTrue:[ |
107 |
index := 1 |
|
108 |
] ifFalse:[ |
|
109 |
index := index + 1 |
|
110 |
]. |
|
111 |
index == startIndex ifTrue:[ |
|
112 |
^ aBlock value |
|
113 |
] |
|
1 | 114 |
] |
115 |
! |
|
116 |
||
13 | 117 |
findKeyOrNil:key |
1126
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
118 |
"Look for the key in the receiver. |
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
119 |
If it is found, return return the index of the first unused slot. |
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
120 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 121 |
|
1126
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
122 |
|index "{ Class:SmallInteger }" |
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
123 |
length "{ Class:SmallInteger }" |
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
124 |
startIndex probe |
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
125 |
delIndex "{ Class:SmallInteger }" | |
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
126 |
|
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
127 |
delIndex := 0. |
1 | 128 |
|
13 | 129 |
length := keyArray basicSize. |
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
130 |
startIndex := index := self initialIndexForKey:key. |
1 | 131 |
|
132 |
[true] whileTrue:[ |
|
6346 | 133 |
probe := keyArray basicAt:index. |
134 |
key == probe ifTrue:[^ index]. |
|
135 |
(self slotIsEmpty:probe) ifTrue:[ |
|
136 |
delIndex == 0 ifTrue:[^ index]. |
|
137 |
keyArray basicAt:delIndex put:nil. |
|
138 |
^ delIndex |
|
139 |
]. |
|
1126
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
140 |
|
6346 | 141 |
probe == DeletedEntry ifTrue:[ |
142 |
delIndex == 0 ifTrue:[ |
|
143 |
delIndex := index |
|
144 |
] |
|
145 |
]. |
|
1 | 146 |
|
6346 | 147 |
index == length ifTrue:[ |
148 |
index := 1 |
|
149 |
] ifFalse:[ |
|
150 |
index := index + 1 |
|
151 |
]. |
|
152 |
index == startIndex ifTrue:[ |
|
153 |
delIndex ~~ 0 ifTrue:[ |
|
154 |
keyArray basicAt:delIndex put:nil. |
|
155 |
^ delIndex |
|
156 |
]. |
|
157 |
^ self grow findKeyOrNil:key |
|
158 |
]. |
|
1 | 159 |
] |
1126
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
160 |
|
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
634
diff
changeset
|
161 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
10 | 162 |
! |
163 |
||
2460 | 164 |
hashFor:aKey |
165 |
"return an initial index given a key." |
|
166 |
||
167 |
^ aKey identityHash |
|
168 |
||
169 |
"Created: 19.3.1997 / 15:18:59 / cg" |
|
1 | 170 |
! ! |
609 | 171 |
|
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
172 |
!IdentitySet methodsFor:'testing'! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
173 |
|
5559 | 174 |
identicalContentsAs:aCollection |
175 |
"return true if the receiver and aCollection represent collections |
|
176 |
with identical contents (but not caring for order)." |
|
177 |
||
178 |
aCollection size == self size ifFalse:[^ false]. |
|
10091
ad0bf1206ce0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
7310
diff
changeset
|
179 |
^ aCollection conform:[:eachElement | (self includesIdentical:eachElement)]. |
5559 | 180 |
|
181 |
" |
|
182 |
|col| |
|
183 |
||
184 |
col := #('aaa' 'bbb' 'ccc' 'ddd'). |
|
185 |
col identicalContentsAs:(col asIdentitySet). |
|
186 |
col identicalContentsAs:(col copy asIdentitySet). |
|
187 |
col identicalContentsAs:(col deepCopy asIdentitySet). |
|
188 |
" |
|
10091
ad0bf1206ce0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
7310
diff
changeset
|
189 |
|
ad0bf1206ce0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
7310
diff
changeset
|
190 |
"Modified: / 13-10-2006 / 12:55:43 / cg" |
5559 | 191 |
! |
192 |
||
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
193 |
includesIdentical:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
194 |
"for identitySet, the #includes: test already tests for identity" |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
195 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
196 |
^ self includes:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
197 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
198 |
"Created: / 11.12.1998 / 20:01:12 / cg" |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
199 |
! ! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
200 |
|
634 | 201 |
!IdentitySet class methodsFor:'documentation'! |
202 |
||
203 |
version |
|
17892 | 204 |
^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.31 2008/06/09 18:47:23 cg Exp $' |
17865 | 205 |
! |
206 |
||
207 |
version_SVN |
|
17892 | 208 |
^ '$Id: IdentitySet.st 10729 2011-10-31 22:19:21Z vranyj1 $' |
17846 | 209 |
! ! |
17892 | 210 |