BoolArray.st
author Claus Gittinger <cg@exept.de>
Fri, 19 Mar 1999 21:42:08 +0100
changeset 736 db164846b078
parent 552 21cfe670f469
child 750 4a5176b04075
permissions -rw-r--r--
documentation

"
 COPYRIGHT (c) 1995 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.

 This is a demo example:

 THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.
"

ArrayedCollection variableByteSubclass:#BooleanArray
	instanceVariableNames:'tally'
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Arrayed'
!

!BooleanArray class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1995 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.

 This is a demo example:

 THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.
"
!

documentation
"
    This is a simple demo class only; currently not used in the system.

    example for bulk boolean data (requires only 1/32th the memory
    compared to an array of booleans).

    This one stores 8 booleans per byte. Since instances store bits in multiples
    of 8, we have to keep the real size of the collection in an extra instance
    variable (tally).
    It may be useful if huge boolean arrays are to be used.

    [memory requirements:]
        OBJ-HEADER + ((size + 7) // 8)

    [see also:]
        ByteArray WordArray Array

    [author:]
        Claus Gittinger
"
!

examples
"
                                                                        [exBegin]
    (BooleanArray new:7) inspect
                                                                        [exEnd]
                                                                        [exBegin]
    (BooleanArray new:7) basicInspect
                                                                        [exEnd]
                                                                        [exBegin]
    |flags|

    flags := BooleanArray new:1000000.
    (flags at:9999) printNL.
    flags at:9999 put:true.
    (flags at:9999) printNL.
                                                                        [exEnd]
"
! !

!BooleanArray class methodsFor:'instance creation'!

new:size
    "return a new instance, capable of holding size booleans"

    |nBytes|

    nBytes := size // 8.
    (size \\ 8) ~~ 0 ifTrue:[nBytes := nBytes + 1].
    ^ (super new:nBytes) setTally:size
! !

!BooleanArray methodsFor:'accessing'!

at:index
    "retrieve the boolean at index"

    |byte mask|

    index > tally ifTrue:[
        ^ self subscriptBoundsError:index
    ].
    byte := super at:(index // 8) + 1.
    mask := 1 bitShift:(index \\ 8).
    ^ (byte bitAnd:mask) ~~ 0

    "
     (BooleanArray new:1000) at:555
    "

    "
     |b|

     b := BooleanArray new:1000.
     b at:555 put:true.
     b at:555   
    "

    "Modified: 31.7.1997 / 18:37:25 / cg"
!

at:index put:aBoolean
    "store the argument, aBoolean at index; return aBoolean."

    |byte mask idx|

    index > tally ifTrue:[
        ^ self subscriptBoundsError:index
    ].

    idx := (index // 8) + 1.
    byte := super at:idx.
    mask := 1 bitShift:(index \\ 8).
    aBoolean ifTrue:[
        byte := byte bitOr:mask
    ] ifFalse:[
        byte := byte bitAnd:(mask bitInvert)
    ].
    super at:idx put:byte.
    ^ aBoolean.

    "
     |b|

     b := BooleanArray new:1000.
     b at:555 put:true.
     b at:555   
    "

    "Modified: 31.7.1997 / 18:37:35 / cg"
!

size
    "return the size of the receiver"

    ^ tally
! !

!BooleanArray methodsFor:'inspecting'!

inspectorClass
    "return an appropriate class for inspecting"

    ^ OrderedCollectionInspectorView
! !

!BooleanArray methodsFor:'private'!

setTally:size
    tally := size
! !

!BooleanArray class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/Attic/BoolArray.st,v 1.3 1999-03-19 20:41:46 cg Exp $'
! !