Skip to content

Commit 115bf2c

Browse files
committed
Add wrapped neighbors and enforce bounds checking for neighbor
1 parent 87df204 commit 115bf2c

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,38 @@ CTArray2DTest >> testNeighborsAtColumnAtRow [
279279
self assert: (topCornerNeighbors includesAll: #(2 3 4)).
280280
]
281281

282+
{ #category : 'tests' }
283+
CTArray2DTest >> testNeighborsAtColumnAtRowOutOfBounds [
284+
285+
| array |
286+
287+
array := self arrayClass width2Height3.
288+
289+
self should: [ array neighborsAtColumn: 99 atRow: 99 ] raise: Error.
290+
]
291+
292+
{ #category : 'tests' }
293+
CTArray2DTest >> testNeighborsAtColumnWrapAtRowWrap [
294+
295+
| array neighbors |
296+
297+
array := self arrayClass fromArray: #(1 2 3 4 5 6 7 8 9) width: 3.
298+
299+
neighbors := array neighborsAtColumnWrap: 1 atRowWrap: 1.
300+
301+
self assert: neighbors size equals: 8.
302+
self assert: (neighbors includesAll: #(2 3 4 5 6 7 8 9)).
303+
]
304+
305+
{ #category : 'tests' }
306+
CTArray2DTest >> testNeighborsAtColumnWrapAtRowWrapOutOfBounds [
307+
| array |
308+
309+
array := self arrayClass width2Height3.
310+
311+
self should: [ array neighborsAtColumnWrap: 99 atRowWrap: 99 ] raise: Error.
312+
]
313+
282314
{ #category : 'tests-accessing' }
283315
CTArray2DTest >> testNeighborsEight [
284316

src/Containers-Array2D/CTArray2D.class.st

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,17 +323,39 @@ CTArray2D >> leftToRightFromBottomToTopDo: aBlock [
323323

324324
{ #category : 'enumerating' }
325325
CTArray2D >> neighborsAtColumn: col atRow: row [
326-
326+
327327
| neighbors |
328+
329+
(col between: 1 and: self width) ifFalse: [ self error: 'Column out of bounds' ].
330+
(row between: 1 and: self height) ifFalse: [ self error: 'Row out of bounds' ].
331+
328332
neighbors := OrderedCollection new.
329-
333+
330334
row - 1 to: row + 1 do: [ :r |
331335
col - 1 to: col + 1 do: [ :c |
332336
(r = row and: [ c = col ]) ifFalse: [
333337
(r between: 1 and: self height) ifTrue: [
334338
(c between: 1 and: self width) ifTrue: [
335339
neighbors add: (self atColumn: c atRow: r) ] ] ] ] ].
336-
340+
341+
^ neighbors
342+
]
343+
344+
{ #category : 'enumerating' }
345+
CTArray2D >> neighborsAtColumnWrap: col atRowWrap: row [
346+
347+
| neighbors |
348+
349+
(col between: 1 and: self width) ifFalse: [ self error: 'Column out of bounds' ].
350+
(row between: 1 and: self height) ifFalse: [ self error: 'Row out of bounds' ].
351+
352+
neighbors := OrderedCollection new.
353+
354+
row - 1 to: row + 1 do: [ :r |
355+
col - 1 to: col + 1 do: [ :c |
356+
(r = row and: [ c = col ]) ifFalse: [
357+
neighbors add: (self atColumnWrap: c atRowWrap: r) ] ] ].
358+
337359
^ neighbors
338360
]
339361

0 commit comments

Comments
 (0)