WordArray.st
author Claus Gittinger <cg@exept.de>
Wed, 18 Dec 2019 19:05:01 +0100
changeset 25155 5ab0366fbef0
parent 24716 d25f4c09bd5b
permissions -rw-r--r--
regenerated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20647
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 1989 by Claus Gittinger
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
	      All Rights Reserved
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 hereby transferred.
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
"
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic' }"
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
"{ NameSpace: Smalltalk }"
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
UnboxedIntegerArray variableWordSubclass:#WordArray
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:''
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	category:'Collections-Arrayed'
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
24716
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    23
!WordArray class methodsFor:'documentation'!
20647
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
24716
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    25
copyright
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    26
"
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    27
 COPYRIGHT (c) 1989 by Claus Gittinger
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    28
	      All Rights Reserved
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    29
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    30
 This software is furnished under a license and may be used
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    31
 only in accordance with the terms of that license and with the
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    33
 be provided or otherwise made available to, or used by, any
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    34
 other person.  No title to or ownership of the software is
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    35
 hereby transferred.
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    36
"
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    37
!
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    38
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    39
documentation
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    40
"
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    41
    WordArrays store integers in the range 0..16rFFFF.
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    42
    In contrast to normal arrays (which store pointers to their elements),
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    43
    wordArrays store the values in a dense & compact way. 
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    44
    Since the representation fits the underlying C-language systems representation
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    45
    of unsigned int16's, this is also useful to pass bulk data to c primitive code.
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    46
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    47
    WordArrays can be used to hold bulk integer data in a more compact way.
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    48
        For example:
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    49
            Array new:100000 withAll:1
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    50
        requires 400k of object memory;
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    51
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    52
        in contrast,
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    53
            WordArray new:100000 withAll:1
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    54
        only requires half of it.
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    55
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    56
    [memory requirements:]
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    57
        OBJ-HEADER + (size * 2)
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    58
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    59
    [see also:]
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    60
        ByteArray BooleanArray FloatArray DoubleArray Array
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    61
        SignedWordArray
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    62
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    63
    [caveat:]
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    64
        should probably be renamed to UnsignedInt16Array or UInt16Array
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    65
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    66
    [author:]
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    67
        Claus Gittinger
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    68
"
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    69
! !
20647
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
21436
0d0ac206b48a #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20647
diff changeset
    71
0d0ac206b48a #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20647
diff changeset
    72
24716
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    73
!WordArray class methodsFor:'queries'!
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    74
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    75
elementByteSize
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    76
    "for bit-like containers, return the number of bytes stored per element.
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    77
     Here, 2 is returned"
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    78
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    79
    ^ 2
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    80
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    81
    "Created: / 15-09-2011 / 14:10:54 / cg"
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    82
!
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    83
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    84
maxVal
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    85
    "the maximum value which can be stored in instances of me.
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    86
     For WordArrays, this is 16rFFFF (largest 16bit unsigned int)"
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    87
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    88
    ^ 16rFFFF
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    89
!
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    90
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    91
minVal
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    92
    "the minimum value which can be stored in instances of me.
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    93
     For WordArrays, this is 0"
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    94
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    95
    ^ 0
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    96
! !
20647
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
21436
0d0ac206b48a #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20647
diff changeset
    98
24716
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
    99
!WordArray methodsFor:'accessing'!
20647
71237c514dee initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
24716
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   101
unsignedInt16At:index MSB:msb
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   102
    "return the 2-bytes starting at index as an (unsigned) Integer.
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   103
     The index is a smalltalk index (i.e. 1-based).
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   104
     The value is retrieved MSB (high 8 bits at lower index) if msb is true;
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   105
     LSB-first (i.e. low 8-bits at lower byte index) if it's false.
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   106
     Notice: 
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   107
        the index is a byte index; thus, this allows for unaligned access to
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   108
        words on any boundary"
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   109
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   110
    |w|
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   111
    
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   112
    index odd ifTrue:[
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   113
        "/ aligned fetch
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   114
        w := self at:(index // 2) + 1.
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   115
        (msb ~~ UninterpretedBytes isBigEndian) ifTrue:[
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   116
            w := w swapBytes
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   117
        ].    
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   118
        ^ w
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   119
    ].
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   120
    ^ super unsignedInt16At:index MSB:msb
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   121
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   122
    "
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   123
     #(16r0201 16r0403 16r0605) asWordArray wordAt:1 MSB:false
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   124
     #(16r0201 16r0403 16r0605) asWordArray wordAt:3 MSB:false
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   125
     #(16r0201 16r0403 16r0605) asWordArray wordAt:5 MSB:false
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   126
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   127
     #(16r0201 16r0403 16r0605) asWordArray wordAt:2 MSB:false
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   128
     #(16r0201 16r0403 16r0605) asWordArray wordAt:4 MSB:false
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   129
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   130
     #(16rFFEE 16r0403 16r0605) asWordArray wordAt:1 MSB:false
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   131
     #(16rFFEE 16r0403 16r0605) asWordArray wordAt:1 MSB:true
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   132
    "
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   133
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   134
    "Modified (comment): / 13-02-2017 / 20:34:38 / cg"
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   135
! !
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   136
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   137
!WordArray class methodsFor:'documentation'!
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   138
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   139
version_CVS
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   140
    ^ '$Header$'
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   141
! !
d25f4c09bd5b #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 24674
diff changeset
   142