author | Stefan Vogel <sv@exept.de> |
Fri, 07 Mar 2014 23:06:40 +0100 | |
changeset 3209 | bed5afb12dcf |
parent 2700 | b480eda50a6a |
child 3461 | f4ff6526ac02 |
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 |
||
0 | 14 |
Dictionary subclass:#CacheDictionary |
238 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Collections-Unordered' |
|
0 | 19 |
! |
20 |
||
31 | 21 |
!CacheDictionary class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
84 | 26 |
All Rights Reserved |
0 | 27 |
|
31 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
0 | 36 |
|
31 | 37 |
documentation |
38 |
" |
|
39 |
a CacheDictionary is a Dictionary which will not grow - i.e. keep |
|
40 |
only a limited number of elements. |
|
41 |
It can be used as a cache, for keeping recently used objects alive. |
|
254 | 42 |
|
43 |
[author:] |
|
44 |
Claus Gittinger |
|
31 | 45 |
" |
538 | 46 |
! |
47 |
||
48 |
examples |
|
49 |
" |
|
910 | 50 |
[exBegin] |
538 | 51 |
|d| |
52 |
||
53 |
d := CacheDictionary new:16. |
|
54 |
1 to:20 do:[:i | |
|
910 | 55 |
d at:i printString put:i. |
538 | 56 |
]. |
57 |
21 to:40 do:[:i | |
|
910 | 58 |
d at:i printString put:i. |
538 | 59 |
]. |
60 |
d inspect |
|
910 | 61 |
[exEnd] |
538 | 62 |
" |
31 | 63 |
! ! |
0 | 64 |
|
65 |
!CacheDictionary methodsFor:'private'! |
|
66 |
||
13 | 67 |
emptyCheck |
489 | 68 |
"redefined - never shrink" |
13 | 69 |
|
70 |
^ self |
|
489 | 71 |
|
72 |
"Modified: 30.1.1997 / 15:17:12 / cg" |
|
13 | 73 |
! |
74 |
||
0 | 75 |
findKeyOrNil:key |
76 |
"Look for the key in the receiver. If it is found, return |
|
77 |
the index of the association containing the key, otherwise |
|
78 |
return the index of the first unused slot. If no empty slot |
|
489 | 79 |
is available, make one empty (but never grow)." |
0 | 80 |
|
81 |
|index "{ Class:SmallInteger }" |
|
82 |
length "{ Class:SmallInteger }" |
|
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
83 |
startIndex delIndex |
0 | 84 |
probe| |
85 |
||
86 |
length := keyArray basicSize. |
|
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
87 |
startIndex := index := self initialIndexForKey:key. |
0 | 88 |
|
3209 | 89 |
[ |
238 | 90 |
probe := keyArray basicAt:index. |
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
91 |
key = probe ifTrue:[^ index]. |
489 | 92 |
probe isNil ifTrue:[ |
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
93 |
delIndex isNil ifTrue:[^ index]. |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
94 |
|
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
95 |
keyArray basicAt:delIndex put:nil. |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
96 |
valueArray basicAt:delIndex put:nil. |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
97 |
^ delIndex |
489 | 98 |
]. |
0 | 99 |
|
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
100 |
(delIndex isNil and:[probe == DeletedEntry]) ifTrue:[ |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
101 |
delIndex := index |
238 | 102 |
]. |
13 | 103 |
|
238 | 104 |
index := index + 1. |
105 |
index > length ifTrue:[ |
|
106 |
index := 1. |
|
107 |
]. |
|
108 |
index == startIndex ifTrue:[ |
|
489 | 109 |
"/ mhmh - actually, a kind of round-robin would be better |
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
110 |
delIndex isNil ifTrue:[ |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
111 |
delIndex := startIndex |
238 | 112 |
]. |
13 | 113 |
|
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
114 |
keyArray basicAt:delIndex put:nil. |
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
115 |
valueArray basicAt:delIndex put:nil. |
238 | 116 |
tally := tally - 1. |
1126
0ca4b7558465
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
910
diff
changeset
|
117 |
^ delIndex |
238 | 118 |
]. |
3209 | 119 |
] loop. |
238 | 120 |
|
2700
b480eda50a6a
comment/format in: #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
1126
diff
changeset
|
121 |
"Modified: / 01-03-1997 / 00:59:55 / cg" |
b480eda50a6a
comment/format in: #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
1126
diff
changeset
|
122 |
"Modified (format): / 26-12-2011 / 10:42:08 / cg" |
489 | 123 |
! |
124 |
||
125 |
fullCheck |
|
126 |
"redefined - never grow" |
|
127 |
||
128 |
^ self |
|
129 |
||
130 |
"Modified: 30.1.1997 / 15:17:18 / cg" |
|
0 | 131 |
! ! |
125 | 132 |
|
134 | 133 |
!CacheDictionary class methodsFor:'documentation'! |
134 |
||
135 |
version |
|
3209 | 136 |
^ '$Header: /cvs/stx/stx/libbasic2/CacheDictionary.st,v 1.22 2014-03-07 22:06:40 stefan Exp $' |
134 | 137 |
! ! |
3209 | 138 |