ByteArray.st
branchjv
changeset 19635 875eb54afd2c
parent 19567 33f60845c4bc
parent 19619 beb60d40e286
child 19659 0f585374259a
equal deleted inserted replaced
19614:d8838105c692 19635:875eb54afd2c
   160     }
   160     }
   161 out:;
   161 out:;
   162 %}.
   162 %}.
   163     ^ self basicNew:anInteger
   163     ^ self basicNew:anInteger
   164 ! !
   164 ! !
       
   165 
   165 
   166 
   166 
   167 
   167 
   168 
   168 !ByteArray class methodsFor:'queries'!
   169 !ByteArray class methodsFor:'queries'!
   169 
   170 
  2330 !
  2331 !
  2331 
  2332 
  2332 reverse
  2333 reverse
  2333     "reverse the order of my elements inplace -
  2334     "reverse the order of my elements inplace -
  2334      WARNING: this is a destructive operation, which modifies the receiver.
  2335      WARNING: this is a destructive operation, which modifies the receiver.
  2335 	      Please use reversed (with a d) for a functional version.
  2336               Please use reversed (with a 'd' at the end) for a functional version.
  2336      Written as a primitive for speed on image manipulations (mirror)"
  2337      Written as a primitive for speed on image manipulations (mirror)"
  2337 
  2338 
  2338 %{  /* NOCONTEXT */
  2339 %{  /* NOCONTEXT */
  2339 
  2340 
  2340     REGISTER unsigned char *p1, *p2;
  2341     REGISTER unsigned char *p1, *p2;
  2341     REGISTER int cnt;
  2342     REGISTER int cnt;
  2342     REGISTER unsigned t;
  2343     REGISTER unsigned t;
  2343     OBJ cls;
  2344     OBJ cls;
  2344 
  2345 
  2345     if (__qClass(self) == @global(ByteArray)) {
  2346     if (__qClass(self) == @global(ByteArray)) {
  2346 	cnt = __byteArraySize(self);
  2347         cnt = __byteArraySize(self);
  2347 	p1 = __ByteArrayInstPtr(self)->ba_element;
  2348         p1 = __ByteArrayInstPtr(self)->ba_element;
  2348 	p2 = p1 + cnt - 1;
  2349         p2 = p1 + cnt - 1;
  2349 
  2350 
  2350 #if defined(__BSWAP)
  2351 #if defined(__BSWAP)
  2351 	/*
  2352         /*
  2352 	 * can we use the bswap instruction ?
  2353          * can we use the bswap instruction ?
  2353 	 * notice - not all CPUs have it (the HAS_BSWAP checks this).
  2354          * notice - not all CPUs have it (the HAS_BSWAP checks this).
  2354 	 */
  2355          */
  2355 	if (__HAS_BSWAP()
  2356         if (__HAS_BSWAP()
  2356 	 && ((cnt & 3) == 0)) {
  2357          && ((cnt & 3) == 0)) {
  2357 	    unsigned int *ip1, *ip2;
  2358             unsigned int *ip1, *ip2;
  2358 
  2359 
  2359 	    ip1 = (unsigned int *)p1;
  2360             ip1 = (unsigned int *)p1;
  2360 	    ip2 = (unsigned int *)(p2 - 3);
  2361             ip2 = (unsigned int *)(p2 - 3);
  2361 
  2362 
  2362 	    ip2 -= 7;
  2363             ip2 -= 7;
  2363 	    while (ip1 <= ip2) {
  2364             while (ip1 <= ip2) {
  2364 		int t1, t2;
  2365                 int t1, t2;
  2365 
  2366 
  2366 		t1 = ip1[0];
  2367                 t1 = ip1[0];
  2367 		t2 = ip2[7];
  2368                 t2 = ip2[7];
  2368 		ip2[7] = __BSWAP(t1);
  2369                 ip2[7] = __BSWAP(t1);
  2369 		ip1[0] = __BSWAP(t2);
  2370                 ip1[0] = __BSWAP(t2);
  2370 
  2371 
  2371 		t1 = ip1[1];
  2372                 t1 = ip1[1];
  2372 		t2 = ip2[6];
  2373                 t2 = ip2[6];
  2373 		ip2[6] = __BSWAP(t1);
  2374                 ip2[6] = __BSWAP(t1);
  2374 		ip1[1] = __BSWAP(t2);
  2375                 ip1[1] = __BSWAP(t2);
  2375 
  2376 
  2376 		t1 = ip1[2];
  2377                 t1 = ip1[2];
  2377 		t2 = ip2[5];
  2378                 t2 = ip2[5];
  2378 		ip2[5] = __BSWAP(t1);
  2379                 ip2[5] = __BSWAP(t1);
  2379 		ip1[2] = __BSWAP(t2);
  2380                 ip1[2] = __BSWAP(t2);
  2380 
  2381 
  2381 		t1 = ip1[3];
  2382                 t1 = ip1[3];
  2382 		t2 = ip2[4];
  2383                 t2 = ip2[4];
  2383 		ip2[4] = __BSWAP(t1);
  2384                 ip2[4] = __BSWAP(t1);
  2384 		ip1[3] = __BSWAP(t2);
  2385                 ip1[3] = __BSWAP(t2);
  2385 
  2386 
  2386 		ip1 += 4;
  2387                 ip1 += 4;
  2387 		ip2 -= 4;
  2388                 ip2 -= 4;
  2388 	    }
  2389             }
  2389 	    ip2 += 7;
  2390             ip2 += 7;
  2390 
  2391 
  2391 	    while (ip1 < ip2) {
  2392             while (ip1 < ip2) {
  2392 		int t;
  2393                 int t;
  2393 
  2394 
  2394 		t = __BSWAP(*ip1);
  2395                 t = __BSWAP(*ip1);
  2395 		*ip1++ = __BSWAP(*ip2);
  2396                 *ip1++ = __BSWAP(*ip2);
  2396 		*ip2-- = t;
  2397                 *ip2-- = t;
  2397 	    }
  2398             }
  2398 
  2399 
  2399 	    if (ip1 == ip2) {
  2400             if (ip1 == ip2) {
  2400 		int t;
  2401                 int t;
  2401 		t = *ip1;
  2402                 t = *ip1;
  2402 		t = __BSWAP(t);
  2403                 t = __BSWAP(t);
  2403 		*ip1 = t;
  2404                 *ip1 = t;
  2404 	    }
  2405             }
  2405 	    RETURN ( self );
  2406             RETURN ( self );
  2406 	}
  2407         }
  2407 #endif /* __i386__ && __GNUC__ */
  2408 #endif /* __BSWAP (i.e. __i386__ && __GNUC__) */
  2408 
  2409 
  2409 	p2 -= 7;
  2410         p2 -= 7;
  2410 	while (p1 <= p2) {
  2411         while (p1 <= p2) {
  2411 	    t = p1[0];
  2412             t = p1[0];
  2412 	    p1[0] = p2[7];
  2413             p1[0] = p2[7];
  2413 	    p2[7] = t;
  2414             p2[7] = t;
  2414 
  2415 
  2415 	    t = p1[1];
  2416             t = p1[1];
  2416 	    p1[1] = p2[6];
  2417             p1[1] = p2[6];
  2417 	    p2[6] = t;
  2418             p2[6] = t;
  2418 
  2419 
  2419 	    t = p1[2];
  2420             t = p1[2];
  2420 	    p1[2] = p2[5];
  2421             p1[2] = p2[5];
  2421 	    p2[5] = t;
  2422             p2[5] = t;
  2422 
  2423 
  2423 	    t = p1[3];
  2424             t = p1[3];
  2424 	    p1[3] = p2[4];
  2425             p1[3] = p2[4];
  2425 	    p2[4] = t;
  2426             p2[4] = t;
  2426 
  2427 
  2427 	    p1 += 4;
  2428             p1 += 4;
  2428 	    p2 -= 4;
  2429             p2 -= 4;
  2429 	}
  2430         }
  2430 	p2 += 7;
  2431         p2 += 7;
  2431 
  2432 
  2432 	while (p1 < p2) {
  2433         while (p1 < p2) {
  2433 	    t = *p1;
  2434             t = *p1;
  2434 	    *p1++ = *p2;
  2435             *p1++ = *p2;
  2435 	    *p2-- = t;
  2436             *p2-- = t;
  2436 	}
  2437         }
  2437 	RETURN ( self );
  2438         RETURN ( self );
  2438     }
  2439     }
  2439 %}.
  2440 %}.
  2440     ^ super reverse
  2441     ^ super reverse
  2441 
  2442 
  2442     "
  2443     "
  2452      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] reverse
  2453      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] reverse
  2453      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] reverse
  2454      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] reverse
  2454      (1 to:255) asByteArray reverse
  2455      (1 to:255) asByteArray reverse
  2455 
  2456 
  2456      1 to:1024 do:[:i|
  2457      1 to:1024 do:[:i|
  2457 	|bytes test rBytes|
  2458         |bytes test rBytes|
  2458 
  2459 
  2459 	bytes := ((1 to:i) asArray collect:[:i | i bitAnd:255]) asByteArray.
  2460         bytes := ((1 to:i) asArray collect:[:i | i bitAnd:255]) asByteArray.
  2460 	test := ((i to:1 by:-1) asArray collect:[:i | i bitAnd:255]) asByteArray.
  2461         test := ((i to:1 by:-1) asArray collect:[:i | i bitAnd:255]) asByteArray.
  2461 	rBytes := bytes copy.
  2462         rBytes := bytes copy.
  2462 	rBytes reverse ~= test ifTrue:[
  2463         rBytes reverse ~= test ifTrue:[
  2463 	    self halt
  2464             self halt
  2464 	].
  2465         ].
  2465 	rBytes := bytes copy.
  2466         rBytes := bytes copy.
  2466 	rBytes reverse reverse ~= bytes ifTrue:[
  2467         rBytes reverse reverse ~= bytes ifTrue:[
  2467 	    self halt
  2468             self halt
  2468 	]
  2469         ]
  2469      ].
  2470      ].
  2470 
  2471 
  2471      Time millisecondsToRun:[
  2472      Time millisecondsToRun:[
  2472 	10000000 timesRepeat:[
  2473         10000000 timesRepeat:[
  2473 	    #[1 2 3 4 5 6 7 8] reverse
  2474             #[1 2 3 4 5 6 7 8] reverse
  2474 	]
  2475         ]
  2475      ]
  2476      ]
  2476 
  2477 
  2477      |b|
  2478      |b|
  2478      b := (0 to:255) asByteArray.
  2479      b := (0 to:255) asByteArray.
  2479      Time millisecondsToRun:[
  2480      Time millisecondsToRun:[
  2480 	10000000 timesRepeat:[
  2481         10000000 timesRepeat:[
  2481 	    b reverse
  2482             b reverse
  2482 	]
  2483         ]
  2483      ]
  2484      ]
  2484     "
  2485     "
  2485 !
  2486 !
  2486 
  2487 
  2487 swapBytes
  2488 swapBytes
  3060     "
  3061     "
  3061      #[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 startingAt:1
  3062      #[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 startingAt:1
  3062     "
  3063     "
  3063 ! !
  3064 ! !
  3064 
  3065 
  3065 
       
  3066 !ByteArray methodsFor:'testing'!
  3066 !ByteArray methodsFor:'testing'!
  3067 
  3067 
  3068 isByteArray
  3068 isByteArray
  3069     "return true, if the receiver is some kind of bytearray;
  3069     "return true, if the receiver is some kind of bytearray;
  3070      true is returned here - the method is redefined from Object."
  3070      true is returned here - the method is redefined from Object."
  3085     ^ self class == ByteArray
  3085     ^ self class == ByteArray
  3086 
  3086 
  3087     "Modified: 22.4.1996 / 12:55:30 / cg"
  3087     "Modified: 22.4.1996 / 12:55:30 / cg"
  3088 ! !
  3088 ! !
  3089 
  3089 
       
  3090 
  3090 !ByteArray class methodsFor:'documentation'!
  3091 !ByteArray class methodsFor:'documentation'!
  3091 
  3092 
  3092 version
  3093 version
  3093     ^ '$Header$'
  3094     ^ '$Header$'
  3094 !
  3095 !