Refactored structure and accessors for arrayed objects (at the VM level)
[panda.git] / st / SequenceableCollection.st
blob17414edd81a5903fc9625e04bbef919efc396731
3 "copying"
5 SequenceableCollection method!
6 , aCollection
7         | collection |
9         collection := self species new: (self size + aCollection size).
11         collection replaceFrom: 1
12                            to: self size
13                            with: self
14                            startingAt: 1.
16         collection replaceFrom: self size + 1
17                            to: collection size
18                            with: aCollection
19                            startingAt: 1.
21         ^ collection!
24 "comparing"
26 SequenceableCollection method!
27 = aCollection
28         | i |
29         
30         self size = aCollection size
31                 ifFalse: [ ^ false ].
33         i := 1.
34         self do: [ :element |
35                 element = (aCollection at: i) ifFalse: [ ^ false ].
36                 i := i + 1 ].
38         ^ true!
40 SequenceableCollection method!
41 hash
42         | hash |
44         hash := 11111111111111111.
46         self do: [ :element |
47                 hash := hash bitXor: ((hash bitShift: 5) + element hash + (hash bitShift: -2))].
49         ^ hash!
51 "accessing"
53 SequenceableCollection method!
54 size
55         self subclassResponsibility!
57 SequenceableCollection method!
58 replaceFrom: start to: stop with: replacement startingAt: repStart 
59         "This destructively replaces elements from start to stop in the receiver 
60         starting at index, repStart, in the sequenceable collection, 
61         replacementCollection. Answer the receiver. No range checks are 
62         performed."
64         | index repOff |
65         repOff := repStart - start.
66         index := start - 1.
67         [(index := index + 1) <= stop]
68                 whileTrue: [self at: index put: (replacement at: repOff + index)]!
71 SequenceableCollection method!
72 first
73         ^ self at: 1!
75 SequenceableCollection method!
76 second
77         ^ self at: 2!
79 SequenceableCollection method!
80 third
81         ^ self at: 3!
83 SequenceableCollection method!
84 last
85         ^ self at: self size!
88 "removing "
90 SequenceableCollection method!
91 remove: anObject ifAbsent: anExceptionBlock
92         self shouldNotImplement!
95 "enumerating"
97 SequenceableCollection method!
98 do: aBlock
99         | index length |
100         index := 0.
101         length := self size.
102         [(index := index + 1) <= length]
103                 whileTrue: [ aBlock value: (self at: index) ].!
105 SequenceableCollection method!
106 collect: aBlock
107         | aStream index length |
108         aStream := WriteStream on: (self species new: self size).
109         index := 0.
110         length := self size.
111         [(index := index + 1) <= length]
112                 whileTrue: [aStream nextPut: (aBlock value: (self at: index))].
113         ^ aStream contents!
115 SequenceableCollection method!
116 select: aBlock
117         | aStream index length |
118         aStream := WriteStream on: (self species new: self size).
119         index := 0.
120         length := self size.
121         [(index := index + 1) <= length]
122                 whileTrue: [(aBlock value: (self at: index))
123                                                 ifTrue: [aStream nextPut: (self at: index)]]. 
124         ^ aStream contents!
127 "copying"
129 SequenceableCollection method!
130 copyFrom: start to: stop 
131         "Answer a copy of a subset of the receiver, starting from element at 
132         index start until element at index stop."
134         | newSize |
135         newSize := stop - start + 1.
136         ^ (self species new: newSize)
137                 replaceFrom: 1
138                 to: newSize
139                 with: self
140                 startingAt: start!
143 "converting"
144 SequenceableCollection method!
145 reversed
146         "Answer a copy of the receiver with element order reversed."
147         "Example: 'frog' reversed"
149         | n result src |
150         n := self size.
151         result := self species new: n.
152         src := n + 1.
153         1 to: n do: [:i | result at: i put: (self at: (src := src - 1))].
154         ^ result!