VirtualArray.st
author Claus Gittinger <cg@exept.de>
Thu, 10 Jul 2014 14:24:25 +0200
changeset 3324 cd20bb3b5472
parent 3119 5ef27ab5efd4
child 3326 e6b2b0bb8037
permissions -rw-r--r--
displayOn: cleanup

"
 COPYRIGHT (c) 2012 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic2' }"

SequenceableCollection subclass:#VirtualArray
	instanceVariableNames:'generator size'
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Arrayed'
!

!VirtualArray class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2012 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    An Array which computes its values on demand and does NOT remember those values.
    Use this to present huge files/hex dumps to a text editor.

    [author:]
        Claus Gittinger

    [see also:]
        LazyArray
"
!

examples
"
                                                                [exBegin]
    |squaresLines|

    squaresLines := VirtualArray new.
    squaresLines generator:[:index | index squared printString].
    squaresLines setSize:100000.

    squaresLines inspect.
    TextView openWith:squaresLines
                                                                [exEnd]
"
! !

!VirtualArray methodsFor:'accessing'!

generator
    ^ generator
!

generator:something
    generator := something.
!

setSize:something 
    size := something.
!

size
    ^ size
! !

!VirtualArray methodsFor:'collection protocol'!

at:index
    ^ generator value:index

    "Created: / 27-02-2012 / 20:40:03 / cg"
!

at:index put:value
    self noModificationError.

    "Created: / 27-02-2012 / 20:40:33 / cg"
! !

!VirtualArray methodsFor:'inspecting'!

displayOn:aGCOrStream
    "print a representation of the receiver on aGCOrStream for display in inspectors etc."

    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
    "/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
    (aGCOrStream isStream) ifFalse:[
        ^ super displayOn:aGCOrStream
    ].

    aGCOrStream nextPutAll:('VirtualArray(size=%1, generator=%2)' 
                                bindWith:size with:generator printString).
! !

!VirtualArray class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/VirtualArray.st,v 1.4 2014-07-10 12:24:25 cg Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic2/VirtualArray.st,v 1.4 2014-07-10 12:24:25 cg Exp $'
! !