1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1992 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 |
Dictionary subclass:#IdentityDictionary
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Unordered'
|
|
18 |
!
|
|
19 |
|
|
20 |
IdentityDictionary comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
same as a Dictionary but key must be identical - not just equal.
|
|
26 |
Since compare is on identity keys, hashing is also done via
|
|
27 |
identityHash instead of hash.
|
|
28 |
|
10
|
29 |
$Header: /cvs/stx/stx/libbasic/IdentityDictionary.st,v 1.4 1993-11-08 02:30:40 claus Exp $
|
1
|
30 |
|
|
31 |
written jul 92 by claus
|
|
32 |
'!
|
|
33 |
|
10
|
34 |
!IdentityDictionary methodsFor:'accessing'!
|
|
35 |
|
|
36 |
keyAtValue:aValue ifAbsent:exceptionBlock
|
|
37 |
"return the key whose value equals the argument, the value of the
|
|
38 |
exceptionBlock if none is found..
|
|
39 |
This is a slow access, since there is no fast reverse mapping"
|
|
40 |
|
|
41 |
keyArray keysAndValuesDo:[:index :aKey |
|
|
42 |
aKey notNil ifTrue:[
|
|
43 |
(valueArray at:index) == aValue ifTrue:[^ aKey].
|
|
44 |
]
|
|
45 |
].
|
|
46 |
^ exceptionBlock value
|
|
47 |
! !
|
|
48 |
|
1
|
49 |
!IdentityDictionary methodsFor:'private'!
|
|
50 |
|
|
51 |
findKeyOrNil:key
|
|
52 |
"Look for the key in the receiver. If it is found, return
|
|
53 |
the index of the association containing the key, otherwise
|
|
54 |
return the index of the first unused slot. Grow the receiver,
|
|
55 |
if key was not found, and no unused slots where present"
|
|
56 |
|
|
57 |
|index "{ Class:SmallInteger }"
|
3
|
58 |
length startIndex probe |
|
1
|
59 |
|
|
60 |
length := keyArray basicSize.
|
|
61 |
startIndex := key identityHash \\ length + 1.
|
|
62 |
|
|
63 |
index := startIndex.
|
|
64 |
[true] whileTrue:[
|
|
65 |
probe := keyArray basicAt:index.
|
|
66 |
(probe isNil or: [probe == key]) ifTrue:[^ index].
|
|
67 |
|
|
68 |
index == length ifTrue:[
|
|
69 |
index := 1
|
|
70 |
] ifFalse:[
|
|
71 |
index := index + 1
|
|
72 |
].
|
|
73 |
index == startIndex ifTrue:[^ self grow findKeyOrNil:key].
|
|
74 |
]
|
|
75 |
!
|
|
76 |
|
|
77 |
findKey:key ifAbsent:aBlock
|
|
78 |
"Look for the key in the receiver. If it is found, return
|
|
79 |
the index of the association containing the key, otherwise
|
|
80 |
return the value of evaluating aBlock."
|
|
81 |
|
|
82 |
|index "{ Class:SmallInteger }"
|
|
83 |
length "{ Class:SmallInteger }"
|
|
84 |
startIndex
|
|
85 |
probe|
|
|
86 |
|
|
87 |
length := keyArray basicSize.
|
|
88 |
length < 10 ifTrue:[
|
|
89 |
"assuming, that for small dictionaries the overhead of hashing
|
|
90 |
is large ... maybe that proves wrong (if overhead of comparing
|
|
91 |
is high)"
|
10
|
92 |
^ keyArray identityIndexOf:key ifAbsent:aBlock.
|
1
|
93 |
].
|
|
94 |
|
|
95 |
startIndex := key identityHash \\ length + 1.
|
|
96 |
|
|
97 |
index := startIndex.
|
|
98 |
[true] whileTrue:[
|
|
99 |
probe := keyArray basicAt:index.
|
|
100 |
probe == key ifTrue:[^ index].
|
|
101 |
|
|
102 |
index == length ifTrue:[
|
|
103 |
index := 1
|
|
104 |
] ifFalse:[
|
|
105 |
index := index + 1
|
|
106 |
].
|
|
107 |
(probe isNil or:[index == startIndex]) ifTrue:[^ aBlock value]
|
|
108 |
]
|
|
109 |
! !
|