module Enumerable
Constants
- SINGLE_SIZE
Public Instance Methods
@note Uses HF::Range Returns the range of the first one-member multiset collecting the specified value at the given multiplicity. This is a collection such as [3, 3, 3 ,3], where there is multiple instances of one member. Formally it is a multiset with only one member. The default multiplicity returns the range of an mset of arbirtrary size. The default value is not specified it returns the range of the first mset with distinct values. @param value [Object] the element in the mset @param multiplicity [Integer] the number of times value appears in mset @return [Range] the first range where the subset occurs
# File lib/hf.rb, line 37 def find_mset(value, multiplicity=-1) is_multiplicity_negative = multiplicity < 0 enums_chunked_on_value = self.chunk{|i| i == value} sub_enums = enums_chunked_on_value.collect{|r| r.last} sub_enums_is_mset = if is_multiplicity_negative enums_chunked_on_value.collect{|r| r.first && not(r.single?) } else enums_chunked_on_value.collect{|r| r.first && (r.last.size >= multiplicity) } end first_mset_chunked_index = sub_enums_is_mset.find_index(true) return Range.new(self.count, self.count, true) if first_mset_chunked_index.nil? first_mset_offset = sub_enums.take(first_mset_chunked_index).flatten.size first_mset_size = is_multiplicity_negative ? sub_enums[first_mset_chunked_index].size : multiplicity Range.new(0, first_mset_size.pred).bump(first_mset_offset) end
Returns the only element in a 1-tuple/single. A more idiomatic and safer accessor than first when the Enumerator is expected to have only one element. @raise [WrongSize] when the enumerator is not a 1-tuple @return [Object] the element in the 1-tuple enumerator
# File lib/hf.rb, line 22 def only return self.first if self.single? raise WrongSize, wrong_size_message(self.count, SINGLE_SIZE) end
@return [Boolean] true when enum is a 1-tuple or single
# File lib/hf.rb, line 12 def single? return true if self.count == SINGLE_SIZE return false end
Private Instance Methods
@param expected_size [Integer] @param actual_size [Integer] @return [String] the message for the WrongSize exception
# File lib/hf.rb, line 59 def wrong_size_message(expected_size, actual_size) elm_word = actual_size == 1 ? "element" : "elements" "Enumerator is not a #{expected_size}-tuple. It has #{actual_size} #{elm_word} instead of #{expected_size}" end