From 37e8795b9bec6d3ce10a4104cfc7df4c9141f9a2 Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind Date: Fri, 2 May 2025 21:14:50 +0530 Subject: [PATCH 1/5] new methods added --- .../BaselineOfContainersArray2D.class.st | 18 +-- .../CTAlternateArray2D.class.st | 152 ------------------ src/Containers-Array2D/CTArray2D.extension.st | 44 +++++ src/Containers-Array2D/CTPoint.extension.st | 10 ++ src/Containers-Array2D/package.st | 2 +- 5 files changed, 63 insertions(+), 163 deletions(-) delete mode 100644 src/Containers-Array2D/CTAlternateArray2D.class.st create mode 100644 src/Containers-Array2D/CTArray2D.extension.st create mode 100644 src/Containers-Array2D/CTPoint.extension.st diff --git a/src/BaselineOfContainersArray2D/BaselineOfContainersArray2D.class.st b/src/BaselineOfContainersArray2D/BaselineOfContainersArray2D.class.st index cf0d5a3..3a1c4cc 100644 --- a/src/BaselineOfContainersArray2D/BaselineOfContainersArray2D.class.st +++ b/src/BaselineOfContainersArray2D/BaselineOfContainersArray2D.class.st @@ -6,13 +6,11 @@ Class { { #category : #baselines } BaselineOfContainersArray2D >> baseline: spec [ - - - spec package: 'Containers-Array2D'. - spec - package: 'Containers-Array2D-Tests' - with: [ spec requires: #( 'Containers-Array2D' ) ]. - spec - package: 'Containers-Array2D-Tools' - with: [ spec requires: #( 'Containers-Array2D' ) ] -] + + 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') ] ] +] \ No newline at end of file diff --git a/src/Containers-Array2D/CTAlternateArray2D.class.st b/src/Containers-Array2D/CTAlternateArray2D.class.st deleted file mode 100644 index 42ccfd4..0000000 --- a/src/Containers-Array2D/CTAlternateArray2D.class.st +++ /dev/null @@ -1,152 +0,0 @@ -" -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' - ], - #category : 'Containers-Array2D', - #package : 'Containers-Array2D' -} - -{ #category : 'examples' } -CTAlternateArray2D class >> width2Height3 [ - - "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 -] - -{ #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 : 'accessing' } -CTAlternateArray2D >> at: aPoint [ - | row | - row := self atRow: aPoint y. - ^ row at: aPoint x -] - -{ #category : 'accessing' } -CTAlternateArray2D >> at: aPoint put: anObject [ - | row | - row := self atRow: aPoint y. - row atWrap: aPoint x put: anObject -] - -{ #category : 'accessing' } -CTAlternateArray2D >> atRow: anInteger [ - ^ self rows at: anInteger -] - -{ #category : 'accessing' } -CTAlternateArray2D >> dimension [ - ^ dimension -] - -{ #category : 'initialize' } -CTAlternateArray2D >> dimension: aPoint [ - dimension := aPoint. - self initializeRows -] - -{ #category : 'iterate' } -CTAlternateArray2D >> do: aBlock [ - self rowsDo: [ :eachRow | eachRow do: aBlock ] -] - -{ #category : 'accessing' } -CTAlternateArray2D >> firstPosition [ - ^ 1@1 -] - -{ #category : 'accessing' } -CTAlternateArray2D >> height [ - ^ self dimension y -] - -{ #category : 'initialize' } -CTAlternateArray2D >> initialize [ - self dimension: 0@0 -] - -{ #category : 'initialize' } -CTAlternateArray2D >> initializeRows [ - | newRows | - newRows := (1 to: self height) collect: [ :each | self newRow ]. - self rows: newRows asArray -] - -{ #category : 'private' } -CTAlternateArray2D >> newRow [ - ^ self newRowWithWidth: self width -] - -{ #category : 'private' } -CTAlternateArray2D >> newRowWithWidth: anInteger [ - ^ Array new: anInteger -] - -{ #category : 'copying' } -CTAlternateArray2D >> postCopy [ - | newRows | - super postCopy. - newRows := self rows collect: [ :each | each copy ]. - self rows: newRows -] - -{ #category : 'accessing' } -CTAlternateArray2D >> rows [ - ^ rows -] - -{ #category : 'accessing' } -CTAlternateArray2D >> rows: someRows [ - rows := someRows -] - -{ #category : 'iterate' } -CTAlternateArray2D >> rowsDo: aBlock [ - self rows do: aBlock -] - -{ #category : 'accessing' } -CTAlternateArray2D >> size [ - ^ self dimension x * self dimension y -] - -{ #category : 'private' } -CTAlternateArray2D >> storageIndexFor: aPoint [ - ^ aPoint y - 1 * self dimension x + aPoint x -] - -{ #category : 'accessing' } -CTAlternateArray2D >> width [ - ^ self dimension x -] diff --git a/src/Containers-Array2D/CTArray2D.extension.st b/src/Containers-Array2D/CTArray2D.extension.st new file mode 100644 index 0000000..55c289c --- /dev/null +++ b/src/Containers-Array2D/CTArray2D.extension.st @@ -0,0 +1,44 @@ +Extension { #name : #CTArray2D } + +{ #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 +] \ No newline at end of file diff --git a/src/Containers-Array2D/CTPoint.extension.st b/src/Containers-Array2D/CTPoint.extension.st new file mode 100644 index 0000000..fd39f13 --- /dev/null +++ b/src/Containers-Array2D/CTPoint.extension.st @@ -0,0 +1,10 @@ +Extension { #name : #Point } + +{ #category : #'iteration' } +Point >> pointTo: endPoint do: aBlock [ + "Iterate over all integer points from self to endPoint (inclusive), applying aBlock to each. + Iterate in row-major order: y from self y to endPoint y, x from self x to endPoint x within each y." + (self y to: endPoint y) do: [ :y | + (self x to: endPoint x) do: [ :x | + aBlock value: x@y ] ] +] \ No newline at end of file diff --git a/src/Containers-Array2D/package.st b/src/Containers-Array2D/package.st index 9de9a48..f05a10b 100644 --- a/src/Containers-Array2D/package.st +++ b/src/Containers-Array2D/package.st @@ -1 +1 @@ -Package { #name : 'Containers-Array2D' } +Package { #name : #'Containers-Array2D' } \ No newline at end of file From 9716a2f645991bba9169771a898d8c6d72e6416d Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind Date: Fri, 2 May 2025 21:17:39 +0530 Subject: [PATCH 2/5] CTAlternateArray2D.class.st restored --- .../CTAlternateArray2D.class.st | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/Containers-Array2D/CTAlternateArray2D.class.st diff --git a/src/Containers-Array2D/CTAlternateArray2D.class.st b/src/Containers-Array2D/CTAlternateArray2D.class.st new file mode 100644 index 0000000..1488a55 --- /dev/null +++ b/src/Containers-Array2D/CTAlternateArray2D.class.st @@ -0,0 +1,152 @@ +" +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' + ], + #category : 'Containers-Array2D', + #package : 'Containers-Array2D' +} + +{ #category : 'examples' } +CTAlternateArray2D class >> width2Height3 [ + + "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 +] + +{ #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 : 'accessing' } +CTAlternateArray2D >> at: aPoint [ + | row | + row := self atRow: aPoint y. + ^ row at: aPoint x +] + +{ #category : 'accessing' } +CTAlternateArray2D >> at: aPoint put: anObject [ + | row | + row := self atRow: aPoint y. + row atWrap: aPoint x put: anObject +] + +{ #category : 'accessing' } +CTAlternateArray2D >> atRow: anInteger [ + ^ self rows at: anInteger +] + +{ #category : 'accessing' } +CTAlternateArray2D >> dimension [ + ^ dimension +] + +{ #category : 'initialize' } +CTAlternateArray2D >> dimension: aPoint [ + dimension := aPoint. + self initializeRows +] + +{ #category : 'iterate' } +CTAlternateArray2D >> do: aBlock [ + self rowsDo: [ :eachRow | eachRow do: aBlock ] +] + +{ #category : 'accessing' } +CTAlternateArray2D >> firstPosition [ + ^ 1@1 +] + +{ #category : 'accessing' } +CTAlternateArray2D >> height [ + ^ self dimension y +] + +{ #category : 'initialize' } +CTAlternateArray2D >> initialize [ + self dimension: 0@0 +] + +{ #category : 'initialize' } +CTAlternateArray2D >> initializeRows [ + | newRows | + newRows := (1 to: self height) collect: [ :each | self newRow ]. + self rows: newRows asArray +] + +{ #category : 'private' } +CTAlternateArray2D >> newRow [ + ^ self newRowWithWidth: self width +] + +{ #category : 'private' } +CTAlternateArray2D >> newRowWithWidth: anInteger [ + ^ Array new: anInteger +] + +{ #category : 'copying' } +CTAlternateArray2D >> postCopy [ + | newRows | + super postCopy. + newRows := self rows collect: [ :each | each copy ]. + self rows: newRows +] + +{ #category : 'accessing' } +CTAlternateArray2D >> rows [ + ^ rows +] + +{ #category : 'accessing' } +CTAlternateArray2D >> rows: someRows [ + rows := someRows +] + +{ #category : 'iterate' } +CTAlternateArray2D >> rowsDo: aBlock [ + self rows do: aBlock +] + +{ #category : 'accessing' } +CTAlternateArray2D >> size [ + ^ self dimension x * self dimension y +] + +{ #category : 'private' } +CTAlternateArray2D >> storageIndexFor: aPoint [ + ^ aPoint y - 1 * self dimension x + aPoint x +] + +{ #category : 'accessing' } +CTAlternateArray2D >> width [ + ^ self dimension x +] \ No newline at end of file From f89152842c8090d6c80bb864311d964002a3d963 Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind Date: Sat, 3 May 2025 14:33:13 +0530 Subject: [PATCH 3/5] Point extension removed --- .../CTAlternateArray2D.class.st | 159 +++++++++--------- src/Containers-Array2D/CTPoint.extension.st | 10 -- 2 files changed, 82 insertions(+), 87 deletions(-) delete mode 100644 src/Containers-Array2D/CTPoint.extension.st diff --git a/src/Containers-Array2D/CTAlternateArray2D.class.st b/src/Containers-Array2D/CTAlternateArray2D.class.st index 1488a55..58ce2a7 100644 --- a/src/Containers-Array2D/CTAlternateArray2D.class.st +++ b/src/Containers-Array2D/CTAlternateArray2D.class.st @@ -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 [ - - "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 ] \ No newline at end of file diff --git a/src/Containers-Array2D/CTPoint.extension.st b/src/Containers-Array2D/CTPoint.extension.st deleted file mode 100644 index fd39f13..0000000 --- a/src/Containers-Array2D/CTPoint.extension.st +++ /dev/null @@ -1,10 +0,0 @@ -Extension { #name : #Point } - -{ #category : #'iteration' } -Point >> pointTo: endPoint do: aBlock [ - "Iterate over all integer points from self to endPoint (inclusive), applying aBlock to each. - Iterate in row-major order: y from self y to endPoint y, x from self x to endPoint x within each y." - (self y to: endPoint y) do: [ :y | - (self x to: endPoint x) do: [ :x | - aBlock value: x@y ] ] -] \ No newline at end of file From a234d8a6790c7f6c6cf70e60d5bab697c1dce791 Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind Date: Sat, 3 May 2025 20:43:54 +0530 Subject: [PATCH 4/5] extension file removed --- src/Containers-Array2D/CTArray2D.extension.st | 44 ------------------- 1 file changed, 44 deletions(-) delete mode 100644 src/Containers-Array2D/CTArray2D.extension.st diff --git a/src/Containers-Array2D/CTArray2D.extension.st b/src/Containers-Array2D/CTArray2D.extension.st deleted file mode 100644 index 55c289c..0000000 --- a/src/Containers-Array2D/CTArray2D.extension.st +++ /dev/null @@ -1,44 +0,0 @@ -Extension { #name : #CTArray2D } - -{ #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 -] \ No newline at end of file From f69967a59ce2e83955506c3a24783666950b1fb9 Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind Date: Sat, 3 May 2025 21:27:18 +0530 Subject: [PATCH 5/5] fixed errors --- src/Containers-Array2D/CTArray2D.class.st | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/Containers-Array2D/CTArray2D.class.st b/src/Containers-Array2D/CTArray2D.class.st index f81d77c..356ce90 100644 --- a/src/Containers-Array2D/CTArray2D.class.st +++ b/src/Containers-Array2D/CTArray2D.class.st @@ -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 +] \ No newline at end of file