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)
A callback that is invoked when an item is added to the list.
def added(node) @size += 1 return node end
def append(node)
Append a node to the end of the list.
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)
@returns [List] Returns self.
@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?
@returns [Boolean] Returns true if the list is empty.
def empty? @size == 0 end
def first
@returns [Node] Returns the first node in the list, if it is not empty.
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)
@returns [Boolean] Returns true if the node is in the list.
@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
Initialize a new, empty, list.
def initialize @head = self @tail = self @size = 0 end
def last
@returns [Node] Returns the last node in the list, if it is not empty.
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)
@returns [Node] Returns the node if it was removed, otherwise nil.
@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)
@returns [Node] Returns the node if it was removed, otherwise nil.
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)
A callback that is invoked when an item is removed from the list.
def removed(node) @size -= 1 return node end
def shift
def shift if node = first remove!(node) end end
def stack(node, &block)
@returns [Object] Returns the result of the 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
Fast, safe, unbounded accumulation of children.
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
Print a short summary of the list.
def to_s sprintf("#<%s:0x%x size=%d>", self.class.name, object_id, @size) end