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