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