Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Containers-Array2D-Tests/CTNewArray2DTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ CTNewArray2DTest >> testFromRightToLeftFromTopToBottomDo [
self assert: res equals: #(2 1 4 3 6 5) asOrderedCollection
]

{ #category : 'tests' }
CTNewArray2DTest >> testFromRowsPad [
| array |
array := self arrayClass fromRows: #( #(1 2) #(3 4 5) #(6) ) pad: 0.

self assert: array width equals: 3.
self assert: array height equals: 3.
self assert: (array atRow: 1) equals: #(1 2 0).
self assert: (array atRow: 2) equals: #(3 4 5).
self assert: (array atRow: 3) equals: #(6 0 0).
]

{ #category : 'tests' }
CTNewArray2DTest >> testFromTopToBottomFromLeftToRightDo [
|foo res|
Expand Down
13 changes: 9 additions & 4 deletions src/Containers-Array2D/CTAlternateArray2D.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,22 @@ CTAlternateArray2D class >> width2Height3 [

{ #category : 'iterate' }
CTAlternateArray2D >> allPositionsDo: aBlock [
"Execute a Block on all the positions (points) of the receiver."
self firstPosition pointTo: self dimension do: aBlock
1 to: self width do: [ :x |
1 to: self height do: [ :y |
aBlock value: x@y ] ]
]

{ #category : 'iterate' }
CTAlternateArray2D >> allPositionsWithin: someDistance from: someOrigin [
CTAlternateArray2D >> allPositionsWithin: someDistance from: someOrigin [
| answer topLeft bottomRight |
answer := OrderedCollection new.
topLeft := someOrigin - someDistance max: self firstPosition.
bottomRight := someOrigin + someDistance min: self dimension.
topLeft pointTo: bottomRight do: [ :each | answer add: each ].

topLeft x to: bottomRight x do: [ :x |
topLeft y to: bottomRight y do: [ :y |
answer add: x@y ] ].

^ answer
]

Expand Down
15 changes: 15 additions & 0 deletions src/Containers-Array2D/CTNewArray2D.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ CTNewArray2D class >> fromArray: aCollection width: aSize [

]

{ #category : 'instance creation' }
CTNewArray2D class >> fromRows: rows pad: paddingElement [
| width contents pad |
width := rows max: [ :row | row size ].
contents := rows first species new: (width * rows size) streamContents: [ :aStream |
rows do: [ :row |
aStream nextPutAll: row.
pad := width - row size.
pad > 0 ifTrue: [ aStream next: pad put: paddingElement ].
]
].

^ self fromArray: contents width: width
]

{ #category : 'instance creation' }
CTNewArray2D class >> new [

Expand Down