class DoubleLinkedList::Element
Attributes
_next[RW]
datum[R]
next[RW]
prev[RW]
previous[RW]
Public Class Methods
new(datum, previous = nil, _next = nil)
click to toggle source
# File lib/double_linked_list/element.rb, line 8 def initialize(datum, previous = nil, _next = nil) @datum = datum @previous = previous @_next = _next end
Public Instance Methods
_each(&block)
click to toggle source
Avoid calling myself on finds
# File lib/double_linked_list/element.rb, line 23 def _each(&block) _next.each(&block) if _next end
_reverse_each(&block)
click to toggle source
This is done to avoid calling self in the block For not finding myself as the first element in the list
# File lib/double_linked_list/element.rb, line 66 def _reverse_each(&block) previous.reverse_each(&block) if previous end
append(datum)
click to toggle source
# File lib/double_linked_list/element.rb, line 75 def append(datum) new_last = Element.new(datum, self, nil) self._next = new_last new_last end
chunk_by(acc, custom_dll = DoubleLinkedList, &block)
click to toggle source
# File lib/double_linked_list/element.rb, line 81 def chunk_by(acc, custom_dll = DoubleLinkedList, &block) if acc.empty? acc << custom_dll.from_a(self.datum) else if block.call(self, acc.last, acc) acc << custom_dll.from_a(self.datum) else acc.last << self.datum end end _next ? _next.chunk_by(acc, custom_dll, &block) : acc end
count()
click to toggle source
# File lib/double_linked_list/element.rb, line 27 def count reduce(0) { |p| p + 1 } end
Also aliased as: included_next_count
each(&block)
click to toggle source
# File lib/double_linked_list/element.rb, line 17 def each(&block) block.call self _next.each(&block) if _next end
find(datum)
click to toggle source
# File lib/double_linked_list/element.rb, line 48 def find(datum) find_next_by do |elem| elem.datum == datum end end
find_next_by(exclude_self = true, &block)
click to toggle source
# File lib/double_linked_list/element.rb, line 59 def find_next_by(exclude_self = true, &block) method = exclude_self ? :_each : :each _finder(method, &block) end
find_previous_by(exclude_self = true, &block)
click to toggle source
# File lib/double_linked_list/element.rb, line 54 def find_previous_by(exclude_self = true, &block) method = exclude_self ? :_reverse_each : :reverse_each _finder(method, &block) end
included_prev_count()
click to toggle source
# File lib/double_linked_list/element.rb, line 44 def included_prev_count prev_count + 1 end
method_missing(method, *args, &block)
click to toggle source
# File lib/double_linked_list/element.rb, line 106 def method_missing(method, *args, &block) datum.send(method, *args, &block) end
next_count()
click to toggle source
# File lib/double_linked_list/element.rb, line 32 def next_count c = 0 _each { c += 1 } c end
prev_count()
click to toggle source
# File lib/double_linked_list/element.rb, line 38 def prev_count c = 0 _reverse_each { c += 1 } c end
respond_to_missing?(method_name, include_private = false)
click to toggle source
# File lib/double_linked_list/element.rb, line 110 def respond_to_missing?(method_name, include_private = false) datum.respond_to?(method_name) ? true : false end
reverse_each(&block)
click to toggle source
# File lib/double_linked_list/element.rb, line 70 def reverse_each(&block) block.call self previous.reverse_each(&block) if previous end
select_by(&block)
click to toggle source
# File lib/double_linked_list/element.rb, line 94 def select_by(&block) sequences = [] find_multiple do |e| head_tail = block.call(e) if head_tail sequences << head_tail end head_tail end extract_sequences(sequences) end
Protected Instance Methods
_finder(direction, &block)
click to toggle source
# File lib/double_linked_list/element.rb, line 143 def _finder(direction, &block) found = nil send(direction) do |elem| found = elem if block.call(elem) break if found end found end
each_until_found(&block)
click to toggle source
# File lib/double_linked_list/element.rb, line 121 def each_until_found(&block) found = block.call(self) return found if found _next.each_until_found(&block) if _next end
extract_sequences(sequences)
click to toggle source
# File lib/double_linked_list/element.rb, line 127 def extract_sequences(sequences) sequences.each_with_object([]) do |seq, coll| head = seq.head head_datum = head.datum list = DoubleLinkedList.from_a(head_datum) last = nil head.find_multiple do |elem| next if head_datum == elem.datum list << elem.datum last = elem.datum if elem.datum == seq.last.datum break if last end coll << list end end
find_multiple(&block)
click to toggle source
# File lib/double_linked_list/element.rb, line 116 def find_multiple(&block) found = each_until_found(&block) found.next.find_multiple(&block) if found && found.next? end