1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
175
|
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 |
"
|
|
12 |
|
|
13 |
Dictionary subclass:#IdentityDictionary
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Unordered'
|
|
18 |
!
|
|
19 |
|
|
20 |
IdentityDictionary comment:'
|
88
|
21 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
175
|
22 |
All Rights Reserved
|
92
|
23 |
|
175
|
24 |
$Header: /cvs/stx/stx/libbasic/IdentityDictionary.st,v 1.10 1994-10-28 01:24:40 claus Exp $
|
88
|
25 |
'!
|
1
|
26 |
|
88
|
27 |
!IdentityDictionary class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
175
|
32 |
All Rights Reserved
|
1
|
33 |
|
88
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
1
|
42 |
|
88
|
43 |
version
|
|
44 |
"
|
175
|
45 |
$Header: /cvs/stx/stx/libbasic/IdentityDictionary.st,v 1.10 1994-10-28 01:24:40 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
1
|
48 |
|
88
|
49 |
documentation
|
|
50 |
"
|
|
51 |
same as a Dictionary but key must be identical - not just equal.
|
|
52 |
Since compare is on identical keys (using ==), hashing is also done via
|
|
53 |
#identityHash instead of #hash.
|
|
54 |
IdentityDictionaries are especially useful, when symbols are used as keys.
|
|
55 |
"
|
|
56 |
! !
|
1
|
57 |
|
|
58 |
!IdentityDictionary methodsFor:'private'!
|
|
59 |
|
|
60 |
findKeyOrNil:key
|
|
61 |
"Look for the key in the receiver. If it is found, return
|
|
62 |
the index of the association containing the key, otherwise
|
|
63 |
return the index of the first unused slot. Grow the receiver,
|
|
64 |
if key was not found, and no unused slots where present"
|
|
65 |
|
|
66 |
|index "{ Class:SmallInteger }"
|
3
|
67 |
length startIndex probe |
|
1
|
68 |
|
|
69 |
length := keyArray basicSize.
|
|
70 |
startIndex := key identityHash \\ length + 1.
|
|
71 |
|
|
72 |
index := startIndex.
|
|
73 |
[true] whileTrue:[
|
175
|
74 |
probe := keyArray basicAt:index.
|
|
75 |
(probe isNil or: [probe == key]) ifTrue:[^ index].
|
|
76 |
probe == DeletedEntry ifTrue:[
|
|
77 |
keyArray basicAt:index put:nil.
|
|
78 |
^ index
|
|
79 |
].
|
1
|
80 |
|
175
|
81 |
index == length ifTrue:[
|
|
82 |
index := 1
|
|
83 |
] ifFalse:[
|
|
84 |
index := index + 1
|
|
85 |
].
|
|
86 |
index == startIndex ifTrue:[^ self grow findKeyOrNil:key].
|
1
|
87 |
]
|
|
88 |
!
|
|
89 |
|
13
|
90 |
findNil:key
|
|
91 |
"Look for the next slot usable for key. This method assumes that
|
|
92 |
key is not already in the receiver - used only while growing/rehashing"
|
|
93 |
|
|
94 |
|index "{ Class:SmallInteger }"
|
|
95 |
length|
|
|
96 |
|
|
97 |
length := keyArray basicSize.
|
|
98 |
index := key identityHash \\ length + 1.
|
|
99 |
|
|
100 |
[(keyArray basicAt:index) notNil] whileTrue:[
|
175
|
101 |
index == length ifTrue:[
|
|
102 |
index := 1
|
|
103 |
] ifFalse:[
|
|
104 |
index := index + 1
|
|
105 |
].
|
|
106 |
"notice: no check for no nil found - we must find one since
|
|
107 |
this is only called after growing"
|
13
|
108 |
].
|
|
109 |
^ index
|
|
110 |
|
|
111 |
!
|
|
112 |
|
|
113 |
find:key ifAbsent:aBlock
|
1
|
114 |
"Look for the key in the receiver. If it is found, return
|
|
115 |
the index of the association containing the key, otherwise
|
|
116 |
return the value of evaluating aBlock."
|
|
117 |
|
|
118 |
|index "{ Class:SmallInteger }"
|
|
119 |
length "{ Class:SmallInteger }"
|
|
120 |
startIndex
|
|
121 |
probe|
|
|
122 |
|
|
123 |
length := keyArray basicSize.
|
|
124 |
length < 10 ifTrue:[
|
175
|
125 |
"assuming, that for small dictionaries the overhead of hashing
|
|
126 |
is large ... maybe that proves wrong (if overhead of comparing
|
|
127 |
is high)"
|
|
128 |
^ keyArray identityIndexOf:key ifAbsent:aBlock.
|
1
|
129 |
].
|
|
130 |
|
|
131 |
startIndex := key identityHash \\ length + 1.
|
|
132 |
|
|
133 |
index := startIndex.
|
|
134 |
[true] whileTrue:[
|
175
|
135 |
probe := keyArray basicAt:index.
|
|
136 |
probe == key ifTrue:[^ index].
|
1
|
137 |
|
175
|
138 |
index == length ifTrue:[
|
|
139 |
index := 1
|
|
140 |
] ifFalse:[
|
|
141 |
index := index + 1
|
|
142 |
].
|
|
143 |
(probe isNil or:[index == startIndex]) ifTrue:[^ aBlock value]
|
1
|
144 |
]
|
77
|
145 |
!
|
|
146 |
|
|
147 |
emptyCollectionForKeys
|
175
|
148 |
"return an empty collection used for keys.
|
|
149 |
Made a separate method to allow redefinition for different kind of
|
|
150 |
containers in subclasses"
|
|
151 |
|
77
|
152 |
^ IdentitySet new:(self size)
|
1
|
153 |
! !
|