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