author | Jan Vrany <jan.vrany@labware.com> |
Tue, 01 Jun 2021 20:19:13 +0100 | |
branch | jv |
changeset 25424 | 51bd8a6b196f |
parent 23107 | 40173e082cbc |
permissions | -rw-r--r-- |
19176 | 1 |
" |
2 |
COPYRIGHT (c) 2016 by eXept Sofware AG |
|
23107 | 3 |
COPYRIGHT (c) 2018 Jan Vrany |
19176 | 4 |
All Rights Reserved |
5 |
||
6 |
This software is furnished under a license and may be used |
|
7 |
only in accordance with the terms of that license and with the |
|
8 |
inclusion of the above copyright notice. This software may not |
|
9 |
be provided or otherwise made available to, or used by, any |
|
10 |
other person. No title to or ownership of the software is |
|
11 |
hereby transferred. |
|
12 |
" |
|
13 |
"{ Package: 'stx:libbasic' }" |
|
14 |
||
15 |
"{ NameSpace: Smalltalk }" |
|
16 |
||
17 |
ByteArray variableByteSubclass:#SignedByteArray |
|
18 |
instanceVariableNames:'' |
|
19 |
classVariableNames:'' |
|
20 |
poolDictionaries:'' |
|
21 |
category:'Collections-Arrayed' |
|
22 |
! |
|
23 |
||
24 |
!SignedByteArray class methodsFor:'documentation'! |
|
25 |
||
26 |
copyright |
|
27 |
" |
|
28 |
COPYRIGHT (c) 2016 by eXept Sofware AG |
|
23107 | 29 |
COPYRIGHT (c) 2018 Jan Vrany |
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 |
||
23102
574962856f04
Issue #200: fixed `#basicAt:` / `#basicAt:put:` to work with objects bigger than 2GB
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19941
diff
changeset
|
78 |
REGISTER INT indx; |
19176 | 79 |
REGISTER OBJ slf; |
80 |
REGISTER OBJ cls; |
|
23102
574962856f04
Issue #200: fixed `#basicAt:` / `#basicAt:put:` to work with objects bigger than 2GB
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19941
diff
changeset
|
81 |
REGISTER INT nIndex; |
19176 | 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); |
|
23102
574962856f04
Issue #200: fixed `#basicAt:` / `#basicAt:put:` to work with objects bigger than 2GB
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19941
diff
changeset
|
95 |
if ((unsigned INT)indx < (unsigned INT)nIndex) { |
19176 | 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 |
||
23102
574962856f04
Issue #200: fixed `#basicAt:` / `#basicAt:put:` to work with objects bigger than 2GB
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19941
diff
changeset
|
116 |
REGISTER INT indx; |
574962856f04
Issue #200: fixed `#basicAt:` / `#basicAt:put:` to work with objects bigger than 2GB
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19941
diff
changeset
|
117 |
unsigned INT nIndex; |
19176 | 118 |
int val; |
119 |
REGISTER OBJ slf; |
|
120 |
REGISTER OBJ cls; |
|
121 |
||
122 |
if (__bothSmallInteger(index, value)) { |
|
123 |
val = __intVal(value); |
|
124 |
if ((val >= -128) && (val <= 127)) { |
|
125 |
indx = __intVal(index) - 1; |
|
126 |
slf = self; |
|
127 |
if ((cls = __qClass(slf)) != @global(SignedByteArray)) { |
|
128 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
|
129 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
130 |
goto fail; |
|
131 |
} |
|
132 |
if (indx < 0) goto fail; |
|
133 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
134 |
} |
|
135 |
nIndex = __byteArraySize(slf); |
|
23102
574962856f04
Issue #200: fixed `#basicAt:` / `#basicAt:put:` to work with objects bigger than 2GB
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19941
diff
changeset
|
136 |
if ((unsigned INT)indx < (unsigned INT)nIndex) { |
19176 | 137 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
138 |
RETURN ( value ); |
|
139 |
} |
|
140 |
} |
|
141 |
} |
|
142 |
fail: ; |
|
143 |
%}. |
|
144 |
^ super basicAt:index put:value |
|
145 |
||
146 |
" |
|
147 |
(self new:5) basicAt:1 put:-1; yourself |
|
148 |
" |
|
149 |
||
150 |
"Modified: 19.4.1996 / 11:14:40 / cg" |
|
151 |
! ! |
|
152 |
||
153 |
!SignedByteArray methodsFor:'converting'! |
|
154 |
||
155 |
asSignedByteArray |
|
156 |
^ self. |
|
157 |
! |
|
158 |
||
159 |
asUnsignedByteArray |
|
160 |
"Answer a unsigned byte array. |
|
161 |
elements < 0 are converted to positive numbers." |
|
162 |
||
163 |
self class == SignedByteArray ifTrue:[ |
|
164 |
self copy changeClassTo:ByteArray. |
|
165 |
] ifFalse:[ |
|
166 |
self shouldNotImplement. |
|
167 |
]. |
|
168 |
||
169 |
" |
|
170 |
#(-1 -128 3 4) asSignedByteArray asUnsignedByteArray |
|
171 |
" |
|
172 |
! |
|
173 |
||
174 |
beSigned |
|
175 |
"that's what I am" |
|
176 |
||
177 |
^ self. |
|
178 |
! |
|
179 |
||
180 |
beUnsigned |
|
19413 | 181 |
"make myself unsigned. |
19176 | 182 |
elements < 0 are converted to positive numbers." |
183 |
||
184 |
self class == SignedByteArray ifTrue:[ |
|
185 |
self changeClassTo:ByteArray. |
|
186 |
] ifFalse:[ |
|
187 |
self shouldNotImplement. |
|
188 |
]. |
|
189 |
||
190 |
" |
|
191 |
#(-1 -128 3 4) asSignedByteArray beUnsigned |
|
192 |
" |
|
193 |
! ! |
|
194 |
||
195 |
!SignedByteArray methodsFor:'filling & replacing'! |
|
196 |
||
197 |
from:start to:stop put:aNumber |
|
198 |
"fill part of the receiver with aNumber. |
|
199 |
- reimplemented here for speed" |
|
200 |
||
201 |
%{ /* NOCONTEXT */ |
|
202 |
||
203 |
REGISTER unsigned char *dstp; |
|
204 |
REGISTER int count, value; |
|
205 |
int len, index1, index2; |
|
206 |
OBJ cls; |
|
207 |
||
208 |
if (__isSmallInteger(aNumber) |
|
209 |
&& __bothSmallInteger(start, stop) |
|
210 |
&& __isBytes(self)) { |
|
211 |
len = __byteArraySize(self); |
|
212 |
index1 = __intVal(start); |
|
213 |
index2 = __intVal(stop); |
|
214 |
||
215 |
dstp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
216 |
if ((cls = __qClass(self)) != @global(SignedByteArray)) { |
|
217 |
int nInst; |
|
218 |
||
219 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
220 |
dstp += nInst; |
|
221 |
len -= nInst; |
|
222 |
} |
|
223 |
||
224 |
value = __intVal(aNumber); |
|
225 |
if ((value >= -128) && (value <= 127) |
|
226 |
&& (index1 <= index2) |
|
227 |
&& (index1 > 0)) { |
|
228 |
if (index2 <= len) { |
|
229 |
count = index2 - index1 + 1; |
|
230 |
||
231 |
#ifdef memset4 |
|
232 |
if (count > 20) { |
|
233 |
/* fill unaligned part */ |
|
234 |
while (((unsigned INT)dstp & 3) != 0) { |
|
235 |
*dstp++ = (unsigned char)value; |
|
236 |
count--; |
|
237 |
} |
|
238 |
/* fill aligned part */ |
|
239 |
{ |
|
240 |
unsigned int n4 = count & ~3; |
|
241 |
unsigned int v4, nW; |
|
242 |
||
243 |
v4 = ((unsigned char)value << 8) | (unsigned char)value; |
|
244 |
v4 = (v4 << 16) | v4; |
|
245 |
nW = n4>>2; |
|
246 |
memset4(dstp, v4, nW); |
|
247 |
count -= n4; |
|
248 |
dstp += n4; |
|
249 |
} |
|
250 |
while (count--) { |
|
251 |
*dstp++ = (unsigned char)value; |
|
252 |
} |
|
253 |
RETURN (self); |
|
254 |
} |
|
255 |
#endif /* memset4 */ |
|
256 |
||
257 |
#if (__POINTER_SIZE__ == 8) |
|
258 |
{ |
|
259 |
unsigned INT v8; |
|
260 |
||
261 |
v8 = ((unsigned char)value << 8) | (unsigned char)value; |
|
262 |
v8 = (v8 << 16) | v8; |
|
263 |
v8 = (v8 << 32) | v8; |
|
264 |
||
265 |
/* fill unaligned part */ |
|
266 |
while ((count > 0) && (((unsigned INT)dstp & 3) != 0)) { |
|
267 |
*dstp++ = (unsigned char)value; |
|
268 |
count--; |
|
269 |
} |
|
270 |
||
271 |
if ((count >= 4) && (((unsigned INT)dstp & 7) != 0)) { |
|
272 |
((unsigned int *)dstp)[0] = v8; |
|
273 |
dstp += 4; |
|
274 |
count -= 4; |
|
275 |
} |
|
276 |
||
277 |
/* fill aligned part */ |
|
278 |
while (count >= 8) { |
|
279 |
((unsigned INT *)dstp)[0] = v8; |
|
280 |
dstp += 8; |
|
281 |
count -= 8; |
|
282 |
} |
|
283 |
||
284 |
/* fill rest */ |
|
285 |
if (count >= 4) { |
|
286 |
((unsigned int *)dstp)[0] = v8; |
|
287 |
dstp += 4; |
|
288 |
count -= 4; |
|
289 |
} |
|
290 |
if (count >= 2) { |
|
291 |
((unsigned short *)dstp)[0] = v8; |
|
292 |
dstp += 2; |
|
293 |
count -= 2; |
|
294 |
} |
|
295 |
if (count) { |
|
296 |
*dstp = (unsigned char)value; |
|
297 |
} |
|
298 |
RETURN (self); |
|
299 |
} |
|
300 |
#endif /* 64bit */ |
|
301 |
||
302 |
#ifdef FAST_MEMSET |
|
303 |
memset(dstp, value, count); |
|
304 |
#else |
|
305 |
# ifdef __UNROLL_LOOPS__ |
|
306 |
while (count >= 8) { |
|
307 |
dstp[0] = dstp[1] = dstp[2] = dstp[3] = |
|
308 |
dstp[4] = dstp[5] = dstp[6] = dstp[7] = (unsigned char)value; |
|
309 |
dstp += 8; |
|
310 |
count -= 8; |
|
311 |
} |
|
312 |
# endif /* __UNROLL_LOOPS__ */ |
|
313 |
while (count--) { |
|
314 |
*dstp++ = (unsigned char)value; |
|
315 |
} |
|
316 |
#endif |
|
317 |
RETURN (self); |
|
318 |
} |
|
319 |
} |
|
320 |
} |
|
321 |
%}. |
|
322 |
" |
|
323 |
fall back in case of non-integer index or out-of-bound index/value; |
|
324 |
will eventually lead to an out-of-bound signal raise |
|
325 |
" |
|
326 |
^ super from:start to:stop put:aNumber |
|
327 |
||
328 |
" |
|
329 |
(self new:10) from:1 to:10 put:-5 |
|
330 |
(self new:20) from:10 to:20 put:-5 |
|
331 |
(self new:20) from:1 to:10 put:-5 |
|
332 |
" |
|
333 |
! ! |
|
334 |
||
335 |
!SignedByteArray methodsFor:'queries'! |
|
336 |
||
337 |
max |
|
338 |
"return the maximum value in the receiver - |
|
339 |
redefined to speedup image processing and sound-player |
|
340 |
(which need a fast method for this on byteArrays)" |
|
341 |
||
342 |
%{ /* NOCONTEXT */ |
|
343 |
||
344 |
signed char *cp; |
|
345 |
int index, max, len; |
|
346 |
||
347 |
if (__qClass(self) == @global(SignedByteArray)) { |
|
348 |
max = -128; |
|
349 |
index = 0; |
|
350 |
len = __qSize(self) - OHDR_SIZE; |
|
19413 | 351 |
if (len > 0) { |
352 |
for (cp = __ByteArrayInstPtr(self)->ba_element; ++index <= len; cp++) { |
|
353 |
int byte = *cp; |
|
19176 | 354 |
|
19413 | 355 |
if (byte > max) { |
356 |
max = byte; |
|
357 |
if (byte == 127) break; |
|
358 |
} |
|
19176 | 359 |
} |
19413 | 360 |
RETURN ( __mkSmallInteger(max) ); |
19176 | 361 |
} |
362 |
} |
|
363 |
%}. |
|
364 |
^ super max |
|
365 |
||
366 |
" |
|
367 |
#[1 2 3 -11 2 3 1 2 19] max |
|
368 |
#(-1 -2 -3 -4) asSignedByteArray max |
|
369 |
#() asSignedByteArray max |
|
19413 | 370 |
#[] max |
371 |
#() max |
|
19176 | 372 |
" |
373 |
! ! |
|
374 |
||
375 |
!SignedByteArray methodsFor:'searching'! |
|
376 |
||
377 |
indexOf:aByte startingAt:start |
|
378 |
"return the index of the first occurrence of the argument, aByte |
|
379 |
in the receiver starting at start, anInteger; return 0 if not found. |
|
380 |
- reimplemented here for speed" |
|
381 |
||
382 |
%{ /* NOCONTEXT */ |
|
383 |
||
384 |
char *cp; |
|
385 |
INT index; |
|
386 |
int len; |
|
387 |
OBJ cls; |
|
388 |
||
389 |
if (__isSmallInteger(aByte) &&__isBytes(self)) { |
|
390 |
int byteValue = __intVal(aByte); |
|
391 |
||
392 |
if ((byteValue < -128) || (byteValue > 127)) { |
|
393 |
/* |
|
394 |
* searching for something which cannot be found |
|
395 |
*/ |
|
396 |
RETURN ( __mkSmallInteger(0) ); |
|
397 |
} |
|
398 |
||
399 |
if (__isSmallInteger(start)) { |
|
400 |
index = __intVal(start); |
|
401 |
len = __byteArraySize(self); |
|
402 |
cp = __ByteArrayInstPtr(self)->ba_element; |
|
403 |
if ((cls = __qClass(self)) != @global(SignedByteArray)) { |
|
404 |
int nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
405 |
cp += nInst; |
|
406 |
len -= nInst; |
|
407 |
} |
|
408 |
cp += index - 1; |
|
409 |
#ifdef __UNROLL_LOOPS__ |
|
410 |
while ((index+4) < len) { |
|
411 |
if (cp[0] == byteValue) { RETURN ( __mkSmallInteger(index) ); } |
|
412 |
if (cp[1] == byteValue) { RETURN ( __mkSmallInteger(index+1) ); } |
|
413 |
if (cp[2] == byteValue) { RETURN ( __mkSmallInteger(index+2) ); } |
|
414 |
if (cp[3] == byteValue) { RETURN ( __mkSmallInteger(index+3) ); } |
|
415 |
index += 4; |
|
416 |
cp += 4; |
|
417 |
} |
|
418 |
#endif |
|
419 |
while (index <= len) { |
|
420 |
if (*cp == byteValue) { |
|
421 |
RETURN ( __mkSmallInteger(index) ); |
|
422 |
} |
|
423 |
index++; |
|
424 |
cp++; |
|
425 |
} |
|
426 |
RETURN ( __mkSmallInteger(0) ); |
|
427 |
} |
|
428 |
} |
|
429 |
%}. |
|
430 |
^ super indexOf:aByte startingAt:start |
|
431 |
||
432 |
" |
|
433 |
#(-1 2 3 -4 5 6 7 8 9 0 1 2 3 4 5) asSignedByteArray indexOf:0 startingAt:1 |
|
434 |
#(-1 2 3 -4 5 6 7 8 9 0 1 2 3 4 5) asSignedByteArray indexOf:-4 startingAt:1 |
|
19413 | 435 |
#() asSignedByteArray indexOf:-4 startingAt:1 |
436 |
#() indexOf:-4 startingAt:1 |
|
19176 | 437 |
" |
438 |
! ! |
|
439 |
||
440 |
!SignedByteArray class methodsFor:'documentation'! |
|
441 |
||
442 |
version |
|
443 |
^ '$Header$' |
|
444 |
! |
|
445 |
||
446 |
version_CVS |
|
447 |
^ '$Header$' |
|
448 |
! ! |
|
449 |