CacheDictionary.st
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--
#TUNING by stefan class: CacheDictionary changed: #findKeyOrNil:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
07d9ee98e092 *** empty log message ***
claus
parents: 0
diff changeset
     1
"
07d9ee98e092 *** empty log message ***
claus
parents: 0
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
84
claus
parents: 80
diff changeset
     3
	      All Rights Reserved
2
07d9ee98e092 *** empty log message ***
claus
parents: 0
diff changeset
     4
07d9ee98e092 *** empty log message ***
claus
parents: 0
diff changeset
     5
 This software is furnished under a license and may be used
07d9ee98e092 *** empty log message ***
claus
parents: 0
diff changeset
     6
 only in accordance with the terms of that license and with the
07d9ee98e092 *** empty log message ***
claus
parents: 0
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
07d9ee98e092 *** empty log message ***
claus
parents: 0
diff changeset
     8
 be provided or otherwise made available to, or used by, any
07d9ee98e092 *** empty log message ***
claus
parents: 0
diff changeset
     9
 other person.  No title to or ownership of the software is
07d9ee98e092 *** empty log message ***
claus
parents: 0
diff changeset
    10
 hereby transferred.
07d9ee98e092 *** empty log message ***
claus
parents: 0
diff changeset
    11
"
910
af28d492d1eb examples
Claus Gittinger <cg@exept.de>
parents: 538
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
af28d492d1eb examples
Claus Gittinger <cg@exept.de>
parents: 538
diff changeset
    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
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    16
Dictionary subclass:#CacheDictionary
238
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
    17
	instanceVariableNames:''
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
    18
	classVariableNames:''
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
    19
	poolDictionaries:''
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
    20
	category:'Collections-Unordered'
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    21
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    22
31
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    23
!CacheDictionary class methodsFor:'documentation'!
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    24
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    25
copyright
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    26
"
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    27
 COPYRIGHT (c) 1993 by Claus Gittinger
84
claus
parents: 80
diff changeset
    28
	      All Rights Reserved
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    29
31
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    30
 This software is furnished under a license and may be used
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    31
 only in accordance with the terms of that license and with the
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    33
 be provided or otherwise made available to, or used by, any
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    34
 other person.  No title to or ownership of the software is
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    35
 hereby transferred.
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    36
"
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    37
!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    38
31
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    39
documentation
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    40
"
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    41
    a CacheDictionary is a Dictionary which will not grow - i.e. keep
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    42
    only a limited number of elements.
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    43
    It can be used as a cache, for keeping recently used objects alive.
3473
6549a6fb4c64 class: CacheDictionary
Claus Gittinger <cg@exept.de>
parents: 3461
diff changeset
    44
    Must be created with an initial (=maximal) size. I.e. (CacheDictionary new:100)
254
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 238
diff changeset
    45
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 238
diff changeset
    46
    [author:]
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 238
diff changeset
    47
        Claus Gittinger
31
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    48
"
538
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    49
!
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    50
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    51
examples
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    52
"
910
af28d492d1eb examples
Claus Gittinger <cg@exept.de>
parents: 538
diff changeset
    53
                                                                        [exBegin]
538
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    54
    |d|
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    55
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    56
    d := CacheDictionary new:16.
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    57
    1 to:20 do:[:i |
910
af28d492d1eb examples
Claus Gittinger <cg@exept.de>
parents: 538
diff changeset
    58
        d at:i printString put:i.
538
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    59
    ].
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    60
    21 to:40 do:[:i |
910
af28d492d1eb examples
Claus Gittinger <cg@exept.de>
parents: 538
diff changeset
    61
        d at:i printString put:i.
538
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    62
    ].
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    63
    d inspect
910
af28d492d1eb examples
Claus Gittinger <cg@exept.de>
parents: 538
diff changeset
    64
                                                                        [exEnd]
538
9cffa21b8dd4 added example
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
    65
"
31
e223f3cf2995 *** empty log message ***
claus
parents: 13
diff changeset
    66
! !
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    67
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    68
!CacheDictionary methodsFor:'private'!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    69
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    70
findKeyOrNil:key  
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    71
    "Look for the key in the receiver.  If it is found, return
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    72
     the index of the association containing the key, otherwise
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    73
     return the index of the first unused slot. If no empty slot
489
27859443b951 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
    74
     is available, make one empty (but never grow)."
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    75
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    76
    |index  "{ Class:SmallInteger }"
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    77
     length "{ Class:SmallInteger }"
3792
ac218195f34c #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3473
diff changeset
    78
     startIndex probe
ac218195f34c #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3473
diff changeset
    79
     delIndex "{ Class:SmallInteger }"|
ac218195f34c #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3473
diff changeset
    80
ac218195f34c #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3473
diff changeset
    81
    delIndex := 0.
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    82
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    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
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    85
3209
bed5afb12dcf [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 2700
diff changeset
    86
    [
238
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
    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
27859443b951 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
    89
        probe isNil ifTrue:[
3792
ac218195f34c #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3473
diff changeset
    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
27859443b951 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
    95
        ].
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    96
3792
ac218195f34c #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3473
diff changeset
    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
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
    99
        ].
13
f089ff744cd1 *** empty log message ***
claus
parents: 2
diff changeset
   100
238
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
   101
        index := index + 1.
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
   102
        index > length ifTrue:[
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
   103
            index := 1.
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
   104
        ].
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
   105
        index == startIndex ifTrue:[
489
27859443b951 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
   106
            "/ mhmh - actually, a kind of round-robin would be better
3792
ac218195f34c #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3473
diff changeset
   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
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
   109
            ].
13
f089ff744cd1 *** empty log message ***
claus
parents: 2
diff changeset
   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
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
   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
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
   115
        ].
3209
bed5afb12dcf [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 2700
diff changeset
   116
    ] loop.
238
5cd00f81c000 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 134
diff changeset
   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
27859443b951 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
   120
!
27859443b951 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
   121
3461
f4ff6526ac02 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 3209
diff changeset
   122
possiblyGrow
489
27859443b951 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
   123
    "redefined - never grow"
27859443b951 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
   124
27859443b951 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
   125
    ^ self
27859443b951 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
   126
27859443b951 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
   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
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   136
! !
125
8ee6268bd5ed checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   137
134
4ef060494a86 version at the end
Claus Gittinger <cg@exept.de>
parents: 125
diff changeset
   138
!CacheDictionary class methodsFor:'documentation'!
4ef060494a86 version at the end
Claus Gittinger <cg@exept.de>
parents: 125
diff changeset
   139
4ef060494a86 version at the end
Claus Gittinger <cg@exept.de>
parents: 125
diff changeset
   140
version
3792
ac218195f34c #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3473
diff changeset
   141
    ^ '$Header$'
134
4ef060494a86 version at the end
Claus Gittinger <cg@exept.de>
parents: 125
diff changeset
   142
! !
3209
bed5afb12dcf [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 2700
diff changeset
   143