24312
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
19176
|
3 |
"
|
21695
|
4 |
COPYRIGHT (c) 2016 by eXept Software AG
|
19176
|
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 |
"{ Package: 'stx:libbasic' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
ByteArray variableByteSubclass:#SignedByteArray
|
|
19 |
instanceVariableNames:''
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-Arrayed'
|
|
23 |
!
|
|
24 |
|
|
25 |
!SignedByteArray class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
21695
|
29 |
COPYRIGHT (c) 2016 by eXept Software AG
|
19176
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
|
|
39 |
"
|
|
40 |
!
|
|
41 |
|
|
42 |
documentation
|
|
43 |
"
|
|
44 |
Instances of this class hold signed bytes in the range -128 ... +127.
|
|
45 |
|
|
46 |
[author:]
|
|
47 |
Stefan Vogel
|
|
48 |
|
|
49 |
[see also:]
|
|
50 |
ByteArray
|
|
51 |
"
|
|
52 |
! !
|
|
53 |
|
19935
|
54 |
!SignedByteArray class methodsFor:'queries'!
|
|
55 |
|
|
56 |
maxVal
|
19941
|
57 |
"the maximum value which can be stored in instances of me.
|
|
58 |
For SignedByteArrays, this is 127 (largest 8bit signed int)"
|
19935
|
59 |
|
|
60 |
^ 127
|
|
61 |
!
|
|
62 |
|
|
63 |
minVal
|
19941
|
64 |
"the minimum value which can be stored in instances of me.
|
|
65 |
For SignedByteArrays, this is -128 (smallest 8bit signed int)"
|
19935
|
66 |
|
|
67 |
^ -128
|
|
68 |
! !
|
|
69 |
|
19176
|
70 |
!SignedByteArray methodsFor:'accessing'!
|
|
71 |
|
|
72 |
basicAt:index
|
|
73 |
"return the indexed instance variable with index, anInteger
|
|
74 |
- redefined here to return a signed interger."
|
|
75 |
|
|
76 |
%{ /* NOCONTEXT */
|
|
77 |
|
|
78 |
REGISTER int indx;
|
|
79 |
REGISTER OBJ slf;
|
|
80 |
REGISTER OBJ cls;
|
|
81 |
REGISTER int nIndex;
|
|
82 |
|
|
83 |
if (__isSmallInteger(index)) {
|
|
84 |
indx = __intVal(index) - 1;
|
|
85 |
slf = self;
|
|
86 |
if ((cls = __qClass(slf)) != @global(SignedByteArray)) {
|
|
87 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK))
|
|
88 |
!= __MASKSMALLINT(BYTEARRAY)) {
|
|
89 |
goto fail;
|
|
90 |
}
|
|
91 |
if (indx < 0) goto fail;
|
|
92 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
|
|
93 |
}
|
|
94 |
nIndex = __byteArraySize(slf);
|
|
95 |
if ((unsigned)indx < (unsigned)nIndex) {
|
|
96 |
int byte = ((signed char *)__ByteArrayInstPtr(slf)->ba_element)[indx];
|
|
97 |
RETURN ( __mkSmallInteger(byte));
|
|
98 |
}
|
|
99 |
}
|
|
100 |
fail: ;
|
|
101 |
%}.
|
|
102 |
^ super basicAt:index
|
|
103 |
|
|
104 |
"
|
|
105 |
#[0 1 2 3 127 128 254 255] copy changeClassTo:self
|
|
106 |
"
|
|
107 |
!
|
|
108 |
|
|
109 |
basicAt:index put:value
|
|
110 |
"set the indexed instance variable with index, anInteger to value.
|
|
111 |
Returns value (sigh).
|
|
112 |
- redefined here to allow value to be a signed integer"
|
|
113 |
|
|
114 |
%{ /* NOCONTEXT */
|
|
115 |
|
|
116 |
REGISTER int indx;
|
|
117 |
int nIndex;
|
|
118 |
int val;
|
|
119 |
REGISTER OBJ slf;
|
|
120 |
REGISTER OBJ cls;
|
|
121 |
|
24317
|
122 |
if (! __isImmutable(self)) {
|
|
123 |
if (__bothSmallInteger(index, value)) {
|
|
124 |
val = __intVal(value);
|
|
125 |
if ((val >= -128) && (val <= 127)) {
|
|
126 |
indx = __intVal(index) - 1;
|
|
127 |
slf = self;
|
|
128 |
if ((cls = __qClass(slf)) != @global(SignedByteArray)) {
|
|
129 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK))
|
|
130 |
!= __MASKSMALLINT(BYTEARRAY)) {
|
|
131 |
goto fail;
|
|
132 |
}
|
|
133 |
if (indx < 0) goto fail;
|
|
134 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
|
19176
|
135 |
}
|
24317
|
136 |
nIndex = __byteArraySize(slf);
|
|
137 |
if ((unsigned)indx < (unsigned)nIndex) {
|
|
138 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val;
|
|
139 |
RETURN ( value );
|
|
140 |
}
|
19176
|
141 |
}
|
|
142 |
}
|
|
143 |
}
|
|
144 |
fail: ;
|
|
145 |
%}.
|
24317
|
146 |
"/ arrive here only in case of an error
|
|
147 |
self isImmutable ifTrue:[
|
|
148 |
self noModificationError
|
|
149 |
].
|
19176
|
150 |
^ super basicAt:index put:value
|
|
151 |
|
|
152 |
"
|
|
153 |
(self new:5) basicAt:1 put:-1; yourself
|
|
154 |
"
|
|
155 |
|
24317
|
156 |
"Modified: / 19-04-1996 / 11:14:40 / cg"
|
|
157 |
"Modified: / 09-06-2019 / 15:29:50 / Claus Gittinger"
|
19176
|
158 |
! !
|
|
159 |
|
|
160 |
!SignedByteArray methodsFor:'converting'!
|
|
161 |
|
|
162 |
asSignedByteArray
|
24312
|
163 |
"return the receiver as a signed byteArray.
|
|
164 |
That is the receiver already"
|
|
165 |
|
19176
|
166 |
^ self.
|
24312
|
167 |
|
|
168 |
"Modified (comment): / 09-06-2019 / 14:50:13 / Claus Gittinger"
|
19176
|
169 |
!
|
|
170 |
|
|
171 |
asUnsignedByteArray
|
|
172 |
"Answer a unsigned byte array.
|
|
173 |
elements < 0 are converted to positive numbers."
|
|
174 |
|
|
175 |
self class == SignedByteArray ifTrue:[
|
|
176 |
self copy changeClassTo:ByteArray.
|
|
177 |
] ifFalse:[
|
|
178 |
self shouldNotImplement.
|
|
179 |
].
|
|
180 |
|
|
181 |
"
|
|
182 |
#(-1 -128 3 4) asSignedByteArray asUnsignedByteArray
|
|
183 |
"
|
|
184 |
!
|
|
185 |
|
|
186 |
beSigned
|
|
187 |
"that's what I am"
|
|
188 |
|
|
189 |
^ self.
|
|
190 |
!
|
|
191 |
|
|
192 |
beUnsigned
|
24312
|
193 |
"destructively make myself unsigned.
|
|
194 |
elements < 0 are converted to positive numbers.
|
|
195 |
WARNING: this changes the receiver itself - use this only for initialization of new instances"
|
19176
|
196 |
|
|
197 |
self class == SignedByteArray ifTrue:[
|
|
198 |
self changeClassTo:ByteArray.
|
|
199 |
] ifFalse:[
|
|
200 |
self shouldNotImplement.
|
|
201 |
].
|
|
202 |
|
|
203 |
"
|
|
204 |
#(-1 -128 3 4) asSignedByteArray beUnsigned
|
|
205 |
"
|
24312
|
206 |
|
24313
|
207 |
"Modified: / 09-06-2019 / 14:57:31 / Claus Gittinger"
|
19176
|
208 |
! !
|
|
209 |
|
|
210 |
!SignedByteArray methodsFor:'filling & replacing'!
|
|
211 |
|
|
212 |
from:start to:stop put:aNumber
|
|
213 |
"fill part of the receiver with aNumber.
|
|
214 |
- reimplemented here for speed"
|
|
215 |
|
|
216 |
%{ /* NOCONTEXT */
|
|
217 |
|
|
218 |
REGISTER unsigned char *dstp;
|
|
219 |
REGISTER int count, value;
|
|
220 |
int len, index1, index2;
|
|
221 |
OBJ cls;
|
|
222 |
|
|
223 |
if (__isSmallInteger(aNumber)
|
|
224 |
&& __bothSmallInteger(start, stop)
|
|
225 |
&& __isBytes(self)) {
|
|
226 |
len = __byteArraySize(self);
|
|
227 |
index1 = __intVal(start);
|
|
228 |
index2 = __intVal(stop);
|
|
229 |
|
|
230 |
dstp = __ByteArrayInstPtr(self)->ba_element + index1 - 1;
|
|
231 |
if ((cls = __qClass(self)) != @global(SignedByteArray)) {
|
|
232 |
int nInst;
|
|
233 |
|
|
234 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
|
|
235 |
dstp += nInst;
|
|
236 |
len -= nInst;
|
|
237 |
}
|
|
238 |
|
|
239 |
value = __intVal(aNumber);
|
|
240 |
if ((value >= -128) && (value <= 127)
|
|
241 |
&& (index1 <= index2)
|
|
242 |
&& (index1 > 0)) {
|
|
243 |
if (index2 <= len) {
|
|
244 |
count = index2 - index1 + 1;
|
|
245 |
|
|
246 |
#ifdef memset4
|
|
247 |
if (count > 20) {
|
|
248 |
/* fill unaligned part */
|
|
249 |
while (((unsigned INT)dstp & 3) != 0) {
|
|
250 |
*dstp++ = (unsigned char)value;
|
|
251 |
count--;
|
|
252 |
}
|
|
253 |
/* fill aligned part */
|
|
254 |
{
|
|
255 |
unsigned int n4 = count & ~3;
|
|
256 |
unsigned int v4, nW;
|
|
257 |
|
|
258 |
v4 = ((unsigned char)value << 8) | (unsigned char)value;
|
|
259 |
v4 = (v4 << 16) | v4;
|
|
260 |
nW = n4>>2;
|
|
261 |
memset4(dstp, v4, nW);
|
|
262 |
count -= n4;
|
|
263 |
dstp += n4;
|
|
264 |
}
|
|
265 |
while (count--) {
|
|
266 |
*dstp++ = (unsigned char)value;
|
|
267 |
}
|
|
268 |
RETURN (self);
|
|
269 |
}
|
|
270 |
#endif /* memset4 */
|
|
271 |
|
|
272 |
#if (__POINTER_SIZE__ == 8)
|
|
273 |
{
|
|
274 |
unsigned INT v8;
|
|
275 |
|
|
276 |
v8 = ((unsigned char)value << 8) | (unsigned char)value;
|
|
277 |
v8 = (v8 << 16) | v8;
|
|
278 |
v8 = (v8 << 32) | v8;
|
|
279 |
|
|
280 |
/* fill unaligned part */
|
|
281 |
while ((count > 0) && (((unsigned INT)dstp & 3) != 0)) {
|
|
282 |
*dstp++ = (unsigned char)value;
|
|
283 |
count--;
|
|
284 |
}
|
|
285 |
|
|
286 |
if ((count >= 4) && (((unsigned INT)dstp & 7) != 0)) {
|
|
287 |
((unsigned int *)dstp)[0] = v8;
|
|
288 |
dstp += 4;
|
|
289 |
count -= 4;
|
|
290 |
}
|
|
291 |
|
|
292 |
/* fill aligned part */
|
|
293 |
while (count >= 8) {
|
|
294 |
((unsigned INT *)dstp)[0] = v8;
|
|
295 |
dstp += 8;
|
|
296 |
count -= 8;
|
|
297 |
}
|
|
298 |
|
|
299 |
/* fill rest */
|
|
300 |
if (count >= 4) {
|
|
301 |
((unsigned int *)dstp)[0] = v8;
|
|
302 |
dstp += 4;
|
|
303 |
count -= 4;
|
|
304 |
}
|
|
305 |
if (count >= 2) {
|
|
306 |
((unsigned short *)dstp)[0] = v8;
|
|
307 |
dstp += 2;
|
|
308 |
count -= 2;
|
|
309 |
}
|
|
310 |
if (count) {
|
|
311 |
*dstp = (unsigned char)value;
|
|
312 |
}
|
|
313 |
RETURN (self);
|
|
314 |
}
|
|
315 |
#endif /* 64bit */
|
|
316 |
|
|
317 |
#ifdef FAST_MEMSET
|
|
318 |
memset(dstp, value, count);
|
|
319 |
#else
|
|
320 |
# ifdef __UNROLL_LOOPS__
|
|
321 |
while (count >= 8) {
|
|
322 |
dstp[0] = dstp[1] = dstp[2] = dstp[3] =
|
|
323 |
dstp[4] = dstp[5] = dstp[6] = dstp[7] = (unsigned char)value;
|
|
324 |
dstp += 8;
|
|
325 |
count -= 8;
|
|
326 |
}
|
|
327 |
# endif /* __UNROLL_LOOPS__ */
|
|
328 |
while (count--) {
|
|
329 |
*dstp++ = (unsigned char)value;
|
|
330 |
}
|
|
331 |
#endif
|
|
332 |
RETURN (self);
|
|
333 |
}
|
|
334 |
}
|
|
335 |
}
|
|
336 |
%}.
|
24317
|
337 |
"/ arrive here only in case of an error
|
|
338 |
self isImmutable ifTrue:[
|
|
339 |
self noModificationError
|
|
340 |
].
|
19176
|
341 |
"
|
|
342 |
fall back in case of non-integer index or out-of-bound index/value;
|
|
343 |
will eventually lead to an out-of-bound signal raise
|
|
344 |
"
|
|
345 |
^ super from:start to:stop put:aNumber
|
|
346 |
|
|
347 |
"
|
|
348 |
(self new:10) from:1 to:10 put:-5
|
|
349 |
(self new:20) from:10 to:20 put:-5
|
|
350 |
(self new:20) from:1 to:10 put:-5
|
|
351 |
"
|
24317
|
352 |
|
|
353 |
"Modified: / 09-06-2019 / 15:30:52 / Claus Gittinger"
|
19176
|
354 |
! !
|
|
355 |
|
|
356 |
!SignedByteArray methodsFor:'queries'!
|
|
357 |
|
|
358 |
max
|
|
359 |
"return the maximum value in the receiver -
|
|
360 |
redefined to speedup image processing and sound-player
|
|
361 |
(which need a fast method for this on byteArrays)"
|
|
362 |
|
|
363 |
%{ /* NOCONTEXT */
|
|
364 |
|
|
365 |
signed char *cp;
|
|
366 |
int index, max, len;
|
|
367 |
|
|
368 |
if (__qClass(self) == @global(SignedByteArray)) {
|
|
369 |
max = -128;
|
|
370 |
index = 0;
|
|
371 |
len = __qSize(self) - OHDR_SIZE;
|
19413
|
372 |
if (len > 0) {
|
|
373 |
for (cp = __ByteArrayInstPtr(self)->ba_element; ++index <= len; cp++) {
|
|
374 |
int byte = *cp;
|
19176
|
375 |
|
19413
|
376 |
if (byte > max) {
|
|
377 |
max = byte;
|
|
378 |
if (byte == 127) break;
|
|
379 |
}
|
19176
|
380 |
}
|
19413
|
381 |
RETURN ( __mkSmallInteger(max) );
|
19176
|
382 |
}
|
|
383 |
}
|
|
384 |
%}.
|
|
385 |
^ super max
|
|
386 |
|
|
387 |
"
|
|
388 |
#[1 2 3 -11 2 3 1 2 19] max
|
|
389 |
#(-1 -2 -3 -4) asSignedByteArray max
|
|
390 |
#() asSignedByteArray max
|
19413
|
391 |
#[] max
|
|
392 |
#() max
|
19176
|
393 |
"
|
|
394 |
! !
|
|
395 |
|
|
396 |
!SignedByteArray methodsFor:'searching'!
|
|
397 |
|
|
398 |
indexOf:aByte startingAt:start
|
|
399 |
"return the index of the first occurrence of the argument, aByte
|
|
400 |
in the receiver starting at start, anInteger; return 0 if not found.
|
|
401 |
- reimplemented here for speed"
|
|
402 |
|
|
403 |
%{ /* NOCONTEXT */
|
|
404 |
|
|
405 |
char *cp;
|
|
406 |
INT index;
|
|
407 |
int len;
|
|
408 |
OBJ cls;
|
|
409 |
|
|
410 |
if (__isSmallInteger(aByte) &&__isBytes(self)) {
|
|
411 |
int byteValue = __intVal(aByte);
|
|
412 |
|
|
413 |
if ((byteValue < -128) || (byteValue > 127)) {
|
|
414 |
/*
|
|
415 |
* searching for something which cannot be found
|
|
416 |
*/
|
|
417 |
RETURN ( __mkSmallInteger(0) );
|
|
418 |
}
|
|
419 |
|
|
420 |
if (__isSmallInteger(start)) {
|
|
421 |
index = __intVal(start);
|
|
422 |
len = __byteArraySize(self);
|
|
423 |
cp = __ByteArrayInstPtr(self)->ba_element;
|
|
424 |
if ((cls = __qClass(self)) != @global(SignedByteArray)) {
|
|
425 |
int nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
|
|
426 |
cp += nInst;
|
|
427 |
len -= nInst;
|
|
428 |
}
|
|
429 |
cp += index - 1;
|
|
430 |
#ifdef __UNROLL_LOOPS__
|
|
431 |
while ((index+4) < len) {
|
|
432 |
if (cp[0] == byteValue) { RETURN ( __mkSmallInteger(index) ); }
|
|
433 |
if (cp[1] == byteValue) { RETURN ( __mkSmallInteger(index+1) ); }
|
|
434 |
if (cp[2] == byteValue) { RETURN ( __mkSmallInteger(index+2) ); }
|
|
435 |
if (cp[3] == byteValue) { RETURN ( __mkSmallInteger(index+3) ); }
|
|
436 |
index += 4;
|
|
437 |
cp += 4;
|
|
438 |
}
|
|
439 |
#endif
|
|
440 |
while (index <= len) {
|
|
441 |
if (*cp == byteValue) {
|
|
442 |
RETURN ( __mkSmallInteger(index) );
|
|
443 |
}
|
|
444 |
index++;
|
|
445 |
cp++;
|
|
446 |
}
|
|
447 |
RETURN ( __mkSmallInteger(0) );
|
|
448 |
}
|
|
449 |
}
|
|
450 |
%}.
|
|
451 |
^ super indexOf:aByte startingAt:start
|
|
452 |
|
|
453 |
"
|
|
454 |
#(-1 2 3 -4 5 6 7 8 9 0 1 2 3 4 5) asSignedByteArray indexOf:0 startingAt:1
|
|
455 |
#(-1 2 3 -4 5 6 7 8 9 0 1 2 3 4 5) asSignedByteArray indexOf:-4 startingAt:1
|
19413
|
456 |
#() asSignedByteArray indexOf:-4 startingAt:1
|
|
457 |
#() indexOf:-4 startingAt:1
|
19176
|
458 |
"
|
|
459 |
! !
|
|
460 |
|
|
461 |
!SignedByteArray class methodsFor:'documentation'!
|
|
462 |
|
|
463 |
version
|
|
464 |
^ '$Header$'
|
|
465 |
!
|
|
466 |
|
|
467 |
version_CVS
|
|
468 |
^ '$Header$'
|
|
469 |
! !
|
|
470 |
|