Array.st
changeset 18246 d4d749c60f22
parent 17376 5c0cab0193cf
child 18261 22bdfc405bca
child 18267 3bc0c58cb8e9
equal deleted inserted replaced
18245:3923781828c2 18246:d4d749c60f22
    36 "
    36 "
    37 !
    37 !
    38 
    38 
    39 documentation
    39 documentation
    40 "
    40 "
    41     Instances of Array store general objects; 
    41     Instances of Array store general objects;
    42     an array's size is fixed, therefore add:/remove: are not allowed.
    42     an array's size is fixed, therefore add:/remove: are not allowed.
    43     (actually, #add: is implemented for compatibility with smalltalks which
    43     (actually, #add: is implemented for compatibility with smalltalks which
    44      provide it, but it is very slow and outputs an annoying warning message...)
    44      provide it, but it is very slow and outputs an annoying warning message...)
    45 
    45 
    46     Access to the individual elements is via an integer index,
    46     Access to the individual elements is via an integer index,
    57     However, subclassing is allowed of course
    57     However, subclassing is allowed of course
    58     - even with added named instance variables.
    58     - even with added named instance variables.
    59 
    59 
    60     Literal arrays (i.e. array-constants) are entered in source as:
    60     Literal arrays (i.e. array-constants) are entered in source as:
    61 
    61 
    62         #( element1 element2 ... element-N)
    62 	#( element1 element2 ... element-N)
    63 
    63 
    64     where each element must be itself a literal constant.
    64     where each element must be itself a literal constant.
    65     Array, symbol and byteArray constants within an array can be written
    65     Array, symbol and byteArray constants within an array can be written
    66     without the initial #-character.
    66     without the initial #-character.
    67     In addition, true, false and nil are also allowed as array-literal.
    67     In addition, true, false and nil are also allowed as array-literal.
    75       #(two [3 3 3] (4 4 4))  -> 3 elements: a symbol, a byteArray and another array
    75       #(two [3 3 3] (4 4 4))  -> 3 elements: a symbol, a byteArray and another array
    76 
    76 
    77     Also, a syntactic sugar piece allows for Array instances to be created dynamcially
    77     Also, a syntactic sugar piece allows for Array instances to be created dynamcially
    78     at runtime with the brace syntax:
    78     at runtime with the brace syntax:
    79 
    79 
    80         { expr1 . expr2 . ... . expr-N }
    80 	{ expr1 . expr2 . ... . expr-N }
    81 
    81 
    82     where each expr-i evaluates to an element of the new array instance.
    82     where each expr-i evaluates to an element of the new array instance.
    83     Notice that the expressions are separated by a period.
    83     Notice that the expressions are separated by a period.
    84     Semantically, this is equivalent to ``Array with:expr1 with:expr2 ... with:expr-N''
    84     Semantically, this is equivalent to ``Array with:expr1 with:expr2 ... with:expr-N''
    85     Examples:
    85     Examples:
    86         { 1 . 2 . 3 }         -> a new 3 element array; similar to #( 1 2 3 ),
    86 	{ 1 . 2 . 3 }         -> a new 3 element array; similar to #( 1 2 3 ),
    87                                  but in contrast, a new array instance is created
    87 				 but in contrast, a new array instance is created
    88         { 
    88 	{
    89             { 'foo' . [ Transcript showCR:'foo' ] } .
    89 	    { 'foo' . [ Transcript showCR:'foo' ] } .
    90             { 'bar' . [ Transcript showCR:'bar' ] } 
    90 	    { 'bar' . [ Transcript showCR:'bar' ] }
    91             { 'baz' . [ Transcript showCR:'baz' ] } 
    91 	    { 'baz' . [ Transcript showCR:'baz' ] }
    92         }                     
    92 	}
    93                               -> a new 3 element array, consisting of 3 new
    93 			      -> a new 3 element array, consisting of 3 new
    94                                  2-element array instances, consisting of a string
    94 				 2-element array instances, consisting of a string
    95                                  and a block each
    95 				 and a block each
    96 
    96 
    97     [memory requirements:]
    97     [memory requirements:]
    98         OBJ-HEADER + (size * ptr-size)
    98 	OBJ-HEADER + (size * ptr-size)
    99 
    99 
   100     [warning:]
   100     [warning:]
   101         read the warning about 'growing fixed size collection'
   101 	read the warning about 'growing fixed size collection'
   102         in ArrayedCollection's documentation
   102 	in ArrayedCollection's documentation
   103 
   103 
   104     [author:]
   104     [author:]
   105         Claus Gittinger
   105 	Claus Gittinger
   106 
   106 
   107     [see also:]
   107     [see also:]
   108         OrderedCollection
   108 	OrderedCollection
   109         ByteArray FloatArray DoubleArray IntegerArray BitArray
   109 	ByteArray FloatArray DoubleArray IntegerArray BitArray
   110         CharacterArray String
   110 	CharacterArray String
   111 "
   111 "
   112 ! !
   112 ! !
   113 
   113 
   114 !Array class methodsFor:'instance creation'!
   114 !Array class methodsFor:'instance creation'!
   115 
   115 
   444     "return the receiver as an array - that's the receiver itself.
   444     "return the receiver as an array - that's the receiver itself.
   445      Notice: Use asNewArray, if you intent to modify the returned collection."
   445      Notice: Use asNewArray, if you intent to modify the returned collection."
   446 
   446 
   447     "could be an instance of a subclass..."
   447     "could be an instance of a subclass..."
   448     self class == Array ifTrue:[
   448     self class == Array ifTrue:[
   449         ^ self
   449 	^ self
   450     ].
   450     ].
   451     ^ super asArray
   451     ^ super asArray
   452 
   452 
   453     "Modified: 22.4.1996 / 12:42:09 / cg"
   453     "Modified: 22.4.1996 / 12:42:09 / cg"
   454 !
   454 !
   465 asNewArray
   465 asNewArray
   466     "return the receiver as an unique new array."
   466     "return the receiver as an unique new array."
   467 
   467 
   468     "could be an instance of a subclass..."
   468     "could be an instance of a subclass..."
   469     self class == Array ifTrue:[
   469     self class == Array ifTrue:[
   470         ^ self copy
   470 	^ self copy
   471     ].
   471     ].
   472     ^ super asArray
   472     ^ super asArray
   473 !
   473 !
   474 
   474 
   475 beImmutable
   475 beImmutable
   483 !Array methodsFor:'copying'!
   483 !Array methodsFor:'copying'!
   484 
   484 
   485 , aCollection
   485 , aCollection
   486 %{
   486 %{
   487     if (__isArray(aCollection)) {
   487     if (__isArray(aCollection)) {
   488         if (__isArray(self)) {
   488 	if (__isArray(self)) {
   489             OBJ newArray;
   489 	    OBJ newArray;
   490             int mySize = __arraySize(self);
   490 	    int mySize = __arraySize(self);
   491             int otherSize = __arraySize(aCollection);
   491 	    int otherSize = __arraySize(aCollection);
   492             REGISTER OBJ src;
   492 	    REGISTER OBJ src;
   493             int srcIdx, dstIdx;
   493 	    int srcIdx, dstIdx;
   494             newArray = __ARRAY_NEW_INT(mySize+otherSize);
   494 	    newArray = __ARRAY_NEW_INT(mySize+otherSize);
   495 
   495 
   496             src = self;
   496 	    src = self;
   497             for (dstIdx=0; dstIdx<mySize; dstIdx++) {
   497 	    for (dstIdx=0; dstIdx<mySize; dstIdx++) {
   498                 OBJ el = __ArrayInstPtr(src)->a_element[dstIdx];
   498 		OBJ el = __ArrayInstPtr(src)->a_element[dstIdx];
   499 
   499 
   500                 __ArrayInstPtr(newArray)->a_element[dstIdx] = el;
   500 		__ArrayInstPtr(newArray)->a_element[dstIdx] = el;
   501                 __STORE(newArray, el);
   501 		__STORE(newArray, el);
   502             }
   502 	    }
   503 
   503 
   504             src = aCollection;
   504 	    src = aCollection;
   505             for (srcIdx=0; srcIdx<otherSize; srcIdx++, dstIdx++) {
   505 	    for (srcIdx=0; srcIdx<otherSize; srcIdx++, dstIdx++) {
   506                 OBJ el = __ArrayInstPtr(src)->a_element[srcIdx];
   506 		OBJ el = __ArrayInstPtr(src)->a_element[srcIdx];
   507 
   507 
   508                 __ArrayInstPtr(newArray)->a_element[dstIdx] = el;
   508 		__ArrayInstPtr(newArray)->a_element[dstIdx] = el;
   509                 __STORE(newArray, el);
   509 		__STORE(newArray, el);
   510             }
   510 	    }
   511             RETURN (newArray);
   511 	    RETURN (newArray);
   512         }
   512 	}
   513     }
   513     }
   514 %}.
   514 %}.
   515     ^ super , aCollection
   515     ^ super , aCollection
   516 !
   516 !
   517 
   517 
  1740 
  1740 
  1741 printOn:aStream
  1741 printOn:aStream
  1742     "append a printed representation of the receiver to aStream"
  1742     "append a printed representation of the receiver to aStream"
  1743 
  1743 
  1744     self isLiteral ifTrue:[
  1744     self isLiteral ifTrue:[
  1745         |limit firstOne s|
  1745 	|limit firstOne s|
  1746 
  1746 
  1747         thisContext isRecursive ifTrue:[
  1747 	thisContext isRecursive ifTrue:[
  1748             'Array [error]: printOn: of self referencing collection.' errorPrintCR.
  1748 	    'Array [error]: printOn: of self referencing collection.' errorPrintCR.
  1749             aStream nextPutAll:'#("recursive")'.
  1749 	    aStream nextPutAll:'#("recursive")'.
  1750             ^ self
  1750 	    ^ self
  1751         ].
  1751 	].
  1752 
  1752 
  1753         aStream nextPutAll:'#('.
  1753 	aStream nextPutAll:'#('.
  1754         firstOne := true.
  1754 	firstOne := true.
  1755 
  1755 
  1756         "
  1756 	"
  1757          if aStream is not positionable, create an temporary positionable stream
  1757 	 if aStream is not positionable, create an temporary positionable stream
  1758          (needed for limit calculation)
  1758 	 (needed for limit calculation)
  1759         "
  1759 	"
  1760         aStream isPositionable ifTrue:[
  1760 	aStream isPositionable ifTrue:[
  1761             s := aStream.
  1761 	    s := aStream.
  1762         ] ifFalse:[
  1762 	] ifFalse:[
  1763             s := WriteStream on:(String uninitializedNew:50).
  1763 	    s := WriteStream on:(String uninitializedNew:50).
  1764         ].
  1764 	].
  1765         limit := s position + self maxPrint.
  1765 	limit := s position + self maxPrint.
  1766 
  1766 
  1767         self printElementsDo:[:element |
  1767 	self printElementsDo:[:element |
  1768             firstOne ifFalse:[
  1768 	    firstOne ifFalse:[
  1769                 s space
  1769 		s space
  1770             ] ifTrue:[
  1770 	    ] ifTrue:[
  1771                 firstOne := false
  1771 		firstOne := false
  1772             ].
  1772 	    ].
  1773             (s position >= limit) ifTrue:[
  1773 	    (s position >= limit) ifTrue:[
  1774                 s ~~ aStream ifTrue:[
  1774 		s ~~ aStream ifTrue:[
  1775                     aStream nextPutAll:(s contents).
  1775 		    aStream nextPutAll:(s contents).
  1776                 ].
  1776 		].
  1777                 aStream nextPutAll:'...etc...)'.
  1777 		aStream nextPutAll:'...etc...)'.
  1778                 ^ self
  1778 		^ self
  1779             ] ifFalse:[
  1779 	    ] ifFalse:[
  1780                 element printOn:s.
  1780 		element printOn:s.
  1781             ].
  1781 	    ].
  1782         ].
  1782 	].
  1783         s ~~ aStream ifTrue:[
  1783 	s ~~ aStream ifTrue:[
  1784             aStream nextPutAll:(s contents).
  1784 	    aStream nextPutAll:(s contents).
  1785         ].
  1785 	].
  1786         aStream nextPut:$)
  1786 	aStream nextPut:$)
  1787     ] ifFalse:[
  1787     ] ifFalse:[
  1788         super printOn:aStream
  1788 	super printOn:aStream
  1789     ]
  1789     ]
  1790 
  1790 
  1791     "
  1791     "
  1792      #(1 2 $a 'hello' sym kewordSymbol:with: #'funny symbol') printString
  1792      #(1 2 $a 'hello' sym kewordSymbol:with: #'funny symbol') printString
  1793      #(1 2 $a [1 2 3] true false nil #true #false #nil) printString
  1793      #(1 2 $a [1 2 3] true false nil #true #false #nil) printString
  1841 
  1841 
  1842 basicSize
  1842 basicSize
  1843     "return the number of indexed elements in the receiver"
  1843     "return the number of indexed elements in the receiver"
  1844 
  1844 
  1845 %{  /* NOCONTEXT */
  1845 %{  /* NOCONTEXT */
       
  1846 #ifdef __SCHTEAM__
       
  1847     return context._RETURN (STInteger._new(self.basicSize()));
       
  1848 #else
  1846     REGISTER OBJ slf = self;
  1849     REGISTER OBJ slf = self;
  1847 
  1850 
  1848     RETURN ( __mkSmallInteger(__arraySize(slf) - __intVal(__ClassInstPtr(__qClass(slf))->c_ninstvars) ));
  1851     RETURN ( __mkSmallInteger(__arraySize(slf) - __intVal(__ClassInstPtr(__qClass(slf))->c_ninstvars) ));
       
  1852 #endif
  1849 %}
  1853 %}
  1850 !
  1854 !
  1851 
  1855 
  1852 refersToLiteral:aLiteral
  1856 refersToLiteral:aLiteral
  1853     "return true if the receiver or recursively any array element in the
  1857     "return true if the receiver or recursively any array element in the
  1854      receiver refers to aLiteral (i.e. a deep search)"
  1858      receiver refers to aLiteral (i.e. a deep search)"
  1855 
  1859 
  1856     self do: [ :el |
  1860     self do: [ :el |
  1857         el == aLiteral ifTrue:[^true].
  1861 	el == aLiteral ifTrue:[^true].
  1858         el class == Array ifTrue:[
  1862 	el class == Array ifTrue:[
  1859             (el refersToLiteral: aLiteral) ifTrue: [^true]
  1863 	    (el refersToLiteral: aLiteral) ifTrue: [^true]
  1860         ]
  1864 	]
  1861     ].
  1865     ].
  1862     ^ false
  1866     ^ false
  1863 
  1867 
  1864     "
  1868     "
  1865      #(1 2 3) refersToLiteral:#foo
  1869      #(1 2 3) refersToLiteral:#foo
  1898      (which we can do here, since when arriving here, #size is obviously not
  1902      (which we can do here, since when arriving here, #size is obviously not
  1899       redefined in a subclass).
  1903       redefined in a subclass).
  1900      This method is the same as basicSize."
  1904      This method is the same as basicSize."
  1901 
  1905 
  1902 %{  /* NOCONTEXT */
  1906 %{  /* NOCONTEXT */
       
  1907 #ifdef __SCHTEAM__
       
  1908     return context._RETURN (STInteger._new(self.basicSize()));
       
  1909 #else
  1903     REGISTER OBJ slf = self;
  1910     REGISTER OBJ slf = self;
  1904 
  1911 
  1905     RETURN ( __mkSmallInteger(__arraySize(slf) - __intVal(__ClassInstPtr(__qClass(slf))->c_ninstvars) ));
  1912     RETURN ( __mkSmallInteger(__arraySize(slf) - __intVal(__ClassInstPtr(__qClass(slf))->c_ninstvars) ));
       
  1913 #endif
  1906 %}
  1914 %}
  1907 ! !
  1915 ! !
  1908 
  1916 
  1909 !Array methodsFor:'searching'!
  1917 !Array methodsFor:'searching'!
  1910 
  1918 
  2631 ! !
  2639 ! !
  2632 
  2640 
  2633 !Array class methodsFor:'documentation'!
  2641 !Array class methodsFor:'documentation'!
  2634 
  2642 
  2635 version
  2643 version
  2636     ^ '$Header: /cvs/stx/stx/libbasic/Array.st,v 1.165 2015-02-03 13:54:47 stefan Exp $'
  2644     ^ '$Header: /cvs/stx/stx/libbasic/Array.st,v 1.166 2015-04-20 14:04:23 cg Exp $'
  2637 !
  2645 !
  2638 
  2646 
  2639 version_CVS
  2647 version_CVS
  2640     ^ '$Header: /cvs/stx/stx/libbasic/Array.st,v 1.165 2015-02-03 13:54:47 stefan Exp $'
  2648     ^ '$Header: /cvs/stx/stx/libbasic/Array.st,v 1.166 2015-04-20 14:04:23 cg Exp $'
  2641 ! !
  2649 ! !
  2642