author | Stefan Vogel <sv@exept.de> |
Thu, 17 Nov 2005 12:07:08 +0100 | |
changeset 8996 | 239e0796331c |
parent 8919 | 707a9ff7f9b2 |
child 9368 | c5a70cfa0f18 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
5425 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
3208
2d71538b9fd5
now subclass of UIBytes - which contains common protocol
Claus Gittinger <cg@exept.de>
parents:
3205
diff
changeset
|
15 |
UninterpretedBytes variableByteSubclass:#ByteArray |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
16 |
instanceVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
17 |
classVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
18 |
poolDictionaries:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
19 |
category:'Collections-Arrayed' |
1 | 20 |
! |
21 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
22 |
!ByteArray class methodsFor:'documentation'! |
88 | 23 |
|
563 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
88 | 38 |
documentation |
39 |
" |
|
40 |
ByteArrays store integers in the range 0..255. |
|
129 | 41 |
In contrast to normal arrays (which store pointers to their elements), |
362 | 42 |
byteArrays store the values in a dense & compact way. ByteArrays can |
129 | 43 |
be used to hold the data for bitmaps, images and other bulk data. |
44 |
ByteArrays are also used to store the bytecode-instructions of an |
|
45 |
interpreted method and are used as superclass for Strings. |
|
46 |
||
47 |
ByteArrays can be used as literals i.e. you can enter ByteArray-constants |
|
48 |
as #[ element1 element2 .... elementN] and also use byteArray constants |
|
49 |
as elements in a constant array. |
|
50 |
As in: #( #[1 1 1] #[2 2 2] #[3 3 3]) |
|
51 |
||
52 |
If you have to communicate structure-data (in the C-sense) with external |
|
53 |
programs/data-bases, see a companion class (Structure) in the goodies directory. |
|
54 |
It allows the definition of subclasses of ByteArray, which transparently fetch |
|
55 |
and store C-structure fields. |
|
1266 | 56 |
|
4062 | 57 |
[memory requirements:] |
4661 | 58 |
OBJ-HEADER + size |
4062 | 59 |
|
2145 | 60 |
[warning:] |
4661 | 61 |
read the warning about 'growing fixed size collection' |
62 |
in ArrayedCollection's documentation |
|
2145 | 63 |
|
1290 | 64 |
[author:] |
4661 | 65 |
Claus Gittinger |
1290 | 66 |
|
1266 | 67 |
[See also:] |
4661 | 68 |
Array CharacterArray String |
88 | 69 |
" |
2 | 70 |
! ! |
71 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
72 |
!ByteArray class methodsFor:'instance creation'! |
1 | 73 |
|
74 |
uninitializedNew:anInteger |
|
75 |
"return a new instance of the receiver with uninitialized |
|
76 |
(i.e. undefined) contents. The indexed elements have any random |
|
8908 | 77 |
value. However, any named instance variables are still nilled. |
75 | 78 |
For use, when contents will be set anyway shortly after - this |
79 |
is a bit faster than the regular basicNew:, which clears the bytes. |
|
80 |
Of course, it only makes a difference for very big ByteArrays, such |
|
81 |
as used for images/bitmaps. |
|
82 |
||
83 |
Notice: if you want to port code using uninitializedNew: to another |
|
8908 | 84 |
smalltalk, you have to add an 'uninitializedNew: -> basicNew:'-calling |
75 | 85 |
method to the ByteArray class of the other smalltalk." |
1 | 86 |
|
87 |
%{ /* NOCONTEXT */ |
|
88 |
OBJ newobj; |
|
89 |
INT instsize, nInstVars, nindexedinstvars; |
|
90 |
REGISTER OBJ *op; |
|
91 |
||
249 | 92 |
if (__isSmallInteger(anInteger)) { |
8908 | 93 |
nindexedinstvars = __intVal(anInteger); |
94 |
if (nindexedinstvars >= 0) { |
|
95 |
if (self == ByteArray) { |
|
96 |
/* |
|
97 |
* the most common case |
|
98 |
*/ |
|
99 |
instsize = OHDR_SIZE + nindexedinstvars; |
|
100 |
if (__CanDoQuickNew(instsize)) { /* OBJECT ALLOCATION */ |
|
101 |
__qCheckedNew(newobj, instsize); |
|
102 |
__InstPtr(newobj)->o_class = self; |
|
103 |
__qSTORE(newobj, self); |
|
104 |
RETURN (newobj ); |
|
105 |
} |
|
106 |
} else { |
|
107 |
/* |
|
108 |
* Take care for subclasses like TwoByteString |
|
109 |
*/ |
|
110 |
switch (__smallIntegerVal(__ClassInstPtr(self)->c_flags) & ARRAYMASK) { |
|
111 |
case BYTEARRAY: |
|
112 |
break; |
|
113 |
||
114 |
case WORDARRAY: |
|
115 |
case SWORDARRAY: |
|
116 |
nindexedinstvars *= 2; |
|
117 |
break; |
|
118 |
||
119 |
case LONGARRAY: |
|
120 |
case SLONGARRAY: |
|
121 |
nindexedinstvars *= 4; |
|
122 |
break; |
|
123 |
||
124 |
default: |
|
125 |
/* don't know about this array type, delegate to super */ |
|
126 |
goto out; |
|
127 |
} |
|
128 |
} |
|
129 |
nInstVars = __intVal(__ClassInstPtr(self)->c_ninstvars); |
|
130 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars; |
|
131 |
__PROTECT_CONTEXT__ |
|
132 |
__qNew(newobj, instsize); /* OBJECT ALLOCATION */ |
|
133 |
__UNPROTECT_CONTEXT__ |
|
134 |
if (newobj != nil) { |
|
135 |
__InstPtr(newobj)->o_class = self; |
|
136 |
__qSTORE(newobj, self); |
|
137 |
if (nInstVars) { |
|
138 |
/* |
|
139 |
* still have to nil out named instvars ... |
|
140 |
*/ |
|
788
e80f1c42b87b
dont use memset4 if its undefined
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
141 |
#if defined(memset4) && defined(FAST_OBJECT_MEMSET4) |
8908 | 142 |
memset4(__InstPtr(newobj)->i_instvars, nil, nInstVars); |
1 | 143 |
#else |
249 | 144 |
# if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES) |
8908 | 145 |
/* |
146 |
* knowing that nil is 0 |
|
147 |
*/ |
|
148 |
memset(__InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
|
249 | 149 |
# else |
8908 | 150 |
op = __InstPtr(newobj)->i_instvars; |
151 |
while (nInstVars--) |
|
152 |
*op++ = nil; |
|
249 | 153 |
# endif |
1 | 154 |
#endif |
8908 | 155 |
} |
156 |
RETURN ( newobj ); |
|
157 |
} |
|
158 |
} |
|
1 | 159 |
} |
8159
ac6725a62e17
#uninitializedNew: - take care of WORDARRAYS etc.
Stefan Vogel <sv@exept.de>
parents:
8096
diff
changeset
|
160 |
out:; |
5425 | 161 |
%}. |
1 | 162 |
^ self basicNew:anInteger |
163 |
! ! |
|
164 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
165 |
!ByteArray class methodsFor:'binary storage'! |
1 | 166 |
|
550 | 167 |
binaryDefinitionFrom: stream manager: manager |
168 |
"get a ByteArray from the binary stream. |
|
3071 | 169 |
ByteArrays are stored as 4-byte size, followed by the bytes. |
8908 | 170 |
This is only invoked for long bytearrays. |
3071 | 171 |
Short ones are stored with 1byte length." |
172 |
||
173 |
|b len| |
|
1 | 174 |
|
550 | 175 |
"take care of subclasses ..." |
176 |
self == ByteArray ifTrue:[ |
|
4661 | 177 |
len := stream nextNumber:4. |
178 |
b := ByteArray uninitializedNew:len. |
|
179 |
stream nextBytes:len into:b startingAt:1. |
|
180 |
^ b |
|
550 | 181 |
]. |
3071 | 182 |
^ super binaryDefinitionFrom:stream manager:manager |
183 |
||
184 |
"Modified: / 2.11.1997 / 16:19:49 / cg" |
|
550 | 185 |
! ! |
186 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
187 |
!ByteArray class methodsFor:'queries'! |
550 | 188 |
|
189 |
isBuiltInClass |
|
1266 | 190 |
"return true if this class is known by the run-time-system. |
191 |
Here, true is returned for myself, false for subclasses." |
|
550 | 192 |
|
193 |
^ self == ByteArray |
|
1266 | 194 |
|
195 |
"Modified: 23.4.1996 / 15:56:25 / cg" |
|
550 | 196 |
! ! |
197 |
||
198 |
!ByteArray methodsFor:'accessing'! |
|
1 | 199 |
|
563 | 200 |
basicAt:index |
201 |
"return the indexed instance variable with index, anInteger |
|
1019 | 202 |
- redefined here to be slighly faster than the default in Object. |
203 |
Q: is it worth the extra code ?" |
|
563 | 204 |
|
205 |
%{ /* NOCONTEXT */ |
|
206 |
||
207 |
REGISTER int indx; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
208 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
209 |
REGISTER OBJ cls; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
210 |
REGISTER int nIndex; |
563 | 211 |
|
212 |
if (__isSmallInteger(index)) { |
|
4661 | 213 |
indx = __intVal(index) - 1; |
214 |
slf = self; |
|
215 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
216 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
|
217 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
218 |
goto fail; |
|
219 |
} |
|
220 |
if (indx < 0) goto fail; |
|
221 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
222 |
} |
|
223 |
nIndex = __byteArraySize(slf); |
|
224 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
8913 | 225 |
RETURN ( __mkSmallInteger((__ByteArrayInstPtr(slf)->ba_element[indx])) ); |
4661 | 226 |
} |
563 | 227 |
} |
1019 | 228 |
fail: ; |
229 |
%}. |
|
563 | 230 |
^ super basicAt:index |
231 |
! |
|
232 |
||
1 | 233 |
basicAt:index put:value |
1230 | 234 |
"set the indexed instance variable with index, anInteger to value. |
235 |
Returns value (sigh). |
|
1019 | 236 |
- redefined here to be slighly faster than the default in Object. |
237 |
Q: is it worth the extra code ?" |
|
1 | 238 |
|
239 |
%{ /* NOCONTEXT */ |
|
240 |
||
241 |
REGISTER int indx; |
|
242 |
int nIndex; |
|
243 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
244 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
245 |
REGISTER OBJ cls; |
1 | 246 |
|
249 | 247 |
if (__bothSmallInteger(index, value)) { |
4661 | 248 |
val = __intVal(value); |
249 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
|
250 |
indx = __intVal(index) - 1; |
|
251 |
slf = self; |
|
252 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
253 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
|
254 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
255 |
goto fail; |
|
256 |
} |
|
257 |
if (indx < 0) goto fail; |
|
258 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
259 |
} |
|
260 |
nIndex = __byteArraySize(slf); |
|
261 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
262 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
|
263 |
RETURN ( value ); |
|
264 |
} |
|
265 |
} |
|
1 | 266 |
} |
1019 | 267 |
fail: ; |
268 |
%}. |
|
1 | 269 |
^ super basicAt:index put:value |
1230 | 270 |
|
271 |
"Modified: 19.4.1996 / 11:14:40 / cg" |
|
2 | 272 |
! |
273 |
||
4128 | 274 |
bitAt:index |
8908 | 275 |
"return the bit at index (1 based index)" |
5439 | 276 |
|
277 |
|byteIndex bitIndex0 byte| |
|
4128 | 278 |
|
279 |
%{ /* NOCONTEXT */ |
|
280 |
||
281 |
REGISTER int indx; |
|
282 |
REGISTER int byte; |
|
283 |
int nIndex; |
|
284 |
REGISTER OBJ slf; |
|
285 |
REGISTER OBJ cls; |
|
286 |
||
287 |
if (__isSmallInteger(index)) { |
|
8908 | 288 |
indx = __intVal(index) - 1; |
289 |
slf = self; |
|
290 |
||
291 |
byte = indx / 8; |
|
292 |
indx = indx % 8; |
|
293 |
||
294 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
295 |
if (indx < 0) goto badIndex; |
|
296 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
297 |
} |
|
298 |
nIndex = __byteArraySize(slf); |
|
299 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
8913 | 300 |
RETURN ( __mkSmallInteger(((__ByteArrayInstPtr(slf)->ba_element[byte] & (1 << indx)) != 0)) ); |
8908 | 301 |
} |
4128 | 302 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
303 |
badIndex: ; |
4128 | 304 |
%}. |
5439 | 305 |
byteIndex := ((index-1) // 8) + 1. |
306 |
bitIndex0 := ((index-1) \\ 8). |
|
307 |
byte := self at:byteIndex. |
|
308 |
^ byte bitTest:(1 bitShift:bitIndex0). |
|
4128 | 309 |
|
310 |
" |
|
311 |
#[ 1 1 1 1 ] bitAt:9 |
|
312 |
#[ 1 1 1 1 ] bitAt:11 |
|
313 |
#[ 2 2 2 2 ] bitAt:10 |
|
314 |
" |
|
315 |
||
316 |
||
317 |
||
318 |
! |
|
319 |
||
5439 | 320 |
bitClearAt:index |
8908 | 321 |
"clear the bit at index (index starts with 1)" |
5439 | 322 |
|
323 |
|byteIndex bitIndex0 byte| |
|
4128 | 324 |
|
325 |
%{ /* NOCONTEXT */ |
|
326 |
||
327 |
REGISTER int indx; |
|
328 |
REGISTER int byte; |
|
329 |
int nIndex; |
|
330 |
REGISTER OBJ slf; |
|
331 |
REGISTER OBJ cls; |
|
332 |
||
5439 | 333 |
if (0 /* __isSmallInteger(index) */) { |
8908 | 334 |
indx = __intVal(index) - 1; |
335 |
slf = self; |
|
336 |
||
337 |
byte = indx / 8; |
|
338 |
indx = indx % 8; |
|
339 |
||
340 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
341 |
if (indx < 0) goto badIndex; |
|
342 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
343 |
} |
|
344 |
nIndex = __byteArraySize(slf); |
|
345 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
346 |
__ByteArrayInstPtr(slf)->ba_element[byte] &= ~(1 << indx); |
|
347 |
RETURN (slf); |
|
348 |
} |
|
4128 | 349 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
350 |
badIndex: ; |
4128 | 351 |
%}. |
5439 | 352 |
byteIndex := ((index-1) // 8) + 1. |
353 |
bitIndex0 := ((index-1) \\ 8). |
|
354 |
byte := self at:byteIndex. |
|
355 |
byte := byte bitClear:(1 bitShift:bitIndex0). |
|
356 |
self at:byteIndex put:byte. |
|
357 |
||
358 |
" |
|
8908 | 359 |
#[0 0 0 0] copy bitClearAt:1 |
360 |
#[0 0 0 0] copy bitClearAt:7 |
|
361 |
#[0 0 0 0] copy bitClearAt:8 |
|
362 |
#[0 0 0 0] copy bitClearAt:9 |
|
5439 | 363 |
" |
4128 | 364 |
" |
8908 | 365 |
#[ 0 0 0 0 ] bitSetAt:1 |
366 |
#[ 0 0 0 0 ] bitSetAt:4 |
|
367 |
#[ 0 0 0 0 ] bitSetAt:8 |
|
368 |
#[ 0 0 0 0 ] bitSetAt:9 |
|
369 |
#[ 0 0 0 0 ] bitSetAt:10 |
|
370 |
#[ 0 0 0 0 ] bitSetAt:11 |
|
5439 | 371 |
" |
372 |
||
373 |
||
374 |
||
375 |
! |
|
376 |
||
377 |
bitSetAt:index |
|
8908 | 378 |
"set the bit at index (index starts with 1)" |
5439 | 379 |
|
380 |
|byteIndex bitIndex0 byte| |
|
381 |
||
382 |
%{ /* NOCONTEXT */ |
|
383 |
||
384 |
REGISTER int indx; |
|
385 |
REGISTER int byte; |
|
386 |
int nIndex; |
|
387 |
REGISTER OBJ slf; |
|
388 |
REGISTER OBJ cls; |
|
389 |
||
390 |
if (0 /* __isSmallInteger(index) */) { |
|
8908 | 391 |
indx = __intVal(index) - 1; |
392 |
slf = self; |
|
393 |
||
394 |
byte = indx / 8; |
|
395 |
indx = indx % 8; |
|
396 |
||
397 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
398 |
if (indx < 0) goto badIndex; |
|
399 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
400 |
} |
|
401 |
nIndex = __byteArraySize(slf); |
|
402 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
403 |
__ByteArrayInstPtr(slf)->ba_element[byte] |= (1 << indx); |
|
404 |
RETURN (slf); |
|
405 |
} |
|
5439 | 406 |
} |
407 |
badIndex: ; |
|
408 |
%}. |
|
409 |
byteIndex := ((index-1) // 8) + 1. |
|
410 |
bitIndex0 := ((index-1) \\ 8). |
|
411 |
byte := self at:byteIndex. |
|
412 |
byte := byte bitOr:(1 bitShift:bitIndex0). |
|
413 |
self at:byteIndex put:byte. |
|
414 |
||
415 |
" |
|
8908 | 416 |
#[0 0 0 0] copy bitSetAt:1 |
417 |
#[0 0 0 0] copy bitSetAt:7 |
|
418 |
#[0 0 0 0] copy bitSetAt:8 |
|
419 |
#[0 0 0 0] copy bitSetAt:9 |
|
5439 | 420 |
" |
421 |
" |
|
8908 | 422 |
#[ 0 0 0 0 ] bitSetAt:1 |
423 |
#[ 0 0 0 0 ] bitSetAt:4 |
|
424 |
#[ 0 0 0 0 ] bitSetAt:8 |
|
425 |
#[ 0 0 0 0 ] bitSetAt:9 |
|
426 |
#[ 0 0 0 0 ] bitSetAt:10 |
|
427 |
#[ 0 0 0 0 ] bitSetAt:11 |
|
4128 | 428 |
" |
429 |
||
430 |
||
431 |
||
5554 | 432 |
! ! |
433 |
||
434 |
!ByteArray methodsFor:'accessing-bytes'! |
|
4128 | 435 |
|
54 | 436 |
byteAt:index |
8908 | 437 |
"return the byte at index. |
438 |
For ByteArray, this is the same as basicAt:; |
|
249 | 439 |
however, for strings or symbols, this returns a numeric byteValue |
440 |
instead of a character." |
|
441 |
||
442 |
%{ /* NOCONTEXT */ |
|
443 |
||
444 |
REGISTER int indx; |
|
445 |
int nIndex; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
446 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
447 |
REGISTER OBJ cls; |
54 | 448 |
|
249 | 449 |
if (__isSmallInteger(index)) { |
4661 | 450 |
indx = __intVal(index) - 1; |
451 |
slf = self; |
|
452 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
453 |
if (indx < 0) goto badIndex; |
|
454 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
455 |
} |
|
456 |
nIndex = __byteArraySize(slf); |
|
457 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
8913 | 458 |
RETURN ( __mkSmallInteger((__ByteArrayInstPtr(slf)->ba_element[indx])) ); |
4661 | 459 |
} |
249 | 460 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
461 |
badIndex: ; |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
462 |
%}. |
249 | 463 |
^ (super basicAt:index) asInteger |
54 | 464 |
! |
465 |
||
249 | 466 |
byteAt:index put:value |
467 |
"set the byte at index. For ByteArray, this is the same as basicAt:put:. |
|
468 |
However, for Strings, this expects a byteValue to be stored." |
|
469 |
||
470 |
%{ /* NOCONTEXT */ |
|
471 |
||
472 |
REGISTER int indx; |
|
473 |
int nIndex; |
|
474 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
475 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
476 |
REGISTER OBJ cls; |
54 | 477 |
|
249 | 478 |
if (__bothSmallInteger(index, value)) { |
4661 | 479 |
val = __intVal(value); |
480 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
|
481 |
indx = __intVal(index) - 1; |
|
482 |
slf = self; |
|
483 |
if ((cls = __qClass(slf)) != ByteArray) { |
|
484 |
if (indx < 0) goto badIndex; |
|
485 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
486 |
} |
|
487 |
nIndex = __byteArraySize(slf); |
|
488 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
489 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
|
490 |
RETURN ( value ); |
|
491 |
} |
|
492 |
} |
|
249 | 493 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
494 |
badIndex: ; |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
495 |
%}. |
249 | 496 |
^ super basicAt:index put:value |
5554 | 497 |
! ! |
498 |
||
499 |
!ByteArray methodsFor:'accessing-longs'! |
|
54 | 500 |
|
2 | 501 |
doubleWordAt:index put:value |
502 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
359 | 503 |
The value should be in the range 0 to 16rFFFFFFFF |
504 |
(for negative values, the stored value is not defined). |
|
505 |
The value is stored in the machines natural byte order. |
|
506 |
Q: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
|
507 |
||
508 |
|t| |
|
509 |
||
510 |
%{ /* NOCONTEXT */ |
|
511 |
||
512 |
REGISTER int indx; |
|
513 |
int nIndex; |
|
514 |
union { |
|
8908 | 515 |
unsigned char u_char[4]; |
516 |
unsigned int u_uint; |
|
359 | 517 |
} val; |
518 |
OBJ cls; |
|
2950 | 519 |
unsigned char *byteP; |
359 | 520 |
|
521 |
if (__isSmallInteger(index)) { |
|
8908 | 522 |
if (__isSmallInteger(value)) { |
523 |
val.u_uint = __intVal(value); |
|
524 |
} else { |
|
525 |
val.u_uint = __longIntVal(value); |
|
526 |
if (val.u_uint == 0) goto error; |
|
527 |
} |
|
528 |
||
529 |
indx = __intVal(index); |
|
530 |
if (indx > 0) { |
|
531 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
|
532 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
533 |
nIndex = __qSize(self) - OHDR_SIZE; |
|
534 |
if ((indx+3) <= nIndex) { |
|
535 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
536 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
8908 | 537 |
((unsigned int *)byteP)[0] = val.u_uint; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
538 |
#else |
8908 | 539 |
if (((unsigned INT)byteP & 3) == 0) { |
540 |
((unsigned int *)byteP)[0] = val.u_uint; |
|
541 |
} else { |
|
542 |
byteP[0] = val.u_char[0]; |
|
543 |
byteP[1] = val.u_char[1]; |
|
544 |
byteP[2] = val.u_char[2]; |
|
545 |
byteP[3] = val.u_char[3]; |
|
546 |
} |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
547 |
#endif |
8908 | 548 |
RETURN ( value ); |
549 |
} |
|
550 |
} |
|
359 | 551 |
} |
552 |
error: ; |
|
553 |
%}. |
|
5439 | 554 |
^ super doubleWordAt:index put:value. |
359 | 555 |
|
556 |
" |
|
557 |
|b| |
|
558 |
b := ByteArray new:4. |
|
559 |
b doubleWordAt:1 put:16r04030201. |
|
560 |
b inspect |
|
561 |
" |
|
562 |
! |
|
563 |
||
564 |
doubleWordAt:index put:value MSB:msb |
|
565 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
129 | 566 |
The value must be in the range 0 to 16rFFFFFFFF. |
359 | 567 |
The value is stored MSB-first if msb is true; LSB-first otherwise. |
129 | 568 |
question: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
2 | 569 |
|
570 |
|t| |
|
571 |
||
572 |
%{ /* NOCONTEXT */ |
|
573 |
||
574 |
REGISTER int indx; |
|
575 |
int nIndex; |
|
576 |
int val; |
|
249 | 577 |
OBJ cls; |
2950 | 578 |
unsigned char *byteP; |
2 | 579 |
|
359 | 580 |
if (__isSmallInteger(index)) { |
8908 | 581 |
if (__isSmallInteger(value)) { |
582 |
val = __intVal(value); |
|
583 |
} else { |
|
584 |
val = __longIntVal(value); |
|
585 |
if (val == 0) goto error; |
|
586 |
} |
|
587 |
indx = __intVal(index); |
|
588 |
if (indx > 0) { |
|
589 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
|
590 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
591 |
nIndex = __qSize(self) - OHDR_SIZE; |
|
592 |
if ((indx+3) <= nIndex) { |
|
593 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
594 |
if (msb == true) { |
|
595 |
/* |
|
596 |
* most significant byte first (i.e sparc order) |
|
597 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
598 |
#if defined(__MSBFIRST__) |
8908 | 599 |
if (((INT)byteP & 3) == 0) { |
600 |
((int *)byteP)[0] = val; |
|
601 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
602 |
#endif |
8908 | 603 |
{ |
604 |
byteP[3] = val & 0xFF; |
|
605 |
val >>= 8; |
|
606 |
byteP[2] = val & 0xFF; |
|
607 |
val >>= 8; |
|
608 |
byteP[1] = val & 0xFF; |
|
609 |
val >>= 8; |
|
610 |
byteP[0] = val & 0xFF; |
|
611 |
} |
|
612 |
} else { |
|
613 |
/* |
|
614 |
* least significant byte first (i.e i386/alpha order) |
|
615 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
616 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
8908 | 617 |
((int *)byteP)[0] = val; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
618 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
619 |
# if defined(__LSBFIRST__) |
8908 | 620 |
if (((unsigned INT)byteP & 3) == 0) { |
621 |
((int *)byteP)[0] = val; |
|
622 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
623 |
# endif |
8908 | 624 |
{ |
625 |
byteP[0] = val & 0xFF; |
|
626 |
val >>= 8; |
|
627 |
byteP[1] = val & 0xFF; |
|
628 |
val >>= 8; |
|
629 |
byteP[2] = val & 0xFF; |
|
630 |
val >>= 8; |
|
631 |
byteP[3] = val & 0xFF; |
|
632 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
633 |
#endif |
8908 | 634 |
} |
635 |
RETURN ( value ); |
|
636 |
} |
|
637 |
} |
|
2 | 638 |
} |
359 | 639 |
error: ; |
640 |
%}. |
|
5439 | 641 |
^ super doubleWordAt:index put:value MSB:msb |
129 | 642 |
|
643 |
" |
|
644 |
|b| |
|
359 | 645 |
b := ByteArray new:8. |
646 |
b doubleWordAt:1 put:16r04030201 MSB:true. |
|
647 |
b doubleWordAt:5 put:16r04030201 MSB:false. |
|
129 | 648 |
b inspect |
649 |
" |
|
5554 | 650 |
! ! |
651 |
||
652 |
!ByteArray methodsFor:'accessing-shorts'! |
|
54 | 653 |
|
563 | 654 |
wordAt:index |
655 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
656 |
The value is retrieved in the machines natural byte order |
|
657 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
658 |
||
659 |
%{ /* NOCONTEXT */ |
|
54 | 660 |
|
563 | 661 |
REGISTER int indx; |
662 |
int nIndex; |
|
663 |
union { |
|
8908 | 664 |
unsigned char u_char[2]; |
665 |
unsigned short u_ushort; |
|
563 | 666 |
} val; |
667 |
OBJ cls; |
|
2950 | 668 |
unsigned char *byteP; |
54 | 669 |
|
563 | 670 |
if (__isSmallInteger(index)) { |
8908 | 671 |
indx = __intVal(index); |
672 |
if (indx > 0) { |
|
673 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
|
674 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
675 |
nIndex = __byteArraySize(self); |
|
676 |
if ((indx+1) <= nIndex) { |
|
677 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
678 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
8908 | 679 |
val.u_ushort = ((unsigned short *)byteP)[0]; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
680 |
#else |
8908 | 681 |
/* |
682 |
* mhmh to be measured: |
|
683 |
* the if may hurt more than the additional |
|
684 |
* memory cycles on some machines ... |
|
685 |
*/ |
|
686 |
if (((INT)byteP & 1) == 0) { |
|
687 |
/* aligned */ |
|
688 |
val.u_ushort = ((unsigned short *)byteP)[0]; |
|
689 |
} else { |
|
690 |
val.u_char[0] = byteP[0]; |
|
691 |
val.u_char[1] = byteP[1]; |
|
692 |
} |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
693 |
#endif |
8913 | 694 |
RETURN ( __mkSmallInteger((val.u_ushort)) ); |
8908 | 695 |
} |
696 |
} |
|
563 | 697 |
} |
698 |
%}. |
|
5439 | 699 |
^ super wordAt:index |
54 | 700 |
! |
701 |
||
563 | 702 |
wordAt:index MSB:msb |
703 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
704 |
The value is retrieved MSB (high 8 bits at lower index) if msb is true; |
|
705 |
LSB-first (i.e. low 8-bits at lower byte index) if its false. |
|
706 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
707 |
||
708 |
%{ /* NOCONTEXT */ |
|
709 |
||
710 |
REGISTER int indx; |
|
711 |
int nIndex; |
|
712 |
int val; |
|
713 |
OBJ cls; |
|
2950 | 714 |
unsigned char *byteP; |
54 | 715 |
|
563 | 716 |
if (__isSmallInteger(index)) { |
8908 | 717 |
indx = __intVal(index); |
718 |
if (indx > 0) { |
|
719 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
|
720 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
721 |
nIndex = __byteArraySize(self); |
|
722 |
if ((indx+1) <= nIndex) { |
|
723 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
724 |
if (msb == true) { |
|
725 |
/* |
|
726 |
* most significant byte first (i.e sparc order) |
|
727 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
728 |
#if defined(__MSBFIRST__) |
8908 | 729 |
/* |
730 |
* mhmh to be measured: |
|
731 |
* the if may hurt more than the additional |
|
732 |
* memory cycles on some machines ... |
|
733 |
*/ |
|
734 |
if (((INT)byteP & 1) == 0) { |
|
735 |
/* aligned */ |
|
736 |
val = ((unsigned short *)byteP)[0]; |
|
737 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
738 |
#endif |
8908 | 739 |
{ |
740 |
val = byteP[0]; |
|
741 |
val = (val << 8) + byteP[1]; |
|
742 |
} |
|
743 |
} else { |
|
744 |
/* |
|
745 |
* least significant byte first (i.e i386/alpha order) |
|
746 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
747 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
8908 | 748 |
val = ((unsigned short *)byteP)[0]; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
749 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
750 |
# if defined(__LSBFIRST__) |
8908 | 751 |
/* |
752 |
* mhmh to be measured: |
|
753 |
* the if may hurt more than the additional |
|
754 |
* memory cycles on some machines ... |
|
755 |
*/ |
|
756 |
if (((INT)byteP & 1) == 0) { |
|
757 |
/* aligned */ |
|
758 |
val = ((unsigned short *)byteP)[0]; |
|
759 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
760 |
# endif |
8908 | 761 |
{ |
762 |
val = byteP[1]; |
|
763 |
val = (val << 8) + byteP[0]; |
|
764 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
765 |
#endif |
8908 | 766 |
} |
8913 | 767 |
RETURN ( __mkSmallInteger(val) ); |
8908 | 768 |
} |
769 |
} |
|
563 | 770 |
} |
771 |
%}. |
|
5439 | 772 |
^ super wordAt:index MSB:msb |
773 |
||
54 | 774 |
! |
775 |
||
563 | 776 |
wordAt:index put:value |
777 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
8908 | 778 |
The stored value must be in the range 0 .. 16rFFFF. |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
779 |
The value is stored in the machines natural byteorder, |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
780 |
i.e. this method should only be used to fill byteArrays which are |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
781 |
used internally (not passed to other machines). |
563 | 782 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
783 |
||
784 |
%{ /* NOCONTEXT */ |
|
54 | 785 |
|
563 | 786 |
REGISTER int indx; |
787 |
int nIndex; |
|
788 |
int v; |
|
789 |
union { |
|
8908 | 790 |
unsigned char u_char[2]; |
791 |
unsigned short u_ushort; |
|
563 | 792 |
} val; |
793 |
OBJ cls; |
|
2950 | 794 |
unsigned char *byteP; |
129 | 795 |
|
563 | 796 |
if (__bothSmallInteger(index, value)) { |
8908 | 797 |
indx = __intVal(index); |
798 |
if (indx > 0) { |
|
799 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
|
800 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
801 |
nIndex = __byteArraySize(self); |
|
802 |
if ((indx+1) <= nIndex) { |
|
803 |
val.u_ushort = v = __intVal(value); |
|
804 |
if ((v & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
|
805 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
806 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
8908 | 807 |
((unsigned short *)byteP)[0] = val.u_ushort; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
808 |
#else |
8908 | 809 |
/* |
810 |
* mhmh to be measured: |
|
811 |
* the if may hurt more than the additional |
|
812 |
* memory cycles on some machines ... |
|
813 |
*/ |
|
814 |
if (((INT)byteP & 1) == 0) { |
|
815 |
/* aligned */ |
|
816 |
((unsigned short *)byteP)[0] = val.u_ushort; |
|
817 |
} else { |
|
818 |
byteP[0] = val.u_char[0]; |
|
819 |
byteP[1] = val.u_char[1]; |
|
820 |
} |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
821 |
#endif |
8908 | 822 |
RETURN ( value ); |
823 |
} |
|
824 |
} |
|
825 |
} |
|
563 | 826 |
} |
827 |
%}. |
|
5439 | 828 |
^ super wordAt:index put:value |
129 | 829 |
|
830 |
" |
|
563 | 831 |
|b| |
832 |
b := ByteArray new:4. |
|
833 |
b wordAt:1 put:16r0102. |
|
834 |
b wordAt:3 put:16r0304. |
|
8908 | 835 |
b inspect |
129 | 836 |
" |
837 |
! |
|
838 |
||
563 | 839 |
wordAt:index put:value MSB:msb |
840 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
8908 | 841 |
The stored value must be in the range 0 .. 16rFFFF. |
563 | 842 |
The value is stored LSB-first (i.e. the low 8bits are stored at the |
843 |
lower index) if msb is false, MSB-first otherwise. |
|
844 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
|
845 |
||
846 |
%{ /* NOCONTEXT */ |
|
847 |
||
848 |
REGISTER int indx; |
|
849 |
int nIndex; |
|
850 |
int val; |
|
851 |
OBJ cls; |
|
2950 | 852 |
unsigned char *byteP; |
563 | 853 |
|
854 |
if (__bothSmallInteger(index, value)) { |
|
8908 | 855 |
indx = __intVal(index); |
856 |
if (indx > 0) { |
|
857 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
|
858 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
859 |
nIndex = __byteArraySize(self); |
|
860 |
if ((indx+1) <= nIndex) { |
|
861 |
val = __intVal(value); |
|
862 |
if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
|
863 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
864 |
if (msb == true) { |
|
865 |
/* |
|
866 |
* most significant byte first (i.e sparc order) |
|
867 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
868 |
#if defined(__MSBFIRST__) |
8908 | 869 |
/* |
870 |
* mhmh to be measured: |
|
871 |
* the if may hurt more than the additional |
|
872 |
* memory cycles on some machines ... |
|
873 |
*/ |
|
874 |
if (((INT)byteP & 1) == 0) { |
|
875 |
/* aligned */ |
|
876 |
((unsigned short *)byteP)[0] = val; |
|
877 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
878 |
#endif |
8908 | 879 |
{ |
880 |
byteP[1] = val & 0xFF; |
|
881 |
byteP[0] = (val>>8) & 0xFF; |
|
882 |
} |
|
883 |
} else { |
|
884 |
/* |
|
885 |
* least significant byte first (i.e i386/alpha order) |
|
886 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
887 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
8908 | 888 |
((unsigned short *)byteP)[0] = val; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
889 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
890 |
# if defined(__LSBFIRST__) |
8908 | 891 |
/* |
892 |
* mhmh to be measured: |
|
893 |
* the if may hurt more than the additional |
|
894 |
* memory cycles on some machines ... |
|
895 |
*/ |
|
896 |
if (((INT)byteP & 1) == 0) { |
|
897 |
/* aligned */ |
|
898 |
((unsigned short *)byteP)[0] = val; |
|
899 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
900 |
# endif |
8908 | 901 |
{ |
902 |
byteP[0] = val & 0xFF; |
|
903 |
byteP[1] = (val>>8) & 0xFF; |
|
904 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
905 |
#endif |
8908 | 906 |
} |
907 |
RETURN ( value ); |
|
908 |
} |
|
909 |
} |
|
910 |
} |
|
563 | 911 |
} |
912 |
%}. |
|
5439 | 913 |
^ super wordAt:index put:value MSB:msb |
129 | 914 |
|
915 |
" |
|
563 | 916 |
|b| |
917 |
b := ByteArray new:8. |
|
918 |
b wordAt:1 put:16r0102 MSB:false. |
|
919 |
b wordAt:3 put:16r0304 MSB:false. |
|
920 |
b wordAt:5 put:16r0102 MSB:true. |
|
921 |
b wordAt:7 put:16r0304 MSB:true. |
|
8908 | 922 |
b inspect |
129 | 923 |
" |
1 | 924 |
! ! |
925 |
||
550 | 926 |
!ByteArray methodsFor:'binary storage'! |
927 |
||
928 |
storeBinaryDefinitionOn:stream manager:manager |
|
929 |
"append a binary representation of the receiver onto stream. |
|
3071 | 930 |
Redefined since short ByteArrays can be stored with a special type code |
1261 | 931 |
in a more compact way. |
3071 | 932 |
This is an internal interface for the binary storage mechanism." |
550 | 933 |
|
2464 | 934 |
|myClass myBasicSize| |
935 |
||
550 | 936 |
"not, if I have named instance variables" |
2464 | 937 |
(myClass := self class) instSize ~~ 0 ifTrue:[ |
8908 | 938 |
^ super storeBinaryDefinitionOn:stream manager:manager |
550 | 939 |
]. |
3071 | 940 |
|
941 |
myBasicSize := self basicSize. |
|
942 |
||
943 |
"/ can use a more compact representation; |
|
944 |
"/ but not for subclasses ... |
|
945 |
||
8908 | 946 |
((myClass == ByteArray) |
3071 | 947 |
and:[myBasicSize <= 255]) ifTrue:[ |
8908 | 948 |
"/ special encoding: <codeForByteArray> <len> <bytes> ... |
949 |
stream nextPut:(manager codeForByteArray); nextPut:myBasicSize. |
|
3071 | 950 |
] ifFalse:[ |
8908 | 951 |
manager putIdOfClass:myClass on:stream. |
952 |
stream nextNumber:4 put:myBasicSize. |
|
3071 | 953 |
]. |
8407
f3a5a24b8fec
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8397
diff
changeset
|
954 |
self storeBinaryElementsOn:stream. |
1261 | 955 |
|
3071 | 956 |
"Modified: / 2.11.1997 / 15:28:45 / cg" |
8407
f3a5a24b8fec
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8397
diff
changeset
|
957 |
! |
f3a5a24b8fec
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8397
diff
changeset
|
958 |
|
f3a5a24b8fec
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8397
diff
changeset
|
959 |
storeBinaryElementsOn:aStream |
f3a5a24b8fec
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8397
diff
changeset
|
960 |
aStream nextPutBytes:self basicSize from:self startingAt:1. |
550 | 961 |
! ! |
962 |
||
3345 | 963 |
!ByteArray methodsFor:'comparing'! |
964 |
||
965 |
= aByteArray |
|
966 |
"Compare the receiver with the argument and return true if the |
|
967 |
receiver is equal to the argument (i.e. has the same size and elements). |
|
968 |
Otherwise return false." |
|
969 |
||
970 |
%{ /* NOCONTEXT */ |
|
971 |
||
972 |
int l1, l2; |
|
973 |
REGISTER OBJ s = aByteArray; |
|
974 |
unsigned char *cp1, *cp2; |
|
975 |
OBJ cls; |
|
976 |
OBJ myCls; |
|
977 |
INT addrDelta; |
|
978 |
||
979 |
if (s == self) { |
|
4661 | 980 |
RETURN ( true ); |
3345 | 981 |
} |
982 |
if (! __isNonNilObject(s)) { |
|
4661 | 983 |
RETURN ( false ); |
3345 | 984 |
} |
985 |
||
986 |
cls = __qClass(s); |
|
987 |
myCls = __qClass(self); |
|
988 |
||
989 |
if (cls == myCls) { |
|
4661 | 990 |
cp2 = __stringVal(s); |
991 |
l2 = __byteArraySize(s); |
|
3345 | 992 |
|
4661 | 993 |
cp1 = __stringVal(self); |
994 |
l1 = __byteArraySize(self); |
|
3345 | 995 |
|
4661 | 996 |
if (l1 != l2) { |
997 |
RETURN ( false ); |
|
998 |
} |
|
3345 | 999 |
|
4661 | 1000 |
/* |
1001 |
* care for instances of subclasses ... |
|
1002 |
*/ |
|
1003 |
if (cls != ByteArray) { |
|
1004 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
3345 | 1005 |
|
4661 | 1006 |
cp2 += n; |
1007 |
cp1 += n; |
|
1008 |
l1 -= n; |
|
1009 |
} |
|
3345 | 1010 |
|
4661 | 1011 |
addrDelta = cp2 - cp1; |
1012 |
while (l1 >= sizeof(unsigned INT)) { |
|
1013 |
if (*((unsigned INT *)cp1) != *((unsigned INT *)(cp1+addrDelta))) { |
|
1014 |
RETURN (false); |
|
1015 |
} |
|
1016 |
l1 -= sizeof(unsigned INT); |
|
1017 |
cp1 += sizeof(unsigned INT); |
|
1018 |
} |
|
1019 |
if (l1 >= sizeof(unsigned short)) { |
|
1020 |
if (*((unsigned short *)cp1) != *((unsigned short *)(cp1+addrDelta))) { |
|
1021 |
RETURN (false); |
|
1022 |
} |
|
1023 |
l1 -= sizeof(unsigned short); |
|
1024 |
cp1 += sizeof(unsigned short); |
|
1025 |
} |
|
1026 |
while (l1) { |
|
1027 |
if (*cp1 != *(cp1+addrDelta)) { |
|
1028 |
RETURN (false); |
|
1029 |
} |
|
1030 |
l1--; |
|
1031 |
cp1++; |
|
1032 |
} |
|
3345 | 1033 |
|
4661 | 1034 |
RETURN (true); |
3345 | 1035 |
} |
1036 |
%}. |
|
1037 |
^ super = aByteArray |
|
1038 |
! ! |
|
1039 |
||
12 | 1040 |
!ByteArray methodsFor:'converting'! |
1041 |
||
1042 |
asByteArray |
|
1043 |
"return the receiver as a byteArray" |
|
1044 |
||
1045 |
"could be an instance of a subclass..." |
|
1046 |
self class == ByteArray ifTrue:[ |
|
155 | 1047 |
^ self |
12 | 1048 |
]. |
1049 |
^ super asByteArray |
|
129 | 1050 |
|
1051 |
" |
|
8908 | 1052 |
'hello world' asByteArray |
1053 |
#(1 2 3 4 5 6 7) asByteArray |
|
1054 |
#(1 2 256 4 5 6 7) asByteArray |
|
129 | 1055 |
" |
4778 | 1056 |
! |
1057 |
||
7117 | 1058 |
asPackedString |
1059 |
"ST-80 compatibility: encode the receiver into an ascii String |
|
1060 |
with 6bits encoded per character. Each group of 6 bits is encoded |
|
1061 |
as a corresponding character (32+value) and the resulting string |
|
1062 |
is returned. The resulting string is always a multiple of 4 (since |
|
1063 |
24 is the lcm of 6 and 8) and the number of remaining characters is |
|
1064 |
encoded in the last character. |
|
1065 |
ST-80 uses this encoding for Images ... |
|
1066 |
This is very similar (but not equal) to the algorithm used in RFC1421. |
|
1067 |
PS: I dont like it ;-)" |
|
1068 |
||
8908 | 1069 |
|outStream |
7117 | 1070 |
index "{ Class:SmallInteger}" |
1071 |
nextIndex "{ Class:SmallInteger}" |
|
1072 |
stop "{ Class:SmallInteger}" |
|
1073 |
n "{ Class:SmallInteger}" |
|
1074 |
mod "{ Class:SmallInteger}" |
|
1075 |
cpl| |
|
1076 |
||
1077 |
outStream := WriteStream on:String new. |
|
8908 | 1078 |
index := 1. |
7117 | 1079 |
stop := self size. |
1080 |
||
1081 |
stop > 100 ifTrue:[ |
|
8908 | 1082 |
"/ cg: |
1083 |
"/ initial lineBreak |
|
1084 |
outStream cr. |
|
7117 | 1085 |
]. |
1086 |
cpl := 0. |
|
1087 |
||
1088 |
[index <= stop] whileTrue:[ |
|
8908 | 1089 |
"take 3 source bytes" |
1090 |
n := (self at:index) bitShift:16. |
|
1091 |
(index < stop) ifTrue:[ |
|
1092 |
nextIndex := index + 1. |
|
1093 |
n := n bitOr:((self at:nextIndex) bitShift:8). |
|
1094 |
(nextIndex < stop) ifTrue:[ |
|
1095 |
n := n bitOr:(self at:(index + 2)). |
|
1096 |
]. |
|
1097 |
]. |
|
1098 |
||
1099 |
"took me a while to find that one out ..." |
|
1100 |
n := n bitXor:16r820820. |
|
1101 |
||
1102 |
outStream nextPut:(Character value:(n bitShift:-18) + 32). |
|
1103 |
outStream nextPut:(Character value:((n bitShift:-12) bitAnd:16r3F) + 32). |
|
1104 |
outStream nextPut:(Character value:((n bitShift:-6) bitAnd:16r3F) + 32). |
|
1105 |
outStream nextPut:(Character value:(n bitAnd:16r3F) + 32). |
|
1106 |
index := index + 3. |
|
1107 |
||
1108 |
"/ cg: |
|
1109 |
"/ lineBreak after every 120 characters |
|
1110 |
"/ fromPackedString will ignore those |
|
1111 |
cpl := cpl + 4. |
|
1112 |
cpl >= 120 ifTrue:[ |
|
1113 |
outStream cr. |
|
1114 |
cpl := 0. |
|
1115 |
]. |
|
7117 | 1116 |
]. |
1117 |
(mod := stop \\ 3) ~~ 0 ifTrue:[ |
|
8908 | 1118 |
outStream backStep. |
1119 |
outStream nextPut:(Character value:(mod + 96)). |
|
7117 | 1120 |
]. |
1121 |
^ outStream contents |
|
1122 |
||
1123 |
"Modified: 12.11.1996 / 15:45:02 / cg" |
|
1124 |
! |
|
1125 |
||
5900 | 1126 |
asString |
1127 |
"speed up string conversions" |
|
1128 |
||
1129 |
|size| |
|
1130 |
||
5901 | 1131 |
self class == ByteArray ifTrue:[ |
8908 | 1132 |
size := self size. |
1133 |
^ (String uninitializedNew:size) replaceBytesFrom:1 to:size with:self startingAt:1. |
|
5901 | 1134 |
]. |
1135 |
^ super asString |
|
5900 | 1136 |
|
1137 |
" |
|
1138 |
#[60 61 62 63] asString |
|
1139 |
" |
|
1140 |
! |
|
1141 |
||
4807 | 1142 |
decodeAsLiteralArray |
1143 |
"given a literalEncoding in the receiver, |
|
1144 |
create & return the corresponding object. |
|
1145 |
The inverse operation to #literalArrayEncoding." |
|
1146 |
||
1147 |
^ self |
|
1148 |
||
1149 |
! |
|
1150 |
||
4778 | 1151 |
literalArrayEncoding |
1152 |
"encode myself as an array literal, from which a copy of the receiver |
|
1153 |
can be reconstructed with #decodeAsLiteralArray." |
|
1154 |
||
1155 |
^ self |
|
1156 |
||
1157 |
" |
|
8908 | 1158 |
#[1 2 3] literalArrayEncoding |
4778 | 1159 |
" |
1160 |
||
1161 |
||
12 | 1162 |
! ! |
1163 |
||
92 | 1164 |
!ByteArray methodsFor:'copying'! |
1165 |
||
5024 | 1166 |
copy |
1167 |
"redefined for a bit more speed" |
|
1168 |
||
1169 |
self class == ByteArray ifTrue:[ |
|
8908 | 1170 |
^ self copyFrom:1 to:(self size) |
5024 | 1171 |
]. |
1172 |
^ super copy |
|
1173 |
! |
|
1174 |
||
92 | 1175 |
copyFrom:start to:stop |
1176 |
"return the subcollection starting at index start, anInteger and ending |
|
1177 |
at stop, anInteger. |
|
1178 |
- reimplemented here for speed" |
|
1179 |
||
1180 |
%{ /* NOCONTEXT */ |
|
1181 |
||
1182 |
REGISTER unsigned char *srcp; |
|
1183 |
REGISTER unsigned char *dstp; |
|
1184 |
REGISTER int count; |
|
1185 |
int len, index1, index2, sz; |
|
1186 |
OBJ newByteArray; |
|
1187 |
||
1188 |
if (__isByteArray(self) |
|
249 | 1189 |
&& __bothSmallInteger(start, stop)) { |
8908 | 1190 |
len = __byteArraySize(self); |
1191 |
index1 = __intVal(start); |
|
1192 |
index2 = __intVal(stop); |
|
1193 |
||
1194 |
if ((index1 <= index2) && (index1 > 0)) { |
|
1195 |
if (index2 <= len) { |
|
1196 |
count = index2 - index1 + 1; |
|
1197 |
__PROTECT_CONTEXT__ |
|
1198 |
sz = OHDR_SIZE + count; |
|
1199 |
__qNew(newByteArray, sz); /* OBJECT ALLOCATION */ |
|
1200 |
__UNPROTECT_CONTEXT__ |
|
1201 |
if (newByteArray != nil) { |
|
1202 |
__InstPtr(newByteArray)->o_class = ByteArray; |
|
1203 |
__qSTORE(newByteArray, ByteArray); |
|
1204 |
dstp = __ByteArrayInstPtr(newByteArray)->ba_element; |
|
1205 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
2648 | 1206 |
|
1207 |
#ifdef bcopy4 |
|
8908 | 1208 |
if (((unsigned INT)srcp & 3) == ((unsigned INT)dstp & 3)) { |
1209 |
int nW; |
|
1210 |
||
1211 |
/* copy unaligned part */ |
|
1212 |
while (count && (((unsigned INT)srcp & 3) != 0)) { |
|
1213 |
*dstp++ = *srcp++; |
|
1214 |
count--; |
|
1215 |
} |
|
1216 |
if (count) { |
|
1217 |
/* copy aligned part */ |
|
1218 |
nW = count >> 2; |
|
1219 |
if (count & 3) { |
|
1220 |
nW++; |
|
1221 |
} |
|
1222 |
bcopy4(srcp, dstp, nW); |
|
1223 |
} |
|
1224 |
RETURN ( newByteArray ); |
|
1225 |
} |
|
2648 | 1226 |
#endif /* bcopy4 */ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
1227 |
#if __POINTER_SIZE__ == 8 |
8908 | 1228 |
if (((unsigned INT)srcp & 7) == ((unsigned INT)dstp & 7)) { |
1229 |
int nW; |
|
1230 |
||
1231 |
/* copy unaligned part */ |
|
1232 |
while (count && (((unsigned INT)srcp & 7) != 0)) { |
|
1233 |
*dstp++ = *srcp++; |
|
1234 |
count--; |
|
1235 |
} |
|
1236 |
/* copy aligned part */ |
|
1237 |
while (count >= 8) { |
|
1238 |
((unsigned INT *)dstp)[0] = ((unsigned INT *)srcp)[0]; |
|
1239 |
dstp += 8; |
|
1240 |
srcp += 8; |
|
1241 |
count -= 8; |
|
1242 |
} |
|
1243 |
/* copy remaining part */ |
|
1244 |
while (count) { |
|
1245 |
*dstp++ = *srcp++; |
|
1246 |
count--; |
|
1247 |
} |
|
1248 |
RETURN ( newByteArray ); |
|
1249 |
} |
|
4943
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1250 |
#endif /* bcopy4 */ |
2648 | 1251 |
|
92 | 1252 |
#ifdef FAST_MEMCPY |
8908 | 1253 |
bcopy(srcp, dstp, count); |
92 | 1254 |
#else |
8908 | 1255 |
while (count--) { |
1256 |
*dstp++ = *srcp++; |
|
1257 |
} |
|
92 | 1258 |
#endif |
8908 | 1259 |
RETURN ( newByteArray ); |
1260 |
} |
|
1261 |
} |
|
1262 |
} |
|
92 | 1263 |
} |
1264 |
%}. |
|
1265 |
" |
|
1266 |
fall back in case of non-integer index or out-of-bound index; |
|
1267 |
will eventually lead to an out-of-bound signal raise |
|
1268 |
" |
|
1269 |
^ super copyFrom:start to:stop |
|
4941
7674fdddb1db
better byteArray copy on Intel
Claus Gittinger <cg@exept.de>
parents:
4931
diff
changeset
|
1270 |
|
7674fdddb1db
better byteArray copy on Intel
Claus Gittinger <cg@exept.de>
parents:
4931
diff
changeset
|
1271 |
" |
8908 | 1272 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:1 to:10 |
1273 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:5 to:7 |
|
1274 |
||
1275 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:5 to:11 |
|
1276 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:0 to:10 |
|
1277 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:0 to:9 |
|
4941
7674fdddb1db
better byteArray copy on Intel
Claus Gittinger <cg@exept.de>
parents:
4931
diff
changeset
|
1278 |
" |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1279 |
! |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1280 |
|
5024 | 1281 |
shallowCopy |
1282 |
"redefined for a bit more speed" |
|
1283 |
||
1284 |
self class == ByteArray ifTrue:[ |
|
8908 | 1285 |
^ self copyFrom:1 to:(self size) |
5024 | 1286 |
]. |
1287 |
^ super shallowCopy |
|
1288 |
! |
|
1289 |
||
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1290 |
symbolFrom:start to:stop |
8908 | 1291 |
"make a symbol from the characters of the subcollection starting |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1292 |
at index start, anInteger and ending at stop, anInteger. |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1293 |
This saves us garbage and character copying." |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1294 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1295 |
|sym| |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1296 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1297 |
%{ /* STACK:1024 */ |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1298 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1299 |
REGISTER unsigned char *srcp; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1300 |
REGISTER unsigned char *endp; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1301 |
REGISTER int count; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1302 |
int len, index1, index2; |
4086 | 1303 |
unsigned char scratchBuffer[1024], savec; |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1304 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1305 |
if (__isByteArray(self) && __bothSmallInteger(start, stop)) { |
4661 | 1306 |
len = __byteArraySize(self); |
1307 |
index1 = __intVal(start); |
|
1308 |
index2 = __intVal(stop); |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1309 |
|
4661 | 1310 |
if ((index1 <= index2) && (index1 > 0) && (index2 <= len)) { |
1311 |
count = index2 - index1 + 1; |
|
1312 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
1313 |
if (index2 < len) { |
|
1314 |
/* temporarily stuff in a '\0' */ |
|
1315 |
endp = srcp + count + 1; |
|
1316 |
savec = *endp; |
|
1317 |
*endp = '\0'; |
|
1318 |
sym = __MKSYMBOL(srcp, 0); |
|
4086 | 1319 |
/* must refetch endp (in case of a GC */ |
4661 | 1320 |
endp = __ByteArrayInstPtr(self)->ba_element + index1 + count; |
1321 |
*endp = savec; |
|
1322 |
} else { |
|
1323 |
/* not enough space for '\0', copy the bytes */ |
|
4128 | 1324 |
if (count < sizeof(scratchBuffer)) { |
4661 | 1325 |
bcopy(srcp, scratchBuffer, count); |
1326 |
scratchBuffer[count] = '\0'; |
|
1327 |
sym = __MKSYMBOL(scratchBuffer, 0); |
|
4086 | 1328 |
} |
4661 | 1329 |
} |
1330 |
} |
|
1331 |
if (sym != nil) |
|
1332 |
RETURN(sym); |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1333 |
} |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1334 |
%}. |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1335 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1336 |
fall back in case of non-integer index or out-of-bound index |
4086 | 1337 |
or not enough stack-memory available; |
1338 |
may eventually lead to an out-of-bound signal raise |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1339 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1340 |
^ (super copyFrom:start to:stop) asString asSymbol |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1341 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1342 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1343 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1344 |
'abcdefghijklmnop' symbolFrom:1 to:3 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1345 |
'abcdefghijklmnop' symbolFrom:3 to:16 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1346 |
'abcdefghijklmnop' symbolFrom:3 to:17 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1347 |
" |
92 | 1348 |
! ! |
1349 |
||
4154 | 1350 |
!ByteArray methodsFor:'filling & replacing'! |
1 | 1351 |
|
155 | 1352 |
from:start to:stop put:aNumber |
1353 |
"fill part of the receiver with aNumber. |
|
1354 |
- reimplemented here for speed" |
|
1355 |
||
1356 |
%{ /* NOCONTEXT */ |
|
1357 |
||
1358 |
REGISTER unsigned char *dstp; |
|
1359 |
REGISTER int count, value; |
|
249 | 1360 |
int len, index1, index2; |
1361 |
OBJ cls; |
|
155 | 1362 |
|
249 | 1363 |
if (__isSmallInteger(aNumber) |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1364 |
&& __bothSmallInteger(start, stop) |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1365 |
&& __isBytes(self)) { |
4969 | 1366 |
len = __byteArraySize(self); |
1367 |
index1 = __intVal(start); |
|
1368 |
index2 = __intVal(stop); |
|
1369 |
||
1370 |
dstp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
1371 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
1372 |
int nInst; |
|
1373 |
||
1374 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1375 |
dstp += nInst; |
|
1376 |
len -= nInst; |
|
1377 |
} |
|
1378 |
||
1379 |
value = __intVal(aNumber); |
|
1380 |
if (((unsigned)value <= 0xFF) /* i.e. (value >= 0) && (value <= 255) */ |
|
8908 | 1381 |
&& (index1 <= index2) |
4969 | 1382 |
&& (index1 > 0)) { |
1383 |
if (index2 <= len) { |
|
1384 |
count = index2 - index1 + 1; |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1385 |
|
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1386 |
#ifdef memset4 |
4969 | 1387 |
if (count > 20) { |
1388 |
/* fill unaligned part */ |
|
1389 |
while (((unsigned INT)dstp & 3) != 0) { |
|
1390 |
*dstp++ = value; |
|
1391 |
count--; |
|
1392 |
} |
|
1393 |
/* fill aligned part */ |
|
1394 |
{ |
|
1395 |
int n4 = count & ~3; |
|
1396 |
int v4, nW; |
|
1397 |
||
1398 |
v4 = (value << 8) | value; |
|
1399 |
v4 = (v4 << 16) | v4; |
|
1400 |
nW = n4>>2; |
|
1401 |
memset4(dstp, v4, nW); |
|
1402 |
count -= n4; |
|
1403 |
dstp += n4; |
|
1404 |
} |
|
1405 |
while (count--) { |
|
1406 |
*dstp++ = value; |
|
1407 |
} |
|
1408 |
RETURN (self); |
|
1409 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1410 |
#endif /* memset4 */ |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1411 |
|
8908 | 1412 |
#if (__POINTER_SIZE__ == 8) |
4969 | 1413 |
{ |
1414 |
INT v8; |
|
1415 |
||
1416 |
v8 = (value << 8) | value; |
|
1417 |
v8 = (v8 << 16) | v8; |
|
1418 |
v8 = (v8 << 32) | v8; |
|
1419 |
||
1420 |
/* fill unaligned part */ |
|
1421 |
while ((count > 0) && (((unsigned INT)dstp & 3) != 0)) { |
|
1422 |
*dstp++ = value; |
|
1423 |
count--; |
|
1424 |
} |
|
1425 |
||
1426 |
if ((count >= 4) && (((unsigned INT)dstp & 7) != 0)) { |
|
1427 |
((unsigned int *)dstp)[0] = v8; |
|
1428 |
dstp += 4; |
|
1429 |
count -= 4; |
|
1430 |
} |
|
1431 |
||
1432 |
/* fill aligned part */ |
|
1433 |
while (count >= 8) { |
|
1434 |
((unsigned INT *)dstp)[0] = v8; |
|
1435 |
dstp += 8; |
|
1436 |
count -= 8; |
|
1437 |
} |
|
1438 |
||
1439 |
/* fill rest */ |
|
1440 |
if (count >= 4) { |
|
1441 |
((unsigned int *)dstp)[0] = v8; |
|
1442 |
dstp += 4; |
|
1443 |
count -= 4; |
|
1444 |
} |
|
1445 |
if (count >= 2) { |
|
1446 |
((unsigned short *)dstp)[0] = v8; |
|
1447 |
dstp += 2; |
|
1448 |
count -= 2; |
|
1449 |
} |
|
1450 |
if (count) { |
|
1451 |
*dstp = value; |
|
1452 |
} |
|
1453 |
RETURN (self); |
|
1454 |
} |
|
8908 | 1455 |
#endif /* 64bit */ |
4945 | 1456 |
|
155 | 1457 |
#ifdef FAST_MEMSET |
4969 | 1458 |
memset(dstp, value, count); |
155 | 1459 |
#else |
8919 | 1460 |
# ifdef __UNROLL_LOOPS__ |
4969 | 1461 |
while (count >= 8) { |
1462 |
dstp[0] = dstp[1] = dstp[2] = dstp[3] = |
|
1463 |
dstp[4] = dstp[5] = dstp[6] = dstp[7] = value; |
|
1464 |
dstp += 8; |
|
1465 |
count -= 8; |
|
1466 |
} |
|
8919 | 1467 |
# endif /* __UNROLL_LOOPS__ */ |
4969 | 1468 |
while (count--) { |
1469 |
*dstp++ = value; |
|
1470 |
} |
|
155 | 1471 |
#endif |
4969 | 1472 |
RETURN (self); |
1473 |
} |
|
1474 |
} |
|
155 | 1475 |
} |
1476 |
%}. |
|
1477 |
" |
|
1478 |
fall back in case of non-integer index or out-of-bound index/value; |
|
1479 |
will eventually lead to an out-of-bound signal raise |
|
1480 |
" |
|
1481 |
^ super from:start to:stop put:aNumber |
|
4945 | 1482 |
|
1483 |
" |
|
8908 | 1484 |
(ByteArray new:10) from:1 to:10 put:1 |
1485 |
(ByteArray new:20) from:10 to:20 put:1 |
|
1486 |
(ByteArray new:20) from:1 to:10 put:1 |
|
4945 | 1487 |
" |
155 | 1488 |
! |
1489 |
||
249 | 1490 |
replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
3676 | 1491 |
"replace elements from another collection, which must be a ByteArray- |
1492 |
like collection. |
|
1493 |
||
1494 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1495 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1496 |
|
54 | 1497 |
%{ /* NOCONTEXT */ |
1 | 1498 |
|
54 | 1499 |
int nIndex, repNIndex; |
1500 |
int startIndex, stopIndex; |
|
1501 |
REGISTER unsigned char *src; |
|
1502 |
REGISTER int repStartIndex; |
|
1503 |
int repStopIndex, count; |
|
1504 |
REGISTER unsigned char *dst; |
|
249 | 1505 |
OBJ cls; |
1 | 1506 |
|
2894 | 1507 |
#ifndef NO_PRIM_BYTEARR |
7194
8c36a414a4e7
replaceBytesFrom:to:with tuned replacing from externalBytes.
Claus Gittinger <cg@exept.de>
parents:
7172
diff
changeset
|
1508 |
if ((__isBytes(aCollection) || __isExternalBytesLike(aCollection)) |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1509 |
&& __isBytes(self) |
249 | 1510 |
&& __bothSmallInteger(start, stop) |
1511 |
&& __isSmallInteger(repStart)) { |
|
8908 | 1512 |
startIndex = __intVal(start) - 1; |
1513 |
if (startIndex >= 0) { |
|
1514 |
dst = (__ByteArrayInstPtr(self)->ba_element) + startIndex; |
|
1515 |
nIndex = __byteArraySize(self); |
|
1516 |
||
1517 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
1518 |
int nInst; |
|
1519 |
||
1520 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1521 |
dst += nInst; |
|
1522 |
nIndex -= nInst; |
|
1523 |
} |
|
1524 |
||
1525 |
stopIndex = __intVal(stop) - 1; |
|
1526 |
count = stopIndex - startIndex + 1; |
|
1527 |
if (count == 0) { |
|
1528 |
RETURN ( self ); |
|
1529 |
} |
|
1530 |
||
1531 |
if ((count > 0) && (stopIndex < nIndex)) { |
|
1532 |
repStartIndex = __intVal(repStart) - 1; |
|
1533 |
if (repStartIndex >= 0) { |
|
1534 |
if (__isExternalBytesLike(aCollection)) { |
|
1535 |
OBJ sz; |
|
1536 |
||
1537 |
src = __externalAddressVal(aCollection); |
|
1538 |
if (src == 0) goto fallBack; |
|
1539 |
||
1540 |
sz = __externalBytesSize(aCollection); |
|
1541 |
if (__isSmallInteger(sz)) { |
|
1542 |
repNIndex = __smallIntegerVal(sz); |
|
1543 |
} else { |
|
1544 |
repNIndex = repStopIndex+1; /* always enough */ |
|
1545 |
} |
|
1546 |
src = src + repStartIndex; |
|
1547 |
} else { |
|
1548 |
repNIndex = __qSize(aCollection) - OHDR_SIZE; |
|
1549 |
src = (__ByteArrayInstPtr(aCollection)->ba_element) + repStartIndex; |
|
1550 |
if ((cls = __qClass(aCollection)) != @global(ByteArray)) { |
|
1551 |
int nInst; |
|
1552 |
||
1553 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1554 |
src += nInst; |
|
1555 |
repNIndex -= nInst; |
|
1556 |
} |
|
1557 |
} |
|
1558 |
repStopIndex = repStartIndex + (stopIndex - startIndex); |
|
1559 |
if (repStopIndex < repNIndex) { |
|
1560 |
if (aCollection == self) { |
|
1561 |
/* take care of overlapping copy */ |
|
1562 |
if (src < dst) { |
|
1563 |
/* must do a reverse copy */ |
|
1564 |
src += count; |
|
1565 |
dst += count; |
|
1566 |
while (count-- > 0) { |
|
1567 |
*--dst = *--src; |
|
1568 |
} |
|
1569 |
RETURN ( self ); |
|
1570 |
} |
|
1571 |
} |
|
7194
8c36a414a4e7
replaceBytesFrom:to:with tuned replacing from externalBytes.
Claus Gittinger <cg@exept.de>
parents:
7172
diff
changeset
|
1572 |
|
8c36a414a4e7
replaceBytesFrom:to:with tuned replacing from externalBytes.
Claus Gittinger <cg@exept.de>
parents:
7172
diff
changeset
|
1573 |
# ifdef bcopy4 |
8908 | 1574 |
if (((unsigned INT)src & 3) == ((unsigned INT)dst & 3)) { |
1575 |
int nW; |
|
1576 |
||
1577 |
/* copy unaligned part */ |
|
1578 |
while (count && ((unsigned INT)src & 3)) { |
|
1579 |
*dst++ = *src++; |
|
1580 |
count--; |
|
1581 |
} |
|
1582 |
||
1583 |
if (count > 0) { |
|
1584 |
/* copy aligned part */ |
|
1585 |
nW = count >> 2; |
|
1586 |
bcopy4(src, dst, nW); |
|
1587 |
if ((count = count & 3) != 0) { |
|
1588 |
/* copy any remaining part */ |
|
1589 |
src += (nW<<2); |
|
1590 |
dst += (nW<<2); |
|
1591 |
while (count--) { |
|
1592 |
*dst++ = *src++; |
|
1593 |
} |
|
1594 |
} |
|
1595 |
} |
|
1596 |
RETURN ( self ); |
|
1597 |
} |
|
7194
8c36a414a4e7
replaceBytesFrom:to:with tuned replacing from externalBytes.
Claus Gittinger <cg@exept.de>
parents:
7172
diff
changeset
|
1598 |
# else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
1599 |
# if __POINTER_SIZE__ == 8 |
8908 | 1600 |
if (((unsigned INT)src & 7) == ((unsigned INT)dst & 7)) { |
1601 |
/* copy unaligned part */ |
|
1602 |
while (count && ((unsigned INT)src & 7)) { |
|
1603 |
*dst++ = *src++; |
|
1604 |
count--; |
|
1605 |
} |
|
1606 |
||
1607 |
/* copy aligned part */ |
|
1608 |
while (count >= 8) { |
|
1609 |
((unsigned INT *)dst)[0] = ((unsigned INT *)src)[0]; |
|
1610 |
dst += 8; |
|
1611 |
src += 8; |
|
1612 |
count -= 8; |
|
1613 |
} |
|
1614 |
while (count--) { |
|
1615 |
*dst++ = *src++; |
|
1616 |
} |
|
1617 |
RETURN ( self ); |
|
1618 |
} |
|
1619 |
# endif /* 64bit */ |
|
7194
8c36a414a4e7
replaceBytesFrom:to:with tuned replacing from externalBytes.
Claus Gittinger <cg@exept.de>
parents:
7172
diff
changeset
|
1620 |
# endif /* bcopy4 */ |
8c36a414a4e7
replaceBytesFrom:to:with tuned replacing from externalBytes.
Claus Gittinger <cg@exept.de>
parents:
7172
diff
changeset
|
1621 |
|
8c36a414a4e7
replaceBytesFrom:to:with tuned replacing from externalBytes.
Claus Gittinger <cg@exept.de>
parents:
7172
diff
changeset
|
1622 |
# ifdef FAST_MEMCPY |
8908 | 1623 |
bcopy(src, dst, count); |
7194
8c36a414a4e7
replaceBytesFrom:to:with tuned replacing from externalBytes.
Claus Gittinger <cg@exept.de>
parents:
7172
diff
changeset
|
1624 |
# else |
8919 | 1625 |
# ifdef __UNROLL_LOOPS__ |
8908 | 1626 |
while (count >= 8) { |
1627 |
dst[0] = src[0]; dst[1] = src[1]; |
|
1628 |
dst[2] = src[2]; dst[3] = src[3]; |
|
1629 |
dst[4] = src[4]; dst[5] = src[5]; |
|
1630 |
dst[6] = src[6]; dst[7] = src[7]; |
|
1631 |
dst += 8; src += 8; |
|
1632 |
count -= 8; |
|
1633 |
} |
|
8919 | 1634 |
# endif /* __UNROLL_LOOPS__ */ |
8908 | 1635 |
while (count-- > 0) { |
1636 |
*dst++ = *src++; |
|
1637 |
} |
|
7194
8c36a414a4e7
replaceBytesFrom:to:with tuned replacing from externalBytes.
Claus Gittinger <cg@exept.de>
parents:
7172
diff
changeset
|
1638 |
# endif |
8908 | 1639 |
RETURN ( self ); |
1640 |
} |
|
1641 |
} |
|
1642 |
} |
|
1643 |
} |
|
54 | 1644 |
} |
7194
8c36a414a4e7
replaceBytesFrom:to:with tuned replacing from externalBytes.
Claus Gittinger <cg@exept.de>
parents:
7172
diff
changeset
|
1645 |
fallBack: ; |
2894 | 1646 |
#endif |
437 | 1647 |
%}. |
1648 |
" |
|
1649 |
fall back in case of non-ByteArray argument, |
|
1650 |
or for the error report if any index is invalid |
|
1651 |
" |
|
7194
8c36a414a4e7
replaceBytesFrom:to:with tuned replacing from externalBytes.
Claus Gittinger <cg@exept.de>
parents:
7172
diff
changeset
|
1652 |
^ super replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
4943
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1653 |
|
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1654 |
" |
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1655 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] |
8908 | 1656 |
copy |
1657 |
replaceFrom:1 to:8 |
|
1658 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
1659 |
startingAt:1 |
|
4943
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1660 |
|
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1661 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] |
8908 | 1662 |
copy |
1663 |
replaceFrom:3 to:10 |
|
1664 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
1665 |
startingAt:1 |
|
4943
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1666 |
|
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1667 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] |
8908 | 1668 |
copy |
1669 |
replaceFrom:3 to:4 |
|
1670 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
1671 |
startingAt:1 |
|
4943
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1672 |
|
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1673 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] |
8908 | 1674 |
copy |
1675 |
replaceFrom:0 to:9 |
|
1676 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
1677 |
startingAt:1 |
|
4943
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1678 |
|
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1679 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] |
8908 | 1680 |
copy |
1681 |
replaceFrom:1 to:10 |
|
1682 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
1683 |
startingAt:0 |
|
4943
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1684 |
" |
563 | 1685 |
! |
1686 |
||
3676 | 1687 |
replaceBytesFrom:startIndex with:replacementCollection startingAt:repStartIndex |
1688 |
"replace elements from another collection, which must be |
|
1689 |
byte-array-like. |
|
1690 |
||
1691 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1692 |
therefore the change may affect all others referencing the receiver." |
|
1693 |
||
8908 | 1694 |
^ self |
1695 |
replaceBytesFrom:startIndex |
|
4661 | 1696 |
to:(startIndex + replacementCollection size - repStartIndex) |
1697 |
with:replacementCollection |
|
1698 |
startingAt:repStartIndex |
|
3676 | 1699 |
|
1700 |
" |
|
1701 |
args: startIndex : <integer> |
|
4661 | 1702 |
replacementCollection : <collection of <bytes> > |
1703 |
repStartIndex : <integer> |
|
3676 | 1704 |
|
1705 |
returns: self |
|
1706 |
" |
|
1707 |
||
1708 |
"Created: / 27.7.1998 / 16:56:46 / cg" |
|
1709 |
"Modified: / 27.7.1998 / 16:58:38 / cg" |
|
1710 |
! |
|
1711 |
||
1712 |
replaceFrom:startIndex to:stopIndex with:aCollection startingAt:repStartIndex |
|
1169 | 1713 |
"replace elements in the receiver between index start and stop, |
3234 | 1714 |
with elements taken from replacementCollection starting at repStart. |
3676 | 1715 |
Return the receiver. |
1716 |
||
1717 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1718 |
therefore the change may affect all others referencing the receiver." |
|
563 | 1719 |
|
8908 | 1720 |
((aCollection class == self class) |
4783 | 1721 |
or:[aCollection class isBytes]) ifTrue:[ |
4969 | 1722 |
^ self replaceBytesFrom:startIndex to:stopIndex with:aCollection startingAt:repStartIndex |
563 | 1723 |
]. |
3676 | 1724 |
^ super replaceFrom:startIndex to:stopIndex with:aCollection startingAt:repStartIndex |
1169 | 1725 |
|
3676 | 1726 |
" |
1727 |
args: startIndex : <integer> |
|
4969 | 1728 |
stopIndex : <integer> |
1729 |
replacementCollection : <collection of <bytes> > |
|
1730 |
repStartIndex : <integer> |
|
3676 | 1731 |
|
1732 |
returns: self |
|
1733 |
" |
|
1734 |
||
1735 |
"Modified: / 27.7.1998 / 16:58:33 / cg" |
|
1 | 1736 |
! ! |
1737 |
||
129 | 1738 |
!ByteArray methodsFor:'image manipulation support'! |
1 | 1739 |
|
8908 | 1740 |
bitAndBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 1741 |
"replace bytes in the receiver with the result of a bitAnd operation. |
1742 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1743 |
||
1744 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitAnd: |
|
1745 |
||
1746 |
" |
|
1747 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 1748 |
bitAndBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1749 |
#[1 2 3 4 5 6 7 8] |
4969 | 1750 |
bitAndBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 1751 |
" |
1752 |
||
1753 |
! |
|
1754 |
||
1755 |
bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:ruleSymbol |
|
1756 |
"perform a special case of an aligned bitBlit operation. |
|
8908 | 1757 |
Bytes in the receiver from dstStart to dstEnd are destructively replaced by the result |
4919 | 1758 |
of some logical operation, as specified by the ruleSymbol. |
1759 |
SourceBytes are fetched starting at sourceOffset. |
|
1760 |
Valid rule symbols are: |
|
8908 | 1761 |
#copy - trivial; same as replaceBytesFrom:to:with:startingAt: |
1762 |
#bitXor: - xoring; byte[dI] = byte[dI] bitXor:(srcByte[sI]) |
|
1763 |
#bitAnd: - anding; byte[dI] = byte[dI] bitAnd:(srcByte[sI]) |
|
1764 |
#bitOr: - oring; byte[dI] = byte[dI] bitOr:(srcByte[sI]) |
|
1765 |
#+ - adding; byte[dI] = (byte[dI] + (srcByte[sI])) mod: 256 |
|
1766 |
#- - subtract; byte[dI] = (byte[dI] - (srcByte[sI])) mod: 256 |
|
4919 | 1767 |
Warning: this is a destructive operation - elements in the receiver are overwritten. |
1768 |
" |
|
1769 |
||
1770 |
|srcIdx| |
|
1771 |
||
1772 |
%{ |
|
8908 | 1773 |
if ((__isByteArray(sourceBytes)) |
4919 | 1774 |
&& (__qClass(self) == ByteArray) |
1775 |
&& __isSmallInteger(dstStart) |
|
1776 |
&& __isSmallInteger(dstEnd) |
|
1777 |
&& __isSmallInteger(sourceStart)) { |
|
8908 | 1778 |
unsigned char *srcP = __ByteArrayInstPtr(sourceBytes)->ba_element; |
1779 |
unsigned char *dstP = __ByteArrayInstPtr(self)->ba_element; |
|
1780 |
int srcLen = __byteArraySize(sourceBytes); |
|
1781 |
int dstLen = __byteArraySize(self); |
|
1782 |
int __srcStart = __intVal(sourceStart); |
|
1783 |
int __dstStart = __intVal(dstStart); |
|
1784 |
int count = __intVal(dstEnd) - __dstStart + 1; |
|
1785 |
||
1786 |
if ((__dstStart >= 1) |
|
1787 |
&& (__srcStart >= 1) |
|
1788 |
&& ((__dstStart + count - 1) <= dstLen) |
|
1789 |
&& ((__srcStart + count - 1) <= srcLen)) { |
|
1790 |
srcP += __srcStart - 1; |
|
1791 |
dstP += __dstStart - 1; |
|
5439 | 1792 |
|
1793 |
#define OP_LOOP_BYTES(OP) \ |
|
1794 |
while (count > 0) { \ |
|
8908 | 1795 |
*dstP OP (*srcP); \ |
1796 |
srcP++; \ |
|
1797 |
dstP++; \ |
|
1798 |
count--; \ |
|
1799 |
} |
|
4919 | 1800 |
|
1801 |
#define OP_LOOP(OP) \ |
|
5439 | 1802 |
while (count >= 16) { \ |
8908 | 1803 |
((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]); \ |
1804 |
((unsigned int *)dstP)[1] OP (((unsigned int *)srcP)[1]); \ |
|
1805 |
((unsigned int *)dstP)[2] OP (((unsigned int *)srcP)[2]); \ |
|
1806 |
((unsigned int *)dstP)[3] OP (((unsigned int *)srcP)[3]); \ |
|
1807 |
srcP += 16; \ |
|
1808 |
dstP += 16; \ |
|
1809 |
count -= 16; \ |
|
4919 | 1810 |
} \ |
5439 | 1811 |
while (count >= 4) { \ |
8908 | 1812 |
((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]); \ |
1813 |
srcP += 4; \ |
|
1814 |
dstP += 4; \ |
|
1815 |
count -= 4; \ |
|
4919 | 1816 |
} \ |
1817 |
while (count > 0) { \ |
|
8908 | 1818 |
*dstP OP (*srcP); \ |
1819 |
srcP++; \ |
|
1820 |
dstP++; \ |
|
1821 |
count--; \ |
|
1822 |
} |
|
1823 |
||
1824 |
||
1825 |
if (ruleSymbol == @symbol(bitXor:)) { |
|
1826 |
OP_LOOP( ^= ) |
|
1827 |
RETURN (self); |
|
1828 |
} |
|
1829 |
if (ruleSymbol == @symbol(bitXorNot:)) { |
|
1830 |
OP_LOOP( ^=~ ) |
|
1831 |
RETURN (self); |
|
1832 |
} |
|
1833 |
if (ruleSymbol == @symbol(bitAnd:)) { |
|
1834 |
OP_LOOP( &= ) |
|
1835 |
RETURN (self); |
|
1836 |
} |
|
1837 |
if (ruleSymbol == @symbol(bitAndNot:)) { |
|
1838 |
OP_LOOP( &=~ ) |
|
1839 |
RETURN (self); |
|
1840 |
} |
|
1841 |
if (ruleSymbol == @symbol(bitOr:)) { |
|
1842 |
OP_LOOP( |= ) |
|
1843 |
RETURN (self); |
|
1844 |
} |
|
1845 |
if (ruleSymbol == @symbol(bitOrNot:)) { |
|
1846 |
OP_LOOP( |=~ ) |
|
1847 |
RETURN (self); |
|
1848 |
} |
|
1849 |
if (ruleSymbol == @symbol(copy)) { |
|
1850 |
OP_LOOP( = ) |
|
1851 |
RETURN (self); |
|
1852 |
} |
|
1853 |
if (ruleSymbol == @symbol(copyNot)) { |
|
1854 |
OP_LOOP( =~ ) |
|
1855 |
RETURN (self); |
|
1856 |
} |
|
1857 |
if (ruleSymbol == @symbol(+)) { |
|
1858 |
OP_LOOP_BYTES( += ) |
|
1859 |
RETURN (self); |
|
1860 |
} |
|
1861 |
if (ruleSymbol == @symbol(-)) { |
|
1862 |
OP_LOOP_BYTES( -= ) |
|
1863 |
RETURN (self); |
|
1864 |
} |
|
1865 |
} |
|
4919 | 1866 |
} |
5549 | 1867 |
#undef OP_LOOP_BYTES |
1868 |
#undef OP_LOOP |
|
1869 |
||
4919 | 1870 |
%}. |
1871 |
ruleSymbol == #copy ifTrue:[ |
|
8908 | 1872 |
self replaceFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart. |
1873 |
^ self |
|
4919 | 1874 |
]. |
1875 |
||
1876 |
srcIdx := sourceStart. |
|
1877 |
dstStart to:dstEnd do:[:dstIdx | |
|
8908 | 1878 |
self at:dstIdx put:((self at:dstIdx) perform:ruleSymbol with:(sourceBytes at:srcIdx)). |
1879 |
srcIdx := srcIdx + 1. |
|
4919 | 1880 |
]. |
1881 |
||
1882 |
" |
|
1883 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 1884 |
bitBlitBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor: |
4919 | 1885 |
#[1 2 3 4 5 6 7 8] |
8908 | 1886 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor: |
4919 | 1887 |
#[1 2 3 4 5 6 7 8] |
8908 | 1888 |
bitBlitBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#bitAnd: |
4919 | 1889 |
#[1 2 3 4 5 6 7 8] |
8908 | 1890 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+ |
5439 | 1891 |
#[255 0 0 0 0 0 0 0] |
8908 | 1892 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+ |
4919 | 1893 |
#[1 2 3 4 5 6 7 8] |
8908 | 1894 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#+ |
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1895 |
#[1 2 3 4 5 6 7 8] |
8908 | 1896 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#+ |
5439 | 1897 |
#[1 2 3 4 5 6 7 8] |
8908 | 1898 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#copyNot |
4919 | 1899 |
|
1900 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 1901 |
bitBlitBytesFrom:1 to:8 with:(1 to:8) startingAt:1 rule:#+ |
5439 | 1902 |
" |
1903 |
! |
|
1904 |
||
1905 |
bitBlitBytesFrom:dstStart to:dstEnd withConstant:sourceByte rule:ruleSymbol |
|
1906 |
"perform a special case of an aligned bitBlit operation. |
|
8908 | 1907 |
Bytes in the receiver from dstStart to dstEnd are destructively replaced by the result |
5439 | 1908 |
of some logical operation, as specified by the ruleSymbol. |
1909 |
Valid rule symbols are: |
|
8908 | 1910 |
#copy - trivial; same as from:to:put: |
1911 |
#bitXor: - xoring; byte[dI] = byte[dI] bitXor:sourceConst |
|
1912 |
#bitAnd: - anding; byte[dI] = byte[dI] bitAnd:sourceConst |
|
1913 |
#bitOr: - oring; byte[dI] = byte[dI] bitOr:sourceConst |
|
1914 |
#+ - adding; byte[dI] = (byte[dI] + sourceConst) mod: 256 |
|
1915 |
#- - subtract; byte[dI] = (byte[dI] - sourceConst) mod: 256 |
|
5439 | 1916 |
Warning: this is a destructive operation - elements in the receiver are overwritten. |
1917 |
" |
|
1918 |
||
1919 |
|srcIdx| |
|
1920 |
||
1921 |
%{ |
|
1922 |
if ((__qClass(self) == ByteArray) |
|
1923 |
&& __isSmallInteger(dstStart) |
|
1924 |
&& __isSmallInteger(dstEnd) |
|
1925 |
&& __isSmallInteger(sourceByte)) { |
|
8908 | 1926 |
unsigned char srcByte = __intVal(sourceByte); |
1927 |
unsigned srcWord; |
|
1928 |
unsigned char *dstP = __ByteArrayInstPtr(self)->ba_element; |
|
1929 |
int dstLen = __byteArraySize(self); |
|
1930 |
int __dstStart = __intVal(dstStart); |
|
1931 |
int count = __intVal(dstEnd) - __dstStart + 1; |
|
1932 |
||
1933 |
srcWord = (srcByte << 8) | srcByte; |
|
1934 |
srcWord = (srcWord << 16) | srcWord; |
|
1935 |
||
1936 |
if ((__dstStart >= 1) |
|
1937 |
&& ((__dstStart + count - 1) <= dstLen)) { |
|
1938 |
dstP += __dstStart - 1; |
|
5439 | 1939 |
|
1940 |
#define OP_LOOP_BYTES(OP) \ |
|
1941 |
while (count > 0) { \ |
|
8908 | 1942 |
*dstP OP srcByte; \ |
1943 |
dstP++; \ |
|
1944 |
count--; \ |
|
1945 |
} |
|
5439 | 1946 |
|
1947 |
#define OP_LOOP(OP) \ |
|
1948 |
while (count >= 16) { \ |
|
8908 | 1949 |
((unsigned int *)dstP)[0] OP srcWord; \ |
1950 |
((unsigned int *)dstP)[1] OP srcWord; \ |
|
1951 |
((unsigned int *)dstP)[2] OP srcWord; \ |
|
1952 |
((unsigned int *)dstP)[3] OP srcWord; \ |
|
1953 |
dstP += 16; \ |
|
1954 |
count -= 16; \ |
|
5439 | 1955 |
} \ |
1956 |
while (count >= 4) { \ |
|
8908 | 1957 |
((unsigned int *)dstP)[0] OP srcWord; \ |
1958 |
dstP += 4; \ |
|
1959 |
count -= 4; \ |
|
5439 | 1960 |
} \ |
1961 |
while (count > 0) { \ |
|
8908 | 1962 |
*dstP OP srcByte; \ |
1963 |
dstP++; \ |
|
1964 |
count--; \ |
|
1965 |
} |
|
1966 |
||
1967 |
||
1968 |
if (ruleSymbol == @symbol(bitXor:)) { |
|
1969 |
OP_LOOP( ^= ) |
|
1970 |
RETURN (self); |
|
1971 |
} |
|
1972 |
if (ruleSymbol == @symbol(bitXorNot:)) { |
|
1973 |
OP_LOOP( ^=~ ) |
|
1974 |
RETURN (self); |
|
1975 |
} |
|
1976 |
if (ruleSymbol == @symbol(bitAnd:)) { |
|
1977 |
OP_LOOP( &= ) |
|
1978 |
RETURN (self); |
|
1979 |
} |
|
1980 |
if (ruleSymbol == @symbol(bitAndNot:)) { |
|
1981 |
OP_LOOP( &=~ ) |
|
1982 |
RETURN (self); |
|
1983 |
} |
|
1984 |
if (ruleSymbol == @symbol(bitOr:)) { |
|
1985 |
OP_LOOP( |= ) |
|
1986 |
RETURN (self); |
|
1987 |
} |
|
1988 |
if (ruleSymbol == @symbol(bitOrNot:)) { |
|
1989 |
OP_LOOP( |=~ ) |
|
1990 |
RETURN (self); |
|
1991 |
} |
|
1992 |
if (ruleSymbol == @symbol(copy)) { |
|
1993 |
OP_LOOP( = ) |
|
1994 |
RETURN (self); |
|
1995 |
} |
|
1996 |
if (ruleSymbol == @symbol(copyNot)) { |
|
1997 |
OP_LOOP( =~ ) |
|
1998 |
RETURN (self); |
|
1999 |
} |
|
2000 |
if (ruleSymbol == @symbol(+)) { |
|
2001 |
OP_LOOP_BYTES( += ) |
|
2002 |
RETURN (self); |
|
2003 |
} |
|
2004 |
if (ruleSymbol == @symbol(-)) { |
|
2005 |
OP_LOOP_BYTES( -= ) |
|
2006 |
RETURN (self); |
|
2007 |
} |
|
2008 |
} |
|
5439 | 2009 |
} |
5549 | 2010 |
#undef OP_LOOP_BYTES |
2011 |
#undef OP_LOOP |
|
5439 | 2012 |
%}. |
2013 |
ruleSymbol == #copy ifTrue:[ |
|
8908 | 2014 |
self from:dstStart to:dstEnd put:sourceByte. |
2015 |
^ self |
|
5439 | 2016 |
]. |
2017 |
||
2018 |
dstStart to:dstEnd do:[:dstIdx | |
|
8908 | 2019 |
self at:dstIdx put:((self at:dstIdx) perform:ruleSymbol with:sourceByte). |
5439 | 2020 |
]. |
2021 |
||
2022 |
" |
|
2023 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 2024 |
bitBlitBytesFrom:1 to:3 withConstant:1 rule:#bitXor: |
5439 | 2025 |
#[1 2 3 4 5 6 7 8] |
8908 | 2026 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#bitXor: |
5439 | 2027 |
#[1 2 3 4 5 6 7 8] |
8908 | 2028 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#bitAnd: |
5439 | 2029 |
#[1 2 3 4 5 6 7 8] |
8908 | 2030 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
5439 | 2031 |
#[255 0 0 0 0 0 0 0] |
8908 | 2032 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
5439 | 2033 |
#[1 2 3 4 5 6 7 8] |
8908 | 2034 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#+ |
5439 | 2035 |
#[1 2 3 4 5 6 7 8] |
8908 | 2036 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#- |
5439 | 2037 |
#[1 2 3 4 5 6 7 8] |
8908 | 2038 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#copyNot |
5439 | 2039 |
|
2040 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 2041 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
4919 | 2042 |
" |
2043 |
! |
|
2044 |
||
8908 | 2045 |
bitOrBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 2046 |
"replace bytes in the receiver with the result of a bitOr operation. |
2047 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
2048 |
||
2049 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitOr: |
|
2050 |
||
2051 |
" |
|
2052 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 2053 |
bitOrBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 2054 |
#[1 2 3 4 5 6 7 8] |
4969 | 2055 |
bitOrBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 2056 |
" |
2057 |
||
2058 |
! |
|
2059 |
||
8908 | 2060 |
bitXorBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 2061 |
"replace bytes in the receiver with the result of an bitXor operation. |
2062 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
2063 |
||
2064 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitXor: |
|
2065 |
||
2066 |
" |
|
2067 |
#[1 2 3 4 5 6 7 8] |
|
4969 | 2068 |
bitXorBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 2069 |
#[1 2 3 4 5 6 7 8] |
8908 | 2070 |
bitXorBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 2071 |
#[1 2 3 4 5 6 7 8] |
4969 | 2072 |
bitXorBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 2073 |
" |
2074 |
||
2075 |
! |
|
2076 |
||
2866 | 2077 |
compressPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
129 | 2078 |
"given the receiver with 8-bit pixels, compress them into aByteArray |
2079 |
with nBitsPerPixel-depth pixels. The width/height-arguments are needed |
|
2080 |
to allow for any padding. On the fly, the source bytes are translated |
|
2081 |
using aMapByteArray (if non-nil). |
|
2082 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
2083 |
value of n is found in map at:(n + 1). |
|
2084 |
Output bits are filled left-to right, i.e. the first byte in the input |
|
2085 |
corresponds to the high bit(s) if the first byte in the input. |
|
8908 | 2086 |
This method can be used to convert 8-bit image data to mono, 2-bit and 4-bit |
129 | 2087 |
bitmaps. |
2088 |
It can also be used to compress byte-arrays into bitArrays." |
|
2089 |
||
2090 |
%{ /* NOCONTEXT */ |
|
2091 |
||
2092 |
REGISTER unsigned char *src, *dst; |
|
2093 |
REGISTER int wrun; |
|
2094 |
unsigned char *dstNext; |
|
1472
abaa73dc0e9a
check for destination overrun
Claus Gittinger <cg@exept.de>
parents:
1459
diff
changeset
|
2095 |
unsigned char *dstEnd; |
129 | 2096 |
int bytesPerRow, mask, shift0, shift; |
2097 |
int w, h, hrun; |
|
2098 |
int srcBytes, dstBytes; |
|
2099 |
int bitsPerPixel; |
|
2100 |
int bits; |
|
2101 |
int ncells; |
|
2102 |
unsigned char *map; |
|
2103 |
||
8908 | 2104 |
if ((__qClass(self) == @global(ByteArray)) |
1133 | 2105 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
249 | 2106 |
&& __isSmallInteger(nBitsPerPixel) |
2107 |
&& __bothSmallInteger(height, width)) { |
|
2915 | 2108 |
if ((aMapByteArray != nil) |
2109 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
2110 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
2111 |
} else { |
|
2112 |
map = (unsigned char *)0; |
|
2113 |
} |
|
129 | 2114 |
|
2915 | 2115 |
bitsPerPixel = __intVal(nBitsPerPixel); |
2116 |
w = __intVal(width); |
|
2117 |
h = __intVal(height); |
|
2118 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
2119 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
2120 |
dstEnd = dst + __byteArraySize(aByteArray); |
|
2121 |
switch (bitsPerPixel) { |
|
2122 |
case 1: |
|
2123 |
mask = 0x01; |
|
2124 |
break; |
|
2125 |
case 2: |
|
2126 |
mask = 0x03; |
|
2127 |
break; |
|
2128 |
case 4: |
|
2129 |
mask = 0x0F; |
|
2130 |
break; |
|
2131 |
case 8: |
|
2132 |
mask = 0xFF; |
|
2133 |
break; |
|
2134 |
default: |
|
2135 |
printf("invalid depth in compressPixels\n"); |
|
2136 |
goto fail; |
|
2137 |
} |
|
2138 |
if (map) { |
|
2139 |
/* |
|
2140 |
* if a map is present, it must have entries for |
|
2141 |
* all possible byte-values (i.e. its size must be >= 256) |
|
2142 |
*/ |
|
2143 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < 256) { |
|
2144 |
printf("invalid map in compressPixels\n"); |
|
2145 |
goto fail; |
|
2146 |
} |
|
2147 |
} |
|
129 | 2148 |
|
2915 | 2149 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
2150 |
dstBytes = bytesPerRow * h; |
|
2151 |
srcBytes = w * h; |
|
2866 | 2152 |
|
2915 | 2153 |
if ((__byteArraySize(self) >= srcBytes) |
2154 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
2155 |
for (hrun=h; hrun; hrun--) { |
|
2156 |
dstNext = dst + bytesPerRow; |
|
2157 |
bits = 0; shift = 8; |
|
2158 |
if (map) { |
|
2159 |
for (wrun=w; wrun; wrun--) { |
|
2160 |
bits = (bits << bitsPerPixel) | (map[*src++] & mask); |
|
2161 |
shift -= bitsPerPixel; |
|
2162 |
if (shift == 0) { |
|
2163 |
if (dst == dstEnd) goto fail; |
|
2164 |
*dst++ = bits; |
|
2165 |
bits = 0; shift = 8; |
|
2166 |
} |
|
2167 |
} |
|
2168 |
} else { |
|
2169 |
for (wrun=w; wrun; wrun--) { |
|
2170 |
bits = (bits << bitsPerPixel) | (*src++ & mask); |
|
2171 |
shift -= bitsPerPixel; |
|
2172 |
if (shift == 0) { |
|
2173 |
if (dst == dstEnd) goto fail; |
|
2174 |
*dst++ = bits; |
|
2175 |
bits = 0; shift = 8; |
|
2176 |
} |
|
2177 |
} |
|
2178 |
} |
|
2179 |
if (shift != 8) { |
|
2180 |
if (dst == dstEnd) goto fail; |
|
2181 |
*dst = bits; |
|
2182 |
} |
|
2183 |
dst = dstNext; |
|
2184 |
} |
|
2185 |
RETURN ( self ); |
|
2186 |
} |
|
129 | 2187 |
} |
2188 |
fail: ; |
|
2866 | 2189 |
%}. |
129 | 2190 |
self primitiveFailed |
2191 |
||
2192 |
"Example1: |
|
2193 |
compress 1 byte-per-pixel bitmap to 1-bit-per-pixel bitmap |
|
2194 |
" |
|
2195 |
" |
|
2196 |
|inBits outBits| |
|
2197 |
||
2198 |
inBits := #[0 0 0 0 1 1 1 1 |
|
2915 | 2199 |
0 0 1 1 0 0 1 1 |
2200 |
0 1 0 1 0 1 0 1 |
|
2201 |
1 1 1 1 0 0 0 0]. |
|
129 | 2202 |
outBits := ByteArray new:4. |
2203 |
inBits compressPixels:1 width:8 height:4 |
|
2915 | 2204 |
into:outBits mapping:nil. |
129 | 2205 |
outBits inspect |
2206 |
" |
|
2207 |
||
2208 |
"Example2: |
|
2209 |
compress byte-array into a bitArray, translating 99 to 0-bits, |
|
2210 |
and 176 to 1-bits. (just a stupid example) |
|
2211 |
" |
|
2212 |
" |
|
2213 |
|inBits outBits map| |
|
2214 |
||
2215 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
2216 |
map := ByteArray new:256. |
|
2217 |
map at:176+1 put:1. |
|
2218 |
||
2219 |
outBits := ByteArray new:2. |
|
8908 | 2220 |
inBits compressPixels:1 width:16 height:1 |
2915 | 2221 |
into:outBits mapping:map. |
129 | 2222 |
outBits inspect |
2223 |
" |
|
2224 |
||
2225 |
"Example3: |
|
2226 |
compress byte-array into a bitArray, translating everything below 128 to 0-bits, |
|
2227 |
and 128 to 255 to 1-bits.99 to 0-bits (another stupid example) |
|
2228 |
" |
|
2229 |
" |
|
2230 |
|inBits outBits map| |
|
2231 |
||
2232 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
2233 |
map := ByteArray new:256. |
|
2234 |
map atAll:(128+1 to:255+1) put:1. |
|
2235 |
||
2236 |
outBits := ByteArray new:2. |
|
8908 | 2237 |
inBits compressPixels:1 width:16 height:1 |
2915 | 2238 |
into:outBits mapping:map. |
129 | 2239 |
outBits inspect |
2240 |
" |
|
563 | 2241 |
! |
2242 |
||
4829 | 2243 |
copyReverse |
4927 | 2244 |
"create a copy of myself with elements reversed in order" |
2245 |
||
2246 |
^ self copy reverse |
|
4829 | 2247 |
|
2248 |
" |
|
2249 |
#[1 2 3 4 5] copyReverse |
|
2250 |
#[1 2 3 4] copyReverse |
|
2251 |
" |
|
2252 |
! |
|
2253 |
||
2866 | 2254 |
expandPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
563 | 2255 |
"given the receiver with nBitsPerPixel-depth pixels, expand them into |
2256 |
aByteArray with 8-bit pixels. The width/height-arguments are needed |
|
2257 |
to skip any padded src-bits. On the fly, the destination pixels |
|
2258 |
are translated using aMapByteArray (if non-nil). |
|
2259 |
Input bits are read left-to right, i.e. the first byte in the output |
|
2260 |
corresponds to the high bit(s) in the inputs first byte. |
|
2261 |
This is used to display mono, 2-bit and 4-bit bitmaps on grey-scale/color |
|
2262 |
machines. With nBitsPerPixel==8, this is a translate operation. |
|
2263 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
2264 |
value of n is found in map at:(n + 1). |
|
2265 |
It can also be used to expand bit-arrays into byteArrays. |
|
2266 |
This method is specialized for ByteArray arguments - it will not handle |
|
2267 |
anything else." |
|
2268 |
||
2269 |
%{ /* NOCONTEXT */ |
|
2270 |
||
2271 |
REGISTER unsigned char *src, *dst; |
|
2272 |
REGISTER int wrun; |
|
2273 |
unsigned char *srcNext; |
|
2274 |
int bytesPerRow, mask, shift0, shift; |
|
2275 |
int w, h, hrun; |
|
2276 |
int srcBytes, dstBytes; |
|
2277 |
int bitsPerPixel; |
|
2278 |
int bits; |
|
2279 |
int ncells; |
|
2280 |
unsigned char *map; |
|
2281 |
||
8908 | 2282 |
if ((__qClass(self) == @global(ByteArray)) |
1133 | 2283 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
563 | 2284 |
&& __isSmallInteger(nBitsPerPixel) |
2285 |
&& __bothSmallInteger(height, width)) { |
|
8908 | 2286 |
if ((aMapByteArray != nil) |
2287 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
2288 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
2289 |
} else { |
|
2290 |
map = (unsigned char *)0; |
|
2291 |
} |
|
2292 |
||
2293 |
bitsPerPixel = __intVal(nBitsPerPixel); |
|
2294 |
w = __intVal(width); |
|
2295 |
h = __intVal(height); |
|
2296 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
2297 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
2298 |
switch (bitsPerPixel) { |
|
2299 |
case 1: |
|
2300 |
mask = 0x01; |
|
2301 |
break; |
|
2302 |
case 2: |
|
2303 |
mask = 0x03; |
|
2304 |
break; |
|
2305 |
case 4: |
|
2306 |
mask = 0x0F; |
|
2307 |
break; |
|
2308 |
case 8: |
|
2309 |
mask = 0xFF; |
|
2310 |
break; |
|
2311 |
default: |
|
2312 |
printf("invalid depth in expandPixels\n"); |
|
2313 |
goto fail; |
|
2314 |
} |
|
2315 |
ncells = mask + 1; |
|
2316 |
if (map) { |
|
2317 |
/* |
|
2318 |
* if a map is present, it must have the correct size |
|
2319 |
* (i.e. 2 raisedTo:nBitsPerPixel) |
|
2320 |
*/ |
|
2321 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells) { |
|
2322 |
printf("invalid map in expandPixels\n"); |
|
2323 |
goto fail; |
|
2324 |
} |
|
2325 |
} |
|
2326 |
||
2327 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
|
2328 |
shift0 = 8 - bitsPerPixel; |
|
2329 |
srcBytes = bytesPerRow * h; |
|
2330 |
dstBytes = w * h; |
|
2331 |
||
2332 |
if ((__byteArraySize(self) >= srcBytes) |
|
2333 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
2334 |
for (hrun=h; hrun; hrun--) { |
|
2335 |
srcNext = src + bytesPerRow; |
|
2336 |
shift = shift0; |
|
2337 |
if (map) { |
|
2338 |
if (shift0 == 0) { |
|
2339 |
/* translate only */ |
|
2340 |
for (wrun=w; wrun; wrun--) { |
|
2341 |
bits = *src++; |
|
2342 |
*dst++ = map[bits]; |
|
2343 |
} |
|
2344 |
} else { |
|
2345 |
for (wrun=w; wrun; wrun--) { |
|
2346 |
if (shift == shift0) { |
|
2347 |
bits = *src++; |
|
2348 |
} |
|
2349 |
*dst++ = map[(bits >> shift) & mask]; |
|
2350 |
shift -= bitsPerPixel; |
|
2351 |
if (shift < 0) { |
|
2352 |
shift = shift0; |
|
2353 |
} |
|
2354 |
} |
|
2355 |
} |
|
2356 |
} else { |
|
2357 |
for (wrun=w; wrun; wrun--) { |
|
2358 |
if (shift == shift0) { |
|
2359 |
bits = *src++; |
|
2360 |
} |
|
2361 |
*dst++ = (bits >> shift) & mask; |
|
2362 |
shift -= bitsPerPixel; |
|
2363 |
if (shift < 0) { |
|
2364 |
shift = shift0; |
|
2365 |
} |
|
2366 |
} |
|
2367 |
} |
|
2368 |
src = srcNext; |
|
2369 |
} |
|
2370 |
RETURN ( self ); |
|
2371 |
} |
|
2372 |
printf("buffer size: self:%d expect at least:%d\n", |
|
2373 |
__byteArraySize(self), srcBytes); |
|
2374 |
printf("buffer size: arg:%d expect at least:%d\n", |
|
2375 |
__byteArraySize(aByteArray), dstBytes); |
|
563 | 2376 |
} |
2866 | 2377 |
printf("invalid args\n"); |
1527
af119b16aa16
better handle the translate-only case in #expandPixels:
ca
parents:
1505
diff
changeset
|
2378 |
|
563 | 2379 |
fail: ; |
2866 | 2380 |
%}. |
563 | 2381 |
self primitiveFailed |
2382 |
||
2383 |
"Example1: |
|
2384 |
expand 1-bit-per-pixel bitmap into a 1byte-per-pixel byteArray |
|
2385 |
" |
|
2386 |
" |
|
2387 |
|inBits outBits| |
|
2388 |
||
8908 | 2389 |
inBits := #[2r11110000 |
2390 |
2r11001100 |
|
2391 |
2r01010101 |
|
2392 |
2r00001111]. |
|
563 | 2393 |
outBits := ByteArray new:(8*4). |
2394 |
inBits expandPixels:1 width:8 height:4 |
|
8908 | 2395 |
into:outBits mapping:nil. |
563 | 2396 |
outBits inspect |
2397 |
" |
|
2398 |
||
2399 |
"Example2: |
|
2400 |
expand bit-array into a byteArray, translating 0-bits to 99, |
|
2401 |
1-bits to 176. (just a stupid example) |
|
2402 |
" |
|
2403 |
" |
|
2404 |
|inBits outBits| |
|
2405 |
||
2406 |
inBits := #[2r11110000 2r11001100]. |
|
2407 |
outBits := ByteArray new:16. |
|
8908 | 2408 |
inBits expandPixels:1 width:16 height:1 |
2409 |
into:outBits mapping:#[99 176]. |
|
563 | 2410 |
outBits inspect |
2411 |
" |
|
4978 | 2412 |
|
2413 |
"This Can also be used to extract nibbles ..." |
|
2414 |
" |
|
2415 |
|inBits outBits| |
|
2416 |
||
8908 | 2417 |
inBits := #[ 16r12 16r34 16r56 16r78 16r9A 16rBC 16rDE 16rF0 ]. |
4978 | 2418 |
outBits := ByteArray new:(inBits size * 2). |
2419 |
inBits expandPixels:4 width:outBits size height:1 into:outBits mapping:nil. |
|
2420 |
outBits inspect |
|
2421 |
" |
|
2422 |
||
563 | 2423 |
! |
2424 |
||
2425 |
invert |
|
2426 |
"invert all bytes - used with image manipulations |
|
2427 |
written as a primitive for speed. |
|
2428 |
Q: is this really needed ?" |
|
2429 |
||
2430 |
%{ /* NOCONTEXT */ |
|
2431 |
||
2432 |
REGISTER unsigned char *dst; |
|
2433 |
REGISTER unsigned long *ldst; |
|
2434 |
REGISTER int cnt; |
|
2435 |
||
1133 | 2436 |
if (__qClass(self) == @global(ByteArray)) { |
8908 | 2437 |
cnt = __byteArraySize(self); |
2438 |
dst = __ByteArrayInstPtr(self)->ba_element; |
|
2439 |
if (! ((INT)dst & (sizeof(long)-1))) { |
|
2440 |
ldst = (unsigned long *)dst; |
|
2441 |
while (cnt >= (sizeof(long))*4) { |
|
2442 |
ldst[0] = ~(ldst[0]); |
|
2443 |
ldst[1] = ~(ldst[1]); |
|
2444 |
ldst[2] = ~(ldst[2]); |
|
2445 |
ldst[3] = ~(ldst[3]); |
|
2446 |
ldst += 4; |
|
2447 |
cnt -= (sizeof(long))*4; |
|
2448 |
} |
|
2449 |
while (cnt >= sizeof(long)) { |
|
2450 |
*ldst = ~(*ldst); |
|
2451 |
ldst++; |
|
2452 |
cnt -= sizeof(long); |
|
2453 |
} |
|
2454 |
dst = (unsigned char *)ldst; |
|
2455 |
} |
|
2456 |
while (cnt--) { |
|
2457 |
*dst = ~(*dst); |
|
2458 |
dst++; |
|
2459 |
} |
|
2460 |
RETURN ( self ); |
|
563 | 2461 |
} |
4927 | 2462 |
%}. |
5439 | 2463 |
self bitBlitBytesFrom:1 to:self size withConstant:16rFF rule:#bitXor: |
4927 | 2464 |
|
2465 |
" |
|
2466 |
#[1 2 3 4 5 6 7 8 9 10] copy invert |
|
8908 | 2467 |
#[1 2 3 4 5 6 7 8 9 10] copy |
2468 |
bitBlitBytesFrom:1 to:10 withConstant:16rFF rule:#bitXor: |
|
4927 | 2469 |
" |
563 | 2470 |
! |
2471 |
||
2472 |
reverse |
|
8908 | 2473 |
"reverse the order of my elements inplace - |
7338 | 2474 |
WARNING: this is a destructive operation, which modifies the receiver. |
2475 |
Written as a primitive for speed on image manipulations (mirror)" |
|
563 | 2476 |
|
2477 |
%{ /* NOCONTEXT */ |
|
2478 |
||
2479 |
REGISTER unsigned char *p1, *p2; |
|
2480 |
REGISTER int cnt; |
|
2481 |
REGISTER unsigned t; |
|
2482 |
OBJ cls; |
|
2483 |
||
1133 | 2484 |
if (__qClass(self) == @global(ByteArray)) { |
8908 | 2485 |
cnt = __byteArraySize(self); |
2486 |
p1 = __ByteArrayInstPtr(self)->ba_element; |
|
2487 |
p2 = p1 + cnt - 1; |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2488 |
|
4927 | 2489 |
#if defined(__BSWAP) |
8908 | 2490 |
/* |
2491 |
* can we use the bswap instruction ? |
|
2492 |
* notice - not all CPUs have it (the HAS_BSWAP checks this). |
|
2493 |
*/ |
|
2494 |
if (__HAS_BSWAP() |
|
2495 |
&& ((cnt & 3) == 0)) { |
|
2496 |
unsigned int *ip1, *ip2; |
|
2497 |
||
2498 |
ip1 = (unsigned int *)p1; |
|
2499 |
ip2 = (unsigned int *)(p2 - 3); |
|
2500 |
||
2501 |
ip2 -= 7; |
|
2502 |
while (ip1 <= ip2) { |
|
2503 |
int t1, t2; |
|
2504 |
||
2505 |
t1 = ip1[0]; |
|
2506 |
t2 = ip2[7]; |
|
2507 |
ip2[7] = __BSWAP(t1); |
|
2508 |
ip1[0] = __BSWAP(t2); |
|
2509 |
||
2510 |
t1 = ip1[1]; |
|
2511 |
t2 = ip2[6]; |
|
2512 |
ip2[6] = __BSWAP(t1); |
|
2513 |
ip1[1] = __BSWAP(t2); |
|
2514 |
||
2515 |
t1 = ip1[2]; |
|
2516 |
t2 = ip2[5]; |
|
2517 |
ip2[5] = __BSWAP(t1); |
|
2518 |
ip1[2] = __BSWAP(t2); |
|
2519 |
||
2520 |
t1 = ip1[3]; |
|
2521 |
t2 = ip2[4]; |
|
2522 |
ip2[4] = __BSWAP(t1); |
|
2523 |
ip1[3] = __BSWAP(t2); |
|
2524 |
||
2525 |
ip1 += 4; |
|
2526 |
ip2 -= 4; |
|
2527 |
} |
|
2528 |
ip2 += 7; |
|
2529 |
||
2530 |
while (ip1 < ip2) { |
|
2531 |
int t; |
|
2532 |
||
2533 |
t = __BSWAP(*ip1); |
|
2534 |
*ip1++ = __BSWAP(*ip2); |
|
2535 |
*ip2-- = t; |
|
2536 |
} |
|
2537 |
||
2538 |
if (ip1 == ip2) { |
|
2539 |
int t; |
|
2540 |
t = *ip1; |
|
2541 |
t = __BSWAP(t); |
|
2542 |
*ip1 = t; |
|
2543 |
} |
|
2544 |
RETURN ( self ); |
|
2545 |
} |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
2546 |
#endif /* __i386__ && __GNUC__ */ |
4927 | 2547 |
|
8908 | 2548 |
p2 -= 7; |
2549 |
while (p1 <= p2) { |
|
2550 |
t = p1[0]; |
|
2551 |
p1[0] = p2[7]; |
|
2552 |
p2[7] = t; |
|
2553 |
||
2554 |
t = p1[1]; |
|
2555 |
p1[1] = p2[6]; |
|
2556 |
p2[6] = t; |
|
2557 |
||
2558 |
t = p1[2]; |
|
2559 |
p1[2] = p2[5]; |
|
2560 |
p2[5] = t; |
|
2561 |
||
2562 |
t = p1[3]; |
|
2563 |
p1[3] = p2[4]; |
|
2564 |
p2[4] = t; |
|
2565 |
||
2566 |
p1 += 4; |
|
2567 |
p2 -= 4; |
|
2568 |
} |
|
2569 |
p2 += 7; |
|
2570 |
||
2571 |
while (p1 < p2) { |
|
2572 |
t = *p1; |
|
2573 |
*p1++ = *p2; |
|
2574 |
*p2-- = t; |
|
2575 |
} |
|
2576 |
RETURN ( self ); |
|
563 | 2577 |
} |
2578 |
%}. |
|
2579 |
^ super reverse |
|
2585 | 2580 |
|
2581 |
" |
|
8908 | 2582 |
#[1 2 3 4 5] reverse |
2583 |
#[1 2 3 4] reverse |
|
2584 |
#[1 2 3 4 5] reverse |
|
2585 |
#[1 2 3 4 5 6] reverse |
|
2586 |
#[1 2 3 4 5 6 7] reverse |
|
2587 |
#[1 2 3 4 5 6 7 8] reverse |
|
2588 |
#[1 2 3 4 5 6 7 8 9] reverse |
|
2589 |
#[1 2 3 4 5 6 7 8 9 10] reverse |
|
2590 |
#[1 2 3 4 5 6 7 8 9 10 11 12] reverse |
|
2591 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] reverse |
|
2592 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] reverse |
|
2593 |
(1 to:255) asByteArray reverse |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2594 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2595 |
1 to:1024 do:[:i| |
8908 | 2596 |
|bytes test rBytes| |
2597 |
||
2598 |
bytes := ((1 to:i) asArray collect:[:i | i bitAnd:255]) asByteArray. |
|
2599 |
test := ((i to:1 by:-1) asArray collect:[:i | i bitAnd:255]) asByteArray. |
|
2600 |
rBytes := bytes copy. |
|
2601 |
rBytes reverse ~= test ifTrue:[ |
|
2602 |
self halt |
|
2603 |
]. |
|
2604 |
rBytes := bytes copy. |
|
2605 |
rBytes reverse reverse ~= bytes ifTrue:[ |
|
2606 |
self halt |
|
2607 |
] |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2608 |
]. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2609 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2610 |
Time millisecondsToRun:[ |
8908 | 2611 |
10000000 timesRepeat:[ |
2612 |
#[1 2 3 4 5 6 7 8] reverse |
|
2613 |
] |
|
2614 |
] |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2615 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2616 |
|b| |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2617 |
b := (0 to:255) asByteArray. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2618 |
Time millisecondsToRun:[ |
8908 | 2619 |
10000000 timesRepeat:[ |
2620 |
b reverse |
|
2621 |
] |
|
2622 |
] |
|
2585 | 2623 |
" |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2624 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2625 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2626 |
swapBytes |
8908 | 2627 |
"swap bytes inplace - |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2628 |
written as a primitive for speed on image grabbing (if display order is different)" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2629 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2630 |
%{ /* NOCONTEXT */ |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2631 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2632 |
REGISTER unsigned char *p; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2633 |
REGISTER int cnt; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2634 |
REGISTER unsigned t; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2635 |
|
1133 | 2636 |
if (__qClass(self) == @global(ByteArray)) { |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2637 |
cnt = __byteArraySize(self); |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2638 |
cnt = cnt & ~1; /* make it even */ |
1133 | 2639 |
p = __ByteArrayInstPtr(self)->ba_element; |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2640 |
while (cnt > 0) { |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2641 |
#ifdef OLD |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2642 |
t = p[0]; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2643 |
p[0] = p[1]; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2644 |
p[1] = t; |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2645 |
#else |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2646 |
unsigned short s; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2647 |
|
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2648 |
s = ((unsigned short *)p)[0]; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2649 |
s = (s >> 8) | (s << 8); |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2650 |
((unsigned short *)p)[0] = s; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2651 |
#endif |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2652 |
p += 2; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2653 |
cnt -= 2; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2654 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2655 |
RETURN ( self ); |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2656 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2657 |
%}. |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2658 |
^ super swapBytes "/ rubbish - there is no one currenly |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2659 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2660 |
" |
8908 | 2661 |
#[1 2 3 4 5 6 7 8 9 10] copy swapBytes |
2662 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapBytes |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2663 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2664 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2665 |
|
4983 | 2666 |
swapIndex:i1 and:i2 |
2667 |
"spap the bytes with i1 and i2" |
|
2668 |
||
2669 |
%{ /* NOCONTEXT */ |
|
2670 |
||
2671 |
REGISTER unsigned char *p; |
|
2672 |
unsigned int __i1, __i2; |
|
2673 |
int cnt; |
|
2674 |
unsigned int t; |
|
2675 |
||
2676 |
if (__qClass(self) == @global(ByteArray) && __bothSmallInteger(i1, i2)) { |
|
8908 | 2677 |
__i1 = __intVal(i1) - 1; |
2678 |
__i2 = __intVal(i2) - 1; |
|
2679 |
cnt = __byteArraySize(self); |
|
2680 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2681 |
if (__i1 < cnt && __i2 < cnt) { |
|
2682 |
t = p[__i1]; |
|
2683 |
p[__i1] = p[__i2]; |
|
2684 |
p[__i2] = t; |
|
2685 |
} |
|
2686 |
RETURN ( self ); |
|
4983 | 2687 |
} |
2688 |
%}. |
|
5439 | 2689 |
^ super swapIndex:i1 and:i2 "/ rubbish - there is no one currenl |
4983 | 2690 |
|
2691 |
" |
|
8908 | 2692 |
#[1 2 3 4 5 6 7 8 9 10] copy swapIndex:1 and:10 |
2693 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapIndex:5 and:6 |
|
4983 | 2694 |
" |
2695 |
||
2696 |
||
2697 |
! |
|
2698 |
||
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2699 |
swapLongs |
8908 | 2700 |
"swap long bytes inplace |
4927 | 2701 |
- any partial longs at the end are not swapped." |
2702 |
||
2703 |
self swapLongsFrom:1 to:self size |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2704 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2705 |
" |
8908 | 2706 |
#[1 2 3 4 5 6 7 8 9] copy swapLongs |
2707 |
#[1 2 3 4 5 6 7 8 9 10] copy swapLongs |
|
2708 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapLongs |
|
2709 |
#[1 2 3 4 5 6 7 8 9 10 11 12] copy swapLongs |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2710 |
" |
4365 | 2711 |
! |
2712 |
||
2713 |
swapLongsFrom:startIndex to:endIndex |
|
8908 | 2714 |
"swap longs inplace |
2715 |
- any partial longs at the end are not swapped. |
|
4367
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2716 |
Swapping is from startIndex to (exclusiv) endIndex; |
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2717 |
indexing starts at 1." |
4365 | 2718 |
|
2719 |
%{ /* NOCONTEXT */ |
|
2720 |
||
2721 |
REGISTER unsigned char *p; |
|
2722 |
REGISTER int limit; |
|
2723 |
REGISTER unsigned t; |
|
2724 |
||
2725 |
if ((__qClass(self) == @global(ByteArray)) |
|
2726 |
&& __isSmallInteger(startIndex) |
|
2727 |
&& __isSmallInteger(endIndex)) { |
|
4969 | 2728 |
int __idx = __intVal(startIndex); |
2729 |
int __endIdx = __intVal(endIndex); |
|
2730 |
||
2731 |
limit = __byteArraySize(self); |
|
2732 |
if (__endIdx < limit) { |
|
2733 |
limit = __endIdx; |
|
2734 |
} |
|
2735 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2736 |
p = p + __idx - 1; |
|
2737 |
||
2738 |
limit = limit - 4 + 1; |
|
4927 | 2739 |
|
2740 |
#if defined(__BSWAP) |
|
4969 | 2741 |
/* |
2742 |
* can we use the bswap instruction ? |
|
2743 |
* notice - not all CPUs have it (the HAS_BSWAP checks this). |
|
2744 |
*/ |
|
2745 |
if (__HAS_BSWAP() |
|
2746 |
&& (((unsigned int)p & 3) == 0)) { |
|
2747 |
unsigned int *ip; |
|
2748 |
ip = (unsigned int *)p; |
|
2749 |
||
2750 |
while (__idx <= limit) { |
|
2751 |
*ip = __BSWAP(*ip); |
|
2752 |
ip++; |
|
2753 |
__idx += 4; |
|
2754 |
} |
|
2755 |
RETURN (self); |
|
2756 |
} |
|
4927 | 2757 |
#endif /* __BSWAP */ |
2758 |
||
4969 | 2759 |
while (__idx <= limit) { |
2760 |
t = p[0]; |
|
2761 |
p[0] = p[3]; |
|
2762 |
p[3] = t; |
|
2763 |
t = p[1]; |
|
2764 |
p[1] = p[2]; |
|
2765 |
p[2] = t; |
|
2766 |
p += 4; |
|
2767 |
__idx += 4; |
|
2768 |
} |
|
2769 |
RETURN ( self ); |
|
4365 | 2770 |
} |
2771 |
%}. |
|
2772 |
^ super swapLongsFrom:startIndex to:endIndex "/ rubbish - there is no one currenly |
|
2773 |
||
2774 |
" |
|
8908 | 2775 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:3 |
2776 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:4 |
|
2777 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:5 |
|
2778 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:6 |
|
2779 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:7 |
|
2780 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:8 |
|
2781 |
#[1 2 3 4 5 6 7 8 9 10] copy swapLongsFrom:1 to:11 |
|
2782 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapLongsFrom:1 to:12 |
|
2783 |
#[1 2 3 4 5 6 7 8 9 10 11 12] copy swapLongsFrom:1 to:13 |
|
2784 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:5 to:10 |
|
4365 | 2785 |
" |
1 | 2786 |
! ! |
550 | 2787 |
|
2788 |
!ByteArray methodsFor:'printing & storing'! |
|
2789 |
||
563 | 2790 |
displayString |
1244 | 2791 |
"return a printed representation of the receiver for displaying" |
2792 |
||
6513
d7bc1d1dab4b
displayString fix for subclasses
Claus Gittinger <cg@exept.de>
parents:
6125
diff
changeset
|
2793 |
self class == ByteArray ifTrue:[ |
8908 | 2794 |
^ self storeString |
6513
d7bc1d1dab4b
displayString fix for subclasses
Claus Gittinger <cg@exept.de>
parents:
6125
diff
changeset
|
2795 |
]. |
d7bc1d1dab4b
displayString fix for subclasses
Claus Gittinger <cg@exept.de>
parents:
6125
diff
changeset
|
2796 |
^ super displayString |
563 | 2797 |
|
2798 |
"Created: 25.10.1995 / 13:33:26 / cg" |
|
1244 | 2799 |
"Modified: 22.4.1996 / 12:54:06 / cg" |
563 | 2800 |
! |
2801 |
||
7172 | 2802 |
hexPrintOn:aStream |
7245 | 2803 |
"print as hex string, eg: 'FF0243'. |
2804 |
This string can be used in #fromHexString: to recreate the byteArray" |
|
7172 | 2805 |
|
2806 |
self do:[:byte| |
|
8908 | 2807 |
byte printOn:aStream base:16 size:2 fill:$0. |
7172 | 2808 |
]. |
2809 |
||
2810 |
" |
|
2811 |
#[1 2 3 4 10 17] hexPrintOn:Transcript |
|
2812 |
" |
|
7245 | 2813 |
|
2814 |
" |
|
2815 |
|s| |
|
2816 |
s := String streamContents:[:s | #[1 2 3 4 10 17] hexPrintOn:s]. |
|
8908 | 2817 |
ByteArray fromHexString:s |
7245 | 2818 |
" |
7172 | 2819 |
! |
2820 |
||
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2821 |
printOn:aStream |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2822 |
"append a printed representation to aStream" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2823 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2824 |
self class == ByteArray ifTrue:[ "/ care for subclasses |
8908 | 2825 |
aStream nextPutAll:'#['. |
2826 |
self |
|
2827 |
do:[:byte | byte printOn:aStream] |
|
2828 |
separatedBy:[aStream space]. |
|
2829 |
aStream nextPut:$]. |
|
2830 |
^ self |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2831 |
]. |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2832 |
^ super printOn:aStream |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2833 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2834 |
" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2835 |
#[1 2 3 4 5] printOn:Transcript |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2836 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2837 |
|
8908 | 2838 |
#[1 2 3 4 5] storeString |
2839 |
#[1 2 3 4 5] displayString |
|
2840 |
#[1 2 3 4 5] printString |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2841 |
" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2842 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2843 |
"Modified: / 12.9.1997 / 22:11:33 / cg" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2844 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2845 |
! |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2846 |
|
4050 | 2847 |
printOn:aStream base:radix |
2848 |
"append a printed representation to aStream in the given number base." |
|
2849 |
||
6125 | 2850 |
^ self printOn:aStream base:radix showRadix:false |
2851 |
||
2852 |
" |
|
2853 |
#[1 2 3 4 5] printOn:Transcript base:2 |
|
2854 |
'Hello World' printOn:Transcript base:2 |
|
2855 |
||
8908 | 2856 |
#[1 2 3 4 5] storeString |
2857 |
#[1 2 3 4 5] displayString |
|
2858 |
#[1 2 3 4 5] printString |
|
6125 | 2859 |
" |
2860 |
||
2861 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
|
2862 |
"Modified: / 31.10.2001 / 09:43:56 / cg" |
|
2863 |
! |
|
2864 |
||
2865 |
printOn:aStream base:radix showRadix:showRadix |
|
2866 |
"append a printed representation to aStream in the given number base." |
|
2867 |
||
4050 | 2868 |
self class == ByteArray ifTrue:[ "/ care for subclasses |
8908 | 2869 |
aStream nextPutAll:'#['. |
2870 |
self |
|
2871 |
do:[:byte | byte printOn:aStream base:radix showRadix:showRadix] |
|
2872 |
separatedBy:[aStream space]. |
|
2873 |
aStream nextPut:$]. |
|
2874 |
^ self |
|
4050 | 2875 |
]. |
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2876 |
^ self printOn:aStream |
4050 | 2877 |
|
2878 |
" |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2879 |
#[1 2 3 4 5] printOn:Transcript base:2 |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2880 |
'Hello World' printOn:Transcript base:2 |
4050 | 2881 |
|
8908 | 2882 |
#[1 2 3 4 5] storeString |
2883 |
#[1 2 3 4 5] displayString |
|
2884 |
#[1 2 3 4 5] printString |
|
4050 | 2885 |
" |
2886 |
||
2887 |
"Modified: / 12.9.1997 / 22:11:33 / cg" |
|
2888 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
|
6125 | 2889 |
"Created: / 31.10.2001 / 09:43:41 / cg" |
4050 | 2890 |
! |
2891 |
||
7599
188489aaaacd
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7338
diff
changeset
|
2892 |
storeArrayElementOn:aStream |
8368 | 2893 |
"Store as element of an array. Omit the leading '#'" |
7599
188489aaaacd
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7338
diff
changeset
|
2894 |
|
188489aaaacd
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7338
diff
changeset
|
2895 |
self class == ByteArray ifTrue:[ "/ care for subclasses |
8908 | 2896 |
aStream nextPut:$[. |
2897 |
self |
|
2898 |
do:[:byte | byte storeOn:aStream] |
|
2899 |
separatedBy:[aStream space]. |
|
2900 |
aStream nextPut:$]. |
|
2901 |
^ self |
|
7599
188489aaaacd
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7338
diff
changeset
|
2902 |
]. |
8368 | 2903 |
super storeArrayElementOn:aStream |
7599
188489aaaacd
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7338
diff
changeset
|
2904 |
|
188489aaaacd
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7338
diff
changeset
|
2905 |
" |
8908 | 2906 |
#[1 2 3 4 5] storeOn:Transcript |
2907 |
#[1 2 3 4 5] storeArrayElementOn:Transcript |
|
2908 |
||
2909 |
#[1 2 3 4 5] storeString |
|
2910 |
#[1 2 3 4 5] displayString |
|
2911 |
#[1 2 3 4 5] printString |
|
7599
188489aaaacd
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7338
diff
changeset
|
2912 |
" |
188489aaaacd
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7338
diff
changeset
|
2913 |
|
188489aaaacd
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7338
diff
changeset
|
2914 |
"Modified: 12.9.1997 / 22:11:33 / cg" |
188489aaaacd
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7338
diff
changeset
|
2915 |
! |
188489aaaacd
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7338
diff
changeset
|
2916 |
|
563 | 2917 |
storeOn:aStream |
2918 |
"append a printed representation from which the receiver can be |
|
2919 |
reconstructed to aStream. (reimplemented to make it look better)" |
|
2920 |
||
1082 | 2921 |
self class == ByteArray ifTrue:[ "/ care for subclasses |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2922 |
aStream nextPutAll:'#['. |
8908 | 2923 |
self |
2915 | 2924 |
do:[:byte | byte storeOn:aStream] |
2925 |
separatedBy:[aStream space]. |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2926 |
aStream nextPutAll:']'. |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2927 |
^ self |
563 | 2928 |
]. |
2929 |
^ super storeOn:aStream |
|
2930 |
||
2931 |
" |
|
8908 | 2932 |
#[1 2 3 4 5] storeOn:Transcript |
2933 |
||
2934 |
#[1 2 3 4 5] storeString |
|
2935 |
#[1 2 3 4 5] displayString |
|
2936 |
#[1 2 3 4 5] printString |
|
563 | 2937 |
" |
1082 | 2938 |
|
2915 | 2939 |
"Modified: 12.9.1997 / 22:11:33 / cg" |
550 | 2940 |
! ! |
2941 |
||
7822 | 2942 |
!ByteArray methodsFor:'queries'! |
1082 | 2943 |
|
563 | 2944 |
max |
2945 |
"return the maximum value in the receiver - |
|
8908 | 2946 |
redefined to speedup image processing and sound-player |
563 | 2947 |
(which need a fast method for this on byteArrays)" |
550 | 2948 |
|
563 | 2949 |
%{ /* NOCONTEXT */ |
550 | 2950 |
|
2951 |
REGISTER unsigned char *cp; |
|
563 | 2952 |
REGISTER int index, max; |
2953 |
int len; |
|
550 | 2954 |
|
1133 | 2955 |
if (__qClass(self) == @global(ByteArray)) { |
4969 | 2956 |
max = 0; |
2957 |
index = 0; |
|
2958 |
len = __qSize(self) - OHDR_SIZE; |
|
2959 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2960 |
||
2961 |
while (++index <= len) { |
|
2962 |
unsigned int byte; |
|
2963 |
||
2964 |
byte = *cp; |
|
2965 |
cp++; |
|
2966 |
if (byte > max) { |
|
2967 |
max = byte; |
|
2968 |
if (byte == 255) break; |
|
2969 |
} |
|
2970 |
} |
|
8913 | 2971 |
RETURN ( __mkSmallInteger(max) ); |
550 | 2972 |
} |
563 | 2973 |
%}. |
2974 |
^ super max |
|
550 | 2975 |
|
2976 |
" |
|
8908 | 2977 |
#[1 2 3 1 2 3 1 2 19] max |
550 | 2978 |
" |
5554 | 2979 |
! |
550 | 2980 |
|
2981 |
usageCounts |
|
2982 |
"return an array filled with value-counts - |
|
2983 |
This is needed in the bitmap/image classes to get info on color usage. |
|
2984 |
(i.e. to build up a histogram of color usage within an image)." |
|
2985 |
||
2986 |
|counts| |
|
2987 |
||
2988 |
counts := Array basicNew:256. |
|
2989 |
||
2990 |
%{ /* STACK: 2000 */ |
|
2991 |
||
2992 |
REGISTER unsigned char *cp; |
|
2993 |
REGISTER int nByte; |
|
2994 |
REGISTER int index; |
|
8913 | 2995 |
INT icounts[256]; |
550 | 2996 |
|
1133 | 2997 |
if ((__qClass(self) == @global(ByteArray)) && __isArray(counts)) { |
550 | 2998 |
/* |
2999 |
* zero counts |
|
3000 |
*/ |
|
3001 |
for (index=0; index<256; index++) { |
|
3002 |
icounts[index] = 0; |
|
3003 |
} |
|
3004 |
||
3005 |
/* |
|
3006 |
* count |
|
3007 |
*/ |
|
3008 |
nByte = __qSize(self) - OHDR_SIZE; |
|
1133 | 3009 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
550 | 3010 |
while (nByte--) { |
3011 |
icounts[*cp++]++; |
|
3012 |
} |
|
3013 |
||
3014 |
/* |
|
3015 |
* make it real counts |
|
3016 |
*/ |
|
3017 |
for (index=0; index<256; index++) { |
|
8913 | 3018 |
__ArrayInstPtr(counts)->a_element[index] = __mkSmallInteger(icounts[index]); |
550 | 3019 |
} |
3020 |
RETURN ( counts ); |
|
3021 |
} |
|
3022 |
%} |
|
3023 |
. |
|
3024 |
self primitiveFailed |
|
3025 |
||
3026 |
" |
|
8908 | 3027 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] usageCounts |
550 | 3028 |
" |
3029 |
! |
|
3030 |
||
563 | 3031 |
usedValues |
3032 |
"return a new ByteArray with all used values (actually a kind of Set); |
|
8908 | 3033 |
This is needed specially in the bitmap/Imageclasses to find used colors |
2866 | 3034 |
of an image." |
550 | 3035 |
|
563 | 3036 |
%{ /* STACK: 400 */ |
550 | 3037 |
|
3038 |
REGISTER unsigned char *cp; |
|
2866 | 3039 |
unsigned char *endp; |
563 | 3040 |
REGISTER int len; |
2890 | 3041 |
OBJ result; |
2866 | 3042 |
union { |
2915 | 3043 |
unsigned char flags[256]; |
3044 |
int toForceAlignmentOfFlags; |
|
2866 | 3045 |
} f; |
3046 |
||
3047 |
#ifdef TO_BE_MEASURED |
|
3048 |
int coverage = 0; |
|
3049 |
#endif |
|
550 | 3050 |
|
1133 | 3051 |
if (__qClass(self) == @global(ByteArray)) { |
2915 | 3052 |
memset(f.flags, 0, sizeof(f.flags)); |
3053 |
len = __qSize(self) - OHDR_SIZE; |
|
3054 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2866 | 3055 |
|
2915 | 3056 |
/* for each used byte, set flag */ |
3057 |
while (len > 0) { |
|
2866 | 3058 |
#ifdef TO_BE_MEASURED |
2915 | 3059 |
unsigned byte; |
563 | 3060 |
|
2915 | 3061 |
byte = *cp; |
3062 |
if (! f.flags[byte]) { |
|
3063 |
f.flags[byte] = 1; |
|
3064 |
coverage++; |
|
3065 |
if (coverage == 256) { |
|
3066 |
/* no need to scan rest */ |
|
3067 |
break; |
|
3068 |
} |
|
3069 |
} |
|
2866 | 3070 |
#else |
2915 | 3071 |
f.flags[*cp] = 1; |
2866 | 3072 |
#endif |
2915 | 3073 |
cp++; |
3074 |
len--; |
|
3075 |
} |
|
563 | 3076 |
|
2915 | 3077 |
/* count 1's */ |
3078 |
len = 0; |
|
3079 |
for (cp=f.flags, endp=f.flags+256; cp < endp;) { |
|
3080 |
if ( *((unsigned int *)cp)) { |
|
3081 |
if (cp[0]) len++; |
|
3082 |
if (cp[1]) len++; |
|
3083 |
if (cp[2]) len++; |
|
3084 |
if (cp[3]) len++; |
|
3085 |
} |
|
3086 |
cp += 4; |
|
3087 |
} |
|
1133 | 3088 |
|
2915 | 3089 |
/* create ByteArray of used values */ |
3090 |
result = __BYTEARRAY_UNINITIALIZED_NEW_INT(len); |
|
3091 |
if (result) { |
|
3092 |
cp = __ByteArrayInstPtr(result)->ba_element; |
|
3093 |
for (len=0; len < 256; len++) { |
|
8908 | 3094 |
if (f.flags[len]) |
2915 | 3095 |
*cp++ = len; |
3096 |
} |
|
3097 |
RETURN ( result ); |
|
3098 |
} |
|
550 | 3099 |
} |
2866 | 3100 |
%}. |
3101 |
^ self asIdentitySet asByteArray |
|
550 | 3102 |
|
3103 |
" |
|
8908 | 3104 |
#[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues |
550 | 3105 |
" |
3106 |
! ! |
|
3107 |
||
8996
239e0796331c
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8919
diff
changeset
|
3108 |
|
7822 | 3109 |
!ByteArray methodsFor:'searching'! |
3110 |
||
3111 |
indexOf:aByte startingAt:start |
|
3112 |
"return the index of the first occurrence of the argument, aByte |
|
3113 |
in the receiver starting at start, anInteger; return 0 if not found. |
|
3114 |
- reimplemented here for speed" |
|
3115 |
||
3116 |
%{ /* NOCONTEXT */ |
|
3117 |
||
3118 |
REGISTER unsigned char *cp; |
|
8913 | 3119 |
REGISTER INT index; |
3120 |
REGISTER int byteValue; |
|
7822 | 3121 |
REGISTER int len; |
3122 |
OBJ cls; |
|
3123 |
||
3124 |
if (__isSmallInteger(aByte) &&__isBytes(self)) { |
|
3125 |
byteValue = __intVal(aByte); |
|
3126 |
||
3127 |
if (byteValue & ~0xFF /* i.e. (byteValue < 0) || (byteValue > 255) */) { |
|
3128 |
/* |
|
3129 |
* searching for something which cannot be found |
|
3130 |
*/ |
|
8913 | 3131 |
RETURN ( __mkSmallInteger(0) ); |
7822 | 3132 |
} |
3133 |
||
3134 |
if (__isSmallInteger(start)) { |
|
3135 |
index = __intVal(start); |
|
3136 |
len = __byteArraySize(self); |
|
3137 |
cp = __ByteArrayInstPtr(self)->ba_element; |
|
3138 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
3139 |
int nInst; |
|
3140 |
||
3141 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
3142 |
cp += nInst; |
|
3143 |
len -= nInst; |
|
3144 |
} |
|
3145 |
cp += index - 1; |
|
3146 |
while (index <= len) { |
|
3147 |
if (*cp == byteValue) { |
|
8913 | 3148 |
RETURN ( __mkSmallInteger(index) ); |
7822 | 3149 |
} |
3150 |
index++; |
|
3151 |
cp++; |
|
3152 |
} |
|
8913 | 3153 |
RETURN ( __mkSmallInteger(0) ); |
7822 | 3154 |
} |
3155 |
} |
|
3156 |
%}. |
|
3157 |
^ super indexOf:aByte startingAt:start |
|
3158 |
||
3159 |
" |
|
8908 | 3160 |
#[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 |
7822 | 3161 |
" |
3162 |
! ! |
|
3163 |
||
5554 | 3164 |
!ByteArray methodsFor:'testing'! |
3165 |
||
3166 |
isByteArray |
|
3167 |
"return true, if the receiver is some kind of bytearray; |
|
3168 |
true is returned here - the method is redefined from Object." |
|
3169 |
||
3170 |
^ true |
|
3171 |
||
3172 |
||
3173 |
! |
|
3174 |
||
3175 |
isLiteral |
|
3176 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
3177 |
(i.e. can be used in constant arrays)" |
|
3178 |
||
3179 |
"/ no, simply returning true here is a mistake: |
|
8908 | 3180 |
"/ it could be a subclass of ByteArray |
5554 | 3181 |
"/ (of which the compiler does not know at all ...) |
3182 |
||
3183 |
^ self class == ByteArray |
|
3184 |
||
3185 |
"Modified: 22.4.1996 / 12:55:30 / cg" |
|
3186 |
! ! |
|
3187 |
||
8397
a1cff241e853
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8368
diff
changeset
|
3188 |
!ByteArray methodsFor:'visiting'! |
a1cff241e853
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8368
diff
changeset
|
3189 |
|
a1cff241e853
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8368
diff
changeset
|
3190 |
acceptVisitor:aVisitor with:aParameter |
a1cff241e853
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8368
diff
changeset
|
3191 |
|
a1cff241e853
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8368
diff
changeset
|
3192 |
^ aVisitor visitByteArray:self with:aParameter |
a1cff241e853
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8368
diff
changeset
|
3193 |
! ! |
a1cff241e853
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8368
diff
changeset
|
3194 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3195 |
!ByteArray class methodsFor:'documentation'! |
628 | 3196 |
|
3197 |
version |
|
8996
239e0796331c
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8919
diff
changeset
|
3198 |
^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.172 2005-11-17 11:07:08 stefan Exp $' |
628 | 3199 |
! ! |