23976
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
20654
|
3 |
"
|
|
4 |
COPYRIGHT (c) 1997 by eXept Software AG / Claus Gittinger
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
|
|
14 |
This is a demo example:
|
|
15 |
|
|
16 |
THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND
|
|
17 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
18 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
19 |
ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE
|
|
20 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
21 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
22 |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
23 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
24 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
25 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
26 |
SUCH DAMAGE.
|
|
27 |
"
|
|
28 |
"{ Package: 'stx:libbasic' }"
|
|
29 |
|
|
30 |
"{ NameSpace: Smalltalk }"
|
|
31 |
|
|
32 |
ArrayedCollection variableByteSubclass:#BitArray
|
|
33 |
instanceVariableNames:'tally'
|
|
34 |
classVariableNames:''
|
|
35 |
poolDictionaries:''
|
|
36 |
category:'Collections-Arrayed'
|
|
37 |
!
|
|
38 |
|
|
39 |
!BitArray class methodsFor:'documentation'!
|
|
40 |
|
|
41 |
copyright
|
|
42 |
"
|
|
43 |
COPYRIGHT (c) 1997 by eXept Software AG / Claus Gittinger
|
|
44 |
All Rights Reserved
|
|
45 |
|
|
46 |
This software is furnished under a license and may be used
|
|
47 |
only in accordance with the terms of that license and with the
|
|
48 |
inclusion of the above copyright notice. This software may not
|
|
49 |
be provided or otherwise made available to, or used by, any
|
|
50 |
other person. No title to or ownership of the software is
|
|
51 |
hereby transferred.
|
|
52 |
|
|
53 |
This is a demo example:
|
|
54 |
|
|
55 |
THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND
|
|
56 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
57 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
58 |
ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE
|
|
59 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
60 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
61 |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
62 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
63 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
64 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
65 |
SUCH DAMAGE.
|
|
66 |
"
|
|
67 |
|
|
68 |
!
|
|
69 |
|
|
70 |
documentation
|
|
71 |
"
|
|
72 |
BitArrays are specially tuned to store bits, and are useful for bulk bit/boolean data.
|
|
73 |
They require only 1/32th (32bit machines) or 1/64th (64bit machines) of the memory
|
|
74 |
compared to an array of booleans.
|
|
75 |
|
21694
|
76 |
They store 8 bits per byte. Since instances store bits in multiples
|
20654
|
77 |
of 8, the real size of the collection is kept in an extra instance variable (tally).
|
|
78 |
It may be useful if huge boolean arrays are needed.
|
|
79 |
|
|
80 |
There are 10 types of people in this world:
|
|
81 |
Those who understand binary, & those who don't.
|
|
82 |
|
|
83 |
ATTENTION:
|
|
84 |
Bits 1 to 8 of the BooleanArray are stored in bits 8 to 1 of the
|
|
85 |
corresponding byte, to allow easy mapping to ASN.1 BIT STRING encoding
|
|
86 |
in the BER. (i.e. MSB-first)
|
|
87 |
Do not change this.
|
|
88 |
|
|
89 |
[memory requirements:]
|
|
90 |
OBJ-HEADER + ((size + 7) // 8)
|
|
91 |
|
|
92 |
[author:]
|
|
93 |
Claus Gittinger
|
|
94 |
|
|
95 |
[see also:]
|
|
96 |
BooleanArray ByteArray WordArray Array
|
|
97 |
"
|
|
98 |
!
|
|
99 |
|
|
100 |
examples
|
|
101 |
"
|
|
102 |
[exBegin]
|
|
103 |
(BitArray new:7) inspect
|
|
104 |
[exEnd]
|
|
105 |
[exBegin]
|
|
106 |
(BitArray new:7) basicInspect
|
|
107 |
[exEnd]
|
|
108 |
[exBegin]
|
|
109 |
|bits|
|
|
110 |
|
|
111 |
bits := BitArray new:1000000.
|
|
112 |
(bits at:9999) printCR.
|
|
113 |
bits at:9999 put:1.
|
|
114 |
(bits at:9999) printCR.
|
|
115 |
[exEnd]
|
|
116 |
"
|
|
117 |
! !
|
|
118 |
|
|
119 |
!BitArray class methodsFor:'instance creation'!
|
|
120 |
|
|
121 |
fromBytes:aByteArray
|
|
122 |
"return a new instance, capable of holding aByteArray size*8 bits, initialized from aByteArray"
|
|
123 |
|
|
124 |
|a|
|
|
125 |
|
|
126 |
a := self new: aByteArray size*8.
|
|
127 |
1 to:aByteArray size do:[:i | a byteAt:i put:(aByteArray at:i)].
|
|
128 |
^ a
|
|
129 |
|
|
130 |
"
|
|
131 |
BitArray fromBytes:#[ 2r00001111 2r10101010 2r01010101]
|
|
132 |
"
|
|
133 |
!
|
|
134 |
|
|
135 |
new
|
|
136 |
"return a new instance, capable of holding size bits"
|
|
137 |
|
|
138 |
^ self new:0
|
|
139 |
|
|
140 |
"
|
|
141 |
BitArray new
|
|
142 |
"
|
|
143 |
!
|
|
144 |
|
|
145 |
new:size
|
|
146 |
"return a new instance, capable of holding size bits"
|
|
147 |
|
|
148 |
|nBytes|
|
|
149 |
|
|
150 |
nBytes := (size + 7) // 8.
|
|
151 |
^ (super new:nBytes) setTally:size
|
|
152 |
|
|
153 |
"
|
|
154 |
BitArray new:10
|
|
155 |
"
|
|
156 |
!
|
|
157 |
|
|
158 |
uninitializedNew:size
|
|
159 |
^ self new:size
|
|
160 |
! !
|
|
161 |
|
|
162 |
!BitArray class methodsFor:'queries'!
|
|
163 |
|
|
164 |
maxVal
|
|
165 |
"the minimum value which can be stored in instances of me.
|
|
166 |
For BitArrays, this is 1"
|
|
167 |
|
|
168 |
^ 1
|
|
169 |
!
|
|
170 |
|
|
171 |
minVal
|
|
172 |
"the minimum value which can be stored in instances of me.
|
|
173 |
For BitArrays, this is 0"
|
|
174 |
|
|
175 |
^ 0
|
|
176 |
! !
|
|
177 |
|
|
178 |
!BitArray methodsFor:'accessing'!
|
|
179 |
|
|
180 |
at:index
|
|
181 |
"retrieve the bit at index (1..)"
|
|
182 |
|
|
183 |
|byte mask i0|
|
|
184 |
|
|
185 |
(index between:1 and:tally) ifFalse:[
|
|
186 |
^ self subscriptBoundsError:index
|
|
187 |
].
|
|
188 |
i0 := index - 1.
|
|
189 |
byte := super basicAt:(i0 // 8)+1.
|
|
190 |
mask := 1 bitShift:(7 - (i0 \\ 8)).
|
|
191 |
^ (byte bitTest:mask) ifTrue:[1] ifFalse:[0]
|
|
192 |
|
|
193 |
"
|
|
194 |
(BitArray new:1000) at:555
|
|
195 |
(BitArray new:1000) at:400 put:1; at:400
|
|
196 |
"
|
|
197 |
|
|
198 |
"
|
|
199 |
|b|
|
|
200 |
|
|
201 |
b := BitArray new:1000.
|
|
202 |
b at:555 put:1.
|
|
203 |
b at:555
|
|
204 |
"
|
|
205 |
!
|
|
206 |
|
|
207 |
at:index put:aNumber
|
|
208 |
"store the argument, aNumber at index (1..);
|
|
209 |
return the argument, aNumber (sigh)."
|
|
210 |
|
|
211 |
|byte mask idx i0|
|
|
212 |
|
|
213 |
(index between:1 and:tally) ifFalse:[
|
|
214 |
^ self subscriptBoundsError:index
|
|
215 |
].
|
|
216 |
|
|
217 |
i0 := index - 1.
|
|
218 |
idx := (i0 // 8) + 1.
|
|
219 |
byte := super basicAt:idx.
|
|
220 |
mask := 1 bitShift:(7 - (i0 \\ 8)).
|
|
221 |
aNumber == 1 ifTrue:[
|
|
222 |
byte := byte bitOr:mask
|
|
223 |
] ifFalse:[
|
|
224 |
aNumber == 0 ifTrue:[
|
|
225 |
byte := byte bitAnd:(mask bitInvert)
|
|
226 |
] ifFalse:[
|
|
227 |
"/ not 0 or 1
|
|
228 |
^ self elementBoundsError:aNumber
|
|
229 |
]
|
|
230 |
].
|
|
231 |
super basicAt:idx put:byte.
|
|
232 |
^ aNumber.
|
|
233 |
|
|
234 |
"
|
|
235 |
|b|
|
|
236 |
|
|
237 |
b := BitArray new:1000.
|
|
238 |
b at:555 put:1.
|
|
239 |
b at:555
|
|
240 |
"
|
|
241 |
!
|
|
242 |
|
|
243 |
byteAt:index
|
|
244 |
"retrieve 8 bits at index; the index is 1 for the first 8 bits, 2 for the next 8 bits etc."
|
|
245 |
|
|
246 |
^ self basicAt:index
|
|
247 |
|
|
248 |
"
|
|
249 |
((BitArray new:8) at:1 put:1); byteAt:1
|
|
250 |
"
|
|
251 |
!
|
|
252 |
|
|
253 |
byteAt:index put:aByte
|
|
254 |
"store 8 bits at index; the index is 1 for the first 8 bits, 2 for the next 8 bits etc."
|
|
255 |
|
|
256 |
^ self basicAt:index put:aByte
|
|
257 |
|
|
258 |
"
|
|
259 |
((BitArray new:8) byteAt:1 put:128); at:1
|
|
260 |
"
|
|
261 |
!
|
|
262 |
|
|
263 |
occurrencesOf:anElement
|
|
264 |
"count the occurrences of the argument, anElement in the receiver"
|
|
265 |
|
|
266 |
|nOnes|
|
|
267 |
|
|
268 |
nOnes := self countOnes.
|
|
269 |
anElement == 1 ifTrue:[
|
|
270 |
^ nOnes
|
|
271 |
].
|
|
272 |
anElement == 0 ifTrue:[
|
|
273 |
^ tally - nOnes
|
|
274 |
].
|
|
275 |
^ 0
|
|
276 |
|
|
277 |
"
|
|
278 |
(BitArray new:10)
|
|
279 |
at:4 put:1;
|
|
280 |
at:6 put:1;
|
|
281 |
at:7 put:1;
|
|
282 |
occurrencesOf:1
|
|
283 |
|
|
284 |
(BitArray new:10)
|
|
285 |
at:4 put:1;
|
|
286 |
at:6 put:1;
|
|
287 |
at:7 put:1;
|
|
288 |
occurrencesOf:0
|
|
289 |
"
|
|
290 |
! !
|
|
291 |
|
|
292 |
!BitArray methodsFor:'converting'!
|
|
293 |
|
|
294 |
bytes
|
|
295 |
"answer myself as a ByteArray containing my bytes"
|
|
296 |
|
|
297 |
|size bytes|
|
|
298 |
|
|
299 |
size := self basicSize.
|
|
300 |
bytes := ByteArray new:size.
|
|
301 |
1 to:size do:[:index|
|
|
302 |
bytes at:index put:(self byteAt:index)
|
|
303 |
].
|
|
304 |
^ bytes
|
|
305 |
! !
|
|
306 |
|
|
307 |
!BitArray methodsFor:'filling & replacing'!
|
|
308 |
|
|
309 |
atAllPut:aNumber
|
|
310 |
"replace all elements of the collection by the argument, aNumber.
|
23976
|
311 |
Return the receiver.
|
20654
|
312 |
The argument, aBoolean must be 0 or 1.
|
|
313 |
Notice: This operation modifies the receiver, NOT a copy;
|
|
314 |
therefore the change may affect all others referencing the receiver."
|
|
315 |
|
|
316 |
|v lastIndex|
|
|
317 |
|
|
318 |
lastIndex := self basicSize.
|
|
319 |
lastIndex == 0 ifTrue:[^ self].
|
|
320 |
|
|
321 |
aNumber == 1 ifTrue:[
|
|
322 |
v := 255
|
|
323 |
] ifFalse:[
|
|
324 |
aNumber == 0 ifTrue:[
|
|
325 |
v := 0
|
|
326 |
] ifFalse:[
|
|
327 |
"/
|
|
328 |
"/ booleanArrays can only hold true and false
|
|
329 |
"/
|
|
330 |
^ self elementBoundsError:aNumber
|
|
331 |
]
|
|
332 |
].
|
|
333 |
1 to:lastIndex-1 do:[:i |
|
|
334 |
self basicAt:i put:v
|
|
335 |
].
|
|
336 |
|
|
337 |
"/ ensure 0-bits above tally
|
|
338 |
v := #[ 2r11111111
|
|
339 |
2r10000000
|
|
340 |
2r11000000
|
|
341 |
2r11100000
|
|
342 |
2r11110000
|
|
343 |
2r11111000
|
|
344 |
2r11111100
|
|
345 |
2r11111110 ] at:(tally\\8)+1.
|
|
346 |
self basicAt:lastIndex put:v.
|
|
347 |
|
|
348 |
"
|
|
349 |
((self new:10) atAllPut:1) countOnes
|
|
350 |
((self new:8) atAllPut:1) countOnes
|
|
351 |
"
|
23976
|
352 |
|
|
353 |
"Modified (comment): / 26-03-2019 / 11:52:55 / Claus Gittinger"
|
20654
|
354 |
! !
|
|
355 |
|
|
356 |
!BitArray methodsFor:'logical operations'!
|
|
357 |
|
|
358 |
bitOr:aBitArray
|
|
359 |
|new mySize "{ Class: SmallInteger }" otherSize "{ Class: SmallInteger }"|
|
|
360 |
|
|
361 |
mySize := self basicSize.
|
|
362 |
otherSize := aBitArray basicSize.
|
|
363 |
|
|
364 |
new := self class basicNew:(mySize max:otherSize).
|
|
365 |
new setTally:(self size max:aBitArray size).
|
|
366 |
|
|
367 |
1 to:mySize do:[:i|
|
|
368 |
new basicAt:i put:(self basicAt:i).
|
|
369 |
].
|
|
370 |
1 to:otherSize do:[:i|
|
|
371 |
new basicAt:i put:((new basicAt:i) bitOr:(aBitArray basicAt:i)).
|
|
372 |
].
|
|
373 |
|
|
374 |
^ new
|
|
375 |
|
|
376 |
"
|
|
377 |
((BitArray new:5) at:3 put:1; yourself) bitOr:((BitArray new:8) at:5 put:1; yourself)
|
|
378 |
"
|
|
379 |
! !
|
|
380 |
|
|
381 |
!BitArray methodsFor:'private'!
|
|
382 |
|
|
383 |
countOnes
|
|
384 |
"count the 1-bits in the receiver"
|
|
385 |
|
|
386 |
|sz bI count|
|
|
387 |
|
|
388 |
count := 0.
|
|
389 |
|
|
390 |
"/ because remaining bits in the highest byte are always 0,
|
|
391 |
"/ we can simply count the 1-bits in ALL bytes... (see lastByte handling in atAllPut:)
|
|
392 |
bI := 1.
|
|
393 |
sz := self basicSize.
|
|
394 |
[bI <= sz] whileTrue:[
|
|
395 |
count := count + (self basicAt:bI) bitCount.
|
|
396 |
bI := bI + 1.
|
|
397 |
].
|
|
398 |
^ count
|
|
399 |
|
|
400 |
"/ |i nI bI bits count|
|
|
401 |
"/ i := bI := 1.
|
|
402 |
"/ [
|
|
403 |
"/ nI := i + 8.
|
|
404 |
"/ nI <= tally
|
|
405 |
"/ ] whileTrue:[
|
|
406 |
"/ bits := self basicAt:bI.
|
|
407 |
"/ count := count + bits bitCount.
|
|
408 |
"/ bI := bI + 1.
|
|
409 |
"/ i := nI
|
|
410 |
"/ ].
|
|
411 |
"/ [i <= tally] whileTrue:[
|
|
412 |
"/ (self at:i) ifTrue:[ count := count + 1].
|
|
413 |
"/ i := i + 1.
|
|
414 |
"/ ].
|
|
415 |
"/ ^ count
|
|
416 |
|
|
417 |
"
|
|
418 |
(BooleanArray new:100)
|
|
419 |
at:14 put:true;
|
|
420 |
at:55 put:true;
|
|
421 |
countOnes
|
|
422 |
|
|
423 |
(BooleanArray new:100)
|
|
424 |
at:14 put:true;
|
|
425 |
at:55 put:true;
|
|
426 |
occurrencesOf:true
|
|
427 |
|
|
428 |
(BooleanArray new:100)
|
|
429 |
at:14 put:true;
|
|
430 |
at:55 put:true;
|
|
431 |
occurrencesOf:false
|
|
432 |
"
|
|
433 |
!
|
|
434 |
|
|
435 |
indexOfNth:n occurrenceOf:what
|
|
436 |
"return the index of the nTh occurrence of a value, or 0 if there are not that many"
|
|
437 |
|
|
438 |
|sz byteIndex count countInByte|
|
|
439 |
|
|
440 |
n > self size ifTrue:[^ 0].
|
|
441 |
|
|
442 |
count := 0.
|
|
443 |
|
|
444 |
byteIndex := 1.
|
|
445 |
sz := self basicSize.
|
|
446 |
[byteIndex <= sz] whileTrue:[
|
|
447 |
countInByte := (self basicAt:byteIndex) bitCount.
|
|
448 |
what = self defaultElement ifTrue:[
|
|
449 |
countInByte := 8-countInByte.
|
|
450 |
].
|
|
451 |
count := count + countInByte.
|
|
452 |
count >= n ifTrue:[
|
|
453 |
count := count - countInByte.
|
|
454 |
(byteIndex-1)*8+1 to:(byteIndex-1)*8+8 do:[:bitIndex |
|
|
455 |
(self at:bitIndex) = what ifTrue:[
|
|
456 |
count := count + 1.
|
|
457 |
count = n ifTrue:[
|
|
458 |
^ bitIndex.
|
|
459 |
]
|
|
460 |
].
|
|
461 |
].
|
|
462 |
^ 0
|
|
463 |
].
|
|
464 |
byteIndex := byteIndex + 1.
|
|
465 |
].
|
|
466 |
^ 0
|
|
467 |
|
|
468 |
"
|
|
469 |
(BooleanArray new:100)
|
|
470 |
at:1 put:true;
|
|
471 |
at:2 put:true;
|
|
472 |
at:4 put:true;
|
|
473 |
at:5 put:true;
|
|
474 |
at:6 put:true;
|
|
475 |
at:7 put:true;
|
|
476 |
at:8 put:true;
|
|
477 |
at:10 put:true;
|
|
478 |
indexOfNth:8 occurrenceOf:false
|
|
479 |
"
|
|
480 |
!
|
|
481 |
|
|
482 |
setTally:size
|
|
483 |
"set my tally - that is the actual number of bits in me
|
|
484 |
(usually a little less than the number of bits in my byte array)"
|
|
485 |
|
|
486 |
tally := size
|
|
487 |
! !
|
|
488 |
|
|
489 |
!BitArray methodsFor:'queries'!
|
|
490 |
|
|
491 |
defaultElement
|
|
492 |
^ 0
|
|
493 |
!
|
|
494 |
|
|
495 |
isValidElement:anObject
|
|
496 |
"return true, if I can hold this kind of object"
|
|
497 |
|
|
498 |
^ anObject == 0 or:[anObject == 1]
|
|
499 |
!
|
|
500 |
|
|
501 |
size
|
|
502 |
"return the size of the receiver"
|
|
503 |
|
|
504 |
^ tally
|
|
505 |
! !
|
|
506 |
|
23976
|
507 |
|
20654
|
508 |
!BitArray methodsFor:'visiting'!
|
|
509 |
|
|
510 |
acceptVisitor:aVisitor with:aParameter
|
|
511 |
"dispatch for visitor pattern; send #visitBitArray:with: to aVisitor"
|
|
512 |
|
|
513 |
^ aVisitor visitBitArray:self with:aParameter
|
|
514 |
! !
|
|
515 |
|
|
516 |
!BitArray class methodsFor:'documentation'!
|
|
517 |
|
|
518 |
version
|
|
519 |
^ '$Header$'
|
|
520 |
!
|
|
521 |
|
|
522 |
version_CVS
|
|
523 |
^ '$Header$'
|
|
524 |
! !
|
|
525 |
|