Class: DoubleLinkedList::Element
- Inherits:
-
Object
- Object
- 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
readonly
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, custom_dll = DoubleLinkedList, &block) ⇒ Object
- #count ⇒ Object (also: #included_next_count)
- #each(&block) ⇒ Object
- #find(datum) ⇒ Object
- #find_next_by(exclude_self = true, &block) ⇒ Object
- #find_previous_by(exclude_self = true, &block) ⇒ Object
- #included_prev_count ⇒ Object
-
#initialize(datum, previous = nil, _next = nil) ⇒ Element
constructor
A new instance of Element.
- #next_count ⇒ Object
- #prev_count ⇒ Object
- #reverse_each(&block) ⇒ Object
- #select_by(&block) ⇒ Object
Constructor Details
#initialize(datum, previous = nil, _next = nil) ⇒ Element
Returns a new instance of Element.
8 9 10 11 12 |
# File 'lib/double_linked_list/element.rb', line 8 def initialize(datum, previous = nil, _next = nil) @datum = datum @previous = previous @_next = _next end |
Instance Attribute Details
#_next ⇒ Object Also known as: next
Returns the value of attribute _next.
6 7 8 |
# File 'lib/double_linked_list/element.rb', line 6 def _next @_next end |
#datum ⇒ Object (readonly)
Returns the value of attribute datum.
5 6 7 |
# File 'lib/double_linked_list/element.rb', line 5 def datum @datum end |
#previous ⇒ Object Also known as: prev
Returns the value of attribute previous.
6 7 8 |
# File 'lib/double_linked_list/element.rb', line 6 def previous @previous end |
Instance Method Details
#_each(&block) ⇒ Object
Avoid calling myself on finds
23 24 25 |
# File 'lib/double_linked_list/element.rb', line 23 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
66 67 68 |
# File 'lib/double_linked_list/element.rb', line 66 def _reverse_each(&block) previous.reverse_each(&block) if previous end |
#append(datum) ⇒ Object
75 76 77 78 79 |
# 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) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# 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 ⇒ Object Also known as: included_next_count
27 28 29 |
# File 'lib/double_linked_list/element.rb', line 27 def count reduce(0) { |p| p + 1 } end |
#each(&block) ⇒ Object
17 18 19 20 |
# File 'lib/double_linked_list/element.rb', line 17 def each(&block) block.call self _next.each(&block) if _next end |
#find(datum) ⇒ Object
48 49 50 51 52 |
# 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) ⇒ Object
59 60 61 62 |
# 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) ⇒ Object
54 55 56 57 |
# 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 ⇒ Object
44 45 46 |
# File 'lib/double_linked_list/element.rb', line 44 def included_prev_count prev_count + 1 end |
#next_count ⇒ Object
32 33 34 35 36 |
# File 'lib/double_linked_list/element.rb', line 32 def next_count c = 0 _each { c += 1 } c end |
#prev_count ⇒ Object
38 39 40 41 42 |
# File 'lib/double_linked_list/element.rb', line 38 def prev_count c = 0 _reverse_each { c += 1 } c end |
#reverse_each(&block) ⇒ Object
70 71 72 73 |
# 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) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# 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 |