1+ "
2+ Class inspired by from the book " " Fundamentals of Smalltalk Programming Technique" "
3+
4+
5+ The first element of an array2D is located on topleft corner.
6+ "
7+ Class {
8+ #name : ' CTAlternateArray2D' ,
9+ #superclass : ' Object' ,
10+ #instVars : [
11+ ' rows' ,
12+ ' dimension'
13+ ],
14+ #category : ' Containers-Array2D' ,
15+ #package : ' Containers-Array2D'
16+ }
17+
18+ { #category : ' examples' }
19+ CTAlternateArray2D class >> width2Height3 [
20+ < sampleInstance>
21+ " self width2Height3"
22+ | i |
23+ i := self new dimension: 2 @3 .
24+ i at: 1 @1 put: 1 .
25+ i at: 2 @1 put: 2 .
26+ i at: 1 @2 put: 3 .
27+ i at: 2 @2 put: 4 .
28+ i at: 1 @3 put: 5 .
29+ i at: 2 @3 put: 6 .
30+ ^ i
31+ ]
32+
33+ { #category : ' iterate' }
34+ CTAlternateArray2D >> allPositionsDo: aBlock [
35+ " Execute a Block on all the positions (points) of the receiver."
36+ self firstPosition pointTo: self dimension do: aBlock
37+ ]
38+
39+ { #category : ' iterate' }
40+ CTAlternateArray2D >> allPositionsWithin: someDistance from: someOrigin [
41+ | answer topLeft bottomRight |
42+ answer := OrderedCollection new .
43+ topLeft := someOrigin - someDistance max: self firstPosition.
44+ bottomRight := someOrigin + someDistance min: self dimension.
45+ topLeft pointTo: bottomRight do: [ :each | answer add: each ].
46+ ^ answer
47+ ]
48+
49+ { #category : ' accessing' }
50+ CTAlternateArray2D >> at: aPoint [
51+ | row |
52+ row := self atRow: aPoint y.
53+ ^ row at: aPoint x
54+ ]
55+
56+ { #category : ' accessing' }
57+ CTAlternateArray2D >> at: aPoint put: anObject [
58+ | row |
59+ row := self atRow: aPoint y.
60+ row atWrap: aPoint x put: anObject
61+ ]
62+
63+ { #category : ' accessing' }
64+ CTAlternateArray2D >> atRow: anInteger [
65+ ^ self rows at: anInteger
66+ ]
67+
68+ { #category : ' accessing' }
69+ CTAlternateArray2D >> dimension [
70+ ^ dimension
71+ ]
72+
73+ { #category : ' initialize' }
74+ CTAlternateArray2D >> dimension: aPoint [
75+ dimension := aPoint.
76+ self initializeRows
77+ ]
78+
79+ { #category : ' iterate' }
80+ CTAlternateArray2D >> do: aBlock [
81+ self rowsDo: [ :eachRow | eachRow do: aBlock ]
82+ ]
83+
84+ { #category : ' accessing' }
85+ CTAlternateArray2D >> firstPosition [
86+ ^ 1 @1
87+ ]
88+
89+ { #category : ' accessing' }
90+ CTAlternateArray2D >> height [
91+ ^ self dimension y
92+ ]
93+
94+ { #category : ' initialize' }
95+ CTAlternateArray2D >> initialize [
96+ self dimension: 0 @0
97+ ]
98+
99+ { #category : ' initialize' }
100+ CTAlternateArray2D >> initializeRows [
101+ | newRows |
102+ newRows := (1 to: self height) collect: [ :each | self newRow ].
103+ self rows: newRows asArray
104+ ]
105+
106+ { #category : ' private' }
107+ CTAlternateArray2D >> newRow [
108+ ^ self newRowWithWidth: self width
109+ ]
110+
111+ { #category : ' private' }
112+ CTAlternateArray2D >> newRowWithWidth: anInteger [
113+ ^ Array new : anInteger
114+ ]
115+
116+ { #category : ' copying' }
117+ CTAlternateArray2D >> postCopy [
118+ | newRows |
119+ super postCopy.
120+ newRows := self rows collect: [ :each | each copy ].
121+ self rows: newRows
122+ ]
123+
124+ { #category : ' accessing' }
125+ CTAlternateArray2D >> rows [
126+ ^ rows
127+ ]
128+
129+ { #category : ' accessing' }
130+ CTAlternateArray2D >> rows: someRows [
131+ rows := someRows
132+ ]
133+
134+ { #category : ' iterate' }
135+ CTAlternateArray2D >> rowsDo: aBlock [
136+ self rows do: aBlock
137+ ]
138+
139+ { #category : ' accessing' }
140+ CTAlternateArray2D >> size [
141+ ^ self dimension x * self dimension y
142+ ]
143+
144+ { #category : ' private' }
145+ CTAlternateArray2D >> storageIndexFor: aPoint [
146+ ^ aPoint y - 1 * self dimension x + aPoint x
147+ ]
148+
149+ { #category : ' accessing' }
150+ CTAlternateArray2D >> width [
151+ ^ self dimension x
152+ ]
0 commit comments