Class: Linked::List::EOL

Inherits:
Item
  • Object
show all
Defined in:
lib/linked/list/eol.rb

Overview

End Of List

This class implements a special list item that is placed at both the end and the beginning of a chain of regular items to form a list. The naming (end of list) comes from the fact that this object, by returning true for calls to #nil?, signifies the end of a list of Items. In both directions as a matter of fact, which is why the head and tail objects defined by Item is combined into one.

In a nutshell, the structure looks something like this:

+-------------------- EOL --------------------+
| (head)                               (tail) |
+---------------------------------------------+
  ^ +-- Item 1 ---+         +-- Item N ---+ ^
  +-| prev | next |<- ... ->| prev | next |-+
    +------+------+         +------+------+

Instance Attribute Summary

Attributes inherited from Item

#list, #next, #prev, #value

Instance Method Summary collapse

Methods inherited from Item

#after, #before, #delete, #first?, #freeze, #in?, #in_list?, #initialize_dup, #inspect, #item, #last?, #next!, #prev!, #split

Constructor Details

#initialize(list:) ⇒ EOL

Returns a new instance of EOL.



24
25
26
27
28
# File 'lib/linked/list/eol.rb', line 24

def initialize(list:)
  super()
  @list = list
  @prev = @next = self
end

Instance Method Details

#append(sibling) ⇒ Object

Inserts one or more items at the end of the list. If the given object is not an Item, or a decendant of Item, it will be treated as a value. Depending on the state of the list the value will be a) wraped in a new instance of Item if the list is empty or b) wraped in an object of the same class as the last item in the list.

sibling - the item or value to be appended.

Returns the item that was appended. In case of a string of items the last one is returned.



52
53
54
55
56
57
58
59
# File 'lib/linked/list/eol.rb', line 52

def append(sibling)
  if @prev == self
    sibling = Item.new sibling unless sibling.is_a? Item
    super sibling
  else
    @prev.append sibling
  end
end

#nil?Boolean

EOL objects will return true when asked if they are nil. This is foremost an implementation detail to comply with the requirements of the Item class, but also logical in the sense that end-of-list objects are not really part of the list, and should therefore be considered nil.

Returns true.

Returns:

  • (Boolean)


37
38
39
# File 'lib/linked/list/eol.rb', line 37

def nil?
  true
end

#prepend(sibling) ⇒ Object

Inserts one or more items at the beginning of the list. If the given object is not an Item, or a decendant of Item, it will be treated as a value. Depending on the state of the list the value will be a) wraped in a new instance of Item if the list is empty or b) wraped in an object of the same class as the last item in the list.

sibling - the item or value to be prepended.

Returns the item that was prepended. In case of a string of items the first one is returned.



72
73
74
75
76
77
78
79
# File 'lib/linked/list/eol.rb', line 72

def prepend(sibling)
  if @next == self
    sibling = Item.new sibling unless sibling.is_a? Item
    super sibling
  else
    @next.prepend sibling
  end
end