Class: Hyperactive::List::Head
- Inherits:
-
Record::Bass
- Object
- Record::Bass
- Hyperactive::List::Head
- Includes:
- Archipelago::Current::ThreadedCollection
- Defined in:
- lib/hyperactive/list.rb
Overview
A List head.
Constant Summary
Constants inherited from Record::Bass
Instance Attribute Summary collapse
-
#first_element ⇒ Object
Returns the value of attribute first_element.
-
#last_element ⇒ Object
Returns the value of attribute last_element.
-
#size ⇒ Object
Returns the value of attribute size.
Attributes inherited from Record::Bass
Attributes included from Transactions::Participant
Instance Method Summary collapse
-
#<<(v) ⇒ Object
Push
vonto the end of this list. -
#clear! ⇒ Object
Remove all elements from this List::Head.
-
#destroy! ⇒ Object
Destroys this list and all its elements.
-
#each(&block) ⇒ Object
Yield to
blockonce for each value in this list. -
#empty? ⇒ Boolean
Return whether this List is empty.
-
#first ⇒ Object
Return the first value of the list.
-
#initialize ⇒ Head
constructor
Create a List::Head.
-
#last ⇒ Object
Return the last value of the list.
-
#pop ⇒ Object
Remove the last value from this list and return it.
-
#shift ⇒ Object
Remove the first value from this list and return it.
-
#unlink!(element) ⇒ Object
Unlinks the given
elementand reconnects the List around it. -
#unshift(v) ⇒ Object
Push
vonto the beginning of this list.
Methods inherited from Record::Bass
#<=>, #create, find, get_instance
Methods included from Hooker::Pimp
append_features, #load_hook, #save_hook
Methods included from Transactions::Participant
append_features, #with_transaction
Methods included from Transactions::Accessors
Methods included from Index::Indexable
Constructor Details
#initialize ⇒ Head
Create a List::Head. This is in essence the linked list.
NB: Remember to call create on the new instance or use Head.get_instance to get that done automatically.
72 73 74 75 76 |
# File 'lib/hyperactive/list.rb', line 72 def initialize super self.size = 0 self.first_element = self.last_element = nil end |
Instance Attribute Details
#first_element ⇒ Object
Returns the value of attribute first_element.
63 64 65 |
# File 'lib/hyperactive/list.rb', line 63 def first_element @first_element end |
#last_element ⇒ Object
Returns the value of attribute last_element.
63 64 65 |
# File 'lib/hyperactive/list.rb', line 63 def last_element @last_element end |
#size ⇒ Object
Returns the value of attribute size.
63 64 65 |
# File 'lib/hyperactive/list.rb', line 63 def size @size end |
Instance Method Details
#<<(v) ⇒ Object
Push v onto the end of this list.
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/hyperactive/list.rb', line 156 def <<(v) if self.first_element new_element = Element.get_instance_with_transaction(@transaction, self.last_element, nil, v, @record_id) self.last_element.next = new_element self.last_element = new_element else start(v) end self.size += 1 return v end |
#clear! ⇒ Object
Remove all elements from this List::Head.
116 117 118 119 120 121 122 |
# File 'lib/hyperactive/list.rb', line 116 def clear! self.first_element.t_each do |element| element.destroy! end self.first_element = self.last_element = nil self.size = 0 end |
#destroy! ⇒ Object
Destroys this list and all its elements.
127 128 129 130 |
# File 'lib/hyperactive/list.rb', line 127 def destroy! self.clear! super end |
#each(&block) ⇒ Object
Yield to block once for each value in this list.
206 207 208 209 210 211 212 |
# File 'lib/hyperactive/list.rb', line 206 def each(&block) element = self.first_element while element yield(element.value) element = element.next end end |
#empty? ⇒ Boolean
Return whether this List is empty.
109 110 111 |
# File 'lib/hyperactive/list.rb', line 109 def empty? return self.size == 0 end |
#first ⇒ Object
Return the first value of the list.
135 136 137 138 139 140 141 |
# File 'lib/hyperactive/list.rb', line 135 def first if self.first_element self.first_element.value else nil end end |
#last ⇒ Object
Return the last value of the list.
146 147 148 149 150 151 |
# File 'lib/hyperactive/list.rb', line 146 def last if self.last_element self.last_element.value else end end |
#pop ⇒ Object
Remove the last value from this list and return it.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/hyperactive/list.rb', line 186 def pop v = nil if size > 1 element = self.last_element self.last_element = element.previous self.last_element.next = nil v = element.value element.destroy! else v = self.first_element.value self.first_element.destroy! self.first_element = self.last_element = nil end self.size -= 1 return v end |
#shift ⇒ Object
Remove the first value from this list and return it.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/hyperactive/list.rb', line 217 def shift v = nil if size > 1 element = self.first_element self.first_element = element.next self.first_element.previous = nil v = element.value element.destroy! else v = self.first_element.value self.first_element.destroy! self.first_element = self.last_element = nil end self.size -= 1 return v end |
#unlink!(element) ⇒ Object
Unlinks the given element and reconnects the List around it.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/hyperactive/list.rb', line 81 def unlink!(element) raise "#{element} is not a part of #{self}" unless element.list_id == @record_id if size > 1 if element == self.first_element self.first_element = element.next self.first_element.previous = nil elsif element == self.last_element self.last_element = element.previous self.last_element.next = nil else element.previous.next = element.next element.next.previous = element.previous end else if element == self.first_element self.first_element = self.last_element = nil else raise "#{element} is not a part of #{self} even though it claims to be" end end self.size -= 1 element.destroy! end |
#unshift(v) ⇒ Object
Push v onto the beginning of this list.
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/hyperactive/list.rb', line 171 def unshift(v) if self.first_element new_element = Element.get_instance_with_transaction(@transaction, nil, self.first_element, v, @record_id) self.first_element.previous = new_element self.first_element = new_element else start(v) end self.size += 1 return v end |