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