Array.st
branchjv
changeset 18261 22bdfc405bca
parent 18257 877a8f1b326d
parent 18246 d4d749c60f22
child 18274 042d13555f1f
equal deleted inserted replaced
18260:e55af242e134 18261:22bdfc405bca
    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 
   450     "return the receiver as an array - that's the receiver itself.
   450     "return the receiver as an array - that's the receiver itself.
   451      Notice: Use asNewArray, if you intent to modify the returned collection."
   451      Notice: Use asNewArray, if you intent to modify the returned collection."
   452 
   452 
   453     "could be an instance of a subclass..."
   453     "could be an instance of a subclass..."
   454     self class == Array ifTrue:[
   454     self class == Array ifTrue:[
   455         ^ self
   455 	^ self
   456     ].
   456     ].
   457     ^ super asArray
   457     ^ super asArray
   458 
   458 
   459     "Modified: 22.4.1996 / 12:42:09 / cg"
   459     "Modified: 22.4.1996 / 12:42:09 / cg"
   460 !
   460 !
   471 asNewArray
   471 asNewArray
   472     "return the receiver as an unique new array."
   472     "return the receiver as an unique new array."
   473 
   473 
   474     "could be an instance of a subclass..."
   474     "could be an instance of a subclass..."
   475     self class == Array ifTrue:[
   475     self class == Array ifTrue:[
   476         ^ self copy
   476 	^ self copy
   477     ].
   477     ].
   478     ^ super asArray
   478     ^ super asArray
   479 !
   479 !
   480 
   480 
   481 beImmutable
   481 beImmutable
   489 !Array methodsFor:'copying'!
   489 !Array methodsFor:'copying'!
   490 
   490 
   491 , aCollection
   491 , aCollection
   492 %{
   492 %{
   493     if (__isArray(aCollection)) {
   493     if (__isArray(aCollection)) {
   494         if (__isArray(self)) {
   494 	if (__isArray(self)) {
   495             OBJ newArray;
   495 	    OBJ newArray;
   496             int mySize = __arraySize(self);
   496 	    int mySize = __arraySize(self);
   497             int otherSize = __arraySize(aCollection);
   497 	    int otherSize = __arraySize(aCollection);
   498             REGISTER OBJ src;
   498 	    REGISTER OBJ src;
   499             int srcIdx, dstIdx;
   499 	    int srcIdx, dstIdx;
   500             newArray = __ARRAY_NEW_INT(mySize+otherSize);
   500 	    newArray = __ARRAY_NEW_INT(mySize+otherSize);
   501 
   501 
   502             src = self;
   502 	    src = self;
   503             for (dstIdx=0; dstIdx<mySize; dstIdx++) {
   503 	    for (dstIdx=0; dstIdx<mySize; dstIdx++) {
   504                 OBJ el = __ArrayInstPtr(src)->a_element[dstIdx];
   504 		OBJ el = __ArrayInstPtr(src)->a_element[dstIdx];
   505 
   505 
   506                 __ArrayInstPtr(newArray)->a_element[dstIdx] = el;
   506 		__ArrayInstPtr(newArray)->a_element[dstIdx] = el;
   507                 __STORE(newArray, el);
   507 		__STORE(newArray, el);
   508             }
   508 	    }
   509 
   509 
   510             src = aCollection;
   510 	    src = aCollection;
   511             for (srcIdx=0; srcIdx<otherSize; srcIdx++, dstIdx++) {
   511 	    for (srcIdx=0; srcIdx<otherSize; srcIdx++, dstIdx++) {
   512                 OBJ el = __ArrayInstPtr(src)->a_element[srcIdx];
   512 		OBJ el = __ArrayInstPtr(src)->a_element[srcIdx];
   513 
   513 
   514                 __ArrayInstPtr(newArray)->a_element[dstIdx] = el;
   514 		__ArrayInstPtr(newArray)->a_element[dstIdx] = el;
   515                 __STORE(newArray, el);
   515 		__STORE(newArray, el);
   516             }
   516 	    }
   517             RETURN (newArray);
   517 	    RETURN (newArray);
   518         }
   518 	}
   519     }
   519     }
   520 %}.
   520 %}.
   521     ^ super , aCollection
   521     ^ super , aCollection
   522 !
   522 !
   523 
   523 
  1746 
  1746 
  1747 printOn:aStream
  1747 printOn:aStream
  1748     "append a printed representation of the receiver to aStream"
  1748     "append a printed representation of the receiver to aStream"
  1749 
  1749 
  1750     self isLiteral ifTrue:[
  1750     self isLiteral ifTrue:[
  1751         |limit firstOne s|
  1751 	|limit firstOne s|
  1752 
  1752 
  1753         thisContext isRecursive ifTrue:[
  1753 	thisContext isRecursive ifTrue:[
  1754             'Array [error]: printOn: of self referencing collection.' errorPrintCR.
  1754 	    'Array [error]: printOn: of self referencing collection.' errorPrintCR.
  1755             aStream nextPutAll:'#("recursive")'.
  1755 	    aStream nextPutAll:'#("recursive")'.
  1756             ^ self
  1756 	    ^ self
  1757         ].
  1757 	].
  1758 
  1758 
  1759         aStream nextPutAll:'#('.
  1759 	aStream nextPutAll:'#('.
  1760         firstOne := true.
  1760 	firstOne := true.
  1761 
  1761 
  1762         "
  1762 	"
  1763          if aStream is not positionable, create an temporary positionable stream
  1763 	 if aStream is not positionable, create an temporary positionable stream
  1764          (needed for limit calculation)
  1764 	 (needed for limit calculation)
  1765         "
  1765 	"
  1766         aStream isPositionable ifTrue:[
  1766 	aStream isPositionable ifTrue:[
  1767             s := aStream.
  1767 	    s := aStream.
  1768         ] ifFalse:[
  1768 	] ifFalse:[
  1769             s := WriteStream on:(String uninitializedNew:50).
  1769 	    s := WriteStream on:(String uninitializedNew:50).
  1770         ].
  1770 	].
  1771         limit := s position + self maxPrint.
  1771 	limit := s position + self maxPrint.
  1772 
  1772 
  1773         self printElementsDo:[:element |
  1773 	self printElementsDo:[:element |
  1774             firstOne ifFalse:[
  1774 	    firstOne ifFalse:[
  1775                 s space
  1775 		s space
  1776             ] ifTrue:[
  1776 	    ] ifTrue:[
  1777                 firstOne := false
  1777 		firstOne := false
  1778             ].
  1778 	    ].
  1779             (s position >= limit) ifTrue:[
  1779 	    (s position >= limit) ifTrue:[
  1780                 s ~~ aStream ifTrue:[
  1780 		s ~~ aStream ifTrue:[
  1781                     aStream nextPutAll:(s contents).
  1781 		    aStream nextPutAll:(s contents).
  1782                 ].
  1782 		].
  1783                 aStream nextPutAll:'...etc...)'.
  1783 		aStream nextPutAll:'...etc...)'.
  1784                 ^ self
  1784 		^ self
  1785             ] ifFalse:[
  1785 	    ] ifFalse:[
  1786                 element printOn:s.
  1786 		element printOn:s.
  1787             ].
  1787 	    ].
  1788         ].
  1788 	].
  1789         s ~~ aStream ifTrue:[
  1789 	s ~~ aStream ifTrue:[
  1790             aStream nextPutAll:(s contents).
  1790 	    aStream nextPutAll:(s contents).
  1791         ].
  1791 	].
  1792         aStream nextPut:$)
  1792 	aStream nextPut:$)
  1793     ] ifFalse:[
  1793     ] ifFalse:[
  1794         super printOn:aStream
  1794 	super printOn:aStream
  1795     ]
  1795     ]
  1796 
  1796 
  1797     "
  1797     "
  1798      #(1 2 $a 'hello' sym kewordSymbol:with: #'funny symbol') printString
  1798      #(1 2 $a 'hello' sym kewordSymbol:with: #'funny symbol') printString
  1799      #(1 2 $a [1 2 3] true false nil #true #false #nil) printString
  1799      #(1 2 $a [1 2 3] true false nil #true #false #nil) printString
  1847 
  1847 
  1848 basicSize
  1848 basicSize
  1849     "return the number of indexed elements in the receiver"
  1849     "return the number of indexed elements in the receiver"
  1850 
  1850 
  1851 %{  /* NOCONTEXT */
  1851 %{  /* NOCONTEXT */
       
  1852 #ifdef __SCHTEAM__
       
  1853     return context._RETURN (STInteger._new(self.basicSize()));
       
  1854 #else
  1852     REGISTER OBJ slf = self;
  1855     REGISTER OBJ slf = self;
  1853 
  1856 
  1854     RETURN ( __mkSmallInteger(__arraySize(slf) - __intVal(__ClassInstPtr(__qClass(slf))->c_ninstvars) ));
  1857     RETURN ( __mkSmallInteger(__arraySize(slf) - __intVal(__ClassInstPtr(__qClass(slf))->c_ninstvars) ));
       
  1858 #endif
  1855 %}
  1859 %}
  1856 !
  1860 !
  1857 
  1861 
  1858 refersToLiteral:aLiteral
  1862 refersToLiteral:aLiteral
  1859     "return true if the receiver or recursively any array element in the
  1863     "return true if the receiver or recursively any array element in the
  1860      receiver refers to aLiteral (i.e. a deep search)"
  1864      receiver refers to aLiteral (i.e. a deep search)"
  1861 
  1865 
  1862     self do: [ :el |
  1866     self do: [ :el |
  1863         el == aLiteral ifTrue:[^true].
  1867 	el == aLiteral ifTrue:[^true].
  1864         el class == Array ifTrue:[
  1868 	el class == Array ifTrue:[
  1865             (el refersToLiteral: aLiteral) ifTrue: [^true]
  1869 	    (el refersToLiteral: aLiteral) ifTrue: [^true]
  1866         ]
  1870 	]
  1867     ].
  1871     ].
  1868     ^ false
  1872     ^ false
  1869 
  1873 
  1870     "
  1874     "
  1871      #(1 2 3) refersToLiteral:#foo
  1875      #(1 2 3) refersToLiteral:#foo
  1904      (which we can do here, since when arriving here, #size is obviously not
  1908      (which we can do here, since when arriving here, #size is obviously not
  1905       redefined in a subclass).
  1909       redefined in a subclass).
  1906      This method is the same as basicSize."
  1910      This method is the same as basicSize."
  1907 
  1911 
  1908 %{  /* NOCONTEXT */
  1912 %{  /* NOCONTEXT */
       
  1913 #ifdef __SCHTEAM__
       
  1914     return context._RETURN (STInteger._new(self.basicSize()));
       
  1915 #else
  1909     REGISTER OBJ slf = self;
  1916     REGISTER OBJ slf = self;
  1910 
  1917 
  1911     RETURN ( __mkSmallInteger(__arraySize(slf) - __intVal(__ClassInstPtr(__qClass(slf))->c_ninstvars) ));
  1918     RETURN ( __mkSmallInteger(__arraySize(slf) - __intVal(__ClassInstPtr(__qClass(slf))->c_ninstvars) ));
       
  1919 #endif
  1912 %}
  1920 %}
  1913 ! !
  1921 ! !
  1914 
  1922 
  1915 !Array methodsFor:'searching'!
  1923 !Array methodsFor:'searching'!
  1916 
  1924 
  2637 ! !
  2645 ! !
  2638 
  2646 
  2639 !Array class methodsFor:'documentation'!
  2647 !Array class methodsFor:'documentation'!
  2640 
  2648 
  2641 version
  2649 version
  2642     ^ '$Header: /cvs/stx/stx/libbasic/Array.st,v 1.165 2015-02-03 13:54:47 stefan Exp $'
  2650     ^ '$Header: /cvs/stx/stx/libbasic/Array.st,v 1.166 2015-04-20 14:04:23 cg Exp $'
  2643 !
  2651 !
  2644 
  2652 
  2645 version_CVS
  2653 version_CVS
  2646     ^ '$Header: /cvs/stx/stx/libbasic/Array.st,v 1.165 2015-02-03 13:54:47 stefan Exp $'
  2654     ^ '$Header: /cvs/stx/stx/libbasic/Array.st,v 1.166 2015-04-20 14:04:23 cg Exp $'
  2647 ! !
  2655 ! !
  2648