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