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.



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

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

Instance Attribute Details

#headObject

Returns the value of attribute head.



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

def head
  @head
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

#tailObject

Returns the value of attribute tail.



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

def tail
  @tail
end

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



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

attr :size

Instance Method Details

#added(node) ⇒ Object

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



51
52
53
54
# File 'lib/async/list.rb', line 51

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

#append(node) ⇒ Object

Append a node to the end of the list.



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

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.



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

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

#empty?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/async/list.rb', line 140

def empty?
	@size == 0
end

#firstObject



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

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)


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

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

#lastObject



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

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



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

attr_accessor :head

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



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

attr_accessor :tail

#prepend(node) ⇒ Object

Prepend a node to the start of the list.



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

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.



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

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)


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

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.



95
96
97
98
# File 'lib/async/list.rb', line 95

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

#shiftObject

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



232
233
234
235
236
# File 'lib/async/list.rb', line 232

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

#stack(node, &block) ⇒ Object

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



87
88
89
90
91
92
# File 'lib/async/list.rb', line 87

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

#to_aObject

Fast, safe, unbounded accumulation of children.



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

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



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

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