ByteArray.st
changeset 12 8e03bd717355
parent 10 4f1f9a91e406
child 37 d9a302eaa3ef
equal deleted inserted replaced
11:6bf3080856be 12:8e03bd717355
    23               All Rights Reserved
    23               All Rights Reserved
    24 
    24 
    25 ByteArrays store integers in the range 0..255
    25 ByteArrays store integers in the range 0..255
    26 unlike Smalltalk/80, my ByteArrays have fixed size - may change
    26 unlike Smalltalk/80, my ByteArrays have fixed size - may change
    27 
    27 
    28 $Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.5 1993-11-08 02:29:19 claus Exp $
    28 $Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.6 1993-12-11 00:44:06 claus Exp $
    29 
    29 
    30 written spring 89 by claus
    30 written spring 89 by claus
    31 '!
    31 '!
    32 
    32 
    33 !ByteArray class methodsFor:'queries'!
    33 !ByteArray class methodsFor:'queries'!
    66     "return a new instance of the receiver with uninitialized
    66     "return a new instance of the receiver with uninitialized
    67      (i.e. undefined) contents. The indexed elements have any random
    67      (i.e. undefined) contents. The indexed elements have any random
    68      value. use, when contents will be set anyway shortly after."
    68      value. use, when contents will be set anyway shortly after."
    69 
    69 
    70 %{  /* NOCONTEXT */
    70 %{  /* NOCONTEXT */
    71 
       
    72     OBJ newobj;
    71     OBJ newobj;
    73     INT instsize, nInstVars, nindexedinstvars;
    72     INT instsize, nInstVars, nindexedinstvars;
    74     REGISTER OBJ *op;
    73     REGISTER OBJ *op;
    75     extern OBJ new();
    74     extern OBJ new();
    76 
    75 
    78         nindexedinstvars = _intVal(anInteger);
    77         nindexedinstvars = _intVal(anInteger);
    79         if (nindexedinstvars >= 0) {
    78         if (nindexedinstvars >= 0) {
    80             nInstVars = _intVal(_ClassInstPtr(self)->c_ninstvars);
    79             nInstVars = _intVal(_ClassInstPtr(self)->c_ninstvars);
    81             if ((_intVal(_ClassInstPtr(self)->c_flags) & ARRAYMASK) == BYTEARRAY) {
    80             if ((_intVal(_ClassInstPtr(self)->c_flags) & ARRAYMASK) == BYTEARRAY) {
    82                 instsize = OHDR_SIZE + nInstVars * sizeof(OBJ) + nindexedinstvars * sizeof(char);
    81                 instsize = OHDR_SIZE + nInstVars * sizeof(OBJ) + nindexedinstvars * sizeof(char);
    83                 PROTECT(self);
    82                 PROTECT_CONTEXT
    84                 _qNew(newobj, instsize, SENDER);
    83                 _qNew(newobj, instsize, SENDER);
    85                 UNPROTECT(self);
    84                 UNPROTECT_CONTEXT
    86                 _InstPtr(newobj)->o_class = self;
    85                 if (newobj) {
       
    86                     _InstPtr(newobj)->o_class = self;
    87 #if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES)
    87 #if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES)
    88                 /*
    88                     /*
    89                  * knowing that nil is 0
    89                      * knowing that nil is 0
    90                  */
    90                      */
    91                 memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
    91                     memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
    92 #else
    92 #else
    93                 op = _InstPtr(newobj)->i_instvars;
    93                     op = _InstPtr(newobj)->i_instvars;
    94                 while (nInstVars--)
    94                     while (nInstVars--)
    95                     *op++ = nil;
    95                         *op++ = nil;
    96 #endif
    96 #endif
    97                 RETURN ( newobj );
    97                     RETURN ( newobj );
    98             }
    98                 }
    99         }
    99             }
   100     }
   100         }
   101 %}
   101     }
   102 .
   102 %}
       
   103 .
       
   104     (anInteger isMemberOf:SmallInteger) ifTrue:[
       
   105         (anInteger < 0) ifTrue:[
       
   106             self error:'bad (negative) argument to new'
       
   107         ] ifFalse:[
       
   108             ObjectMemory allocationFailureSignal raise.
       
   109         ].
       
   110         ^ nil
       
   111     ].
   103     ^ self basicNew:anInteger
   112     ^ self basicNew:anInteger
   104 
   113 
   105 ! !
   114 ! !
   106 
   115 
   107 !ByteArray methodsFor:'accessing'!
   116 !ByteArray methodsFor:'accessing'!
   293     self wordAt:(index+2) put:t.
   302     self wordAt:(index+2) put:t.
   294     self wordAt:(index) put:(value - (t * 256 * 256)).
   303     self wordAt:(index) put:(value - (t * 256 * 256)).
   295     ^ value
   304     ^ value
   296 ! !
   305 ! !
   297 
   306 
       
   307 !ByteArray methodsFor:'converting'!
       
   308 
       
   309 asByteArray
       
   310     "return the receiver as a byteArray"
       
   311 
       
   312     "could be an instance of a subclass..."
       
   313     self class == ByteArray ifTrue:[
       
   314         ^ self
       
   315     ].
       
   316     ^ super asByteArray
       
   317 ! !
       
   318 
       
   319 !ByteArray methodsFor:'printing & storing'!
       
   320 
       
   321 isLiteral
       
   322     "return true, if the receiver can be used as a literal"
       
   323 
       
   324     ^ true
       
   325 !
       
   326 
       
   327 storeOn:aStream
       
   328     aStream nextPutAll:'#['.
       
   329     self do:[:byte | byte storeOn:aStream. aStream space. ].
       
   330     aStream nextPutAll:']'
       
   331 ! !
       
   332 
   298 !ByteArray methodsFor:'queries'!
   333 !ByteArray methodsFor:'queries'!
   299 
   334 
   300 indexOf:aByte startingAt:start
   335 indexOf:aByte startingAt:start
   301     "return the index of the first occurrence of the argument, aByte
   336     "return the index of the first occurrence of the argument, aByte
   302      in the receiver starting at start, anInteger; return 0 if not found.
   337      in the receiver starting at start, anInteger; return 0 if not found.
   347 usedValues
   382 usedValues
   348     "return a new ByteArray with all used values (actually a kind of Set);
   383     "return a new ByteArray with all used values (actually a kind of Set);
   349      needed specially for Image class."
   384      needed specially for Image class."
   350 
   385 
   351     |result l|
   386     |result l|
   352 %{
   387 
       
   388 %{  /* STACK: 400 */
       
   389 
   353     REGISTER unsigned char *cp;
   390     REGISTER unsigned char *cp;
   354     REGISTER int len;
   391     REGISTER int len;
   355     unsigned char flags[256];
   392     unsigned char flags[256];
   356     static struct inlineCache nw = _ILC1;
   393     static struct inlineCache nw = _ILC1;
   357     extern OBJ ByteArray, _new_;
   394     extern OBJ ByteArray, _new_;
   371         len = 0;
   408         len = 0;
   372         for (cp=flags+255; cp >= flags; cp--)
   409         for (cp=flags+255; cp >= flags; cp--)
   373             if (*cp) len++;
   410             if (*cp) len++;
   374 
   411 
   375         /* create ByteArray of used values */
   412         /* create ByteArray of used values */
   376 #ifdef PASS_ARG_REF
       
   377         l = _MKSMALLINT(len);
       
   378         result = (*nw.ilc_func)(ByteArray, _new_, CON_COMMA nil, &nw, &l);
       
   379 #else
       
   380         result = (*nw.ilc_func)(ByteArray, _new_, CON_COMMA nil, &nw, _MKSMALLINT(len));
   413         result = (*nw.ilc_func)(ByteArray, _new_, CON_COMMA nil, &nw, _MKSMALLINT(len));
   381 #endif
       
   382         if (_Class(result) == ByteArray) {
   414         if (_Class(result) == ByteArray) {
   383             cp = &(_ByteArrayInstPtr(result)->ba_element[0]);
   415             cp = &(_ByteArrayInstPtr(result)->ba_element[0]);
   384             for (len=0; len < 256; len++) {
   416             for (len=0; len < 256; len++) {
   385                 if (flags[len]) 
   417                 if (flags[len]) 
   386                     *cp++ = len;
   418                     *cp++ = len;
   398      added for Image handling"
   430      added for Image handling"
   399 
   431 
   400     |counts|
   432     |counts|
   401 
   433 
   402     counts := Array new:256.
   434     counts := Array new:256.
   403 %{
   435 
       
   436 %{  /* STACK: 2000 */
       
   437 
   404     REGISTER unsigned char *cp;
   438     REGISTER unsigned char *cp;
   405     REGISTER int nByte;
   439     REGISTER int nByte;
   406     REGISTER int index;
   440     REGISTER int index;
   407     int icounts[256];
   441     int icounts[256];
   408 
   442 
   484             startIndex = _intVal(start) - 1;
   518             startIndex = _intVal(start) - 1;
   485             if (startIndex >= 0) {
   519             if (startIndex >= 0) {
   486               nIndex = _qSize(self) - OHDR_SIZE;
   520               nIndex = _qSize(self) - OHDR_SIZE;
   487               stopIndex = _intVal(stop) - 1;
   521               stopIndex = _intVal(stop) - 1;
   488               count = stopIndex - startIndex + 1;
   522               count = stopIndex - startIndex + 1;
       
   523               if (count == 0) {
       
   524                   RETURN ( self );
       
   525               }
   489               if ((count > 0) && (stopIndex < nIndex)) {
   526               if ((count > 0) && (stopIndex < nIndex)) {
   490                 repStartIndex = _intVal(repStart) - 1;
   527                 repStartIndex = _intVal(repStart) - 1;
   491                 if (repStartIndex >= 0) {
   528                 if (repStartIndex >= 0) {
   492                   repNIndex = _qSize(aCollection) - OHDR_SIZE;
   529                   repNIndex = _qSize(aCollection) - OHDR_SIZE;
   493                   repStopIndex = repStartIndex + (stopIndex - startIndex);
   530                   repStopIndex = repStartIndex + (stopIndex - startIndex);
   556         RETURN ( self );
   593         RETURN ( self );
   557     }
   594     }
   558 %}
   595 %}
   559 .
   596 .
   560     self primitiveFailed
   597     self primitiveFailed
       
   598 !
       
   599 
       
   600 reverse
       
   601     "reverse order of elements inplace - 
       
   602      written as a primitive for speed on image manipulations"
       
   603 
       
   604 %{  /* NOCONTEXT */
       
   605 
       
   606     REGISTER unsigned char *p1, *p2;
       
   607     REGISTER int cnt;
       
   608     REGISTER unsigned t;
       
   609 
       
   610     if (_qClass(self) == ByteArray) {
       
   611         cnt = _qSize(self) - OHDR_SIZE;
       
   612         p1 = _ByteArrayInstPtr(self)->ba_element;
       
   613         p2 = _ByteArrayInstPtr(self)->ba_element + cnt - 1;
       
   614         while (cnt > 0) {
       
   615             t = *p1;
       
   616             *p1++ = *p2;
       
   617             *p2-- = t;
       
   618             cnt-=2;
       
   619         }
       
   620         RETURN ( self );
       
   621     }
       
   622 %}
       
   623 .
       
   624     ^ super reverse
   561 !
   625 !
   562 
   626 
   563 expandPixels:nBitsPerPixel width:width height:height into:aByteArray
   627 expandPixels:nBitsPerPixel width:width height:height into:aByteArray
   564                          mapping:aMapByteArray
   628                          mapping:aMapByteArray
   565     "given the receiver with nBitsPerPixel-depth pixels, expand them into
   629     "given the receiver with nBitsPerPixel-depth pixels, expand them into