author | Claus Gittinger <cg@exept.de> |
Mon, 16 Dec 2019 11:42:04 +0100 | |
changeset 25127 | 22ff3c08a076 |
parent 24815 | 6c60da5b8b14 |
child 25376 | 88a3329875ba |
permissions | -rw-r--r-- |
328 | 1 |
" |
2183 | 2 |
COPYRIGHT (c) 1995 by eXept Software AG |
2110 | 3 |
All Rights Reserved |
328 | 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 |
" |
|
5557 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
19843 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
9481 | 16 |
KeyedCollection variableSubclass:#MethodDictionary |
2246
57451418f10a
make certain, loops are inlined
Claus Gittinger <cg@exept.de>
parents:
2183
diff
changeset
|
17 |
instanceVariableNames:'' |
23819 | 18 |
classVariableNames:'EmptySingleton' |
2246
57451418f10a
make certain, loops are inlined
Claus Gittinger <cg@exept.de>
parents:
2183
diff
changeset
|
19 |
poolDictionaries:'' |
57451418f10a
make certain, loops are inlined
Claus Gittinger <cg@exept.de>
parents:
2183
diff
changeset
|
20 |
category:'Kernel-Methods' |
328 | 21 |
! |
22 |
||
2109
c019b0e813ec
oops removeKey:ifAbsent: should send #value to the block
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
23 |
!MethodDictionary class methodsFor:'documentation'! |
328 | 24 |
|
25 |
copyright |
|
26 |
" |
|
2183 | 27 |
COPYRIGHT (c) 1995 by eXept Software AG |
2110 | 28 |
All Rights Reserved |
328 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
Instances of MethodDictionary store selector/method associations |
|
8467 | 42 |
in classes. Conceptionally, they behave like IdentityDictionaries, but are |
43 |
implemented using a single array (instead of Dictionary, which uses |
|
328 | 44 |
two arrays to store keys and values separately). |
45 |
Also, they do not use hashing, since due to caching in the VM, hashing |
|
46 |
does not make too much of a difference in speed, but complicates the |
|
8467 | 47 |
VM implementation. |
1465 | 48 |
|
49 |
[author:] |
|
50 |
Stefan Vogel |
|
51 |
||
52 |
[see also:] |
|
53 |
Dictionary |
|
8467 | 54 |
IdentityDictionary |
1465 | 55 |
Behavior Class |
8467 | 56 |
Method |
57 |
Symbol |
|
328 | 58 |
" |
59 |
! ! |
|
60 |
||
2109
c019b0e813ec
oops removeKey:ifAbsent: should send #value to the block
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
61 |
!MethodDictionary class methodsFor:'instance creation'! |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
62 |
|
23819 | 63 |
new |
64 |
"create and return an empty methodDictionary. |
|
65 |
Because mDicts cannot grow, we return a singleton |
|
66 |
(if any selectors are added later, a new instance |
|
67 |
will be created anyway). |
|
68 |
Using a singleton will speed up dynamic creation of |
|
69 |
new classes which will be filled in later" |
|
70 |
||
71 |
EmptySingleton isNil ifTrue:[ |
|
72 |
EmptySingleton := super new. |
|
73 |
]. |
|
74 |
^ EmptySingleton |
|
75 |
||
76 |
"Created: / 03-03-2019 / 15:27:43 / Claus Gittinger" |
|
77 |
! |
|
78 |
||
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
79 |
new:sz |
2897 | 80 |
"create and return a new methodDictionary holding sz |
81 |
key->value associations" |
|
82 |
||
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
83 |
^ self basicNew:(sz * 2) |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
84 |
! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
85 |
|
22309 | 86 |
newWithCapacity:size |
87 |
"return a new empty Collection with capacity for n elements." |
|
88 |
||
89 |
^ self new:size |
|
90 |
||
91 |
"Created: / 10-10-2017 / 17:51:14 / stefan" |
|
92 |
! |
|
93 |
||
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
94 |
withAll:aDictionary |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
95 |
"create a MethodDictionary from another Dictionary" |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
96 |
|
1519 | 97 |
|newDict i "{ Class: SmallInteger }" | |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
98 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
99 |
newDict := self new:aDictionary size. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
100 |
i := 1. |
1519 | 101 |
aDictionary keysAndValuesDo:[:key :value | |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
102 |
newDict basicAt:i put:key. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
103 |
newDict basicAt:i+1 put:value. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
104 |
i := i+2. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
105 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
106 |
^ newDict |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
107 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
108 |
" |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
109 |
|d| |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
110 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
111 |
d := Dictionary withKeys:#(a b c d e) andValues:#(1 2 3 4 5). |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
112 |
MethodDictionary withAll:d. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
113 |
" |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
114 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
115 |
"Created: 12.6.1996 / 13:46:43 / stefan" |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
116 |
"Modified: 12.6.1996 / 13:56:36 / stefan" |
1519 | 117 |
"Modified: 3.7.1996 / 11:05:55 / cg" |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
118 |
! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
119 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
120 |
withKeys:keys andValues:values |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
121 |
"create a MethodDictionary from a key (selector) array and value (method) array" |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
122 |
|
2897 | 123 |
|inst |
124 |
sz "{ Class: SmallInteger }" |
|
125 |
idx "{ Class: SmallInteger }" | |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
126 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
127 |
sz := keys size. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
128 |
inst := self new:sz. |
2897 | 129 |
idx := 2. |
1519 | 130 |
1 to:sz do:[:i | |
2897 | 131 |
inst basicAt:(idx-1) put:(keys at:i). |
132 |
inst basicAt:(idx) put:(values at:i). |
|
133 |
idx := idx + 2. |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
134 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
135 |
^ inst |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
136 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
137 |
"Created: 12.6.1996 / 13:46:43 / stefan" |
1519 | 138 |
"Modified: 3.7.1996 / 11:05:34 / cg" |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
139 |
! ! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
140 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
141 |
|
2109
c019b0e813ec
oops removeKey:ifAbsent: should send #value to the block
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
142 |
!MethodDictionary class methodsFor:'queries'! |
328 | 143 |
|
144 |
isBuiltInClass |
|
145 |
"this class is known by the run-time-system" |
|
146 |
||
147 |
^ self == MethodDictionary |
|
9481 | 148 |
|
149 |
"Modified: / 08-08-2006 / 16:06:26 / cg" |
|
328 | 150 |
! ! |
151 |
||
152 |
!MethodDictionary methodsFor:'accessing'! |
|
153 |
||
154 |
at:key ifAbsent:exceptionBlock |
|
155 |
"return the element indexed by aKey - |
|
156 |
return result of exceptionBlock if no element is stored under aKey" |
|
157 |
||
158 |
|sz "{ Class: SmallInteger }"| |
|
159 |
||
160 |
sz := self basicSize. |
|
161 |
1 to:sz by:2 do:[:i | |
|
2897 | 162 |
(self basicAt:i) == key ifTrue:[^ self basicAt:(i + 1)] |
328 | 163 |
]. |
164 |
^ exceptionBlock value |
|
165 |
! |
|
166 |
||
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
167 |
at:key put:value |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
168 |
"set the value for a given key, which is supposed to be a symbol. |
2246
57451418f10a
make certain, loops are inlined
Claus Gittinger <cg@exept.de>
parents:
2183
diff
changeset
|
169 |
In contrast to dictionaries, we allow adding elements only, if there is an |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
170 |
empty slot (nil key) present." |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
171 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
172 |
|slot sz "{ Class: SmallInteger }"| |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
173 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
174 |
sz := self basicSize. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
175 |
1 to:sz by:2 do:[:i | |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
176 |
slot := self basicAt:i. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
177 |
(slot == key) ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
178 |
^ self basicAt:(i + 1) put:value |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
179 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
180 |
slot isNil ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
181 |
self basicAt:i put:key. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
182 |
^ self basicAt:(i + 1) put:value |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
183 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
184 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
185 |
^ self errorKeyNotFound:key |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
186 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
187 |
"Modified: 7.6.1996 / 09:39:04 / stefan" |
2246
57451418f10a
make certain, loops are inlined
Claus Gittinger <cg@exept.de>
parents:
2183
diff
changeset
|
188 |
"Modified: 23.1.1997 / 13:59:29 / cg" |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
189 |
! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
190 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
191 |
at:key putOrAppend:value |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
192 |
"set the value for a given key, which is supposed to be a symbol. |
2246
57451418f10a
make certain, loops are inlined
Claus Gittinger <cg@exept.de>
parents:
2183
diff
changeset
|
193 |
In contrast to dictionaries, we allow adding elements only, if there is an |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
194 |
empty slot (nil key) present. |
2246
57451418f10a
make certain, loops are inlined
Claus Gittinger <cg@exept.de>
parents:
2183
diff
changeset
|
195 |
Otherwise a new MethodDictionary is created & returned" |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
196 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
197 |
|slot emptySlot newDict sz "{ Class: SmallInteger }"| |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
198 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
199 |
sz := self basicSize. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
200 |
1 to:sz by:2 do:[:i | |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
201 |
slot := self basicAt:i. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
202 |
(slot == key) ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
203 |
self basicAt:(i + 1) put:value . |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
204 |
^ self. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
205 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
206 |
slot isNil ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
207 |
emptySlot := i. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
208 |
] |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
209 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
210 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
211 |
emptySlot notNil ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
212 |
self basicAt:emptySlot put:key. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
213 |
self basicAt:(emptySlot + 1) put:value. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
214 |
^ self. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
215 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
216 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
217 |
"/ not enough room for new entry, copy to new dictionary |
2406 | 218 |
newDict := self class new:sz//2+1. |
2183 | 219 |
|
21656 | 220 |
"/ cannot do this (not defined in Object!!) ... |
2183 | 221 |
"/ newDict replaceFrom:1 to:sz with:self startingAt:1. |
222 |
"/ must use basicAt |
|
223 |
1 to:sz do:[:i | |
|
2246
57451418f10a
make certain, loops are inlined
Claus Gittinger <cg@exept.de>
parents:
2183
diff
changeset
|
224 |
newDict basicAt:i put:(self basicAt:i). |
2183 | 225 |
]. |
226 |
||
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
227 |
newDict basicAt:(sz+1) put:key. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
228 |
newDict basicAt:(sz+2) put:value. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
229 |
^ newDict. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
230 |
|
21656 | 231 |
"Created: / 07-06-1996 / 15:01:54 / stefan" |
232 |
"Modified: / 07-06-1996 / 17:32:40 / stefan" |
|
233 |
"Modified: / 23-01-1997 / 14:00:03 / cg" |
|
234 |
"Modified (comment): / 17-03-2017 / 11:26:44 / stefan" |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
235 |
! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
236 |
|
21370 | 237 |
keyAtIdenticalValue:value ifAbsent:exceptionBlock |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
238 |
"return the first key with value - |
21370 | 239 |
return result of exceptionBlock if no key can be found. |
240 |
This is a slow access, since the receiver is searched sequentially. |
|
241 |
NOTICE: |
|
242 |
The value is searched using identity compare" |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
243 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
244 |
|sz "{ Class: SmallInteger }"| |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
245 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
246 |
sz := self basicSize. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
247 |
2 to:sz by:2 do:[:i | |
2897 | 248 |
(self basicAt:i) == value ifTrue:[^ self basicAt:(i - 1)] |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
249 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
250 |
^ exceptionBlock value |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
251 |
|
21370 | 252 |
"Created: / 07-02-2017 / 11:14:40 / cg" |
21654 | 253 |
! |
254 |
||
255 |
keyAtValue:value |
|
256 |
"return the key under which value is stored. |
|
257 |
Raise an error if not found. |
|
258 |
This is a slow access, since the receiver is searched sequentially. |
|
259 |
NOTICE: |
|
260 |
The value is searched using identity compare. |
|
261 |
use #keyAtEqualValue:ifAbsent: to compare for equality." |
|
262 |
||
263 |
^ self keyAtIdenticalValue:value. |
|
264 |
||
265 |
"Created: / 19-06-1998 / 00:49:16 / cg" |
|
266 |
"Modified (comment): / 07-02-2017 / 11:13:29 / cg" |
|
267 |
! |
|
268 |
||
269 |
keyAtValue:value ifAbsent:exceptionBlock |
|
270 |
"return the key under which value is stored. |
|
271 |
If not found, return the value from evaluating exceptionBlock. |
|
272 |
This is a slow access, since the receiver is searched sequentially. |
|
273 |
NOTICE: |
|
274 |
The value is searched using identity compare; |
|
275 |
use #keyAtEqualValue:ifAbsent: to compare for equality." |
|
276 |
||
277 |
^ self keyAtIdenticalValue:value ifAbsent:exceptionBlock. |
|
278 |
||
279 |
"Created: / 19-06-1998 / 00:50:34 / cg" |
|
280 |
"Modified: / 07-02-2017 / 11:13:20 / cg" |
|
9481 | 281 |
! ! |
282 |
||
283 |
||
284 |
!MethodDictionary methodsFor:'enumerating'! |
|
285 |
||
17251 | 286 |
do:aBlock |
287 |
"evaluate aBlock for each value (i.e. each method)" |
|
288 |
||
289 |
|key value sz "{ Class: SmallInteger }"| |
|
290 |
||
291 |
sz := self basicSize. |
|
292 |
1 to:sz by:2 do:[:i | |
|
293 |
key := self basicAt:i. |
|
294 |
key notNil ifTrue:[ |
|
295 |
value := self basicAt:(i+1). |
|
296 |
value notNil ifTrue:[ |
|
297 |
aBlock value:value. |
|
298 |
] |
|
299 |
] |
|
300 |
]. |
|
301 |
! |
|
302 |
||
9481 | 303 |
keysAndValuesDo:aBlock |
304 |
"evaluate the 2 arg block aBlock for each key (i.e. each selector) |
|
305 |
and each value (i.e. each method)" |
|
306 |
||
307 |
|key value sz "{ Class: SmallInteger }"| |
|
308 |
||
309 |
sz := self basicSize. |
|
310 |
1 to:sz by:2 do:[:i | |
|
21656 | 311 |
key := self basicAt:i. |
312 |
value := self basicAt:(i+1). |
|
313 |
(key notNil and:[value notNil]) ifTrue:[ |
|
314 |
aBlock value:key value:value. |
|
9481 | 315 |
] |
316 |
]. |
|
317 |
||
318 |
"Created: / 07-06-1996 / 09:27:42 / stefan" |
|
319 |
"Modified: / 08-08-2006 / 16:11:30 / cg" |
|
21656 | 320 |
"Modified: / 17-03-2017 / 11:07:07 / stefan" |
24480 | 321 |
! |
322 |
||
323 |
keysDo:aBlock |
|
324 |
"evaluate aBlock for each key (i.e. each selector)" |
|
325 |
||
326 |
|key value sz "{ Class: SmallInteger }"| |
|
327 |
||
328 |
sz := self basicSize. |
|
329 |
1 to:sz by:2 do:[:i | |
|
330 |
key := self basicAt:i. |
|
331 |
value := self basicAt:(i+1). |
|
332 |
(key notNil and:[value notNil]) ifTrue:[ |
|
333 |
aBlock value:key. |
|
334 |
] |
|
335 |
]. |
|
9481 | 336 |
! ! |
337 |
||
338 |
!MethodDictionary methodsFor:'private'! |
|
339 |
||
23821 | 340 |
atIndex:idx putKey:key andValue:value |
341 |
"set the key, which is supposed to be a symbol, |
|
342 |
and the value at idx. |
|
343 |
Only use this to initialize a methodDictionary fast, |
|
344 |
when the set of methods is known beforehand |
|
345 |
(avoids search for nil slots)" |
|
346 |
||
23823 | 347 |
self basicAt:(idx*2)-1 put:key. |
348 |
self basicAt:(idx*2) put:value |
|
23821 | 349 |
|
350 |
"Created: / 03-03-2019 / 19:18:44 / Claus Gittinger" |
|
351 |
! |
|
352 |
||
9481 | 353 |
compressed |
354 |
"compress - return either the myself or a new, compressed MethodDictionary" |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
355 |
|
9481 | 356 |
|newDict tally key mySize |
357 |
dstIndex "{ Class: SmallInteger }" |
|
358 |
sz "{ Class: SmallInteger }" | |
|
359 |
||
360 |
sz := self basicSize. |
|
361 |
mySize := sz // 2. |
|
362 |
tally := 0. |
|
363 |
1 to:sz by:2 do:[:i | |
|
364 |
(self basicAt:i) notNil ifTrue:[ |
|
365 |
tally := tally + 1 |
|
366 |
] |
|
367 |
]. |
|
368 |
||
369 |
tally == mySize ifTrue:[^ self]. |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
370 |
|
9481 | 371 |
newDict := self species new:tally. |
372 |
dstIndex := 1. |
|
373 |
1 to:sz by:2 do:[:i | |
|
374 |
key := self basicAt:i. |
|
375 |
key notNil ifTrue:[ |
|
376 |
newDict basicAt:dstIndex put:key. |
|
377 |
newDict basicAt:dstIndex+1 put:(self basicAt:i+1). |
|
378 |
dstIndex := dstIndex + 2. |
|
379 |
] |
|
380 |
]. |
|
381 |
^ newDict |
|
382 |
||
383 |
"Modified: / 05-08-2004 / 20:05:44 / stefan" |
|
384 |
! ! |
|
385 |
||
386 |
!MethodDictionary methodsFor:'queries'! |
|
387 |
||
388 |
size |
|
389 |
"return the number of elements (associations) in the receiver" |
|
390 |
||
391 |
^ self basicSize // 2 |
|
21266 | 392 |
! |
393 |
||
24815 | 394 |
speciesForAdding |
395 |
"like species, but redefined for collections which cannot grow easily. |
|
396 |
Used by functions which create a growing collection (see collect:with:, for example)" |
|
397 |
||
398 |
^ Dictionary |
|
399 |
! |
|
400 |
||
21266 | 401 |
speciesForCollecting |
402 |
^ Bag |
|
403 |
||
404 |
"Created: / 20-01-2017 / 18:07:53 / stefan" |
|
9481 | 405 |
! ! |
406 |
||
407 |
!MethodDictionary methodsFor:'removing'! |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
408 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
409 |
removeKey:key ifAbsent:failBlock |
19843 | 410 |
"remove key from dictionary, |
411 |
return the value previously stored there. |
|
412 |
If it was not in the collection return the result |
|
413 |
from evaluating failBlock. |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
414 |
We actually do not remove it, but set it to nil." |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
415 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
416 |
|value sz "{ Class: SmallInteger }"| |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
417 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
418 |
sz := self basicSize. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
419 |
1 to:sz by:2 do:[:i | |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
420 |
(self basicAt:i) == key ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
421 |
value := self basicAt:(i + 1). |
9444 | 422 |
self basicAt:i put:nil. |
423 |
self basicAt:(i+1) put:nil. |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
424 |
^ value |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
425 |
] |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
426 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
427 |
|
2109
c019b0e813ec
oops removeKey:ifAbsent: should send #value to the block
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
428 |
^ failBlock value. |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
429 |
|
9444 | 430 |
"Created: / 07-06-1996 / 15:57:56 / stefan" |
431 |
"Modified: / 16-07-2006 / 13:08:41 / cg" |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
432 |
! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
433 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
434 |
removeKeyAndCompress:key |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
435 |
"remove key from dictionary. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
436 |
A new, compressed MethodDictionary will be returned, |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
437 |
or nil, if key is not present." |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
438 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
439 |
|newDict dstIndex sz "{ Class: SmallInteger }"| |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
440 |
|
2402
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
441 |
sz := self basicSize. |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
442 |
1 to:sz by:2 do:[:i | |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
443 |
(self basicAt:i) == key ifTrue:[ |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
444 |
dstIndex := i |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
445 |
] |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
446 |
]. |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
447 |
|
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
448 |
dstIndex isNil ifTrue:[^ self]. |
1508
960d6e7e563b
removeKeyAndCompress - failed if key was not present.
Claus Gittinger <cg@exept.de>
parents:
1485
diff
changeset
|
449 |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
450 |
sz := self basicSize. |
2406 | 451 |
newDict := self class new:(sz//2-1). |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
452 |
dstIndex := 1. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
453 |
1 to:sz by:2 do:[:i | |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
454 |
(self basicAt:i) ~~ key ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
455 |
newDict basicAt:dstIndex put:(self basicAt:i). |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
456 |
newDict basicAt:dstIndex+1 put:(self basicAt:i+1). |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
457 |
dstIndex := dstIndex + 2. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
458 |
] |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
459 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
460 |
dstIndex > sz ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
461 |
^ nil |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
462 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
463 |
^ newDict |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
464 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
465 |
"Created: 7.6.1996 / 15:57:56 / stefan" |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
466 |
"Modified: 7.6.1996 / 16:47:02 / stefan" |
2402
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
467 |
"Modified: 12.2.1997 / 19:47:23 / cg" |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
468 |
! ! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
469 |
|
2109
c019b0e813ec
oops removeKey:ifAbsent: should send #value to the block
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
470 |
!MethodDictionary class methodsFor:'documentation'! |
328 | 471 |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
472 |
version |
19843 | 473 |
^ '$Header$' |
13514 | 474 |
! |
475 |
||
476 |
version_CVS |
|
19843 | 477 |
^ '$Header$' |
328 | 478 |
! ! |
17251 | 479 |