Class: Rake::LinkedList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rake/linked_list.rb

Overview

Polylithic linked list structure used to implement several data structures in Rake.

Direct Known Subclasses

InvocationChain, EmptyLinkedList, Scope

Defined Under Namespace

Classes: EmptyLinkedList

Constant Summary collapse

EMPTY =
EmptyLinkedList.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head, tail = EMPTY) ⇒ LinkedList

Returns a new instance of LinkedList.



10
11
12
13
# File 'lib/rake/linked_list.rb', line 10

def initialize(head, tail=EMPTY)
  @head = head
  @tail = tail
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



8
9
10
# File 'lib/rake/linked_list.rb', line 8

def head
  @head
end

#tailObject (readonly)

Returns the value of attribute tail.



8
9
10
# File 'lib/rake/linked_list.rb', line 8

def tail
  @tail
end

Class Method Details

.cons(head, tail) ⇒ Object

Cons a new head onto the tail list.



70
71
72
# File 'lib/rake/linked_list.rb', line 70

def self.cons(head, tail)
  new(head, tail)
end

.emptyObject

The standard empty list class for the given LinkedList class.



75
76
77
# File 'lib/rake/linked_list.rb', line 75

def self.empty
  self::EMPTY
end

.make(*args) ⇒ Object

Make a list out of the given arguments. This method is polymorphic



61
62
63
64
65
66
67
# File 'lib/rake/linked_list.rb', line 61

def self.make(*args)
  result = empty
  args.reverse_each do |item|
    result = cons(item, result)
  end
  result
end

Instance Method Details

#==(other) ⇒ Object

Lists are structurally equivalent.



27
28
29
30
31
32
33
34
35
# File 'lib/rake/linked_list.rb', line 27

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) ⇒ Object

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.



17
18
19
# File 'lib/rake/linked_list.rb', line 17

def conj(item)
  self.class.cons(item, self)
end

#eachObject

For each item in the list.



50
51
52
53
54
55
56
57
# File 'lib/rake/linked_list.rb', line 50

def each
  current = self
  while ! current.empty?
    yield(current.head)
    current = current.tail
  end
  self
end

#empty?Boolean

Is the list empty?

Returns:

  • (Boolean)


22
23
24
# File 'lib/rake/linked_list.rb', line 22

def empty?
  false
end

#inspectObject

Same as to_s, but with inspected items.



44
45
46
47
# File 'lib/rake/linked_list.rb', line 44

def inspect
  items = map { |item| item.inspect }.join(", ")
  "LL(#{items})"
end

#to_sObject

Convert to string: LL(item, item…)



38
39
40
41
# File 'lib/rake/linked_list.rb', line 38

def to_s
  items = map { |item| item.to_s }.join(", ")
  "LL(#{items})"
end