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