Class: CombinationIndex
- Inherits:
-
Object
- Object
- CombinationIndex
- Defined in:
- lib/pattern_expander/combination_index.rb
Instance Attribute Summary collapse
-
#element_lists ⇒ Object
readonly
Returns the value of attribute element_lists.
Instance Method Summary collapse
- #[](index_or_range) ⇒ Object
- #_element_list_sizes ⇒ Object
-
#_multi_array_indexes(i) ⇒ Object
converts a single index to several indexes ie converts from base ten (index) to a mixed base value with each place (element array) being a new base (size of the element array).
- #_sample_one ⇒ Object
- #_single_combination(i) ⇒ Object
-
#initialize(element_lists) ⇒ CombinationIndex
constructor
A new instance of CombinationIndex.
- #sample(quantity = 1) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(element_lists) ⇒ CombinationIndex
Returns a new instance of CombinationIndex.
4 5 6 |
# File 'lib/pattern_expander/combination_index.rb', line 4 def initialize(element_lists) @element_lists = element_lists end |
Instance Attribute Details
#element_lists ⇒ Object (readonly)
Returns the value of attribute element_lists.
2 3 4 |
# File 'lib/pattern_expander/combination_index.rb', line 2 def element_lists @element_lists end |
Instance Method Details
#[](index_or_range) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/pattern_expander/combination_index.rb', line 8 def [](index_or_range) if index_or_range.class == Range index_or_range.to_a.map { |index| _single_combination(index) } else _single_combination(index_or_range) end end |
#_element_list_sizes ⇒ Object
45 46 47 |
# File 'lib/pattern_expander/combination_index.rb', line 45 def _element_list_sizes element_lists.map(&:size) end |
#_multi_array_indexes(i) ⇒ Object
converts a single index to several indexes ie converts from base ten (index) to a mixed base value with each place (element array) being a new base (size of the element array)
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pattern_expander/combination_index.rb', line 33 def _multi_array_indexes(i) element_list_sizes_tmp = _element_list_sizes array_indexes = [] while element_list_size = element_list_sizes_tmp.pop i, remainder = i.divmod(element_list_size) array_indexes.unshift remainder end array_indexes end |
#_sample_one ⇒ Object
27 28 29 |
# File 'lib/pattern_expander/combination_index.rb', line 27 def _sample_one self[rand(size)] end |
#_single_combination(i) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pattern_expander/combination_index.rb', line 49 def _single_combination(i) combination = "" element_list_indexes = _multi_array_indexes(i) element_lists.each do |element_list| element_list_index = element_list_indexes.shift combination += element_list[element_list_index] end combination end |
#sample(quantity = 1) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/pattern_expander/combination_index.rb', line 16 def sample(quantity=1) return _sample_one if quantity == 1 result = [] quantity.times { result << _sample_one } result end |
#size ⇒ Object
23 24 25 |
# File 'lib/pattern_expander/combination_index.rb', line 23 def size _element_list_sizes.reduce(:*) end |