Class: ThreadSafeLru::DoubleLinkedList

Inherits:
Object
  • Object
show all
Defined in:
lib/threadsafe-lru/DoubleLinkedList.rb

Instance Method Summary collapse

Constructor Details

#initializeDoubleLinkedList

Returns a new instance of DoubleLinkedList.



30
31
32
33
34
35
# File 'lib/threadsafe-lru/DoubleLinkedList.rb', line 30

def initialize
  @head=ListNode.new "head"
  @tail=ListNode.new "tail"
  @head.next=@tail
  @tail.prev=@head      
end

Instance Method Details

#add_to_head(value) ⇒ Object



38
39
40
41
42
# File 'lib/threadsafe-lru/DoubleLinkedList.rb', line 38

def add_to_head value
  new_node=ListNode.new value
  new_node.insert_between @head,@head.next
  new_node
end

#bump_to_top(node) ⇒ Object



44
45
46
47
# File 'lib/threadsafe-lru/DoubleLinkedList.rb', line 44

def bump_to_top node
  node.remove
  node.insert_between @head,@head.next
end

#clearObject



55
56
57
58
# File 'lib/threadsafe-lru/DoubleLinkedList.rb', line 55

def clear
  @head.next=@tail
  @tail.prev=@head
end

#remove_lastObject



49
50
51
52
53
# File 'lib/threadsafe-lru/DoubleLinkedList.rb', line 49

def remove_last
  node=@tail.prev
  node.remove
  node
end

#to_aObject



61
62
63
64
65
66
67
68
69
# File 'lib/threadsafe-lru/DoubleLinkedList.rb', line 61

def to_a
  result=[]
  current=@head.next
  while current != @tail
    result << current.value
    current=current.next
  end
  result
end