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.3"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
19
20
21
|
# File 'lib/double_linked_list.rb', line 19
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
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/double_linked_list.rb', line 24
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:
<<
36
37
38
|
# File 'lib/double_linked_list.rb', line 36
def append(datum)
self.last = last.append(datum)
end
|
#chunk_by(custom_dll = DoubleLinkedList, &block) ⇒ Object
61
62
63
|
# File 'lib/double_linked_list.rb', line 61
def chunk_by(custom_dll = DoubleLinkedList, &block)
head.chunk_by([], custom_dll, &block)
end
|
#find(datum) ⇒ Object
41
42
43
44
45
|
# File 'lib/double_linked_list.rb', line 41
def find(datum)
find_by do |elem|
elem.datum == datum
end
end
|
#find_by(&block) ⇒ Object
47
48
49
|
# File 'lib/double_linked_list.rb', line 47
def find_by(&block)
head.find_next_by(false, &block)
end
|
#reverse_find(datum) ⇒ Object
51
52
53
54
55
|
# File 'lib/double_linked_list.rb', line 51
def reverse_find(datum)
reverse_find_by do |elem|
elem.datum == datum
end
end
|
#reverse_find_by(&block) ⇒ Object
57
58
59
|
# File 'lib/double_linked_list.rb', line 57
def reverse_find_by(&block)
last.find_previous_by(false, &block)
end
|
#select_by(&block) ⇒ Object
65
66
67
|
# File 'lib/double_linked_list.rb', line 65
def select_by(&block)
head.select_by(&block)
end
|
#to_a ⇒ Object
69
70
71
72
73
|
# File 'lib/double_linked_list.rb', line 69
def to_a
[].tap do |ary|
each{ |elem| ary << elem.datum }
end
end
|