Class: Async::List

Inherits:
Object
  • Object
show all
Defined in:
lib/async/list.rb

Overview

A general doublely linked list. This is used internally by Barrier and Condition to manage child tasks.

Direct Known Subclasses

Children

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

Initialize a new, empty, list.



10
11
12
13
14
# File 'lib/async/list.rb', line 10

def initialize
	@head = self
	@tail = self
	@size = 0
end

Instance Attribute Details

#headObject

Points at the end of the list.



40
41
42
# File 'lib/async/list.rb', line 40

def head
  @head
end

#sizeObject (readonly)

Returns the value of attribute size.



45
46
47
# File 'lib/async/list.rb', line 45

def size
  @size
end

#tailObject

Points at the start of the list.



43
44
45
# File 'lib/async/list.rb', line 43

def tail
  @tail
end

Instance Method Details

#added(node) ⇒ Object

A callback that is invoked when an item is added to the list.



48
49
50
51
# File 'lib/async/list.rb', line 48

def added(node)
	@size += 1
	return node
end

#append(node) ⇒ Object

Append a node to the end of the list.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/async/list.rb', line 54

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

#each(&block) ⇒ Object

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.



173
174
175
176
177
178
179
# File 'lib/async/list.rb', line 173

def each(&block)
	return to_enum unless block_given?
	
	Iterator.each(self, &block)
	
	return self
end

#empty?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/async/list.rb', line 136

def empty?
	@size == 0
end

#firstObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/async/list.rb', line 194

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

#include?(needle) ⇒ Boolean

Determine whether the given node is included in the list.

Returns:

  • (Boolean)


185
186
187
188
189
190
191
# File 'lib/async/list.rb', line 185

def include?(needle)
	self.each do |item|
		return true if needle.equal?(item)
	end
	
	return false
end

#lastObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/async/list.rb', line 211

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

#prepend(node) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/async/list.rb', line 67

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

#remove(node) ⇒ Object

Remove the node. If it was already removed, this will raise an error.

You should be careful to only remove nodes that are part of this list.



115
116
117
118
119
120
121
122
# File 'lib/async/list.rb', line 115

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

#remove?(node) ⇒ Boolean

Remove the node if it is in a list.

You should be careful to only remove nodes that are part of this list.

Returns:

  • (Boolean)


101
102
103
104
105
106
107
# File 'lib/async/list.rb', line 101

def remove?(node)
	if node.head
		return remove!(node)
	end
	
	return nil
end

#removed(node) ⇒ Object

A callback that is invoked when an item is removed from the list.



91
92
93
94
# File 'lib/async/list.rb', line 91

def removed(node)
	@size -= 1
	return node
end

#shiftObject



227
228
229
230
231
# File 'lib/async/list.rb', line 227

def shift
	if node = first
		remove!(node)
	end
end

#stack(node, &block) ⇒ Object

Add the node, yield, and the remove the node.



83
84
85
86
87
88
# File 'lib/async/list.rb', line 83

def stack(node, &block)
	append(node)
	return yield(node)
ensure
	remove!(node)
end

#to_aObject

Fast, safe, unbounded accumulation of children.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/async/list.rb', line 24

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

#to_sObject Also known as: inspect

Print a short summary of the list.



17
18
19
# File 'lib/async/list.rb', line 17

def to_s
	sprintf("#<%s:0x%x size=%d>", self.class.name, object_id, @size)
end