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