Skip to content

New methods added #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ Class {

{ #category : #baselines }
BaselineOfContainersArray2D >> baseline: spec [

<baseline>
spec package: 'Containers-Array2D'.
spec
package: 'Containers-Array2D-Tests'
with: [ spec requires: #( 'Containers-Array2D' ) ].
spec
package: 'Containers-Array2D-Tools'
with: [ spec requires: #( 'Containers-Array2D' ) ]
]
<baseline>
spec
for: #common
do: [
spec package: 'Containers-Array2D'.
spec package: 'Containers-Array2D-Tests' with: [ spec requires: #('Containers-Array2D') ].
spec package: 'Containers-Array2D-Tools' with: [ spec requires: #('Containers-Array2D') ] ]
]
161 changes: 83 additions & 78 deletions src/Containers-Array2D/CTAlternateArray2D.class.st
Original file line number Diff line number Diff line change
@@ -1,152 +1,157 @@
"
Class inspired by from the book ""Fundamentals of Smalltalk Programming Technique""


The first element of an array2D is located on topleft corner.
"
Class {
#name : 'CTAlternateArray2D',
#superclass : 'Object',
#instVars : [
'rows',
'dimension'
'contents',
'width',
'height'
],
#category : 'Containers-Array2D',
#package : 'Containers-Array2D'
}

{ #category : 'examples' }
{ #category : 'instance creation' }
CTAlternateArray2D class >> new [
^ self basicNew initialize
]

{ #category : 'instance creation' }
CTAlternateArray2D class >> width: aWidth height: aHeight [
^ self new
dimension: aWidth @ aHeight;
yourself
]

{ #category : 'instance creation' }
CTAlternateArray2D class >> width2Height3 [
<sampleInstance>
"self width2Height3"
| i |
i := self new dimension: 2@3.
i at: 1@1 put: 1.
i at: 2@1 put: 2.
i at: 1@2 put: 3.
i at: 2@2 put: 4.
i at: 1@3 put: 5.
i at: 2@3 put: 6.
^ i
]

{ #category : 'iterate' }
CTAlternateArray2D >> allPositionsDo: aBlock [
"Execute a Block on all the positions (points) of the receiver."
self firstPosition pointTo: self dimension do: aBlock
^ self fromArray: #(1 2 3 4 5 6) width: 2
]

{ #category : 'iterate' }
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 ].
^ answer
{ #category : 'instance creation' }
CTAlternateArray2D class >> fromArray: anArray width: aWidth [
| height |
height := anArray size // aWidth.
^ self new
dimension: aWidth @ height;
setContents: anArray;
yourself
]

{ #category : 'accessing' }
CTAlternateArray2D >> at: aPoint [
| row |
row := self atRow: aPoint y.
^ row at: aPoint x
^ contents at: (self storageIndexFor: aPoint)
]

{ #category : 'accessing' }
CTAlternateArray2D >> at: aPoint put: anObject [
| row |
row := self atRow: aPoint y.
row atWrap: aPoint x put: anObject
CTAlternateArray2D >> at: aPoint put: aValue [
^ contents at: (self storageIndexFor: aPoint) put: aValue
]

{ #category : 'accessing' }
CTAlternateArray2D >> atRow: anInteger [
^ self rows at: anInteger
CTAlternateArray2D >> atRow: rowIndex [
(rowIndex < 1 or: [ rowIndex > self height ])
ifTrue: [ self errorSubscriptBounds: rowIndex ].
^ contents copyFrom: (rowIndex - 1) * width + 1 to: rowIndex * width
]

{ #category : 'iterating' }
CTAlternateArray2D >> allPositionsDo: aBlock [
1 to: self width do: [ :x |
1 to: self height do: [ :y |
aBlock value: x @ y ] ]
]

{ #category : 'iterating' }
CTAlternateArray2D >> allPositionsWithin: aPoint from: anotherPoint [
| result minX maxX minY maxY |
result := OrderedCollection new.
minX := (anotherPoint x - aPoint x) max: 1.
maxX := (anotherPoint x + aPoint x) min: self width.
minY := (anotherPoint y - aPoint y) max: 1.
maxY := (anotherPoint y + aPoint y) min: self height.
minX to: maxX do: [ :x |
minY to: maxY do: [ :y |
result add: x @ y ] ].
^ result
]

{ #category : 'accessing' }
CTAlternateArray2D >> dimension [
^ dimension
^ width @ height
]

{ #category : 'initialize' }
{ #category : 'accessing' }
CTAlternateArray2D >> dimension: aPoint [
dimension := aPoint.
self initializeRows
width := aPoint x.
height := aPoint y.
contents := Array new: width * height
]

{ #category : 'iterate' }
{ #category : 'iterating' }
CTAlternateArray2D >> do: aBlock [
self rowsDo: [ :eachRow | eachRow do: aBlock ]
contents do: aBlock
]

{ #category : 'accessing' }
CTAlternateArray2D >> firstPosition [
^ 1@1
^ 1 @ 1
]

{ #category : 'accessing' }
CTAlternateArray2D >> height [
^ self dimension y
^ height
]

{ #category : 'initialize' }
{ #category : 'initialization' }
CTAlternateArray2D >> initialize [
self dimension: 0@0
]

{ #category : 'initialize' }
CTAlternateArray2D >> initializeRows [
| newRows |
newRows := (1 to: self height) collect: [ :each | self newRow ].
self rows: newRows asArray
width := 0.
height := 0.
contents := #().
]

{ #category : 'private' }
CTAlternateArray2D >> newRow [
^ self newRowWithWidth: self width
^ Array new: width
]

{ #category : 'private' }
CTAlternateArray2D >> newRowWithWidth: anInteger [
^ Array new: anInteger
CTAlternateArray2D >> newRowWithWidth: aWidth [
^ Array new: aWidth
]

{ #category : 'copying' }
CTAlternateArray2D >> postCopy [
| newRows |
super postCopy.
newRows := self rows collect: [ :each | each copy ].
self rows: newRows
contents := contents copy
]

{ #category : 'accessing' }
CTAlternateArray2D >> rows [
^ rows
^ (1 to: self height) collect: [ :rowIndex | self atRow: rowIndex ]
]

{ #category : 'accessing' }
CTAlternateArray2D >> rows: someRows [
rows := someRows
{ #category : 'iterating' }
CTAlternateArray2D >> rowsDo: aBlock [
1 to: self height do: [ :i | aBlock value: (self atRow: i) ]
]

{ #category : 'iterate' }
CTAlternateArray2D >> rowsDo: aBlock [
self rows do: aBlock
{ #category : 'private' }
CTAlternateArray2D >> setContents: anArray [
contents := anArray copy
]

{ #category : 'accessing' }
CTAlternateArray2D >> size [
^ self dimension x * self dimension y
^ width * height
]

{ #category : 'private' }
CTAlternateArray2D >> storageIndexFor: aPoint [
^ aPoint y - 1 * self dimension x + aPoint x
(aPoint x < 1 or: [ aPoint x > width or: [ aPoint y < 1 or: [ aPoint y > height ] ] ])
ifTrue: [ self errorSubscriptBounds: aPoint ].
^ (aPoint y - 1) * width + aPoint x
]

{ #category : 'accessing' }
CTAlternateArray2D >> width [
^ self dimension x
]
^ width
]
43 changes: 43 additions & 0 deletions src/Containers-Array2D/CTArray2D.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,46 @@ CTArray2D >> width: x height: y type: collectionClass [
width := x.
contents := collectionClass new: x * y
]

{ #category : #'accessing' }
CTArray2D >> at: rowIndex at: columnIndex [
"Answer the element at rowIndex, columnIndex. Maps to atX:atY:."
^ self atX: columnIndex atY: rowIndex
]

{ #category : #'accessing' }
CTArray2D >> at: rowIndex at: columnIndex put: value [
"Store value at rowIndex, columnIndex and answer it. Maps to atX:atY:put:."
^ self atX: columnIndex atY: rowIndex put: value
]

{ #category : #'accessing' }
CTArray2D >> columnCount [
"Answer the number of columns in the matrix."
^ self width
]

{ #category : #'accessing' }
CTArray2D >> rowCount [
"Answer the number of rows in the matrix."
^ self height
]

{ #category : #'instance creation' }
CTArray2D class >> rows: numberOfRows columns: numberOfColumns [
"Create a new CTArray2D instance with the specified number of rows and columns."
^ self width: numberOfColumns height: numberOfRows
]

{ #category : #'instance creation' }
CTArray2D class >> rows: numberOfRows columns: numberOfColumns contents: anArray [
"Create a new CTArray2D instance with the specified number of rows, columns, and contents."
| newArray |
newArray := self width: numberOfColumns height: numberOfRows.
1 to: numberOfRows do: [ :row |
1 to: numberOfColumns do: [ :col |
| index |
index := (row - 1) * numberOfColumns + col.
newArray atX: col atY: row put: (anArray at: index) ] ].
^ newArray
]
2 changes: 1 addition & 1 deletion src/Containers-Array2D/package.st
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Package { #name : 'Containers-Array2D' }
Package { #name : #'Containers-Array2D' }