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