flex_array 0.3.2 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.gitignore +0 -15
- data/.reek.yml +17 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/README.md +67 -0
- data/bench/benchmark.rb +243 -0
- data/bench/results.txt +97 -0
- data/flex_array.gemspec +6 -10
- data/irbt.rb +18 -0
- data/lib/flex_array.rb +13 -30
- data/lib/flex_array/array.rb +9 -24
- data/lib/flex_array/flex_array_append.rb +4 -23
- data/lib/flex_array/flex_array_each.rb +33 -208
- data/lib/flex_array/flex_array_forever.rb +6 -4
- data/lib/flex_array/flex_array_index.rb +4 -29
- data/lib/flex_array/flex_array_new.rb +9 -56
- data/lib/flex_array/flex_array_process.rb +23 -63
- data/lib/flex_array/flex_array_reshape.rb +13 -23
- data/lib/flex_array/flex_array_transpose.rb +7 -16
- data/lib/flex_array/flex_array_validate.rb +6 -15
- data/lib/flex_array/integer.rb +4 -13
- data/lib/flex_array/object.rb +4 -13
- data/lib/flex_array/range.rb +4 -13
- data/lib/flex_array/spec_array.rb +5 -9
- data/lib/flex_array/spec_component.rb +14 -22
- data/lib/flex_array/version.rb +1 -2
- data/rakefile.rb +1 -24
- data/reek.txt +9 -2
- data/tests/array_test.rb +0 -4
- data/tests/flex_array_append_test.rb +0 -23
- data/tests/flex_array_each_test.rb +483 -487
- data/tests/flex_array_index_test.rb +0 -4
- data/tests/flex_array_new_test.rb +0 -4
- data/tests/flex_array_reshape_test.rb +4 -4
- data/tests/flex_array_test.rb +0 -4
- data/tests/flex_array_transpose_test.rb +0 -4
- data/tests/flex_array_validate_test.rb +5 -4
- data/tests/integer_test.rb +0 -4
- data/tests/object_test.rb +0 -4
- data/tests/range_test.rb +0 -4
- data/tests/spec_array_test.rb +0 -4
- data/tests/spec_component_test.rb +0 -4
- metadata +19 -49
- data/docs/Flex_Array_UG.odt +0 -0
- data/docs/Flex_Array_UG_Version_0_3_0.pdf +0 -0
- data/flex_array.reek +0 -109
- data/readme.txt +0 -2
- data/sire.rb +0 -82
data/lib/flex_array/range.rb
CHANGED
@@ -1,10 +1,7 @@
|
|
1
|
-
#Extensions to Range needed to support flex array.
|
1
|
+
# Extensions to Range needed to support flex array.
|
2
|
+
|
2
3
|
class Range
|
3
|
-
#Convert this integer to a limits component.
|
4
|
-
#<br>Parameters
|
5
|
-
#* stride - the number of cells separating data with adjacent indexes.
|
6
|
-
#<br>Returns
|
7
|
-
#* A SpecComponent object with the same range as self.
|
4
|
+
# Convert this integer to a limits component.
|
8
5
|
def to_spec_component(stride)
|
9
6
|
min = self.min
|
10
7
|
|
@@ -17,13 +14,7 @@ class Range
|
|
17
14
|
end
|
18
15
|
end
|
19
16
|
|
20
|
-
#Convert this range to an range index against the spec.
|
21
|
-
#<br>Parameters
|
22
|
-
#* spec - The spec component used to validate this index.
|
23
|
-
#<br>Returns
|
24
|
-
#* A range.
|
25
|
-
#<br>Exceptions
|
26
|
-
#* IndexError if the range is not valid.
|
17
|
+
# Convert this range to an range index against the spec.
|
27
18
|
def to_index_range(spec)
|
28
19
|
self_min, self_max, spec_max = self.begin, self.end, spec.max
|
29
20
|
self_max -= 1 if self_max > 0 && self.exclude_end?
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#This helper class encapsulates an array of flex array spec components.
|
1
|
+
# This helper class encapsulates an array of flex array spec components.
|
2
|
+
|
2
3
|
class SpecArray < Array
|
3
4
|
#The number of elements defined by this array specification.
|
4
5
|
attr_reader :spec_count
|
@@ -6,12 +7,7 @@ class SpecArray < Array
|
|
6
7
|
#The number of dimensions specified by this specification
|
7
8
|
alias spec_dimensions length
|
8
9
|
|
9
|
-
#Create a flex array specification.
|
10
|
-
#<br>Parameters
|
11
|
-
#* array_specs - The specification of the flex array subscript limits.
|
12
|
-
# These can either be an array of containing integers, in which case
|
13
|
-
# the limits are 0...n or they can be ranges, in which case the
|
14
|
-
# limits are those of the range.
|
10
|
+
# Create a flex array specification.
|
15
11
|
def initialize(array_specs)
|
16
12
|
super(0)
|
17
13
|
@spec_count = 1
|
@@ -25,7 +21,7 @@ class SpecArray < Array
|
|
25
21
|
self
|
26
22
|
end
|
27
23
|
|
28
|
-
#Is this array specification transposed in any way?
|
24
|
+
# Is this array specification transposed in any way?
|
29
25
|
def transposed?
|
30
26
|
check = 1
|
31
27
|
|
@@ -37,7 +33,7 @@ class SpecArray < Array
|
|
37
33
|
false
|
38
34
|
end
|
39
35
|
|
40
|
-
#Enlarge the flex array along its first dimension.
|
36
|
+
# Enlarge the flex array along its first dimension.
|
41
37
|
def enlarge(growth)
|
42
38
|
self[0].enlarge(growth)
|
43
39
|
|
@@ -1,50 +1,44 @@
|
|
1
|
-
#This helper class encapsulates a single component of the flex array spec.
|
1
|
+
# This helper class encapsulates a single component of the flex array spec.
|
2
|
+
|
2
3
|
class SpecComponent
|
3
|
-
#The range of acceptable values for this limit.
|
4
|
+
# The range of acceptable values for this limit.
|
4
5
|
attr_reader :range
|
5
6
|
|
6
|
-
#The stride of this array dimension. That is, for each step in this
|
7
|
-
#dimension, how many steps are required in the low level array?
|
7
|
+
# The stride of this array dimension. That is, for each step in this
|
8
|
+
# dimension, how many steps are required in the low level array?
|
8
9
|
attr_reader :stride
|
9
10
|
|
10
|
-
#Create a limits component from its constituent data.
|
11
|
-
#<br>Parameters
|
12
|
-
#* range - the range of values for this index limit.
|
13
|
-
#* stride - the number of cells separating data with adjacent indexes.
|
11
|
+
# Create a limits component from its constituent data.
|
14
12
|
def initialize(range, stride)
|
15
13
|
@range, @stride = range, stride
|
16
14
|
end
|
17
15
|
|
18
|
-
#Forward '=== value' to the range.
|
16
|
+
# Forward '=== value' to the range.
|
19
17
|
def ===(value)
|
20
18
|
@range === value
|
21
19
|
end
|
22
20
|
|
23
|
-
#Limits are equal if their ranges are equal.
|
24
|
-
#<br>Returns
|
25
|
-
#* true if the spec components have equal ranges.
|
21
|
+
# Limits are equal if their ranges are equal.
|
26
22
|
def ==(other)
|
27
23
|
@range == other.range
|
28
24
|
end
|
29
25
|
|
30
|
-
#Forward 'min' to the range.
|
26
|
+
# Forward 'min' to the range.
|
31
27
|
def min
|
32
28
|
@range.min
|
33
29
|
end
|
34
30
|
|
35
|
-
#Forward 'max' to the range.
|
31
|
+
# Forward 'max' to the range.
|
36
32
|
def max
|
37
33
|
@range.max
|
38
34
|
end
|
39
35
|
|
40
|
-
#Forward 'each' and the block to the range.
|
36
|
+
# Forward 'each' and the block to the range.
|
41
37
|
def each(&block)
|
42
38
|
@range.each(&block)
|
43
39
|
end
|
44
40
|
|
45
|
-
#Compute the span of indexes in this limit component.
|
46
|
-
#<br>Returns
|
47
|
-
#* The span of the range or zero if there is none.
|
41
|
+
# Compute the span of indexes in this limit component.
|
48
42
|
def span
|
49
43
|
if @range.none?
|
50
44
|
0
|
@@ -53,7 +47,7 @@ class SpecComponent
|
|
53
47
|
end
|
54
48
|
end
|
55
49
|
|
56
|
-
#Enlarge the range of this spec by the growth term.
|
50
|
+
# Enlarge the range of this spec by the growth term.
|
57
51
|
def enlarge(growth)
|
58
52
|
if @range.none?
|
59
53
|
@range = 0...growth
|
@@ -62,9 +56,7 @@ class SpecComponent
|
|
62
56
|
end
|
63
57
|
end
|
64
58
|
|
65
|
-
#Compute the step required for the index value.
|
66
|
-
#<br>Returns
|
67
|
-
#* The number of array cells to be skipped for this index.
|
59
|
+
# Compute the step required for the index value.
|
68
60
|
def index_step(index)
|
69
61
|
(index - @range.min) * @stride
|
70
62
|
end
|
data/lib/flex_array/version.rb
CHANGED
data/rakefile.rb
CHANGED
@@ -2,18 +2,8 @@
|
|
2
2
|
# coding: utf-8
|
3
3
|
|
4
4
|
require 'rake/testtask'
|
5
|
-
require 'rdoc/task'
|
6
5
|
require "bundler/gem_tasks"
|
7
6
|
|
8
|
-
RDoc::Task.new do |rdoc|
|
9
|
-
rdoc.rdoc_dir = "rdoc"
|
10
|
-
|
11
|
-
#List out all the files to be documented.
|
12
|
-
rdoc.rdoc_files.include("lib/**/*.rb", "license.txt", "readme.txt")
|
13
|
-
|
14
|
-
rdoc.options << '--visibility' << 'private'
|
15
|
-
end
|
16
|
-
|
17
7
|
Rake::TestTask.new do |t|
|
18
8
|
#List out all the test files.
|
19
9
|
t.test_files = FileList['tests/**/*.rb']
|
@@ -25,22 +15,9 @@ task :reek do |t|
|
|
25
15
|
`reek --no-color lib > reek.txt`
|
26
16
|
end
|
27
17
|
|
28
|
-
def eval_puts(str)
|
29
|
-
puts str
|
30
|
-
eval str
|
31
|
-
end
|
32
|
-
|
33
18
|
desc "Run an interactive flex array session."
|
34
19
|
task :console do
|
35
|
-
|
36
|
-
require 'irb/completion'
|
37
|
-
require_relative 'lib/flex_array'
|
38
|
-
eval_puts "@a = FlexArray.new([2,3,4]) {|i| i.clone}"
|
39
|
-
eval_puts "@b = FlexArray.new([2,3,4]) {|i| (i[0]+i[2])*(i[1] + i[2])}"
|
40
|
-
eval_puts "@c = FlexArray.new([0,3])"
|
41
|
-
eval_puts "@d = FlexArray.new([3,3]) {|i| i[0]*3 + i[1]}"
|
42
|
-
ARGV.clear
|
43
|
-
IRB.start
|
20
|
+
system "ruby irbt.rb local"
|
44
21
|
end
|
45
22
|
|
46
23
|
desc "What version of flex_array is this?"
|
data/reek.txt
CHANGED
@@ -1,2 +1,9 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
lib/flex_array/flex_array_each.rb -- 2 warnings:
|
2
|
+
[178, 198, 217, 237]:RepeatedConditional: FlexArray tests 'blk' at least 4 times [https://github.com/troessner/reek/blob/v5.0.2/docs/Repeated-Conditional.md]
|
3
|
+
[180, 200, 219, 239]:RepeatedConditional: FlexArray tests 'blk.call(@array_data[posn])' at least 4 times [https://github.com/troessner/reek/blob/v5.0.2/docs/Repeated-Conditional.md]
|
4
|
+
lib/flex_array/spec_array.rb -- 1 warning:
|
5
|
+
[3]:SubclassedFromCoreClass: SpecArray inherits from core class 'Array' [https://github.com/troessner/reek/blob/v5.0.2/docs/Subclassed-From-Core-Class.md]
|
6
|
+
lib/flex_array.rb -- 2 warnings:
|
7
|
+
[40]:Attribute: FlexArray#array_data is a writable attribute [https://github.com/troessner/reek/blob/v5.0.2/docs/Attribute.md]
|
8
|
+
[37]:Attribute: FlexArray#array_specs is a writable attribute [https://github.com/troessner/reek/blob/v5.0.2/docs/Attribute.md]
|
9
|
+
5 total warnings
|
data/tests/array_test.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
1
|
require_relative '../lib/flex_array'
|
2
2
|
gem 'minitest'
|
3
3
|
require 'minitest/autorun'
|
4
|
-
require 'minitest_visible'
|
5
4
|
|
6
5
|
class ArrayTester < Minitest::Test
|
7
6
|
|
8
|
-
#Track mini-test progress.
|
9
|
-
include MinitestVisible
|
10
|
-
|
11
7
|
def test_the_limits_method
|
12
8
|
test = [1,2,3,4,5,6]
|
13
9
|
assert_equal([0...6], test.limits)
|
@@ -1,13 +1,9 @@
|
|
1
1
|
require_relative '../lib/flex_array'
|
2
2
|
gem 'minitest'
|
3
3
|
require 'minitest/autorun'
|
4
|
-
require 'minitest_visible'
|
5
4
|
|
6
5
|
class FlexArrayAppendTester < Minitest::Test
|
7
6
|
|
8
|
-
#Track mini-test progress.
|
9
|
-
include MinitestVisible
|
10
|
-
|
11
7
|
def test_append_onto_an_existing_array
|
12
8
|
f = FlexArray.new([1,3], "Title")
|
13
9
|
assert_equal(2, f.dimensions)
|
@@ -54,23 +50,4 @@ class FlexArrayAppendTester < Minitest::Test
|
|
54
50
|
assert_raises(ArgumentError) { f << g }
|
55
51
|
end
|
56
52
|
|
57
|
-
def test_the_copy_data_method
|
58
|
-
f = FlexArray.new([2,3], "Title")
|
59
|
-
g = FlexArray.new([2,3], "Fight")
|
60
|
-
f.copy_data(g)
|
61
|
-
assert_equal(f.array_data, g.array_data)
|
62
|
-
assert(f.array_data.object_id != g.array_data.object_id)
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_copy_data_method_failures
|
66
|
-
f = FlexArray.new([2,3], "Title")
|
67
|
-
g = FlexArray.new([2,4], "Fight")
|
68
|
-
assert_raises(ArgumentError) { f.copy_data(g) }
|
69
|
-
|
70
|
-
g = FlexArray.new([1,3], "Fight")
|
71
|
-
assert_raises(ArgumentError) { f.copy_data(g) }
|
72
|
-
|
73
|
-
g = FlexArray.new([2,2], "Fight")
|
74
|
-
assert_raises(ArgumentError) { f.copy_data(g) }
|
75
|
-
end
|
76
53
|
end
|
@@ -1,487 +1,483 @@
|
|
1
|
-
require_relative '../lib/flex_array'
|
2
|
-
gem 'minitest'
|
3
|
-
require 'minitest/autorun'
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
result
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
result
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
d.each
|
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
|
-
q
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
q
|
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
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
d
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
d
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
d
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
d
|
328
|
-
|
329
|
-
|
330
|
-
b
|
331
|
-
|
332
|
-
|
333
|
-
b = q.select_flatten_collect([
|
334
|
-
assert_equal(d, b)
|
335
|
-
|
336
|
-
d = [0,
|
337
|
-
b = q.select_flatten_collect([
|
338
|
-
assert_equal(d, b)
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
assert_equal(d,
|
361
|
-
|
362
|
-
d = [0,1,
|
363
|
-
|
364
|
-
assert_equal(d,
|
365
|
-
|
366
|
-
d = [0,1,256,
|
367
|
-
|
368
|
-
assert_equal(d,
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
q.select_collect!([:all]) {|v| v * v }
|
386
|
-
assert_equal(d, q.array_data)
|
387
|
-
|
388
|
-
d = [0,1,
|
389
|
-
q.select_collect!([
|
390
|
-
assert_equal(d, q.array_data)
|
391
|
-
|
392
|
-
d = [0,1,256,
|
393
|
-
q.select_collect!([
|
394
|
-
assert_equal(d, q.array_data)
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
i
|
407
|
-
|
408
|
-
|
409
|
-
i
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
i
|
422
|
-
|
423
|
-
|
424
|
-
i
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
i
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
i
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
i
|
452
|
-
|
453
|
-
|
454
|
-
i
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
i = q.select_find_indexes([:all, 2], 107)
|
485
|
-
assert_equal([], i)
|
486
|
-
end
|
487
|
-
end
|
1
|
+
require_relative '../lib/flex_array'
|
2
|
+
gem 'minitest'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
|
5
|
+
class FlexArrayEachTester < Minitest::Test
|
6
|
+
|
7
|
+
def test_each
|
8
|
+
d = [0,1,2,3,4,5,6,7,8]
|
9
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
10
|
+
it = d.each
|
11
|
+
|
12
|
+
result = q.each do |v|
|
13
|
+
assert_equal(it.next, v)
|
14
|
+
end
|
15
|
+
|
16
|
+
assert_equal(result, q)
|
17
|
+
|
18
|
+
it = q.each
|
19
|
+
|
20
|
+
result = d.each do |v|
|
21
|
+
assert_equal(it.next, v)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_select_each
|
26
|
+
d = [0,1,2,3,4,5,6,7,8]
|
27
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
28
|
+
it = d.each
|
29
|
+
|
30
|
+
result = q.select_each([:all]) do |v|
|
31
|
+
assert_equal(it.next, v)
|
32
|
+
end
|
33
|
+
|
34
|
+
assert_equal(result, q)
|
35
|
+
|
36
|
+
it = q.select_each([:all])
|
37
|
+
d.each do |v|
|
38
|
+
assert_equal(it.next, v)
|
39
|
+
end
|
40
|
+
|
41
|
+
it = d.each
|
42
|
+
|
43
|
+
result = q.select_each([:all, :all]) do |v|
|
44
|
+
assert_equal(it.next, v)
|
45
|
+
end
|
46
|
+
|
47
|
+
d = [0,1,2]
|
48
|
+
it = d.each
|
49
|
+
|
50
|
+
result = q.select_each([0, :all]) do |v|
|
51
|
+
assert_equal(it.next, v)
|
52
|
+
end
|
53
|
+
|
54
|
+
d = [0,3,6]
|
55
|
+
it = d.each
|
56
|
+
|
57
|
+
result = q.select_each([:all, 0]) do |v|
|
58
|
+
assert_equal(it.next, v)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_each_with_index
|
63
|
+
d1 = [0,1,2,3,4,5,6,7,8]
|
64
|
+
d2 = [[0,0], [0,1], [0,2],
|
65
|
+
[1,0], [1,1], [1,2],
|
66
|
+
[2,0], [2,1], [2,2]]
|
67
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
68
|
+
i1 = d1.each
|
69
|
+
i2 = d2.each
|
70
|
+
|
71
|
+
q.each_with_index do |v, i|
|
72
|
+
assert_equal(v, i1.next)
|
73
|
+
assert_equal(i, i2.next)
|
74
|
+
end
|
75
|
+
|
76
|
+
iq = q.each_with_index
|
77
|
+
i2 = d2.each
|
78
|
+
|
79
|
+
d1.each do |v|
|
80
|
+
a,b = iq.next
|
81
|
+
assert_equal(a, v)
|
82
|
+
assert_equal(b, i2.next)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_select_each_with_index
|
87
|
+
d1 = [0,1,2,3,4,5,6,7,8]
|
88
|
+
d2 = [[0,0], [0,1], [0,2],
|
89
|
+
[1,0], [1,1], [1,2],
|
90
|
+
[2,0], [2,1], [2,2]]
|
91
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
92
|
+
i1 = d1.each
|
93
|
+
i2 = d2.each
|
94
|
+
|
95
|
+
q.select_each_with_index([:all]) do |v, i|
|
96
|
+
assert_equal(v, i1.next)
|
97
|
+
assert_equal(i, i2.next)
|
98
|
+
end
|
99
|
+
|
100
|
+
iq = q.select_each_with_index([:all])
|
101
|
+
i2 = d2.each
|
102
|
+
|
103
|
+
d1.each do |v|
|
104
|
+
a,b = iq.next
|
105
|
+
assert_equal(a, v)
|
106
|
+
assert_equal(b, i2.next)
|
107
|
+
end
|
108
|
+
|
109
|
+
i1 = d1.each
|
110
|
+
i2 = d2.each
|
111
|
+
|
112
|
+
q.select_each_with_index([:all, :all]) do |v, i|
|
113
|
+
assert_equal(v, i1.next)
|
114
|
+
assert_equal(i, i2.next)
|
115
|
+
end
|
116
|
+
|
117
|
+
iq = q.select_each_with_index([:all, :all])
|
118
|
+
i2 = d2.each
|
119
|
+
|
120
|
+
d1.each do |v|
|
121
|
+
a,b = iq.next
|
122
|
+
assert_equal(a, v)
|
123
|
+
assert_equal(b, i2.next)
|
124
|
+
end
|
125
|
+
|
126
|
+
d1 = [0,1,2]
|
127
|
+
d2 = [[0,0], [0,1], [0,2]]
|
128
|
+
|
129
|
+
i1 = d1.each
|
130
|
+
i2 = d2.each
|
131
|
+
|
132
|
+
q.select_each_with_index([0, :all]) do |v, i|
|
133
|
+
assert_equal(v, i1.next)
|
134
|
+
assert_equal(i, i2.next)
|
135
|
+
end
|
136
|
+
|
137
|
+
d1 = [0,3,6]
|
138
|
+
d2 = [[0,0], [1,0], [2,0]]
|
139
|
+
|
140
|
+
i1 = d1.each
|
141
|
+
i2 = d2.each
|
142
|
+
|
143
|
+
q.select_each_with_index([:all, 0]) do |v, i|
|
144
|
+
assert_equal(v, i1.next)
|
145
|
+
assert_equal(i, i2.next)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_each_raw
|
150
|
+
d0 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
151
|
+
d1 = [100, 101, 102, 103, 104, 105, 106, 107, 108]
|
152
|
+
d2 = [[0,0], [0,1], [0,2],
|
153
|
+
[1,0], [1,1], [1,2],
|
154
|
+
[2,0], [2,1], [2,2]]
|
155
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
|
156
|
+
i0 = d0.each
|
157
|
+
i1 = d1.each
|
158
|
+
i2 = d2.each
|
159
|
+
|
160
|
+
q._each_raw do |i, posn|
|
161
|
+
assert_equal(posn, i0.next)
|
162
|
+
assert_equal(q.array_data[posn], i1.next)
|
163
|
+
assert_equal(i, i2.next)
|
164
|
+
end
|
165
|
+
|
166
|
+
iq = q._each_raw
|
167
|
+
i1 = d1.each
|
168
|
+
i2 = d2.each
|
169
|
+
|
170
|
+
d0.each do |v|
|
171
|
+
i, posn = iq.next
|
172
|
+
assert_equal(posn, v)
|
173
|
+
assert_equal(q.array_data[posn], i1.next)
|
174
|
+
assert_equal(i, i2.next)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_select_each_raw
|
179
|
+
d0 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
180
|
+
d1 = [100, 101, 102, 103, 104, 105, 106, 107, 108]
|
181
|
+
d2 = [[0,0], [0,1], [0,2],
|
182
|
+
[1,0], [1,1], [1,2],
|
183
|
+
[2,0], [2,1], [2,2]]
|
184
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
|
185
|
+
i0 = d0.each
|
186
|
+
i1 = d1.each
|
187
|
+
i2 = d2.each
|
188
|
+
|
189
|
+
q._select_each_raw([:all]) do |i, posn|
|
190
|
+
assert_equal(posn, i0.next)
|
191
|
+
assert_equal(q.array_data[posn], i1.next)
|
192
|
+
assert_equal(i, i2.next)
|
193
|
+
end
|
194
|
+
|
195
|
+
i0 = d0.each
|
196
|
+
i1 = d1.each
|
197
|
+
i2 = d2.each
|
198
|
+
|
199
|
+
q._select_each_raw([:all, :all]) do |i, posn|
|
200
|
+
assert_equal(posn, i0.next)
|
201
|
+
assert_equal(q.array_data[posn], i1.next)
|
202
|
+
assert_equal(i, i2.next)
|
203
|
+
end
|
204
|
+
|
205
|
+
d0 = [0, 1, 2]
|
206
|
+
d1 = [100, 101, 102]
|
207
|
+
d2 = [[0,0], [0,1], [0,2]]
|
208
|
+
|
209
|
+
i0 = d0.each
|
210
|
+
i1 = d1.each
|
211
|
+
i2 = d2.each
|
212
|
+
|
213
|
+
q._select_each_raw([0, :all]) do |i, posn|
|
214
|
+
assert_equal(posn, i0.next)
|
215
|
+
assert_equal(q.array_data[posn], i1.next)
|
216
|
+
assert_equal(i, i2.next)
|
217
|
+
end
|
218
|
+
|
219
|
+
d0 = [0, 3, 6]
|
220
|
+
d1 = [100, 103, 106]
|
221
|
+
d2 = [[0,0], [1,0], [2,0]]
|
222
|
+
|
223
|
+
i0 = d0.each
|
224
|
+
i1 = d1.each
|
225
|
+
i2 = d2.each
|
226
|
+
|
227
|
+
q._select_each_raw([:all, 0]) do |i, posn|
|
228
|
+
assert_equal(posn, i0.next)
|
229
|
+
assert_equal(q.array_data[posn], i1.next)
|
230
|
+
assert_equal(i, i2.next)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
def test_cycle
|
235
|
+
d = [0,1,2,3,4,5,6,7,8,0,1,2]
|
236
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
237
|
+
i = 0
|
238
|
+
|
239
|
+
q.cycle do |v|
|
240
|
+
assert_equal(d[i], v)
|
241
|
+
i += 1
|
242
|
+
break if i == 12
|
243
|
+
end
|
244
|
+
|
245
|
+
iq = q.cycle
|
246
|
+
|
247
|
+
d.each do |v|
|
248
|
+
assert_equal(iq.next, v)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_select_cycle
|
253
|
+
d = [0,1,2,3,4,5,6,7,8,0,1,2]
|
254
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
255
|
+
i = 0
|
256
|
+
|
257
|
+
q.select_cycle([:all]) do |v|
|
258
|
+
assert_equal(d[i], v)
|
259
|
+
i += 1
|
260
|
+
break if i == 12
|
261
|
+
end
|
262
|
+
|
263
|
+
iq = q.select_cycle([:all])
|
264
|
+
|
265
|
+
d.each do |v|
|
266
|
+
assert_equal(iq.next, v)
|
267
|
+
end
|
268
|
+
|
269
|
+
i = 0
|
270
|
+
|
271
|
+
q.select_cycle([:all, :all]) do |v|
|
272
|
+
assert_equal(d[i], v)
|
273
|
+
i += 1
|
274
|
+
break if i == 12
|
275
|
+
end
|
276
|
+
|
277
|
+
iq = q.select_cycle([:all, :all])
|
278
|
+
|
279
|
+
d.each do |v|
|
280
|
+
assert_equal(iq.next, v)
|
281
|
+
end
|
282
|
+
|
283
|
+
d = [0,1,2,0,1,2,0,1,2,0,1,2]
|
284
|
+
i = 0
|
285
|
+
|
286
|
+
q.select_cycle([0, :all]) do |v|
|
287
|
+
assert_equal(d[i], v)
|
288
|
+
i += 1
|
289
|
+
break if i == 12
|
290
|
+
end
|
291
|
+
|
292
|
+
iq = q.select_cycle([0, :all])
|
293
|
+
|
294
|
+
d.each do |v|
|
295
|
+
assert_equal(iq.next, v)
|
296
|
+
end
|
297
|
+
|
298
|
+
d = [0,3,6,0,3,6,0,3,6,0,3,6]
|
299
|
+
i = 0
|
300
|
+
|
301
|
+
q.select_cycle([:all, 0]) do |v|
|
302
|
+
assert_equal(d[i], v)
|
303
|
+
i += 1
|
304
|
+
break if i == 12
|
305
|
+
end
|
306
|
+
|
307
|
+
iq = q.select_cycle([:all, 0])
|
308
|
+
|
309
|
+
d.each do |v|
|
310
|
+
assert_equal(iq.next, v)
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
def test_flatten_collect
|
315
|
+
d = [0,1,4,9,16,25,36,49,64]
|
316
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
317
|
+
|
318
|
+
b = q.flatten_collect {|v| v * v }
|
319
|
+
assert_equal(d, b)
|
320
|
+
end
|
321
|
+
|
322
|
+
def test_select_flatten_collect
|
323
|
+
d = [0,1,4,9,16,25,36,49,64]
|
324
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
325
|
+
|
326
|
+
b = q.select_flatten_collect([:all]) {|v| v * v }
|
327
|
+
assert_equal(d, b)
|
328
|
+
|
329
|
+
b = q.select_flatten_collect([:all, :all]) {|v| v * v }
|
330
|
+
assert_equal(d, b)
|
331
|
+
|
332
|
+
d = [0,1,4]
|
333
|
+
b = q.select_flatten_collect([0, :all]) {|v| v * v }
|
334
|
+
assert_equal(d, b)
|
335
|
+
|
336
|
+
d = [0,9,36]
|
337
|
+
b = q.select_flatten_collect([:all, 0]) {|v| v * v }
|
338
|
+
assert_equal(d, b)
|
339
|
+
end
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
def test_collect
|
346
|
+
d = [0,1,4,9,16,25,36,49,64]
|
347
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
348
|
+
a = q.collect {|v| v * v }
|
349
|
+
assert_equal(d, a.array_data)
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_select_collect
|
353
|
+
d = [0,1,4,9,16,25,36,49,64]
|
354
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
355
|
+
a = q.select_collect([:all]) {|v| v * v }
|
356
|
+
assert_equal(d, a.array_data)
|
357
|
+
|
358
|
+
d = [0,1,16,81,256,625,1296,2401,4096]
|
359
|
+
b = a.select_collect([:all, :all]) {|v| v * v }
|
360
|
+
assert_equal(d, b.array_data)
|
361
|
+
|
362
|
+
d = [0,1,256,81,256,625,1296,2401,4096]
|
363
|
+
c = b.select_collect([0, :all]) {|v| v * v }
|
364
|
+
assert_equal(d, c.array_data)
|
365
|
+
|
366
|
+
d = [0,1,256,6561,256,625,1679616,2401,4096]
|
367
|
+
e = c.select_collect([:all, 0]) {|v| v * v }
|
368
|
+
assert_equal(d, e.array_data)
|
369
|
+
end
|
370
|
+
|
371
|
+
def test_collect_em
|
372
|
+
d = [0,1,4,9,16,25,36,49,64]
|
373
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
374
|
+
q.collect! {|v| v * v }
|
375
|
+
assert_equal(d, q.array_data)
|
376
|
+
end
|
377
|
+
|
378
|
+
def test_select_collect_em
|
379
|
+
d = [0,1,4,9,16,25,36,49,64]
|
380
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
|
381
|
+
q.select_collect!([:all]) {|v| v * v }
|
382
|
+
assert_equal(d, q.array_data)
|
383
|
+
|
384
|
+
d = [0,1,16,81,256,625,1296,2401,4096]
|
385
|
+
q.select_collect!([:all, :all]) {|v| v * v }
|
386
|
+
assert_equal(d, q.array_data)
|
387
|
+
|
388
|
+
d = [0,1,256,81,256,625,1296,2401,4096]
|
389
|
+
q.select_collect!([0, :all]) {|v| v * v }
|
390
|
+
assert_equal(d, q.array_data)
|
391
|
+
|
392
|
+
d = [0,1,256,6561,256,625,1679616,2401,4096]
|
393
|
+
q.select_collect!([:all, 0]) {|v| v * v }
|
394
|
+
assert_equal(d, q.array_data)
|
395
|
+
end
|
396
|
+
|
397
|
+
def test_find_index
|
398
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
|
399
|
+
i = q.find_index(107)
|
400
|
+
assert_equal([2, 1], i)
|
401
|
+
|
402
|
+
i = q.find_index { |v| v > 106 }
|
403
|
+
assert_equal([2, 1], i)
|
404
|
+
|
405
|
+
i = q.find_index(300)
|
406
|
+
assert_nil(i)
|
407
|
+
|
408
|
+
i = q.find_index { |v| v > 206 }
|
409
|
+
assert_nil(i)
|
410
|
+
end
|
411
|
+
|
412
|
+
def test_select_find_index
|
413
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
|
414
|
+
i = q.select_find_index([:all], 107)
|
415
|
+
assert_equal([2, 1], i)
|
416
|
+
|
417
|
+
i = q.select_find_index([:all, :all], 107)
|
418
|
+
assert_equal([2, 1], i)
|
419
|
+
|
420
|
+
i = q.select_find_index([0, :all], 107)
|
421
|
+
assert_nil(i)
|
422
|
+
|
423
|
+
i = q.select_find_index([1, :all], 107)
|
424
|
+
assert_nil(i)
|
425
|
+
|
426
|
+
i = q.select_find_index([2, :all], 107)
|
427
|
+
assert_equal([2, 1], i)
|
428
|
+
|
429
|
+
i = q.select_find_index([:all, 0], 107)
|
430
|
+
assert_nil(i)
|
431
|
+
|
432
|
+
i = q.select_find_index([:all, 1], 107)
|
433
|
+
assert_equal([2, 1], i)
|
434
|
+
|
435
|
+
i = q.select_find_index([:all, 2], 107)
|
436
|
+
assert_nil(i)
|
437
|
+
end
|
438
|
+
|
439
|
+
def test_find_indexes
|
440
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
|
441
|
+
i = q.find_indexes(107)
|
442
|
+
assert_equal([[2, 1]], i)
|
443
|
+
|
444
|
+
i = q.find_indexes { |v| v == 107 }
|
445
|
+
assert_equal([[2, 1]], i)
|
446
|
+
|
447
|
+
i = q.find_indexes { |v| v > 106 }
|
448
|
+
assert_equal([[2, 1], [2, 2]], i)
|
449
|
+
|
450
|
+
i = q.find_indexes(307)
|
451
|
+
assert_equal([], i)
|
452
|
+
|
453
|
+
i = q.find_indexes { |v| v > 406 }
|
454
|
+
assert_equal([], i)
|
455
|
+
end
|
456
|
+
|
457
|
+
def test_select_find_indexes
|
458
|
+
q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
|
459
|
+
i = q.select_find_indexes([:all], 107)
|
460
|
+
assert_equal([[2, 1]], i)
|
461
|
+
|
462
|
+
i = q.select_find_indexes([:all, :all], 107)
|
463
|
+
assert_equal([[2, 1]], i)
|
464
|
+
|
465
|
+
i = q.select_find_indexes([0, :all], 107)
|
466
|
+
assert_equal([], i)
|
467
|
+
|
468
|
+
i = q.select_find_indexes([1, :all], 107)
|
469
|
+
assert_equal([], i)
|
470
|
+
|
471
|
+
i = q.select_find_indexes([2, :all], 107)
|
472
|
+
assert_equal([[2, 1]], i)
|
473
|
+
|
474
|
+
i = q.select_find_indexes([:all, 0], 107)
|
475
|
+
assert_equal([], i)
|
476
|
+
|
477
|
+
i = q.select_find_indexes([:all, 1], 107)
|
478
|
+
assert_equal([[2, 1]], i)
|
479
|
+
|
480
|
+
i = q.select_find_indexes([:all, 2], 107)
|
481
|
+
assert_equal([], i)
|
482
|
+
end
|
483
|
+
end
|