CacheDictionaryWithFactory.st
author mawalch
Wed, 07 Sep 2016 09:34:15 +0200
changeset 4062 47fa84c4d9a9
parent 1204 bf0c322155c3
child 4138 6719d62342d4
permissions -rw-r--r--
#OTHER by mawalch class: CacheDictionaryWithFactory comment/format in: #documentation
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
"
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    like a Dictionary, but does not grow (i.e. only keeps size items),
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
    42
    and also keeps a factoryBlock to automatically compute missing elements.
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
    [author:]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
        Claus Gittinger (cg@alan)
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
    [instance variables:]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
    [class variables:]
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:]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
"
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
examples
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
"
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
  caches reverse strings (stupid usage)
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
                                                                [exBegin]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
    |c|
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
    62
    c := CacheDictionaryWithFactory
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
            new:100 factory:[:key | key reversed].
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
    65
    c at:'hello'.
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
    c at:'hello'.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
    1 to:1000 do:[:i | c at:i printString].
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
    c at:'hello'.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
                                                                [exEnd]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
  Test: should halt (compute 'hello') only twice.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
                                                                [exBegin]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
    |c numHalts|
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
    numHalts := 0.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
    78
    c := CacheDictionaryWithFactory
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
            new:100 factory:[:key | key = 'hello' ifTrue:[numHalts := numHalts + 1]. key reversed].
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
    81
    c at:'hello'.
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    c at:'hello'.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
    1 to:1000 do:[:i | c at:i printString].
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
    c at:'hello'.
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
    self assert:( numHalts == 2 ).
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
                                                                [exEnd]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
"
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
!CacheDictionaryWithFactory class methodsFor:'instance creation'!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
new:cacheSize factory:aBlock
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
    ^ (self new:cacheSize) factory:aBlock
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
! !
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
!CacheDictionaryWithFactory methodsFor:'accessing'!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
at:key
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
    ^ super at:key ifAbsentPut:[factoryBlock value:key]
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
! !
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
!CacheDictionaryWithFactory methodsFor:'initialization'!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
factory:aBlock
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
    factoryBlock := aBlock
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
! !
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
!CacheDictionaryWithFactory class methodsFor:'documentation'!
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
version
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
   111
    ^ '$Header$'
1204
bf0c322155c3 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
! !
4062
47fa84c4d9a9 #OTHER by mawalch
mawalch
parents: 1204
diff changeset
   113