flex_array 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -15
  3. data/.reek.yml +17 -0
  4. data/README.md +11 -0
  5. data/bench/benchmark.rb +243 -0
  6. data/bench/results.txt +97 -0
  7. data/flex_array.gemspec +5 -9
  8. data/lib/flex_array.rb +13 -30
  9. data/lib/flex_array/array.rb +9 -24
  10. data/lib/flex_array/flex_array_append.rb +4 -23
  11. data/lib/flex_array/flex_array_each.rb +33 -208
  12. data/lib/flex_array/flex_array_forever.rb +6 -4
  13. data/lib/flex_array/flex_array_index.rb +4 -29
  14. data/lib/flex_array/flex_array_new.rb +9 -56
  15. data/lib/flex_array/flex_array_process.rb +23 -63
  16. data/lib/flex_array/flex_array_reshape.rb +13 -23
  17. data/lib/flex_array/flex_array_transpose.rb +7 -16
  18. data/lib/flex_array/flex_array_validate.rb +5 -14
  19. data/lib/flex_array/integer.rb +4 -13
  20. data/lib/flex_array/object.rb +4 -13
  21. data/lib/flex_array/range.rb +4 -13
  22. data/lib/flex_array/spec_array.rb +5 -9
  23. data/lib/flex_array/spec_component.rb +14 -22
  24. data/lib/flex_array/version.rb +1 -2
  25. data/rakefile.rb +0 -10
  26. data/reek.txt +9 -2
  27. data/tests/array_test.rb +0 -4
  28. data/tests/flex_array_append_test.rb +0 -23
  29. data/tests/flex_array_each_test.rb +483 -487
  30. data/tests/flex_array_index_test.rb +0 -4
  31. data/tests/flex_array_new_test.rb +0 -4
  32. data/tests/flex_array_reshape_test.rb +4 -4
  33. data/tests/flex_array_test.rb +0 -4
  34. data/tests/flex_array_transpose_test.rb +0 -4
  35. data/tests/flex_array_validate_test.rb +0 -4
  36. data/tests/integer_test.rb +0 -4
  37. data/tests/object_test.rb +0 -4
  38. data/tests/range_test.rb +0 -4
  39. data/tests/spec_array_test.rb +0 -4
  40. data/tests/spec_component_test.rb +0 -4
  41. metadata +12 -43
  42. data/docs/Flex_Array_UG.odt +0 -0
  43. data/docs/Flex_Array_UG_Version_0_3_0.pdf +0 -0
  44. data/flex_array.reek +0 -109
  45. data/sire.rb +0 -82
@@ -1,33 +1,22 @@
1
- #Extensions to Array needed to support flex array.
1
+ # Extensions to Array needed to support flex array.
2
+
2
3
  class Array
3
- #Get the specifications of the array index values.
4
- #<br>Returns
5
- #* An array with one range in it.
4
+ # Get the specifications of the array index values.
6
5
  def limits
7
6
  [0...self.length]
8
7
  end
9
8
 
10
- #Quick access to the limits for internal use.
11
- #<br>Returns
12
- #* An array with one spec component in it.
9
+ # Quick access to the limits for internal use.
13
10
  def array_specs
14
11
  SpecArray.new([0...self.length])
15
12
  end
16
13
 
17
- #Quick access to the array data for internal use.
18
- #<br>Returns
19
- #* An array -- self
14
+ # Quick access to the array data for internal use.
20
15
  def array_data
21
16
  self
22
17
  end
23
18
 
24
- #Convert this array to an range index against the spec.
25
- #<br>Parameters
26
- #* spec - The spec component used to validate this index.
27
- #<br>Returns
28
- #* A range.
29
- #<br>Exceptions
30
- #* IndexError if the range is not valid.
19
+ # Convert this array to an range index against the spec.
31
20
  def to_index_range(spec)
32
21
  spec_max = spec.max
33
22
 
@@ -43,13 +32,9 @@ class Array
43
32
  end
44
33
  end
45
34
 
46
- #Return this flex array as a flex array!
47
- #<br>Returns
48
- #* A flex array that references this array.
49
- #<br>Note
50
- #* To avoid a shared reference, use my_array.dup.to_flex_array or
51
- # FlexArray.new_from_array(my_array.dup) instead.
35
+ # Return this flex array as a flex array!
52
36
  def to_flex_array
53
37
  FlexArray.new_from_array(self)
54
38
  end
55
- end
39
+
40
+ end
@@ -1,12 +1,7 @@
1
- #* flex_array_append.rb - The flexible array class << and related methods.
1
+ # Append method support for the flex array.
2
+
2
3
  class FlexArray
3
- #Append to the flex array.
4
- #<br>Parameters
5
- #* data - the data being appended to this array.
6
- #<br>Returns
7
- #* The array spec of the array being appended.
8
- #<br>Exceptions
9
- #* ArgumentError if the data may not be appended.
4
+ # Append to the flex array.
10
5
  def << (data)
11
6
  fail "Cannot append to a transposed array." if @transposed
12
7
  specs = get_append_specs(data = data.in_array)
@@ -15,22 +10,8 @@ class FlexArray
15
10
  self
16
11
  end
17
12
 
18
- #Make a copy of the other's data.
19
- #<br>Parameters
20
- #* other - The flex array whose data is to be copied.
21
- def copy_data(other)
22
- fail ArgumentError, "Incompatible data copy." unless compatible?(other)
23
- @array_data = other.array_data.dup
24
- end
25
-
26
13
  private
27
- #Extract and validate the append array_spec
28
- #<br>Parameters
29
- #* data - the data being appended to this array.
30
- #<br>Returns
31
- #* The array spec of the array being appended.
32
- #<br>Exceptions
33
- #* ArgumentError if the data may not be appended.
14
+ # Extract and validate the append array_spec
34
15
  def get_append_specs(data)
35
16
  spec_len = (specs = data.array_specs).length
36
17
 
@@ -1,21 +1,9 @@
1
- #* flex_array_each.rb - The flexible array class each and related methods.
2
- # :reek:RepeatedConditional - @transposed determines if short cuts are allowed.
1
+ # Support for each and related methods for the flex array.
2
+
3
3
  class FlexArray
4
4
  include Enumerable
5
5
 
6
- #Retrieve data from the array endlessly repeating as needed.
7
- #<br>Parameters
8
- #* count - The number of times to cycle through the flex array. Defaults to
9
- # cycling forever.
10
- #* block - The optional block to be executed for each selected array element.
11
- # The return value is the last value returned by the block. If the block
12
- # is not present, then an enumerator object is returned.
13
- #<br>Returns
14
- #* nil or an enumerator if no block is provided.
15
- #<br>Block Arguments
16
- #* value - Each value selected by the iteration.
17
- #<br>Endemic Code Smells
18
- #* :reek:NestedIterators
6
+ # Retrieve data from the array endlessly repeating as needed.
19
7
  def cycle(count = FOREVER, &block)
20
8
  if block_given?
21
9
  if @transposed && length > 0
@@ -34,22 +22,7 @@ class FlexArray
34
22
  end
35
23
  end
36
24
 
37
- #Retrieve data from a subset of the flex array endlessly repeating as needed.
38
- #<br>Parameters
39
- #* indexes - An array with as many entries as the flexible array has
40
- # dimensions. See [] for more details. Note that since indexes is NOT
41
- # a splat parameter, it must be passed as an array explicitly.
42
- #* count - The number of times to cycle through the flex array. Defaults to
43
- # cycling forever.
44
- #* block - The optional block to be executed for each selected array element.
45
- # The return value is the last value returned by the block. If the block
46
- # is not present, then an enumerator object is returned.
47
- #<br>Returns
48
- #* nil or an enumerator if no block is provided.
49
- #<br>Block Arguments
50
- #* value - Each value selected by the iteration.
51
- #<br>Endemic Code Smells
52
- #* :reek:NestedIterators
25
+ # Retrieve data from a subset of the flex array endlessly repeating as needed.
53
26
  def select_cycle(indexes, count = FOREVER, &block)
54
27
  validate_index_count(indexes)
55
28
 
@@ -68,14 +41,7 @@ class FlexArray
68
41
  end
69
42
  end
70
43
 
71
- #Process the standard each operator.
72
- #<br>Parameters
73
- #* block - The optional block to be executed for each selected array element.
74
- #<br>Returns
75
- #* If the block is not present, then an enumerator object is returned.
76
- # Otherwise the flex array is returned.
77
- #<br>Block Arguments
78
- #* value - Each value selected by the iteration.
44
+ # Process the standard each operator.
79
45
  def each(&block)
80
46
  if block_given?
81
47
  if @transposed
@@ -90,17 +56,7 @@ class FlexArray
90
56
  end
91
57
  end
92
58
 
93
- #Process the enhanced select_each operator.
94
- #<br>Parameters
95
- #* indexes - An array with as many entries as the flexible array has
96
- # dimensions. See [] for more details. Note that since indexes is NOT
97
- # a splat parameter, it must be passed as an array explicitly
98
- #* block - The optional block to be executed for each selected array element.
99
- #<br>Returns
100
- #* If the block is not present, then an enumerator object is returned.
101
- # Otherwise the flex array is returned.
102
- #<br>Block Arguments
103
- #* value - Each value selected by the iteration.
59
+ # Process the enhanced select_each operator.
104
60
  def select_each(indexes, &block)
105
61
  validate_index_count(indexes)
106
62
 
@@ -112,15 +68,7 @@ class FlexArray
112
68
  end
113
69
  end
114
70
 
115
- #Process the standard each_with_index operator.
116
- #<br>Parameters
117
- #* block - The optional block to be executed for each selected array element.
118
- #<br>Returns
119
- #* If the block is not present, then an enumerator object is returned.
120
- # Otherwise the flex array is returned.
121
- #<br>Block Arguments
122
- #* value - Each value selected by the iteration.
123
- #* index - An array with the full index of the selected value.
71
+ # Process the standard each_with_index operator.
124
72
  def each_with_index(&block)
125
73
  if block_given?
126
74
  process_all {|index, posn| block.call(@array_data[posn], index)}
@@ -130,18 +78,7 @@ class FlexArray
130
78
  end
131
79
  end
132
80
 
133
- #Process the enhanced select_each_with_index operator.
134
- #<br>Parameters
135
- #* indexes - An array with as many entries as the flexible array has
136
- # dimensions. See [] for more details. Note that since indexes is NOT
137
- # a splat parameter, it must be passed as an array explicitly.
138
- #* block - The optional block to be executed for each selected array element.
139
- #<br>Returns
140
- #* If the block is not present, then an enumerator object is returned.
141
- # Otherwise the flex array is returned.
142
- #<br>Block Arguments
143
- #* value - Each value selected by the iteration.
144
- #* index - An array with the full index of the selected value.
81
+ # Process the enhanced select_each_with_index operator.
145
82
  def select_each_with_index(indexes, &block)
146
83
  validate_index_count(indexes)
147
84
 
@@ -153,18 +90,8 @@ class FlexArray
153
90
  end
154
91
  end
155
92
 
156
- #A specialized each variant that passes the low level data, the index
157
- #and the position to the block.
158
- #<br>Parameters
159
- #* block - The optional block to be executed for each selected array element.
160
- # The return value is the last value returned by the block. If the block
161
- # is not present, then an enumerator object is returned.
162
- #<br>Returns
163
- #* If the block is not present, then an enumerator object is returned.
164
- # Otherwise the flex array is returned.
165
- #<br>Block Arguments
166
- #* index - An array with the full index of the selected value.
167
- #* posn - The position of the data in the low level data store.
93
+ # A specialized each variant that passes the low level data, the index
94
+ # and the position to the block.
168
95
  def _each_raw(&block)
169
96
  if block_given?
170
97
  process_all {|index, posn| block.call(index, posn)}
@@ -174,21 +101,8 @@ class FlexArray
174
101
  end
175
102
  end
176
103
 
177
- #An enhanced specialized each variant that passes the low level data,
178
- #the index and the position to the block.
179
- #<br>Parameters
180
- #* indexes - An array with as many entries as the flexible array has
181
- # dimensions. See [] for more details. Note that since indexes is NOT
182
- # a splat parameter, it must be passed as an array explicitly.
183
- #* block - The optional block to be executed for each selected array element.
184
- # The return value is the last value returned by the block. If the block
185
- # is not present, then an enumerator object is returned.
186
- #<br>Returns
187
- #* If the block is not present, then an enumerator object is returned.
188
- # Otherwise the flex array is returned.
189
- #<br>Block Arguments
190
- #* index - An array with the full index of the selected value.
191
- #* posn - The position of the data in the low level data store.
104
+ # An enhanced specialized each variant that passes the low level data,
105
+ # the index and the position to the block.
192
106
  def _select_each_raw(indexes, &block)
193
107
  validate_index_count(indexes)
194
108
 
@@ -202,47 +116,21 @@ class FlexArray
202
116
 
203
117
  alias_method :flatten_collect, :collect
204
118
 
205
- #The flex array version of collect that returns a flex array.
206
- #<br>Parameters
207
- #* block - The optional block to be executed for each selected array element.
208
- #<br>Returns
209
- #* An array of the computed objects retuned by the block.
210
- #<br>Block Arguments
211
- #* value - Each value selected by the iteration.
119
+ # The flex array version of collect that returns a flex array.
212
120
  def collect(&block)
213
121
  result = self.dup
214
122
  result.collect!(&block)
215
123
  end
216
124
 
217
- #The flex array version of collect that accepts an optional set of indexes
218
- #to select the data being collected into a flex array.
219
- #<br>Parameters
220
- #* indexes - An array with as many entries as the flexible array has
221
- # dimensions. See [] for more details. Note that since indexes is NOT
222
- # a splat parameter, it must be passed as an array explicitly.
223
- #* block - The optional block to be executed for each selected array element.
224
- #<br>Returns
225
- #* An array of the computed objects retuned by the block.
226
- # If the block is not present, an enumerator object is returned.
227
- #<br>Block Arguments
228
- #* value - Each value selected by the iteration.
125
+ # The flex array version of collect that accepts an optional set of indexes
126
+ # to select the data being collected into a flex array.
229
127
  def select_collect(indexes, &block)
230
128
  result = self.dup
231
129
  result.select_collect!(indexes, &block)
232
130
  end
233
131
 
234
- #The flex array version of collect that accepts an optional set of indexes
235
- #to select the data being collected into a standard array.
236
- #<br>Parameters
237
- #* indexes - An array with as many entries as the flexible array has
238
- # dimensions. See [] for more details. Note that since indexes is NOT
239
- # a splat parameter, it must be passed as an array explicitly.
240
- #* block - The optional block to be executed for each selected array element.
241
- #<br>Returns
242
- #* An array of the computed objects retuned by the block.
243
- # If the block is not present, an enumerator object is returned.
244
- #<br>Block Arguments
245
- #* value - Each value selected by the iteration.
132
+ # The flex array version of collect that accepts an optional set of indexes
133
+ # to select the data being collected into a standard array.
246
134
  def select_flatten_collect(indexes, &block)
247
135
  validate_index_count(indexes)
248
136
 
@@ -255,15 +143,7 @@ class FlexArray
255
143
  end
256
144
  end
257
145
 
258
- #The flex array version of collect!
259
- #<br>Parameters
260
- #* block - The *required* block to be executed for each selected array element.
261
- # The return value is the last value returned by the block. If the block
262
- # is not present, an enumerator object is returned.
263
- #<br>Returns
264
- #* self
265
- #<br>Block Arguments
266
- #* value - Each value selected by the iteration.
146
+ # The flex array version of collect!
267
147
  def collect!(&block)
268
148
  fail ArgumentError, "A block is required." unless block_given?
269
149
 
@@ -277,19 +157,8 @@ class FlexArray
277
157
  self
278
158
  end
279
159
 
280
- #The enhanced flex array version of collect! that accepts a set of indexes
281
- #to select the data being collected.
282
- #<br>Parameters
283
- #* indexes - An array with as many entries as the flexible array has
284
- # dimensions. See [] for more details. Note that since indexes is NOT
285
- # a splat parameter, it must be passed as an array explicitly.
286
- #* block - The *required* block to be executed for each selected array element.
287
- # The return value is the last value returned by the block. If the block
288
- # is not present, an enumerator object is returned.
289
- #<br>Returns
290
- #* self
291
- #<br>Block Arguments
292
- #* value - Each value selected by the iteration.
160
+ # The enhanced flex array version of collect! that accepts a set of indexes
161
+ # to select the data being collected.
293
162
  def select_collect!(indexes, &block)
294
163
  fail ArgumentError, "A block is required." unless block_given?
295
164
  validate_index_count(indexes)
@@ -300,17 +169,9 @@ class FlexArray
300
169
  self
301
170
  end
302
171
 
303
- #The flex array version of find_index. This returns the
304
- #coordinates of the first object that matches the search object or is
305
- #flagged true by the search block.
306
- #<br>Parameters
307
- #* object - The optional value to search for.
308
- #* block - The optional block to be executed for each selected array element.
309
- # If the block or object are not present, an enumerator object is returned.
310
- #<br>Returns
311
- #* The index of the first place that matched or nil if none matched.
312
- #<br>Block Arguments
313
- #* value - Each value selected by the iteration.
172
+ # The flex array version of find_index. This returns the coordinates of the
173
+ # first object that matches the search object or is flagged true by the
174
+ # search block.
314
175
  def find_index(value = nil, &block)
315
176
  blk = get_find_block(value, &block)
316
177
 
@@ -327,20 +188,9 @@ class FlexArray
327
188
  end
328
189
  end
329
190
 
330
- #The enhanced flex array version of find_index. This returns the
331
- #coordinates of the first object that matches the search object or is
332
- #flagged true by the search block.
333
- #<br>Parameters
334
- #* indexes - An array with as many entries as the flexible array has
335
- # dimensions. See [] for more details. Note that since indexes is NOT
336
- # a splat parameter, it must be passed as an array explicitly.
337
- #* object - The optional value to search for.
338
- #* block - The optional block to be executed for each selected array element.
339
- # If the block or object are not present, an enumerator object is returned.
340
- #<br>Returns
341
- #* The index of the first place that matched or nil if none matched.
342
- #<br>Block Arguments
343
- #* value - Each value selected by the iteration.
191
+ # The enhanced flex array version of find_index. This returns the coordinates
192
+ # of the first object that matches the search object or is flagged true by
193
+ # the search block.
344
194
  def select_find_index(indexes, value = nil, &block)
345
195
  validate_index_count(indexes)
346
196
  blk = get_find_block(value, &block)
@@ -358,17 +208,9 @@ class FlexArray
358
208
  end
359
209
  end
360
210
 
361
- #The improved flex array version of find_index. This returns the
362
- #coordinates of objects that match the search object or are
363
- #flagged true by the search block.
364
- #<br>Parameters
365
- #* object - The optional value to search for.
366
- #* block - The optional block to be executed for each selected array element.
367
- # If the block or object are not present, an enumerator object is returned.
368
- #<br>Returns
369
- #* An array of the indexes of the places that matched.
370
- #<br>Block Arguments
371
- #* value - Each value selected by the iteration.
211
+ # The improved flex array version of find_index. This returns the coordinates
212
+ # of objects that match the search object or are flagged true by the search
213
+ # block.
372
214
  def find_indexes(value = nil, &block)
373
215
  blk, result = get_find_block(value, &block), []
374
216
 
@@ -385,20 +227,9 @@ class FlexArray
385
227
  end
386
228
  end
387
229
 
388
- #The enhanced and improved flex array version of find_index. This returns the
389
- #coordinates of objects that match the search object or are
390
- #flagged true by the search block.
391
- #<br>Parameters
392
- #* indexes - An array with as many entries as the flexible array has
393
- # dimensions. See [] for more details. Note that since indexes is NOT
394
- # a splat parameter, it must be passed as an array explicitly.
395
- #* object - The optional value to search for.
396
- #* block - The optional block to be executed for each selected array element.
397
- # If the block or object are not present, an enumerator object is returned.
398
- #<br>Returns
399
- #* An array of the indexes of the places that matched.
400
- #<br>Block Arguments
401
- #* value - Each value selected by the iteration.
230
+ # The enhanced and improved flex array version of find_index. This returns
231
+ # the coordinates of objects that match the search object or are flagged true
232
+ # by the search block.
402
233
  def select_find_indexes(indexes, value = nil, &block)
403
234
  validate_index_count(indexes)
404
235
  blk, result = get_find_block(value, &block), []
@@ -418,16 +249,10 @@ class FlexArray
418
249
 
419
250
  private
420
251
 
421
- #A helper method to determine which block to use in the find_index family.
422
- #<br>Returns
423
- #* The block to use or nil.
424
- #<br>Endemic Code Smells
425
- #* :reek:NilCheck
252
+ # A helper method to determine which block to use in the find_index family.
426
253
  def get_find_block(value, &block)
427
254
  if block_given?
428
255
  block
429
- elsif value.nil?
430
- nil
431
256
  else
432
257
  lambda {|obj| obj == value }
433
258
  end