CacheDictionaryWithFactory.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 4138 6719d62342d4
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 2003 by eXept Software AG
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
              All Rights Reserved
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 hereby transferred.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
"
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
    14
"{ NameSpace: Smalltalk }"
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
CacheDictionary subclass:#CacheDictionaryWithFactory
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:'factoryBlock'
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	category:'Collections-Unordered'
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!CacheDictionaryWithFactory class methodsFor:'documentation'!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
copyright
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
"
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
 COPYRIGHT (c) 2003 by eXept Software AG
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
              All Rights Reserved
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
 This software is furnished under a license and may be used
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
 only in accordance with the terms of that license and with the
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 be provided or otherwise made available to, or used by, any
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
 other person.  No title to or ownership of the software is
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
 hereby transferred.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
"
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
documentation
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
"
4138
6719d62342d4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
    41
    like a Dictionary, but does not grow beyond a given max. size
6719d62342d4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
    42
    (i.e. only keeps size items),
6719d62342d4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
    43
    It can be used as a cache, for keeping recently used objects alive.
6719d62342d4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
    44
    Must be created with an initial (=maximal) size. 
6719d62342d4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
    45
    In addition to the normal CacheDictionary, 
6719d62342d4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
    46
    this also keeps a factoryBlock to automatically compute missing elements.
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
    [author:]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
        Claus Gittinger (cg@alan)
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
    [see also:]
4138
6719d62342d4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
    52
        CacheDictionary
6719d62342d4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
    53
        Dictionary
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
"
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
examples
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
"
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
  caches reverse strings (stupid usage)
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
                                                                [exBegin]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
    |c|
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
    63
    c := CacheDictionaryWithFactory
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
            new:100 factory:[:key | key reversed].
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
    66
    c at:'hello'.
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
    c at:'hello'.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
    1 to:1000 do:[:i | c at:i printString].
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
    c at:'hello'.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
                                                                [exEnd]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
  Test: should halt (compute 'hello') only twice.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
                                                                [exBegin]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
    |c numHalts|
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
    numHalts := 0.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
    79
    c := CacheDictionaryWithFactory
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
            new:100 factory:[:key | key = 'hello' ifTrue:[numHalts := numHalts + 1]. key reversed].
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
    82
    c at:'hello'.
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
    c at:'hello'.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
    1 to:1000 do:[:i | c at:i printString].
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
    c at:'hello'.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
    self assert:( numHalts == 2 ).
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
                                                                [exEnd]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
"
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
! !
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
!CacheDictionaryWithFactory class methodsFor:'instance creation'!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
new:cacheSize factory:aBlock
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
    ^ (self new:cacheSize) factory:aBlock
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
! !
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
!CacheDictionaryWithFactory methodsFor:'accessing'!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
at:key
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
    ^ super at:key ifAbsentPut:[factoryBlock value:key]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
! !
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
!CacheDictionaryWithFactory methodsFor:'initialization'!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
factory:aBlock
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
    factoryBlock := aBlock
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
! !
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
!CacheDictionaryWithFactory class methodsFor:'documentation'!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
version
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
   112
    ^ '$Header$'
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
! !
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
   114