class MarkovTextGenerator::Api::Utils::DataStructures::LinkedList::Impl
Attributes
length[R]
Public Class Methods
new()
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 11 def initialize @head = nil @tail = nil @length = 0 @op_counter = 0 end
Public Instance Methods
each(&block)
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 34 def each(&block) next_node = @head nodes = [] while next_node nodes << next_node next_node = next_node.succ end nodes.each(&block) end
first()
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 26 def first @head ? @head.dup : fail_index_error end
last()
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 30 def last @tail ? @tail.dup : fail_index_error end
push(data)
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 22 def push(data) link_tail_node data end
unshift(data)
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 18 def unshift(data) link_head_node data end
Private Instance Methods
bump_length!()
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 80 def bump_length! @length = @length.next end
bump_op_counter!()
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 84 def bump_op_counter! @op_counter = @op_counter.next end
fail_index_error(message = nil)
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 88 def fail_index_error(message = nil) fail IndexError, message end
link_head_node(data)
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 46 def link_head_node(data) prev_head = @head node = make_node data, nil, prev_head @head = node if prev_head prev_head.pred = node else @tail = node end bump_length! bump_op_counter! end
link_tail_node(data)
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 61 def link_tail_node(data) prev_tail = @tail node = make_node data, prev_tail, nil @tail = node if prev_tail prev_tail.succ = node else @head = node end bump_length! bump_op_counter! end
make_node(data, pred = nil, succ = nil)
click to toggle source
# File lib/markov_text_generator/api/utils/data_structures/linked_list/impl.rb, line 76 def make_node(data, pred = nil, succ = nil) Node.new data, pred, succ end