class Async::List
A double linked list.
def delete(item)
def delete(item) if @head.equal?(item) @head = @head.tail else item.head.tail = item.tail end if @tail.equal?(item) @tail = @tail.head else item.tail.head = item.head end item.head = nil item.tail = nil @size -= 1 return self end
def each
def each return to_enum unless block_given? item = @head while item # We store the tail pointer so we can remove the current item from the linked list: tail = item.tail yield item item = tail end end
def empty?
def empty? @head.nil? end
def first
def first @head end
def include?(needle)
def include?(needle) self.each do |item| return true if needle.equal?(item) end return false end
def initialize
def initialize @head = nil @tail = nil @size = 0 end
def insert(item)
def insert(item) unless @head @head = item @tail = item # Consistency: item.head = nil item.tail = nil else @tail.tail = item item.head = @tail # Consistency: item.tail = nil @tail = item end @size += 1 return self end
def last
def last @tail end
def nil?
def nil? @head.nil? end