class Async::List
A general doublely linked list. This is used internally by {Async::Barrier} and {Async::Condition} to manage child tasks.
def added(node)
def added(node) @size += 1 return node end
def append(node)
def append(node) if node.head raise ArgumentError, "Node is already in a list!" end node.tail = self @head.tail = node node.head = @head @head = node return added(node) end
def each(&block)
@yields {|node| ...} Yields each node in the list.
Iterate over each node in the linked list. It is generally safe to remove the current node, any previous node or any future node during iteration.
def each(&block) return to_enum unless block_given? Iterator.each(self, &block) return self end
def empty?
def empty? @size == 0 end
def first
def first # validate! current = @tail while !current.equal?(self) if current.is_a?(Iterator) current = current.tail else return current end end return nil end
def include?(needle)
@parameter needle [Node] The node to search for.
Determine whether the given node is included in the list.
def include?(needle) self.each do |item| return true if needle.equal?(item) end return false end
def initialize
def initialize @head = self @tail = self @size = 0 end
def last
def last # validate! current = @head while !current.equal?(self) if current.is_a?(Iterator) current = current.head else return current end end return nil end
def prepend(node)
def prepend(node) if node.head raise ArgumentError, "Node is already in a list!" end node.head = self @tail.head = node node.tail = @tail @tail = node return added(node) end
def remove(node)
@raises [ArgumentError] If the node is not part of this list.
You should be careful to only remove nodes that are part of this list.
Remove the node. If it was already removed, this will raise an error.
def remove(node) # One downside of this interface is we don't actually check if the node is part of the list defined by `self`. This means that there is a potential for a node to be removed from a different list using this method, which in can throw off book-keeping when lists track size, etc. unless node.head raise ArgumentError, "Node is not in a list!" end remove!(node) end
def remove!(node)
def remove!(node) ad.tail = node.tail il.head = node.head marks the node as being removed, and causes remove to fail if called a 2nd time. ad = nil tail = nil removed(node)
def remove?(node)
You should be careful to only remove nodes that are part of this list.
Remove the node if it is in a list.
def remove?(node) if node.head return remove!(node) end return nil end
def removed(node)
def removed(node) @size -= 1 return node end
def shift
def shift if node = first remove!(node) end end
def stack(node, &block)
@yields {|node| ...} Yields the node.
Add the node, yield, and the remove the node.
def stack(node, &block) append(node) return yield(node) ensure remove!(node) end
def to_a
def to_a items = [] current = self while current.tail != self unless current.tail.is_a?(Iterator) items << current.tail end current = current.tail end return items end
def to_s
def to_s sprintf("#<%s:0x%x size=%d>", self.class.name, object_id, @size) end