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

Returns the value of attribute head.



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

def head
  @head
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

#tailObject

Returns the value of attribute tail.



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

def tail
  @tail
end

#The number of nodes in the list.(numberofnodes) ⇒ Object (readonly)



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

attr :size

Instance Method Details

#added(node) ⇒ Object

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



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

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

#append(node) ⇒ Object

Append a node to the end of the list.



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

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.



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

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

#empty?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/async/list.rb', line 138

def empty?
	@size == 0
end

#firstObject



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

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)


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

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

#lastObject



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

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

#Points at the end of the list.=(attheend) ⇒ Object



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

attr_accessor :head

#Points at the start of the list.=(atthestartofthelist. = (value)) ⇒ Object



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

attr_accessor :tail

#prepend(node) ⇒ Object

Prepend a node to the start of the list.



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

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.



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

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)


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

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.



93
94
95
96
# File 'lib/async/list.rb', line 93

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

#shiftObject

Shift the first node off the list, if it is not empty.



230
231
232
233
234
# File 'lib/async/list.rb', line 230

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

#stack(node, &block) ⇒ Object

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



85
86
87
88
89
90
# File 'lib/async/list.rb', line 85

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



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