class Rake::LinkedList
Polylithic linked list structure used to implement several data structures in Rake.
Constants
- EMPTY
Attributes
head[R]
tail[R]
Public Class Methods
cons(head, tail)
click to toggle source
Cons a new head onto the tail list.
# File lib/rake/linked_list.rb, line 73 def self.cons(head, tail) new(head, tail) end
empty()
click to toggle source
The standard empty list class for the given LinkedList class.
# File lib/rake/linked_list.rb, line 78 def self.empty self::EMPTY end
make(*args)
click to toggle source
Make a list out of the given arguments. This method is polymorphic
# File lib/rake/linked_list.rb, line 59 def self.make(*args) # return an EmptyLinkedList if there are no arguments return empty if !args || args.empty? # build a LinkedList by starting at the tail and iterating # through each argument # inject takes an EmptyLinkedList to start args.reverse.inject(empty) do |list, item| list = cons(item, list) list # return the newly created list for each item in the block end end
new(head, tail=EMPTY)
click to toggle source
# File lib/rake/linked_list.rb, line 84 def initialize(head, tail=EMPTY) @head = head @tail = tail end
Public Instance Methods
==(other)
click to toggle source
Lists are structurally equivalent.
# File lib/rake/linked_list.rb, line 25 def ==(other) current = self while !current.empty? && !other.empty? return false if current.head != other.head current = current.tail other = other.tail end current.empty? && other.empty? end
conj(item)
click to toggle source
Polymorphically add a new element to the head of a list. The type of head node will be the same list type as the tail.
# File lib/rake/linked_list.rb, line 12 def conj(item) self.class.cons(item, self) end
each() { |head| ... }
click to toggle source
For each item in the list.
# File lib/rake/linked_list.rb, line 48 def each current = self while !current.empty? yield(current.head) current = current.tail end self end
empty?()
click to toggle source
Is the list empty? .make guards against a list being empty making any instantiated LinkedList object not empty by default You should consider overriding this method if you implement your own .make method
# File lib/rake/linked_list.rb, line 20 def empty? false end
inspect()
click to toggle source
Same as to_s
, but with inspected items.
# File lib/rake/linked_list.rb, line 42 def inspect items = map(&:inspect).join(", ") "LL(#{items})" end
to_s()
click to toggle source
Convert to string: LL(item, item…)
# File lib/rake/linked_list.rb, line 36 def to_s items = map(&:to_s).join(", ") "LL(#{items})" end