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.3.4"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of DoubleLinkedList.
23
24
25
|
# File 'lib/double_linked_list.rb', line 23
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
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/double_linked_list.rb', line 28
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:
<<
40
41
42
|
# File 'lib/double_linked_list.rb', line 40
def append(datum)
self.last = last.append(datum)
end
|
#chunk_by(custom_dll = DoubleLinkedList, &block) ⇒ Object
65
66
67
|
# File 'lib/double_linked_list.rb', line 65
def chunk_by(custom_dll = DoubleLinkedList, &block)
head.chunk_by([], custom_dll, &block)
end
|
#find(datum) ⇒ Object
45
46
47
48
49
|
# File 'lib/double_linked_list.rb', line 45
def find(datum)
find_by do |elem|
elem.datum == datum
end
end
|
#find_by(&block) ⇒ Object
51
52
53
|
# File 'lib/double_linked_list.rb', line 51
def find_by(&block)
head.find_next_by(false, &block)
end
|
#reverse_find(datum) ⇒ Object
55
56
57
58
59
|
# File 'lib/double_linked_list.rb', line 55
def reverse_find(datum)
reverse_find_by do |elem|
elem.datum == datum
end
end
|
#reverse_find_by(&block) ⇒ Object
61
62
63
|
# File 'lib/double_linked_list.rb', line 61
def reverse_find_by(&block)
last.find_previous_by(false, &block)
end
|
#select_by(&block) ⇒ Object
69
70
71
|
# File 'lib/double_linked_list.rb', line 69
def select_by(&block)
head.select_by(&block)
end
|
#to_a ⇒ Object
73
74
75
76
77
|
# File 'lib/double_linked_list.rb', line 73
def to_a
[].tap do |ary|
each{ |elem| ary << elem.datum }
end
end
|