Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- implements #rowsAndColumnsPut: in CTArray2D
- implements #width:height:tabulate: in CTArray2D class
- implements #testRowsAndColumnsPut: and #testWidthHeightTabulate in CTArray2DTest
  • Loading branch information
n1toxyz committed Dec 20, 2022
1 parent 9a6eb60 commit 96f1bff
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Containers-Array2D-Tests/CTArray2DTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,21 @@ CTArray2DTest >> testRowsAndColumnsDo [
foo rowsAndColumnsDo: [ :y :x | self assert: (foo atColumn: x atRow: y) equals: (array at: (y - 1) * 2 + x) ]
]

{ #category : #'tests-enumeration' }
CTArray2DTest >> testRowsAndColumnsPut [

| foo array |
foo := self arrayClass width: 2 height: 3.
self
shouldnt: [ foo rowsAndColumnsPut: [ :row :col | row * 10 + col ] ]
raise: Error.
array := #( 11 12 21 22 31 32 ).
foo rowsAndColumnsDo: [ :y :x |
self
assert: (foo atColumn: x atRow: y)
equals: (array at: y - 1 * 2 + x) ]
]

{ #category : #'tests-private' }
CTArray2DTest >> testSetContents [
| foo |
Expand Down Expand Up @@ -290,6 +305,21 @@ CTArray2DTest >> testWidth [
equals: 5
]

{ #category : #'tests-enumeration' }
CTArray2DTest >> testWidthHeightTabulate [

| foo array |
foo := self arrayClass
width: 2
height: 3
tabulate: [ :x :y | y * 10 + x ].
array := #( 11 12 21 22 31 32 ).
foo rowsAndColumnsDo: [ :y :x |
self
assert: (foo atColumn: x atRow: y)
equals: (array at: y - 1 * 2 + x) ]
]

{ #category : #'tests-private' }
CTArray2DTest >> testWidthHeightType [
| foo |
Expand Down
26 changes: 26 additions & 0 deletions src/Containers-Array2D/CTArray2D.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ CTArray2D class >> width: width height: height [
^ self basicNew width: width height: height type: Array
]

{ #category : #'instance creation' }
CTArray2D class >> width: width height: height tabulate: aTwoArgumentBlock [

"Answer a new CTArray2D of the given dimensions where
result atX: x atY: y is aTwoArgumentBlock value: x value: y"

| newArray |
newArray := self basicNew width: width height: height type: Array.
1 to: newArray width do: [ :x |
1 to: newArray height do: [ :y |
newArray atX: x atY: y put: (aTwoArgumentBlock value: x value: y) ] ].
^ newArray
]

{ #category : #'instance creation' }
CTArray2D class >> width: width height: height type: collectionClass [
^ self basicNew width: width height: height type: collectionClass
Expand Down Expand Up @@ -255,6 +269,18 @@ CTArray2D >> rowsAndColumnsDo: aBlock [
aBlock value: row value: col]]
]

{ #category : #enumeration }
CTArray2D >> rowsAndColumnsPut: aTwoArgumentBlock [

"Set the value at col,row as the value of aTwoArgumentBlock with row and column as inputs."

1 to: self width do: [ :col |
1 to: self height do: [ :row |
contents
at: (self indexX: col y: row)
put: (aTwoArgumentBlock value: row value: col) ] ]
]

{ #category : #private }
CTArray2D >> setContents: aCollection [
"set the content of the array with aCollection"
Expand Down

0 comments on commit 96f1bff

Please sign in to comment.