author | Claus Gittinger <cg@exept.de> |
Tue, 18 Dec 2018 12:52:15 +0100 | |
changeset 4777 | b22943151ce0 |
parent 4674 | 519d0604d2bc |
child 4843 | 49f5a601dee9 |
permissions | -rw-r--r-- |
4673 | 1 |
"{ Encoding: utf8 }" |
2 |
||
2 | 3 |
" |
4 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
84 | 5 |
All Rights Reserved |
2 | 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 |
" |
|
910 | 14 |
"{ Package: 'stx:libbasic2' }" |
15 |
||
3461
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
16 |
"{ NameSpace: Smalltalk }" |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
17 |
|
0 | 18 |
Dictionary subclass:#CacheDictionary |
238 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Unordered' |
|
0 | 23 |
! |
24 |
||
31 | 25 |
!CacheDictionary class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
84 | 30 |
All Rights Reserved |
0 | 31 |
|
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 |
! |
|
0 | 40 |
|
31 | 41 |
documentation |
42 |
" |
|
4141 | 43 |
a CacheDictionary is a Dictionary which will not grow beyond a given max. size |
44 |
- i.e. keep only a limited number of elements. |
|
31 | 45 |
It can be used as a cache, for keeping recently used objects alive. |
4141 | 46 |
Must be created with an initial (=maximal) size. |
47 |
I.e. (CacheDictionary new:100) |
|
254 | 48 |
|
4141 | 49 |
[see also:] |
50 |
Dictionary |
|
51 |
OrderedCollection |
|
52 |
OrderedDictionary |
|
53 |
||
254 | 54 |
[author:] |
55 |
Claus Gittinger |
|
31 | 56 |
" |
538 | 57 |
! |
58 |
||
59 |
examples |
|
60 |
" |
|
910 | 61 |
[exBegin] |
538 | 62 |
|d| |
63 |
||
64 |
d := CacheDictionary new:16. |
|
65 |
1 to:20 do:[:i | |
|
910 | 66 |
d at:i printString put:i. |
538 | 67 |
]. |
68 |
21 to:40 do:[:i | |
|
910 | 69 |
d at:i printString put:i. |
538 | 70 |
]. |
71 |
d inspect |
|
910 | 72 |
[exEnd] |
538 | 73 |
" |
31 | 74 |
! ! |
0 | 75 |
|
76 |
!CacheDictionary methodsFor:'private'! |
|
77 |
||
78 |
findKeyOrNil:key |
|
79 |
"Look for the key in the receiver. If it is found, return |
|
80 |
the index of the association containing the key, otherwise |
|
81 |
return the index of the first unused slot. If no empty slot |
|
489 | 82 |
is available, make one empty (but never grow)." |
0 | 83 |
|
84 |
|index "{ Class:SmallInteger }" |
|
85 |
length "{ Class:SmallInteger }" |
|
3792 | 86 |
startIndex probe |
87 |
delIndex "{ Class:SmallInteger }"| |
|
88 |
||
89 |
delIndex := 0. |
|
0 | 90 |
|
91 |
length := keyArray basicSize. |
|
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
92 |
startIndex := index := self initialIndexForKey:key. |
0 | 93 |
|
3209 | 94 |
[ |
238 | 95 |
probe := keyArray basicAt:index. |
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
96 |
key = probe ifTrue:[^ index]. |
489 | 97 |
probe isNil ifTrue:[ |
3792 | 98 |
delIndex == 0 ifTrue:[^ index]. |
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
99 |
|
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
100 |
keyArray basicAt:delIndex put:nil. |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
101 |
valueArray basicAt:delIndex put:nil. |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
102 |
^ delIndex |
489 | 103 |
]. |
0 | 104 |
|
3792 | 105 |
(delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[ |
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
106 |
delIndex := index |
238 | 107 |
]. |
13 | 108 |
|
238 | 109 |
index := index + 1. |
110 |
index > length ifTrue:[ |
|
111 |
index := 1. |
|
112 |
]. |
|
113 |
index == startIndex ifTrue:[ |
|
4673 | 114 |
"/ cache is full; |
115 |
"/ replace the slot at the original startIndex (the H(key)) |
|
116 |
"/ |
|
117 |
"/ mhmh - a kind of round-robin might be better - this will always |
|
118 |
"/ remove the first key with this H(key) |
|
3792 | 119 |
delIndex == 0 ifTrue:[ |
4673 | 120 |
delIndex := startIndex. |
121 |
tally := tally - 1. |
|
238 | 122 |
]. |
13 | 123 |
|
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
124 |
keyArray basicAt:delIndex put:nil. |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
125 |
valueArray basicAt:delIndex put:nil. |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
126 |
^ delIndex |
238 | 127 |
]. |
3209 | 128 |
] loop. |
238 | 129 |
|
2700
b480eda50a6a
comment/format in: #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
1126
diff
changeset
|
130 |
"Modified: / 01-03-1997 / 00:59:55 / cg" |
b480eda50a6a
comment/format in: #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
1126
diff
changeset
|
131 |
"Modified (format): / 26-12-2011 / 10:42:08 / cg" |
489 | 132 |
! |
133 |
||
4024 | 134 |
findKeyOrNilOrDeletedEntry:key |
135 |
"Look for the key in the receiver. If it is found, return |
|
136 |
the index of the association containing the key, otherwise |
|
137 |
return the index of the first unused slot. If no empty slot |
|
138 |
is available, make one empty (but never grow)." |
|
139 |
||
140 |
|index "{ Class:SmallInteger }" |
|
141 |
length "{ Class:SmallInteger }" |
|
142 |
startIndex probe |
|
143 |
delIndex "{ Class:SmallInteger }"| |
|
144 |
||
145 |
delIndex := 0. |
|
146 |
||
147 |
length := keyArray basicSize. |
|
148 |
startIndex := index := self initialIndexForKey:key. |
|
149 |
||
150 |
[ |
|
151 |
probe := keyArray basicAt:index. |
|
152 |
key = probe ifTrue:[^ index]. |
|
153 |
probe isNil ifTrue:[ |
|
154 |
delIndex == 0 ifTrue:[^ index]. |
|
155 |
||
156 |
valueArray basicAt:delIndex put:nil. |
|
157 |
^ delIndex |
|
158 |
]. |
|
159 |
||
160 |
(delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[ |
|
161 |
delIndex := index |
|
162 |
]. |
|
163 |
||
164 |
index := index + 1. |
|
165 |
index > length ifTrue:[ |
|
166 |
index := 1. |
|
167 |
]. |
|
168 |
index == startIndex ifTrue:[ |
|
169 |
"/ mhmh - actually, a kind of round-robin would be better |
|
170 |
delIndex == 0 ifTrue:[ |
|
4673 | 171 |
delIndex := startIndex. |
172 |
tally := tally - 1. |
|
4024 | 173 |
]. |
174 |
||
175 |
valueArray basicAt:delIndex put:DeletedEntry. |
|
176 |
valueArray basicAt:delIndex put:nil. |
|
177 |
^ delIndex |
|
178 |
]. |
|
179 |
] loop. |
|
180 |
||
181 |
"Modified: / 01-03-1997 / 00:59:55 / cg" |
|
182 |
"Modified (format): / 26-12-2011 / 10:42:08 / cg" |
|
183 |
! |
|
184 |
||
3461
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
185 |
possiblyGrow |
489 | 186 |
"redefined - never grow" |
187 |
||
188 |
^ self |
|
189 |
||
190 |
"Modified: 30.1.1997 / 15:17:18 / cg" |
|
3461
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
191 |
! |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
192 |
|
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
193 |
possiblyShrink |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
194 |
"redefined - never shrink" |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
195 |
|
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
196 |
^ self |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
197 |
|
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
198 |
"Modified: 30.1.1997 / 15:17:12 / cg" |
4674 | 199 |
! |
200 |
||
201 |
possiblyShrinkToZero |
|
202 |
"here, we keep the old containers" |
|
203 |
||
204 |
keyArray atAllPut:nil. |
|
205 |
valueArray atAllPut:nil. |
|
206 |
tally := 0. |
|
0 | 207 |
! ! |
125 | 208 |
|
134 | 209 |
!CacheDictionary class methodsFor:'documentation'! |
210 |
||
211 |
version |
|
3792 | 212 |
^ '$Header$' |
4674 | 213 |
! |
214 |
||
215 |
version_CVS |
|
216 |
^ '$Header$' |
|
134 | 217 |
! ! |
3209 | 218 |