1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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 |
"
|
|
12 |
|
|
13 |
Set subclass:#IdentitySet
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Unordered'
|
|
18 |
!
|
|
19 |
|
|
20 |
IdentitySet comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
same as a Set but compares elements using == (i.e. they must be identical
|
|
26 |
- not just equal in structure).
|
|
27 |
Since compare is on identity, hashing is also done via
|
|
28 |
identityHash instead of hash.
|
|
29 |
|
10
|
30 |
$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.4 1993-11-08 02:30:42 claus Exp $
|
1
|
31 |
|
|
32 |
written jan 93 by claus
|
|
33 |
'!
|
|
34 |
|
|
35 |
!IdentitySet methodsFor:'private'!
|
|
36 |
|
|
37 |
find:key ifAbsent:aBlock
|
|
38 |
"Look for the key in the receiver. If it is found, return
|
|
39 |
the index of the slot containing the key, otherwise
|
|
40 |
return the value of evaluating aBlock.
|
|
41 |
Redefined to compare for identity instead of equality"
|
|
42 |
|
|
43 |
|index "{ Class:SmallInteger }"
|
3
|
44 |
length startIndex probe |
|
1
|
45 |
|
|
46 |
length := contentsArray basicSize.
|
10
|
47 |
length < 10 ifTrue:[
|
|
48 |
"assuming, that for small sets the overhead of hashing
|
|
49 |
is large ... maybe that proves wrong (if overhead of comparing
|
|
50 |
is high)"
|
|
51 |
^ contentsArray identityIndexOf:key ifAbsent:aBlock.
|
|
52 |
].
|
|
53 |
|
1
|
54 |
startIndex := key identityHash \\ length + 1.
|
|
55 |
|
|
56 |
index := startIndex.
|
|
57 |
[true] whileTrue:[
|
|
58 |
probe := (contentsArray basicAt:index).
|
|
59 |
probe == key ifTrue:[^ index].
|
|
60 |
probe isNil ifTrue:[^ aBlock value].
|
|
61 |
|
|
62 |
index == length ifTrue:[
|
|
63 |
index := 1
|
|
64 |
] ifFalse:[
|
10
|
65 |
index := index + 1
|
|
66 |
].
|
1
|
67 |
index == startIndex ifTrue:[
|
|
68 |
^ aBlock value
|
|
69 |
]
|
|
70 |
]
|
|
71 |
!
|
|
72 |
|
|
73 |
findElementOrNil:key
|
|
74 |
"Look for the key in the receiver. Redefined to compare for
|
|
75 |
identity instead of equality"
|
|
76 |
|
|
77 |
|index "{ Class:SmallInteger }"
|
3
|
78 |
length startIndex probe |
|
1
|
79 |
|
|
80 |
length := contentsArray basicSize.
|
|
81 |
startIndex := key identityHash \\ length + 1.
|
|
82 |
|
|
83 |
index := startIndex.
|
|
84 |
[true] whileTrue:[
|
|
85 |
probe := contentsArray basicAt:index.
|
|
86 |
(probe isNil or: [key == probe]) ifTrue:[^ index].
|
|
87 |
|
|
88 |
index == length ifTrue:[
|
|
89 |
index := 1
|
|
90 |
] ifFalse:[
|
10
|
91 |
index := index + 1
|
|
92 |
].
|
1
|
93 |
index == startIndex ifTrue: [
|
|
94 |
^ self grow findElementOrNil:key
|
|
95 |
]
|
|
96 |
]
|
10
|
97 |
!
|
|
98 |
|
|
99 |
findNil:key
|
|
100 |
"Look for the next slot usable for key. This method assumes that
|
|
101 |
key is not already in the receiver - used only while growing/rehashing"
|
|
102 |
|
|
103 |
|index "{ Class:SmallInteger }"
|
|
104 |
length|
|
|
105 |
|
|
106 |
length := contentsArray basicSize.
|
|
107 |
index := key identityHash \\ length + 1.
|
|
108 |
|
|
109 |
[(contentsArray basicAt:index) notNil] whileTrue:[
|
|
110 |
index == length ifTrue:[
|
|
111 |
index := 1
|
|
112 |
] ifFalse:[
|
|
113 |
index := index + 1
|
|
114 |
].
|
|
115 |
"notice: no check for no nil found - we must find one since
|
|
116 |
this is only called after growing"
|
|
117 |
].
|
|
118 |
^ index
|
|
119 |
|
1
|
120 |
! !
|