author | Stefan Vogel <sv@exept.de> |
Tue, 05 Apr 2016 20:52:14 +0200 | |
changeset 3792 | ac218195f34c |
parent 3473 | 6549a6fb4c64 |
child 4024 | 626a498ce5bf |
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 |
" |
|
41 |
a CacheDictionary is a Dictionary which will not grow - i.e. keep |
|
42 |
only a limited number of elements. |
|
43 |
It can be used as a cache, for keeping recently used objects alive. |
|
3473 | 44 |
Must be created with an initial (=maximal) size. I.e. (CacheDictionary new:100) |
254 | 45 |
|
46 |
[author:] |
|
47 |
Claus Gittinger |
|
31 | 48 |
" |
538 | 49 |
! |
50 |
||
51 |
examples |
|
52 |
" |
|
910 | 53 |
[exBegin] |
538 | 54 |
|d| |
55 |
||
56 |
d := CacheDictionary new:16. |
|
57 |
1 to:20 do:[:i | |
|
910 | 58 |
d at:i printString put:i. |
538 | 59 |
]. |
60 |
21 to:40 do:[:i | |
|
910 | 61 |
d at:i printString put:i. |
538 | 62 |
]. |
63 |
d inspect |
|
910 | 64 |
[exEnd] |
538 | 65 |
" |
31 | 66 |
! ! |
0 | 67 |
|
68 |
!CacheDictionary methodsFor:'private'! |
|
69 |
||
70 |
findKeyOrNil:key |
|
71 |
"Look for the key in the receiver. If it is found, return |
|
72 |
the index of the association containing the key, otherwise |
|
73 |
return the index of the first unused slot. If no empty slot |
|
489 | 74 |
is available, make one empty (but never grow)." |
0 | 75 |
|
76 |
|index "{ Class:SmallInteger }" |
|
77 |
length "{ Class:SmallInteger }" |
|
3792 | 78 |
startIndex probe |
79 |
delIndex "{ Class:SmallInteger }"| |
|
80 |
||
81 |
delIndex := 0. |
|
0 | 82 |
|
83 |
length := keyArray basicSize. |
|
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
84 |
startIndex := index := self initialIndexForKey:key. |
0 | 85 |
|
3209 | 86 |
[ |
238 | 87 |
probe := keyArray basicAt:index. |
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
88 |
key = probe ifTrue:[^ index]. |
489 | 89 |
probe isNil ifTrue:[ |
3792 | 90 |
delIndex == 0 ifTrue:[^ index]. |
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
91 |
|
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
92 |
keyArray basicAt:delIndex put:nil. |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
93 |
valueArray basicAt:delIndex put:nil. |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
94 |
^ delIndex |
489 | 95 |
]. |
0 | 96 |
|
3792 | 97 |
(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
|
98 |
delIndex := index |
238 | 99 |
]. |
13 | 100 |
|
238 | 101 |
index := index + 1. |
102 |
index > length ifTrue:[ |
|
103 |
index := 1. |
|
104 |
]. |
|
105 |
index == startIndex ifTrue:[ |
|
489 | 106 |
"/ mhmh - actually, a kind of round-robin would be better |
3792 | 107 |
delIndex == 0 ifTrue:[ |
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
108 |
delIndex := startIndex |
238 | 109 |
]. |
13 | 110 |
|
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
111 |
keyArray basicAt:delIndex put:nil. |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
112 |
valueArray basicAt:delIndex put:nil. |
238 | 113 |
tally := tally - 1. |
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
114 |
^ delIndex |
238 | 115 |
]. |
3209 | 116 |
] loop. |
238 | 117 |
|
2700
b480eda50a6a
comment/format in: #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
1126
diff
changeset
|
118 |
"Modified: / 01-03-1997 / 00:59:55 / cg" |
b480eda50a6a
comment/format in: #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
1126
diff
changeset
|
119 |
"Modified (format): / 26-12-2011 / 10:42:08 / cg" |
489 | 120 |
! |
121 |
||
3461
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
122 |
possiblyGrow |
489 | 123 |
"redefined - never grow" |
124 |
||
125 |
^ self |
|
126 |
||
127 |
"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
|
128 |
! |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
129 |
|
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
130 |
possiblyShrink |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
131 |
"redefined - never shrink" |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
132 |
|
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
133 |
^ self |
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
134 |
|
f4ff6526ac02
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
3209
diff
changeset
|
135 |
"Modified: 30.1.1997 / 15:17:12 / cg" |
0 | 136 |
! ! |
125 | 137 |
|
134 | 138 |
!CacheDictionary class methodsFor:'documentation'! |
139 |
||
140 |
version |
|
3792 | 141 |
^ '$Header$' |
134 | 142 |
! ! |
3209 | 143 |