328
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Array subclass:#MethodDictionary
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Kernel-Methods'
|
|
18 |
!
|
|
19 |
|
|
20 |
MethodDictionary comment:'
|
|
21 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
|
22 |
All Rights Reserved
|
|
23 |
|
384
|
24 |
$Header: /cvs/stx/stx/libbasic/MethodDictionary.st,v 1.3 1995-08-11 03:02:07 claus Exp $
|
328
|
25 |
'!
|
|
26 |
|
|
27 |
!MethodDictionary class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
|
32 |
All Rights Reserved
|
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
384
|
45 |
$Header: /cvs/stx/stx/libbasic/MethodDictionary.st,v 1.3 1995-08-11 03:02:07 claus Exp $
|
328
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
Instances of MethodDictionary store selector/method associations
|
|
52 |
in classes. Conceptionally, they behave like Dictionaries, but are
|
|
53 |
implemented using a single array (instead of dictionary, which uses
|
|
54 |
two arrays to store keys and values separately).
|
|
55 |
Also, they do not use hashing, since due to caching in the VM, hashing
|
|
56 |
does not make too much of a difference in speed, but complicates the
|
|
57 |
VM implementations.
|
|
58 |
"
|
|
59 |
! !
|
|
60 |
|
|
61 |
!MethodDictionary class methodsFor:'queries'!
|
|
62 |
|
|
63 |
isBuiltInClass
|
|
64 |
"this class is known by the run-time-system"
|
|
65 |
|
|
66 |
^ self == MethodDictionary
|
|
67 |
! !
|
|
68 |
|
|
69 |
!MethodDictionary class methodsFor:'instance creation'!
|
|
70 |
|
|
71 |
new:sz
|
|
72 |
^ self basicNew:(sz * 2)
|
|
73 |
! !
|
|
74 |
|
|
75 |
!MethodDictionary methodsFor:'accessing'!
|
|
76 |
|
|
77 |
size
|
|
78 |
"return the number of elements (associations) in the receiver"
|
|
79 |
|
|
80 |
^ self basicSize // 2
|
|
81 |
!
|
|
82 |
|
|
83 |
at:key
|
|
84 |
"return the value for a given key, which is supposed to be a symbol"
|
|
85 |
|
|
86 |
|sz "{ Class: SmallInteger }"|
|
|
87 |
|
|
88 |
sz := self basicSize.
|
|
89 |
1 to:sz by:2 do:[:i |
|
|
90 |
(self basicAt:i) == key ifTrue:[
|
|
91 |
^ self basicAt:(i + 1)
|
|
92 |
]
|
|
93 |
].
|
|
94 |
^ self errorKeyNotFound
|
|
95 |
!
|
|
96 |
|
|
97 |
at:key ifAbsent:exceptionBlock
|
|
98 |
"return the element indexed by aKey -
|
|
99 |
return result of exceptionBlock if no element is stored under aKey"
|
|
100 |
|
|
101 |
|sz "{ Class: SmallInteger }"|
|
|
102 |
|
|
103 |
sz := self basicSize.
|
|
104 |
1 to:sz by:2 do:[:i |
|
|
105 |
(self basicAt:i) == key ifTrue:[
|
|
106 |
^ self basicAt:(i + 1)
|
|
107 |
]
|
|
108 |
].
|
|
109 |
^ exceptionBlock value
|
|
110 |
!
|
|
111 |
|
|
112 |
associationAt:key
|
|
113 |
"return an association consisting of aKey and the element indexed
|
|
114 |
by aKey -
|
|
115 |
report an error, if no element is stored under aKey."
|
|
116 |
|
|
117 |
|sz "{ Class: SmallInteger }"|
|
|
118 |
|
|
119 |
sz := self basicSize.
|
|
120 |
1 to:sz by:2 do:[:i |
|
|
121 |
(self basicAt:i) == key ifTrue:[
|
|
122 |
^ key -> (self basicAt:(i + 1))
|
|
123 |
]
|
|
124 |
].
|
|
125 |
^ self errorKeyNotFound
|
|
126 |
!
|
|
127 |
|
|
128 |
at:key put:value
|
|
129 |
"set the value for a given key, which is supposed to be a symbol.
|
|
130 |
In contrast to dictionaries, we do not allow adding elements here."
|
|
131 |
|
|
132 |
|sz "{ Class: SmallInteger }"|
|
|
133 |
|
|
134 |
sz := self basicSize.
|
|
135 |
1 to:sz by:2 do:[:i |
|
|
136 |
(self basicAt:i) == key ifTrue:[
|
|
137 |
^ self basicAt:(i + 1) put:value
|
|
138 |
]
|
|
139 |
].
|
|
140 |
^ self errorKeyNotFound
|
|
141 |
! !
|
|
142 |
|
|
143 |
!MethodDictionary methodsFor:'queries'!
|
|
144 |
|
|
145 |
keys
|
|
146 |
"return a collection containing all keys of the receiver"
|
|
147 |
|
|
148 |
^ (1 to:self basicSize by:2) collect:[:index | self basicAt:index].
|
|
149 |
!
|
|
150 |
|
|
151 |
|
|
152 |
!MethodDictionary methodsFor:'enumeration'!
|
|
153 |
|
|
154 |
do:aBlock
|
|
155 |
"evaluate the argument, aBlock for each element in the collection."
|
|
156 |
|
|
157 |
|sz "{ Class: SmallInteger }"|
|
|
158 |
|
|
159 |
sz := self basicSize.
|
|
160 |
1 to:sz by:2 do:[:i |
|
|
161 |
aBlock value:(self basicAt:i)
|
|
162 |
].
|
|
163 |
!
|
|
164 |
|
|
165 |
keysAndValuesDo:aBlock
|
|
166 |
"evaluate the argument, aBlock for each element in the collection.
|
|
167 |
Pass both index and element to the block."
|
|
168 |
|
|
169 |
|sz "{ Class: SmallInteger }"|
|
|
170 |
|
|
171 |
sz := self basicSize.
|
|
172 |
1 to:sz by:2 do:[:i |
|
|
173 |
aBlock value:(self basicAt:i) value:(self basicAt:(i + 1))
|
|
174 |
].
|
|
175 |
! !
|