VirtualArray.st
author Claus Gittinger <cg@exept.de>
Wed, 15 Feb 2017 21:45:55 +0100
changeset 4324 c3d7f79e34b0
parent 3859 3f598428b3e8
child 4350 717f67e37157
permissions -rw-r--r--
#DOCUMENTATION by cg class: RandomKISS comment/format in: #documentation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2906
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
     1
"
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
     2
 COPYRIGHT (c) 2012 by Claus Gittinger
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
     3
              All Rights Reserved
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
     4
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
     5
 This software is furnished under a license and may be used
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
     6
 only in accordance with the terms of that license and with the
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
     8
 be provided or otherwise made available to, or used by, any
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
     9
 other person.  No title to or ownership of the software is
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    10
 hereby transferred.
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    11
"
2714
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
3479
c3921810b0a2 class: VirtualArray
Stefan Vogel <sv@exept.de>
parents: 3326
diff changeset
    14
"{ NameSpace: Smalltalk }"
c3921810b0a2 class: VirtualArray
Stefan Vogel <sv@exept.de>
parents: 3326
diff changeset
    15
2714
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
SequenceableCollection subclass:#VirtualArray
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:'generator size'
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	category:'Collections-Arrayed'
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!VirtualArray class methodsFor:'documentation'!
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
2906
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    25
copyright
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    26
"
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    27
 COPYRIGHT (c) 2012 by Claus Gittinger
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    28
              All Rights Reserved
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    29
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    30
 This software is furnished under a license and may be used
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    31
 only in accordance with the terms of that license and with the
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    33
 be provided or otherwise made available to, or used by, any
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    34
 other person.  No title to or ownership of the software is
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    35
 hereby transferred.
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    36
"
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    37
!
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
    38
2714
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
documentation
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
"
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    An Array which computes its values on demand and does NOT remember those values.
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
    Use this to present huge files/hex dumps to a text editor.
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
    [author:]
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
        Claus Gittinger
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
    [see also:]
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
        LazyArray
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
"
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
!
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
examples
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
"
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
                                                                [exBegin]
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
    |squaresLines|
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
    squaresLines := VirtualArray new.
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
    squaresLines generator:[:index | index squared printString].
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
    squaresLines setSize:100000.
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
    squaresLines inspect.
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
    TextView openWith:squaresLines
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
                                                                [exEnd]
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
"
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
! !
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
!VirtualArray methodsFor:'accessing'!
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
generator
3768
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
    70
    "the element value generator; a block which gets the index as argument"
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
    71
2714
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
    ^ generator
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
!
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
3768
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
    75
generator:aBlock
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
    76
    "set the element value generator; a block which gets the index as argument"
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
    77
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
    78
    generator := aBlock.
2714
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
!
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
3768
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
    81
setSize:anInteger 
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
    82
    size := anInteger.
2714
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
!
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
size
3768
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
    86
    "the virtual size"
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
    87
2714
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
    ^ size
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
! !
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
!VirtualArray methodsFor:'collection protocol'!
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
at:index
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
    ^ generator value:index
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
    "Created: / 27-02-2012 / 20:40:03 / cg"
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
!
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
at:index put:value
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
    self noModificationError.
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
    "Created: / 27-02-2012 / 20:40:33 / cg"
3326
e6b2b0bb8037 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 3324
diff changeset
   103
!
e6b2b0bb8037 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 3324
diff changeset
   104
e6b2b0bb8037 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 3324
diff changeset
   105
grow:howBig
e6b2b0bb8037 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 3324
diff changeset
   106
    "change the receiver's size"
e6b2b0bb8037 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 3324
diff changeset
   107
e6b2b0bb8037 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 3324
diff changeset
   108
    self setSize:howBig
2714
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
! !
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
3119
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   111
!VirtualArray methodsFor:'inspecting'!
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   112
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   113
displayOn:aGCOrStream
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   114
    "print a representation of the receiver on aGCOrStream for display in inspectors etc."
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   115
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   116
    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   117
    "/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
3324
cd20bb3b5472 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   118
    (aGCOrStream isStream) ifFalse:[
3119
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   119
        ^ super displayOn:aGCOrStream
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   120
    ].
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   121
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   122
    aGCOrStream nextPutAll:('VirtualArray(size=%1, generator=%2)' 
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   123
                                bindWith:size with:generator printString).
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   124
! !
5ef27ab5efd4 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2906
diff changeset
   125
3479
c3921810b0a2 class: VirtualArray
Stefan Vogel <sv@exept.de>
parents: 3326
diff changeset
   126
!VirtualArray methodsFor:'queries'!
c3921810b0a2 class: VirtualArray
Stefan Vogel <sv@exept.de>
parents: 3326
diff changeset
   127
c3921810b0a2 class: VirtualArray
Stefan Vogel <sv@exept.de>
parents: 3326
diff changeset
   128
species
3859
3f598428b3e8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3768
diff changeset
   129
    "return the type of collection to be returned by collect, select etc."
3479
c3921810b0a2 class: VirtualArray
Stefan Vogel <sv@exept.de>
parents: 3326
diff changeset
   130
c3921810b0a2 class: VirtualArray
Stefan Vogel <sv@exept.de>
parents: 3326
diff changeset
   131
    ^ OrderedCollection
c3921810b0a2 class: VirtualArray
Stefan Vogel <sv@exept.de>
parents: 3326
diff changeset
   132
! !
c3921810b0a2 class: VirtualArray
Stefan Vogel <sv@exept.de>
parents: 3326
diff changeset
   133
2714
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
!VirtualArray class methodsFor:'documentation'!
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
version
3768
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
   137
    ^ '$Header$'
2714
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
!
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
version_CVS
3768
dd420ec16e1e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3479
diff changeset
   141
    ^ '$Header$'
2714
8b675b3e4a3b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
! !
2906
6838bf53c834 class: VirtualArray
Claus Gittinger <cg@exept.de>
parents: 2714
diff changeset
   143