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
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. -
#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 inherited from Record::Bass
#<=>, attr_accessor, attr_reader, attr_writer, #create, create_hooks, destroy_hooks, find, get_instance, get_instance_with_transaction, index_by, #load_hook, load_hooks, reject, #save_hook, save_hooks, select, setup, #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 |
# File 'lib/hyperactive/list.rb', line 72 def initialize 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.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/hyperactive/list.rb', line 148 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.
108 109 110 111 112 113 114 |
# File 'lib/hyperactive/list.rb', line 108 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.
119 120 121 122 |
# File 'lib/hyperactive/list.rb', line 119 def destroy! self.clear! super end |
#each(&block) ⇒ Object
Yield to block
once for each value in this list.
198 199 200 201 202 203 204 |
# File 'lib/hyperactive/list.rb', line 198 def each(&block) element = self.first_element while element yield(element.value) element = element.next end end |
#first ⇒ Object
Return the first value of the list.
127 128 129 130 131 132 133 |
# File 'lib/hyperactive/list.rb', line 127 def first if self.first_element self.first_element.value else nil end end |
#last ⇒ Object
Return the last value of the list.
138 139 140 141 142 143 |
# File 'lib/hyperactive/list.rb', line 138 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.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/hyperactive/list.rb', line 178 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.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/hyperactive/list.rb', line 209 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.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/hyperactive/list.rb', line 80 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.
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/hyperactive/list.rb', line 163 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 |