forked from pharo-containers/Containers-Array2D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTAlternateArray2D.class.st
More file actions
157 lines (131 loc) · 3.13 KB
/
CTAlternateArray2D.class.st
File metadata and controls
157 lines (131 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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'
],
#category : 'Containers-Array2D',
#package : 'Containers-Array2D'
}
{ #category : 'examples' }
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 [
1 to: self width do: [ :x |
1 to: self height do: [ :y |
aBlock value: x@y ] ]
]
{ #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 x to: bottomRight x do: [ :x |
topLeft y to: bottomRight y do: [ :y |
answer add: x@y ] ].
^ 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
]