Skip to content

Commit 2989d8f

Browse files
authored
Merge pull request #34 from HossamSaberr/add-columns-do
Feat: Add full column iteration and directional navigation paths to CTArray2D
2 parents 1442262 + 389e05c commit 2989d8f

File tree

2 files changed

+157
-25
lines changed

2 files changed

+157
-25
lines changed

src/Containers-Array2D-Tests/CTArray2DTest.class.st

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ CTArray2DTest >> testCannotAccessWithWrongCoordinates [
136136
self should: [ self arrayClass width2Height3 atColumn: 6 atRow: 1 ] raise: SubscriptOutOfBounds
137137
]
138138

139+
{ #category : 'tests' }
140+
CTArray2DTest >> testColumns [
141+
| foo |
142+
foo := self arrayClass width2Height3.
143+
self assert: foo columns equals: #(#(1 3 5) #(2 4 6))
144+
]
145+
146+
{ #category : 'tests' }
147+
CTArray2DTest >> testColumnsDo [
148+
| foo array columnIndex |
149+
foo := self arrayClass width2Height3.
150+
array := Array new: 2.
151+
columnIndex := 1.
152+
foo columnsDo: [ :column |
153+
array at: columnIndex put: column.
154+
columnIndex := columnIndex + 1 ].
155+
self assert: array equals: #(#(1 3 5) #(2 4 6))
156+
]
157+
139158
{ #category : 'tests-copying' }
140159
CTArray2DTest >> testCopy [
141160
| foo cop |
@@ -211,6 +230,78 @@ CTArray2DTest >> testFromArray [
211230

212231
]
213232

233+
{ #category : 'tests-enumeration' }
234+
CTArray2DTest >> testFromBottomToTopFromLeftToRightDo [
235+
|foo res|
236+
foo := self arrayClass width2Height3.
237+
res := OrderedCollection new.
238+
foo fromBottomToTopFromLeftToRightDo: [ :each | res add: each ].
239+
self assert: res equals: #(5 3 1 6 4 2) asOrderedCollection
240+
]
241+
242+
{ #category : 'tests-enumeration' }
243+
CTArray2DTest >> testFromBottomToTopFromRightToLeftDo [
244+
|foo res|
245+
foo := self arrayClass width2Height3.
246+
res := OrderedCollection new.
247+
foo fromBottomToTopFromRightToLeftDo: [ :each | res add: each ].
248+
self assert: res equals: #(6 4 2 5 3 1) asOrderedCollection
249+
]
250+
251+
{ #category : 'tests-enumeration' }
252+
CTArray2DTest >> testFromLeftToRightFromBottomToTopDo [
253+
|foo res|
254+
foo := self arrayClass width2Height3.
255+
res := OrderedCollection new.
256+
foo fromLeftToRightFromBottomToTopDo: [ :each | res add: each ].
257+
self assert: res equals: #(5 6 3 4 1 2) asOrderedCollection
258+
]
259+
260+
{ #category : 'tests-enumeration' }
261+
CTArray2DTest >> testFromLeftToRightFromTopToBottomDo [
262+
|foo res|
263+
foo := self arrayClass width2Height3.
264+
res := OrderedCollection new.
265+
foo fromLeftToRightFromTopToBottomDo: [ :each | res add: each ].
266+
self assert: res equals: #(1 2 3 4 5 6) asOrderedCollection
267+
]
268+
269+
{ #category : 'tests-enumeration' }
270+
CTArray2DTest >> testFromRightToLeftFromBottomToTopDo [
271+
|foo res|
272+
foo := self arrayClass width2Height3.
273+
res := OrderedCollection new.
274+
foo fromRightToLeftFromBottomToTopDo: [ :each | res add: each ].
275+
self assert: res equals: #(6 5 4 3 2 1) asOrderedCollection
276+
]
277+
278+
{ #category : 'tests-enumeration' }
279+
CTArray2DTest >> testFromRightToLeftFromTopToBottomDo [
280+
|foo res|
281+
foo := self arrayClass width2Height3.
282+
res := OrderedCollection new.
283+
foo fromRightToLeftFromTopToBottomDo: [ :each | res add: each ].
284+
self assert: res equals: #(2 1 4 3 6 5) asOrderedCollection
285+
]
286+
287+
{ #category : 'tests-enumeration' }
288+
CTArray2DTest >> testFromTopToBottomFromLeftToRightDo [
289+
|foo res|
290+
foo := self arrayClass width2Height3.
291+
res := OrderedCollection new.
292+
foo fromTopToBottomFromLeftToRightDo: [ :each | res add: each ].
293+
self assert: res equals: #(1 3 5 2 4 6) asOrderedCollection
294+
]
295+
296+
{ #category : 'tests-enumeration' }
297+
CTArray2DTest >> testFromTopToBottomFromRightToLeftDo [
298+
|foo res|
299+
foo := self arrayClass width2Height3.
300+
res := OrderedCollection new.
301+
foo fromTopToBottomFromRightToLeftDo: [ :each | res add: each ].
302+
self assert: res equals: #(2 4 6 1 3 5) asOrderedCollection
303+
]
304+
214305
{ #category : 'tests-accessing' }
215306
CTArray2DTest >> testHeight [
216307

@@ -225,17 +316,6 @@ CTArray2DTest >> testIndexXY [
225316
self assert: (foo indexX: 2 y: 3) equals: (3-1)*(foo width)+2
226317
]
227318

228-
{ #category : 'tests' }
229-
CTArray2DTest >> testLeftToRightFromBottomToTopDo [
230-
231-
| a2d res |
232-
a2d := CTArray2D fromArray: #(1 2 3 4 5 6) width: 3.
233-
res := OrderedCollection new.
234-
a2d leftToRightFromBottomToTopDo: [ :each | res add: each ].
235-
self assert: res equals: #(4 5 6 1 2 3 ) asOrderedCollection
236-
237-
]
238-
239319
{ #category : 'tests-printing' }
240320
CTArray2DTest >> testPrinting [
241321

src/Containers-Array2D/CTArray2D.class.st

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ CTArray2D >> atX: x atY: y put: value [
225225
^ contents at: (self indexX: x y: y) put: value
226226
]
227227

228+
{ #category : 'converting' }
229+
CTArray2D >> columns [
230+
^ (1 to: self width) collect: [ :columnIndex | self atColumn: columnIndex ]
231+
]
232+
233+
{ #category : 'enumeration' }
234+
CTArray2D >> columnsDo: aBlock [
235+
1 to: self width do: [ :i | aBlock value: (self atColumn: i) ]
236+
]
237+
228238
{ #category : 'private' }
229239
CTArray2D >> contents [
230240

@@ -255,6 +265,62 @@ CTArray2D >> extent: extent fromArray: anArray [
255265
contents := anArray
256266
]
257267

268+
{ #category : 'enumerating' }
269+
CTArray2D >> fromBottomToTopFromLeftToRightDo: aBlock [
270+
1 to: self width do: [:col |
271+
self height to: 1 by: -1 do: [:row |
272+
aBlock value: (self atColumn: col atRow: row)]]
273+
]
274+
275+
{ #category : 'enumerating' }
276+
CTArray2D >> fromBottomToTopFromRightToLeftDo: aBlock [
277+
self width to: 1 by: -1 do: [:col |
278+
self height to: 1 by: -1 do: [:row |
279+
aBlock value: (self atColumn: col atRow: row)]]
280+
]
281+
282+
{ #category : 'enumerating' }
283+
CTArray2D >> fromLeftToRightFromBottomToTopDo: aBlock [
284+
self height to: 1 by: -1 do: [:row |
285+
1 to: self width do: [:col |
286+
aBlock value: (self atColumn: col atRow: row)]]
287+
]
288+
289+
{ #category : 'enumerating' }
290+
CTArray2D >> fromLeftToRightFromTopToBottomDo: aBlock [
291+
1 to: self height do: [ :row |
292+
1 to: self width do: [ :col |
293+
aBlock value: (self atColumn: col atRow: row) ] ]
294+
]
295+
296+
{ #category : 'enumerating' }
297+
CTArray2D >> fromRightToLeftFromBottomToTopDo: aBlock [
298+
self height to: 1 by: -1 do: [:row |
299+
self width to: 1 by: -1 do: [:col |
300+
aBlock value: (self atColumn: col atRow: row)]]
301+
]
302+
303+
{ #category : 'enumerating' }
304+
CTArray2D >> fromRightToLeftFromTopToBottomDo: aBlock [
305+
1 to: self height do: [:row |
306+
self width to: 1 by: -1 do: [:col |
307+
aBlock value: (self atColumn: col atRow: row)]]
308+
]
309+
310+
{ #category : 'enumerating' }
311+
CTArray2D >> fromTopToBottomFromLeftToRightDo: aBlock [
312+
1 to: self width do: [:col |
313+
1 to: self height do: [:row |
314+
aBlock value: (self atColumn: col atRow: row)]]
315+
]
316+
317+
{ #category : 'enumerating' }
318+
CTArray2D >> fromTopToBottomFromRightToLeftDo: aBlock [
319+
self width to: 1 by: -1 do: [:col |
320+
1 to: self height do: [:row |
321+
aBlock value: (self atColumn: col atRow: row)]]
322+
]
323+
258324
{ #category : 'comparing' }
259325
CTArray2D >> hash [
260326

@@ -287,20 +353,6 @@ CTArray2D >> isSelfEvaluating [
287353
^ self class == CTArray2D and: [ contents isSelfEvaluating ]
288354
]
289355

290-
{ #category : 'enumerating' }
291-
CTArray2D >> leftToRightFromBottomToTopDo: aBlock [
292-
"Apply a block to each element following that order left to right but from bottom to top"
293-
"123
294-
456
295-
=>
296-
456123
297-
"
298-
299-
self height to: 1 by: -1 do: [:row |
300-
1 to: self width do: [:col |
301-
aBlock value: (self atColumn: col atRow: row)]]
302-
]
303-
304356
{ #category : 'accessing - compatibility' }
305357
CTArray2D >> numberOfColumns [
306358
"Answer the receiver's width, i.e., its number of x"

0 commit comments

Comments
 (0)