Class: DoubleLinkedList
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/double_linked_list.rb,
lib/double_linked_list/element.rb,
lib/double_linked_list/version.rb,
lib/double_linked_list/sequence.rb
Defined Under Namespace
Classes: Element, Sequence
Constant Summary
collapse
- VERSION =
"0.4.0"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of DoubleLinkedList.
25
26
27
|
# File 'lib/double_linked_list.rb', line 25
def initialize(datum)
@head = (datum.is_a?(Element) ? datum : Element.new(datum))
end
|
Instance Attribute Details
#head ⇒ Object
Returns the value of attribute head.
5
6
7
|
# File 'lib/double_linked_list.rb', line 5
def head
@head
end
|
#last ⇒ Object
Returns the value of attribute last.
5
6
7
|
# File 'lib/double_linked_list.rb', line 5
def last
@last
end
|
Class Method Details
.from_a(*ary) ⇒ Object
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/double_linked_list.rb', line 30
def from_a(*ary)
elems = ary.flatten
head = Element.new(elems.first)
last = elems.drop(1).reduce(head) do |cur_last, datum|
cur_last.append(datum)
end
new(head).tap do |o|
o.last = last
end
end
|
Instance Method Details
#append(datum) ⇒ Object
Also known as:
<<
42
43
44
|
# File 'lib/double_linked_list.rb', line 42
def append(datum)
self.last = last.append(datum)
end
|
#chunk_by(custom_dll = DoubleLinkedList, &block) ⇒ Object
67
68
69
|
# File 'lib/double_linked_list.rb', line 67
def chunk_by(custom_dll = DoubleLinkedList, &block)
head.chunk_by([], custom_dll, &block)
end
|
#find(datum) ⇒ Object
47
48
49
50
51
|
# File 'lib/double_linked_list.rb', line 47
def find(datum)
find_by do |elem|
elem.datum == datum
end
end
|
#find_by(&block) ⇒ Object
53
54
55
|
# File 'lib/double_linked_list.rb', line 53
def find_by(&block)
head.find_next_by(false, &block)
end
|
#reverse_find(datum) ⇒ Object
57
58
59
60
61
|
# File 'lib/double_linked_list.rb', line 57
def reverse_find(datum)
reverse_find_by do |elem|
elem.datum == datum
end
end
|
#reverse_find_by(&block) ⇒ Object
63
64
65
|
# File 'lib/double_linked_list.rb', line 63
def reverse_find_by(&block)
last.find_previous_by(false, &block)
end
|
#select_by(&block) ⇒ Object
71
72
73
|
# File 'lib/double_linked_list.rb', line 71
def select_by(&block)
head.select_by(&block)
end
|
#to_a ⇒ Object
75
76
77
78
79
|
# File 'lib/double_linked_list.rb', line 75
def to_a
[].tap do |ary|
each{ |elem| ary << elem.datum }
end
end
|