93 the argument" |
95 the argument" |
94 |
96 |
95 |newCollection| |
97 |newCollection| |
96 |
98 |
97 newCollection := self species new:(self size + aCollection size). |
99 newCollection := self species new:(self size + aCollection size). |
|
100 newCollection finalizeCopyFrom:self. |
98 self do:[:element | |
101 self do:[:element | |
99 newCollection add:element |
102 newCollection add:element |
100 ]. |
103 ]. |
101 aCollection do:[:element | |
104 aCollection do:[:element | |
102 newCollection add:element |
105 newCollection add:element |
103 ]. |
106 ]. |
104 ^ newCollection |
107 ^ newCollection |
105 |
108 |
106 "#(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection" |
109 " |
|
110 #(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection |
|
111 #(1 2 3) asSortedCollection , #(99 101 100) asSortedCollection |
|
112 " |
107 ! |
113 ! |
108 |
114 |
109 copyWith:newElement |
115 copyWith:newElement |
110 "return a new collection consisting of a copy of the receivers elements |
116 "return a new collection consisting of a copy of the receivers elements |
111 plus the argument." |
117 plus the argument." |
112 |
118 |
113 |newCollection mySize newSize| |
119 |newCollection mySize newSize| |
114 |
120 |
115 mySize := self size. |
121 mySize := self size. |
116 newSize := mySize + 1. |
122 newSize := mySize + 1. |
117 newCollection := self species new:newSize. |
123 "/ newCollection := self species new:newSize. |
|
124 newCollection := (self species new:newSize) finalizeCopyFrom:self. |
118 newCollection grow:newSize. |
125 newCollection grow:newSize. |
119 newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
126 newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
120 newCollection at:newSize put:newElement. |
127 newCollection at:newSize put:newElement. |
121 ^newCollection |
128 ^newCollection |
122 |
129 |
123 "#(1 2 3 4 5) copyWith:$a" |
130 " |
124 "'abcdefg' copyWith:$h" |
131 #(1 2 3 4 5) copyWith:$a |
125 "'abcdefg' copyWith:'123'" "-- will fail: string cannot be stored into string" |
132 'abcdefg' copyWith:$h |
126 "'abcdefg' copyWith:1" "-- will fail: integer cannot be stored into string" |
133 'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
134 'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
135 " |
127 ! |
136 ! |
128 |
137 |
129 copy |
138 copy |
130 "return a new OrderedCollection containing the elements of the receiver." |
139 "return a new OrderedCollection containing the elements of the receiver." |
131 |
140 |
283 contentsArray at:firstIndex put:anObject. |
293 contentsArray at:firstIndex put:anObject. |
284 ^ anObject |
294 ^ anObject |
285 |
295 |
286 " |
296 " |
287 |c| |
297 |c| |
288 c := #(4 3 2 1) asOrderedCollection. |
298 c := #(1 2 3 4) asOrderedCollection. |
289 c addFirst:'here' |
299 c addFirst:'here'. |
|
300 c |
|
301 " |
|
302 |
|
303 " |
|
304 |c| |
|
305 c := #() asOrderedCollection. |
|
306 c addFirst:'here'. |
|
307 c |
290 " |
308 " |
291 ! |
309 ! |
292 |
310 |
293 add:anObject beforeIndex:index |
311 add:anObject beforeIndex:index |
294 "insert the argument, anObject to become located at index. |
312 "insert the argument, anObject to become located at index. |
558 newSize "{ Class:SmallInteger }" |
576 newSize "{ Class:SmallInteger }" |
559 startIndex "{ Class:SmallInteger }" |
577 startIndex "{ Class:SmallInteger }" |
560 sz "{ Class:SmallInteger }"| |
578 sz "{ Class:SmallInteger }"| |
561 |
579 |
562 oldSize := contentsArray size. |
580 oldSize := contentsArray size. |
|
581 oldSize == 0 ifTrue:[ |
|
582 contentsArray := Array new:3. |
|
583 firstIndex := 2. lastIndex := 1. |
|
584 ^ self |
|
585 ]. |
|
586 |
563 sz := self size. |
587 sz := self size. |
564 |
588 |
565 "if there is lots of room at the end (> 50%), shift instead of growing" |
589 "if there is lots of room at the end (> 50%), shift instead of growing" |
566 oldSize > (sz * 2) ifTrue:[ |
590 oldSize > (sz * 2) ifTrue:[ |
567 startIndex := oldSize // 4. |
591 startIndex := oldSize // 4. |
756 |
780 |
757 |newCollection |
781 |newCollection |
758 start "{ Class:SmallInteger }" |
782 start "{ Class:SmallInteger }" |
759 stop "{ Class:SmallInteger }" | |
783 stop "{ Class:SmallInteger }" | |
760 |
784 |
761 newCollection := (self species new). |
785 "/ newCollection := (self species new). |
|
786 newCollection := (self species new) finalizeCopyFrom:self. |
762 stop := lastIndex. |
787 stop := lastIndex. |
763 start := firstIndex. |
788 start := firstIndex. |
764 start to:stop do:[:index | |
789 start to:stop do:[:index | |
765 newCollection add:(aBlock value:(contentsArray at:index)). |
790 newCollection add:(aBlock value:(contentsArray at:index)). |
766 ]. |
791 ]. |