author | Claus Gittinger <cg@exept.de> |
Sat, 02 May 2020 21:40:13 +0200 | |
changeset 5476 | 7355a4b11cb6 |
parent 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" |
4843 | 132 |
"Modified: / 15-03-2019 / 15:27:16 / Stefan Vogel" |
489 | 133 |
! |
134 |
||
4024 | 135 |
findKeyOrNilOrDeletedEntry:key |
136 |
"Look for the key in the receiver. If it is found, return |
|
137 |
the index of the association containing the key, otherwise |
|
138 |
return the index of the first unused slot. If no empty slot |
|
139 |
is available, make one empty (but never grow)." |
|
140 |
||
141 |
|index "{ Class:SmallInteger }" |
|
142 |
length "{ Class:SmallInteger }" |
|
143 |
startIndex probe |
|
144 |
delIndex "{ Class:SmallInteger }"| |
|
145 |
||
146 |
delIndex := 0. |
|
147 |
||
148 |
length := keyArray basicSize. |
|
149 |
startIndex := index := self initialIndexForKey:key. |
|
150 |
||
151 |
[ |
|
152 |
probe := keyArray basicAt:index. |
|
153 |
key = probe ifTrue:[^ index]. |
|
154 |
probe isNil ifTrue:[ |
|
155 |
delIndex == 0 ifTrue:[^ index]. |
|
156 |
||
157 |
valueArray basicAt:delIndex put:nil. |
|
158 |
^ delIndex |
|
159 |
]. |
|
160 |
||
161 |
(delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[ |
|
162 |
delIndex := index |
|
163 |
]. |
|
164 |
||
165 |
index := index + 1. |
|
166 |
index > length ifTrue:[ |
|
167 |
index := 1. |
|
168 |
]. |
|
169 |
index == startIndex ifTrue:[ |
|
4843 | 170 |
"/ cache is full; |
171 |
"/ replace the slot at the original startIndex (the H(key)) |
|
172 |
"/ |
|
173 |
"/ mhmh - a kind of round-robin might be better - this will always |
|
174 |
"/ remove the first key with this H(key) |
|
4024 | 175 |
delIndex == 0 ifTrue:[ |
4673 | 176 |
delIndex := startIndex. |
177 |
tally := tally - 1. |
|
4024 | 178 |
]. |
179 |
||
4843 | 180 |
keyArray basicAt:delIndex put:DeletedEntry. |
4024 | 181 |
valueArray basicAt:delIndex put:nil. |
182 |
^ delIndex |
|
183 |
]. |
|
184 |
] loop. |
|
185 |
||
186 |
"Modified: / 01-03-1997 / 00:59:55 / cg" |
|
187 |
"Modified (format): / 26-12-2011 / 10:42:08 / cg" |
|
4843 | 188 |
"Modified (format): / 15-03-2019 / 15:05:44 / Stefan Vogel" |
4024 | 189 |
! |
190 |
||
3461
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
191 |
possiblyGrow |
489 | 192 |
"redefined - never grow" |
193 |
||
194 |
^ self |
|
195 |
||
196 |
"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
|
197 |
! |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
198 |
|
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
199 |
possiblyShrink |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
200 |
"redefined - never shrink" |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
201 |
|
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
202 |
^ self |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
203 |
|
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
204 |
"Modified: 30.1.1997 / 15:17:12 / cg" |
4674 | 205 |
! |
206 |
||
207 |
possiblyShrinkToZero |
|
208 |
"here, we keep the old containers" |
|
209 |
||
210 |
keyArray atAllPut:nil. |
|
211 |
valueArray atAllPut:nil. |
|
212 |
tally := 0. |
|
0 | 213 |
! ! |
125 | 214 |
|
134 | 215 |
!CacheDictionary class methodsFor:'documentation'! |
216 |
||
217 |
version |
|
3792 | 218 |
^ '$Header$' |
4674 | 219 |
! |
220 |
||
221 |
version_CVS |
|
222 |
^ '$Header$' |
|
134 | 223 |
! ! |
3209 | 224 |