Class: DoubleLinkedList::Element
- Inherits:
-
Struct
- Object
- Struct
- DoubleLinkedList::Element
- Includes:
- Enumerable
- Defined in:
- lib/double_linked_list/element.rb
Instance Attribute Summary collapse
-
#_next ⇒ Object
(also: #next)
Returns the value of attribute _next.
-
#datum ⇒ Object
Returns the value of attribute datum.
-
#previous ⇒ Object
(also: #prev)
Returns the value of attribute previous.
Instance Method Summary collapse
-
#_each(&block) ⇒ Object
Avoid calling myself on finds.
-
#_reverse_each(&block) ⇒ Object
This is done to avoid calling self in the block For not finding myself as the first element in the list.
- #append(datum) ⇒ Object
- #chunk_by(acc, &block) ⇒ Object
- #each(&block) ⇒ Object
- #find(datum) ⇒ Object
- #find_next_by(exclude_self = true, &block) ⇒ Object
- #find_previous_by(exclude_self = true, &block) ⇒ Object
- #reverse_each(&block) ⇒ Object
- #select_by(&block) ⇒ Object
Instance Attribute Details
#_next ⇒ Object Also known as: next
Returns the value of attribute _next
2 3 4 |
# File 'lib/double_linked_list/element.rb', line 2 def _next @_next end |
#datum ⇒ Object
Returns the value of attribute datum
2 3 4 |
# File 'lib/double_linked_list/element.rb', line 2 def datum @datum end |
#previous ⇒ Object Also known as: prev
Returns the value of attribute previous
2 3 4 |
# File 'lib/double_linked_list/element.rb', line 2 def previous @previous end |
Instance Method Details
#_each(&block) ⇒ Object
Avoid calling myself on finds
13 14 15 |
# File 'lib/double_linked_list/element.rb', line 13 def _each(&block) _next.each(&block) if _next end |
#_reverse_each(&block) ⇒ Object
This is done to avoid calling self in the block For not finding myself as the first element in the list
35 36 37 |
# File 'lib/double_linked_list/element.rb', line 35 def _reverse_each(&block) previous.reverse_each(&block) if previous end |
#append(datum) ⇒ Object
44 45 46 47 48 |
# File 'lib/double_linked_list/element.rb', line 44 def append(datum) new_last = Element.new(datum, self, nil) self._next = new_last new_last end |
#chunk_by(acc, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/double_linked_list/element.rb', line 50 def chunk_by(acc, &block) if acc.empty? acc << DoubleLinkedList.from_a(self.datum) else if block.call(self, acc.last, acc) acc << DoubleLinkedList.from_a(self.datum) else acc.last << self.datum end end _next ? _next.chunk_by(acc, &block) : acc end |
#each(&block) ⇒ Object
7 8 9 10 |
# File 'lib/double_linked_list/element.rb', line 7 def each(&block) block.call self _next.each(&block) if _next end |
#find(datum) ⇒ Object
17 18 19 20 21 |
# File 'lib/double_linked_list/element.rb', line 17 def find(datum) find_next_by do |elem| elem.datum == datum end end |
#find_next_by(exclude_self = true, &block) ⇒ Object
28 29 30 31 |
# File 'lib/double_linked_list/element.rb', line 28 def find_next_by(exclude_self = true, &block) method = exclude_self ? :_each : :each _finder(method, &block) end |
#find_previous_by(exclude_self = true, &block) ⇒ Object
23 24 25 26 |
# File 'lib/double_linked_list/element.rb', line 23 def find_previous_by(exclude_self = true, &block) method = exclude_self ? :_reverse_each : :reverse_each _finder(method, &block) end |
#reverse_each(&block) ⇒ Object
39 40 41 42 |
# File 'lib/double_linked_list/element.rb', line 39 def reverse_each(&block) block.call self previous.reverse_each(&block) if previous end |
#select_by(&block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/double_linked_list/element.rb', line 63 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 |