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