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.0"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of DoubleLinkedList.
18
19
20
|
# File 'lib/double_linked_list.rb', line 18
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
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/double_linked_list.rb', line 23
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:
<<
35
36
37
|
# File 'lib/double_linked_list.rb', line 35
def append(datum)
self.last = last.append(datum)
end
|
#chunk_by(&block) ⇒ Object
60
61
62
|
# File 'lib/double_linked_list.rb', line 60
def chunk_by(&block)
head.chunk_by([], &block)
end
|
#find(datum) ⇒ Object
40
41
42
43
44
|
# File 'lib/double_linked_list.rb', line 40
def find(datum)
find_by do |elem|
elem.datum == datum
end
end
|
#find_by(&block) ⇒ Object
46
47
48
|
# File 'lib/double_linked_list.rb', line 46
def find_by(&block)
head.find_next_by(false, &block)
end
|
#reverse_find(datum) ⇒ Object
50
51
52
53
54
|
# File 'lib/double_linked_list.rb', line 50
def reverse_find(datum)
reverse_find_by do |elem|
elem.datum == datum
end
end
|
#reverse_find_by(&block) ⇒ Object
56
57
58
|
# File 'lib/double_linked_list.rb', line 56
def reverse_find_by(&block)
last.find_previous_by(false, &block)
end
|
#select_by(&block) ⇒ Object
64
65
66
|
# File 'lib/double_linked_list.rb', line 64
def select_by(&block)
head.select_by(&block)
end
|
#to_a ⇒ Object
68
69
70
71
72
|
# File 'lib/double_linked_list.rb', line 68
def to_a
[].tap do |ary|
each{ |elem| ary << elem.datum }
end
end
|