author | Claus Gittinger <cg@exept.de> |
Sun, 03 May 2020 13:20:12 +0200 | |
changeset 25379 | 609d492f29a7 |
parent 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 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
86 |
withAll:aDictionary |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
87 |
"create a MethodDictionary from another Dictionary" |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
88 |
|
1519 | 89 |
|newDict i "{ Class: SmallInteger }" | |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
90 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
91 |
newDict := self new:aDictionary size. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
92 |
i := 1. |
1519 | 93 |
aDictionary keysAndValuesDo:[:key :value | |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
94 |
newDict basicAt:i put:key. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
95 |
newDict basicAt:i+1 put:value. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
96 |
i := i+2. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
97 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
98 |
^ newDict |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
99 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
100 |
" |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
101 |
|d| |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
102 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
103 |
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
|
104 |
MethodDictionary withAll:d. |
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 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
107 |
"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
|
108 |
"Modified: 12.6.1996 / 13:56:36 / stefan" |
1519 | 109 |
"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
|
110 |
! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
111 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
112 |
withKeys:keys andValues:values |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
113 |
"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
|
114 |
|
2897 | 115 |
|inst |
116 |
sz "{ Class: SmallInteger }" |
|
117 |
idx "{ Class: SmallInteger }" | |
|
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 |
sz := keys size. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
120 |
inst := self new:sz. |
2897 | 121 |
idx := 2. |
1519 | 122 |
1 to:sz do:[:i | |
2897 | 123 |
inst basicAt:(idx-1) put:(keys at:i). |
124 |
inst basicAt:(idx) put:(values at:i). |
|
125 |
idx := idx + 2. |
|
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 |
^ inst |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
128 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
129 |
"Created: 12.6.1996 / 13:46:43 / stefan" |
1519 | 130 |
"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
|
131 |
! ! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
132 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
133 |
|
2109
c019b0e813ec
oops removeKey:ifAbsent: should send #value to the block
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
134 |
!MethodDictionary class methodsFor:'queries'! |
328 | 135 |
|
136 |
isBuiltInClass |
|
137 |
"this class is known by the run-time-system" |
|
138 |
||
139 |
^ self == MethodDictionary |
|
9481 | 140 |
|
141 |
"Modified: / 08-08-2006 / 16:06:26 / cg" |
|
328 | 142 |
! ! |
143 |
||
144 |
!MethodDictionary methodsFor:'accessing'! |
|
145 |
||
146 |
at:key ifAbsent:exceptionBlock |
|
147 |
"return the element indexed by aKey - |
|
148 |
return result of exceptionBlock if no element is stored under aKey" |
|
149 |
||
150 |
|sz "{ Class: SmallInteger }"| |
|
151 |
||
152 |
sz := self basicSize. |
|
153 |
1 to:sz by:2 do:[:i | |
|
2897 | 154 |
(self basicAt:i) == key ifTrue:[^ self basicAt:(i + 1)] |
328 | 155 |
]. |
156 |
^ exceptionBlock value |
|
157 |
! |
|
158 |
||
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
159 |
at:key put:value |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
160 |
"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
|
161 |
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
|
162 |
empty slot (nil key) present." |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
163 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
164 |
|slot sz "{ Class: SmallInteger }"| |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
165 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
166 |
sz := self basicSize. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
167 |
1 to:sz by:2 do:[:i | |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
168 |
slot := self basicAt:i. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
169 |
(slot == key) ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
170 |
^ self basicAt:(i + 1) put:value |
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 isNil ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
173 |
self basicAt:i put:key. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
174 |
^ self basicAt:(i + 1) put:value |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
175 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
176 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
177 |
^ self errorKeyNotFound:key |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
178 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
179 |
"Modified: 7.6.1996 / 09:39:04 / stefan" |
2246
57451418f10a
make certain, loops are inlined
Claus Gittinger <cg@exept.de>
parents:
2183
diff
changeset
|
180 |
"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
|
181 |
! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
182 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
183 |
at:key putOrAppend:value |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
184 |
"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
|
185 |
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
|
186 |
empty slot (nil key) present. |
2246
57451418f10a
make certain, loops are inlined
Claus Gittinger <cg@exept.de>
parents:
2183
diff
changeset
|
187 |
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
|
188 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
189 |
|slot emptySlot newDict sz "{ Class: SmallInteger }"| |
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 |
sz := self basicSize. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
192 |
1 to:sz by:2 do:[:i | |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
193 |
slot := self basicAt:i. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
194 |
(slot == key) ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
195 |
self basicAt:(i + 1) put:value . |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
196 |
^ self. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
197 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
198 |
slot isNil ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
199 |
emptySlot := i. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
200 |
] |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
201 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
202 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
203 |
emptySlot notNil ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
204 |
self basicAt:emptySlot put:key. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
205 |
self basicAt:(emptySlot + 1) put:value. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
206 |
^ self. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
207 |
]. |
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 |
"/ not enough room for new entry, copy to new dictionary |
2406 | 210 |
newDict := self class new:sz//2+1. |
2183 | 211 |
|
21656 | 212 |
"/ cannot do this (not defined in Object!!) ... |
2183 | 213 |
"/ newDict replaceFrom:1 to:sz with:self startingAt:1. |
214 |
"/ must use basicAt |
|
215 |
1 to:sz do:[:i | |
|
2246
57451418f10a
make certain, loops are inlined
Claus Gittinger <cg@exept.de>
parents:
2183
diff
changeset
|
216 |
newDict basicAt:i put:(self basicAt:i). |
2183 | 217 |
]. |
218 |
||
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
219 |
newDict basicAt:(sz+1) put:key. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
220 |
newDict basicAt:(sz+2) put:value. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
221 |
^ newDict. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
222 |
|
21656 | 223 |
"Created: / 07-06-1996 / 15:01:54 / stefan" |
224 |
"Modified: / 07-06-1996 / 17:32:40 / stefan" |
|
225 |
"Modified: / 23-01-1997 / 14:00:03 / cg" |
|
226 |
"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
|
227 |
! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
228 |
|
21370 | 229 |
keyAtIdenticalValue:value ifAbsent:exceptionBlock |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
230 |
"return the first key with value - |
21370 | 231 |
return result of exceptionBlock if no key can be found. |
232 |
This is a slow access, since the receiver is searched sequentially. |
|
233 |
NOTICE: |
|
234 |
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
|
235 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
236 |
|sz "{ Class: SmallInteger }"| |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
237 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
238 |
sz := self basicSize. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
239 |
2 to:sz by:2 do:[:i | |
2897 | 240 |
(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
|
241 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
242 |
^ exceptionBlock value |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
243 |
|
21370 | 244 |
"Created: / 07-02-2017 / 11:14:40 / cg" |
21654 | 245 |
! |
246 |
||
247 |
keyAtValue:value |
|
248 |
"return the key under which value is stored. |
|
249 |
Raise an error if not found. |
|
250 |
This is a slow access, since the receiver is searched sequentially. |
|
251 |
NOTICE: |
|
252 |
The value is searched using identity compare. |
|
253 |
use #keyAtEqualValue:ifAbsent: to compare for equality." |
|
254 |
||
255 |
^ self keyAtIdenticalValue:value. |
|
256 |
||
257 |
"Created: / 19-06-1998 / 00:49:16 / cg" |
|
258 |
"Modified (comment): / 07-02-2017 / 11:13:29 / cg" |
|
259 |
! |
|
260 |
||
261 |
keyAtValue:value ifAbsent:exceptionBlock |
|
262 |
"return the key under which value is stored. |
|
263 |
If not found, return the value from evaluating exceptionBlock. |
|
264 |
This is a slow access, since the receiver is searched sequentially. |
|
265 |
NOTICE: |
|
266 |
The value is searched using identity compare; |
|
267 |
use #keyAtEqualValue:ifAbsent: to compare for equality." |
|
268 |
||
269 |
^ self keyAtIdenticalValue:value ifAbsent:exceptionBlock. |
|
270 |
||
271 |
"Created: / 19-06-1998 / 00:50:34 / cg" |
|
272 |
"Modified: / 07-02-2017 / 11:13:20 / cg" |
|
9481 | 273 |
! ! |
274 |
||
275 |
||
276 |
!MethodDictionary methodsFor:'enumerating'! |
|
277 |
||
17251 | 278 |
do:aBlock |
279 |
"evaluate aBlock for each value (i.e. each method)" |
|
280 |
||
281 |
|key value sz "{ Class: SmallInteger }"| |
|
282 |
||
283 |
sz := self basicSize. |
|
284 |
1 to:sz by:2 do:[:i | |
|
285 |
key := self basicAt:i. |
|
286 |
key notNil ifTrue:[ |
|
287 |
value := self basicAt:(i+1). |
|
288 |
value notNil ifTrue:[ |
|
289 |
aBlock value:value. |
|
290 |
] |
|
291 |
] |
|
292 |
]. |
|
293 |
! |
|
294 |
||
9481 | 295 |
keysAndValuesDo:aBlock |
296 |
"evaluate the 2 arg block aBlock for each key (i.e. each selector) |
|
297 |
and each value (i.e. each method)" |
|
298 |
||
299 |
|key value sz "{ Class: SmallInteger }"| |
|
300 |
||
301 |
sz := self basicSize. |
|
302 |
1 to:sz by:2 do:[:i | |
|
21656 | 303 |
key := self basicAt:i. |
304 |
value := self basicAt:(i+1). |
|
305 |
(key notNil and:[value notNil]) ifTrue:[ |
|
306 |
aBlock value:key value:value. |
|
9481 | 307 |
] |
308 |
]. |
|
309 |
||
310 |
"Created: / 07-06-1996 / 09:27:42 / stefan" |
|
311 |
"Modified: / 08-08-2006 / 16:11:30 / cg" |
|
21656 | 312 |
"Modified: / 17-03-2017 / 11:07:07 / stefan" |
24480 | 313 |
! |
314 |
||
315 |
keysDo:aBlock |
|
316 |
"evaluate aBlock for each key (i.e. each selector)" |
|
317 |
||
318 |
|key value sz "{ Class: SmallInteger }"| |
|
319 |
||
320 |
sz := self basicSize. |
|
321 |
1 to:sz by:2 do:[:i | |
|
322 |
key := self basicAt:i. |
|
323 |
value := self basicAt:(i+1). |
|
324 |
(key notNil and:[value notNil]) ifTrue:[ |
|
325 |
aBlock value:key. |
|
326 |
] |
|
327 |
]. |
|
9481 | 328 |
! ! |
329 |
||
330 |
!MethodDictionary methodsFor:'private'! |
|
331 |
||
23821 | 332 |
atIndex:idx putKey:key andValue:value |
333 |
"set the key, which is supposed to be a symbol, |
|
334 |
and the value at idx. |
|
335 |
Only use this to initialize a methodDictionary fast, |
|
336 |
when the set of methods is known beforehand |
|
337 |
(avoids search for nil slots)" |
|
338 |
||
23823 | 339 |
self basicAt:(idx*2)-1 put:key. |
340 |
self basicAt:(idx*2) put:value |
|
23821 | 341 |
|
342 |
"Created: / 03-03-2019 / 19:18:44 / Claus Gittinger" |
|
343 |
! |
|
344 |
||
9481 | 345 |
compressed |
346 |
"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
|
347 |
|
9481 | 348 |
|newDict tally key mySize |
349 |
dstIndex "{ Class: SmallInteger }" |
|
350 |
sz "{ Class: SmallInteger }" | |
|
351 |
||
352 |
sz := self basicSize. |
|
353 |
mySize := sz // 2. |
|
354 |
tally := 0. |
|
355 |
1 to:sz by:2 do:[:i | |
|
356 |
(self basicAt:i) notNil ifTrue:[ |
|
357 |
tally := tally + 1 |
|
358 |
] |
|
359 |
]. |
|
360 |
||
361 |
tally == mySize ifTrue:[^ self]. |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
362 |
|
9481 | 363 |
newDict := self species new:tally. |
364 |
dstIndex := 1. |
|
365 |
1 to:sz by:2 do:[:i | |
|
366 |
key := self basicAt:i. |
|
367 |
key notNil ifTrue:[ |
|
368 |
newDict basicAt:dstIndex put:key. |
|
369 |
newDict basicAt:dstIndex+1 put:(self basicAt:i+1). |
|
370 |
dstIndex := dstIndex + 2. |
|
371 |
] |
|
372 |
]. |
|
373 |
^ newDict |
|
374 |
||
375 |
"Modified: / 05-08-2004 / 20:05:44 / stefan" |
|
376 |
! ! |
|
377 |
||
378 |
!MethodDictionary methodsFor:'queries'! |
|
379 |
||
380 |
size |
|
381 |
"return the number of elements (associations) in the receiver" |
|
382 |
||
383 |
^ self basicSize // 2 |
|
21266 | 384 |
! |
385 |
||
24815 | 386 |
speciesForAdding |
387 |
"like species, but redefined for collections which cannot grow easily. |
|
388 |
Used by functions which create a growing collection (see collect:with:, for example)" |
|
389 |
||
390 |
^ Dictionary |
|
391 |
! |
|
392 |
||
21266 | 393 |
speciesForCollecting |
394 |
^ Bag |
|
395 |
||
396 |
"Created: / 20-01-2017 / 18:07:53 / stefan" |
|
9481 | 397 |
! ! |
398 |
||
399 |
!MethodDictionary methodsFor:'removing'! |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
400 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
401 |
removeKey:key ifAbsent:failBlock |
19843 | 402 |
"remove key from dictionary, |
403 |
return the value previously stored there. |
|
404 |
If it was not in the collection return the result |
|
405 |
from evaluating failBlock. |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
406 |
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
|
407 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
408 |
|value sz "{ Class: SmallInteger }"| |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
409 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
410 |
sz := self basicSize. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
411 |
1 to:sz by:2 do:[:i | |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
412 |
(self basicAt:i) == key ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
413 |
value := self basicAt:(i + 1). |
9444 | 414 |
self basicAt:i put:nil. |
415 |
self basicAt:(i+1) put:nil. |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
416 |
^ value |
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 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
419 |
|
2109
c019b0e813ec
oops removeKey:ifAbsent: should send #value to the block
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
420 |
^ failBlock value. |
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
421 |
|
9444 | 422 |
"Created: / 07-06-1996 / 15:57:56 / stefan" |
423 |
"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
|
424 |
! |
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 |
removeKeyAndCompress:key |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
427 |
"remove key from dictionary. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
428 |
A new, compressed MethodDictionary will be returned, |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
429 |
or nil, if key is not present." |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
430 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
431 |
|newDict dstIndex sz "{ Class: SmallInteger }"| |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
432 |
|
2402
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
433 |
sz := self basicSize. |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
434 |
1 to:sz by:2 do:[:i | |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
435 |
(self basicAt:i) == key ifTrue:[ |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
436 |
dstIndex := i |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
437 |
] |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
438 |
]. |
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
439 |
|
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
440 |
dstIndex isNil ifTrue:[^ self]. |
1508
960d6e7e563b
removeKeyAndCompress - failed if key was not present.
Claus Gittinger <cg@exept.de>
parents:
1485
diff
changeset
|
441 |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
442 |
sz := self basicSize. |
2406 | 443 |
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
|
444 |
dstIndex := 1. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
445 |
1 to:sz by:2 do:[:i | |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
446 |
(self basicAt:i) ~~ key ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
447 |
newDict basicAt:dstIndex put:(self basicAt:i). |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
448 |
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
|
449 |
dstIndex := dstIndex + 2. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
450 |
] |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
451 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
452 |
dstIndex > sz ifTrue:[ |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
453 |
^ nil |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
454 |
]. |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
455 |
^ newDict |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
456 |
|
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
457 |
"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
|
458 |
"Modified: 7.6.1996 / 16:47:02 / stefan" |
2402
fc35bd8b4343
#identityIndexOf: is not allowed;
Claus Gittinger <cg@exept.de>
parents:
2386
diff
changeset
|
459 |
"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
|
460 |
! ! |
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
461 |
|
2109
c019b0e813ec
oops removeKey:ifAbsent: should send #value to the block
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
462 |
!MethodDictionary class methodsFor:'documentation'! |
328 | 463 |
|
1461
dd25bb1e9973
Use methodDictionary instead of selector/method arrays.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
464 |
version |
19843 | 465 |
^ '$Header$' |
13514 | 466 |
! |
467 |
||
468 |
version_CVS |
|
19843 | 469 |
^ '$Header$' |
328 | 470 |
! ! |
17251 | 471 |