LazyArray.st
author Claus Gittinger <cg@exept.de>
Thu, 09 Jun 2016 12:42:10 +0200
changeset 3902 7e672411d94c
parent 3845 d3e54fd83dd6
child 4144 9093e8aaf912
permissions -rw-r--r--
class: HTMLPrinterStream changed: #initialize lazy package dependency
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1300
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"{ Package: 'stx:libbasic2' }"
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
3845
d3e54fd83dd6 #OTHER by mawalch
mawalch
parents: 3306
diff changeset
     3
"{ NameSpace: Smalltalk }"
d3e54fd83dd6 #OTHER by mawalch
mawalch
parents: 3306
diff changeset
     4
1300
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
ArrayedCollection variableSubclass:#LazyArray
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	instanceVariableNames:'valueGenerator'
3306
b58330b15063 class: LazyArray
Claus Gittinger <cg@exept.de>
parents: 2713
diff changeset
     7
	classVariableNames:'UncomputedValueSingleton'
1300
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
	poolDictionaries:''
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
	category:'Collections-Arrayed'
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
!
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
3306
b58330b15063 class: LazyArray
Claus Gittinger <cg@exept.de>
parents: 2713
diff changeset
    12
Object subclass:#UncomputedValue
b58330b15063 class: LazyArray
Claus Gittinger <cg@exept.de>
parents: 2713
diff changeset
    13
	instanceVariableNames:''
b58330b15063 class: LazyArray
Claus Gittinger <cg@exept.de>
parents: 2713
diff changeset
    14
	classVariableNames:''
b58330b15063 class: LazyArray
Claus Gittinger <cg@exept.de>
parents: 2713
diff changeset
    15
	poolDictionaries:''
b58330b15063 class: LazyArray
Claus Gittinger <cg@exept.de>
parents: 2713
diff changeset
    16
	privateIn:LazyArray
b58330b15063 class: LazyArray
Claus Gittinger <cg@exept.de>
parents: 2713
diff changeset
    17
!
b58330b15063 class: LazyArray
Claus Gittinger <cg@exept.de>
parents: 2713
diff changeset
    18
1300
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
!LazyArray class methodsFor:'documentation'!
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
documentation
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
"
3845
d3e54fd83dd6 #OTHER by mawalch
mawalch
parents: 3306
diff changeset
    23
    An Array which computes its value lazily (on demand) and remembers those values.
1300
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
    [author:]
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
        Claus Gittinger (cg@alan)
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
    [see also:]
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
        Lazy
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
"
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
!
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
examples
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
"
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
                                                                [exBegin]
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
    |squares|
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
    squares := LazyArray new:100.
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
    squares valueGenerator:[:index | index squared].
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    squares at:50.   
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
    squares inspect.
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
                                                                [exEnd]
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
"
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
! !
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
!LazyArray class methodsFor:'initialization'!
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
initialize
3306
b58330b15063 class: LazyArray
Claus Gittinger <cg@exept.de>
parents: 2713
diff changeset
    50
    UncomputedValueSingleton isNil ifTrue:[
b58330b15063 class: LazyArray
Claus Gittinger <cg@exept.de>
parents: 2713
diff changeset
    51
        UncomputedValueSingleton := UncomputedValue new.
1300
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
    ]
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
    "
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
     self initialize
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
    "
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
! !
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
!LazyArray class methodsFor:'instance creation'!
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
new:size
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
    ^ (super new:size) atAllPut:UncomputedValue
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
! !
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
!LazyArray methodsFor:'accessing'!
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
at:index
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
    |val|
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
    val := super at:index.
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
    val == UncomputedValue ifTrue:[
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
        val := valueGenerator value:index.
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
        self at:index put:val.
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
    ].
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
    ^ val.
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
!
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
valueGenerator:aBlock
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
    valueGenerator := aBlock
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
! !
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
!LazyArray class methodsFor:'documentation'!
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
version
3845
d3e54fd83dd6 #OTHER by mawalch
mawalch
parents: 3306
diff changeset
    85
    ^ '$Header$'
1300
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
! !
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
3306
b58330b15063 class: LazyArray
Claus Gittinger <cg@exept.de>
parents: 2713
diff changeset
    88
1300
e606c08de0a6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
LazyArray initialize!