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.
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.
Instance Method Summary collapse
-
#<<(v) ⇒ Object
Push
v
onto 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
block
once 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
element
and reconnects the List around it. -
#unshift(v) ⇒ Object
Push
v
onto the beginning of this list.
Methods included from Transactions::Accessors
Methods included from Index::Indexable
Methods included from Record::Persistent
#<=>, append_features, #create, #with_transaction
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.
65 66 67 |
# File 'lib/hyperactive/list.rb', line 65 def first_element @first_element end |
#last_element ⇒ Object
Returns the value of attribute last_element.
65 66 67 |
# File 'lib/hyperactive/list.rb', line 65 def last_element @last_element end |
#size ⇒ Object
Returns the value of attribute size.
65 66 67 |
# File 'lib/hyperactive/list.rb', line 65 def size @size end |
Instance Method Details
#<<(v) ⇒ Object
Push v
onto the end of this list.
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/hyperactive/list.rb', line 157 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.
117 118 119 120 121 122 123 |
# File 'lib/hyperactive/list.rb', line 117 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.
128 129 130 131 |
# File 'lib/hyperactive/list.rb', line 128 def destroy! self.clear! super end |
#each(&block) ⇒ Object
Yield to block
once for each value in this list.
207 208 209 210 211 212 213 |
# File 'lib/hyperactive/list.rb', line 207 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.
110 111 112 |
# File 'lib/hyperactive/list.rb', line 110 def empty? return self.size == 0 end |
#first ⇒ Object
Return the first value of the list.
136 137 138 139 140 141 142 |
# File 'lib/hyperactive/list.rb', line 136 def first if self.first_element self.first_element.value else nil end end |
#last ⇒ Object
Return the last value of the list.
147 148 149 150 151 152 |
# File 'lib/hyperactive/list.rb', line 147 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.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/hyperactive/list.rb', line 187 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.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/hyperactive/list.rb', line 218 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 105 |
# 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! return nil end |
#unshift(v) ⇒ Object
Push v
onto the beginning of this list.
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/hyperactive/list.rb', line 172 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 |