Class: Hyperactive::List::Head
- Inherits:
-
Record::Bass
- Object
- Record::Bass
- Hyperactive::List::Head
- Defined in:
- lib/hyperactive/list.rb
Overview
A List head.
Constant Summary
Constants inherited from Record::Bass
Instance Attribute Summary collapse
-
#first_element ⇒ Object
readonly
Returns the value of attribute first_element.
-
#last_element ⇒ Object
readonly
Returns the value of attribute last_element.
-
#size ⇒ Object
readonly
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. -
#first ⇒ Object
Return the first value of the list.
-
#initialize ⇒ Head
constructor
Create a 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.
-
#unshift(v) ⇒ Object
Push
v
onto the beginning of this list.
Methods inherited from Record::Bass
create_hooks, #destroy, destroy_hooks, find, get_instance, index_by, reject, #save_hook, save_hooks, select, setup, transaction
Constructor Details
#initialize ⇒ Head
Create a Head.
NB: As usual, dont call this. Use Head.get_instance instead.
47 48 49 50 |
# File 'lib/hyperactive/list.rb', line 47 def initialize @size = 0 @first_element = @last_element = nil end |
Instance Attribute Details
#first_element ⇒ Object (readonly)
Returns the value of attribute first_element.
40 41 42 |
# File 'lib/hyperactive/list.rb', line 40 def first_element @first_element end |
#last_element ⇒ Object (readonly)
Returns the value of attribute last_element.
40 41 42 |
# File 'lib/hyperactive/list.rb', line 40 def last_element @last_element end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
40 41 42 |
# File 'lib/hyperactive/list.rb', line 40 def size @size end |
Instance Method Details
#<<(v) ⇒ Object
Push v
onto the end of this list.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/hyperactive/list.rb', line 69 def <<(v) if @first_element new_element = Element.get_instance new_element.value = v new_element.previous = @last_element @last_element.next = new_element @last_element = new_element else start(v) end @size += 1 return v end |
#first ⇒ Object
Return the first value of the list.
55 56 57 |
# File 'lib/hyperactive/list.rb', line 55 def first @first_element.value end |
#last ⇒ Object
Return the last value of the list.
62 63 64 |
# File 'lib/hyperactive/list.rb', line 62 def last @last_element.value end |
#pop ⇒ Object
Remove the last value from this list and return it.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/hyperactive/list.rb', line 103 def pop v = nil if size > 1 element = @last_element @last_element = element.previous @last_element.next = nil v = element.value element.destroy else v = @first_element.value @first_element.destroy @first_element = @last_element = nil end @size -= 1 return v end |
#shift ⇒ Object
Remove the first value from this list and return it.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/hyperactive/list.rb', line 123 def shift v = nil if size > 1 element = @first_element @first_element = element.next @first_element.previous = nil v = element.value element.destroy else v = @first_element.value @first_element.destroy @first_element = @last_element = nil end @size -= 1 return v end |
#unshift(v) ⇒ Object
Push v
onto the beginning of this list.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/hyperactive/list.rb', line 86 def unshift(v) if @first_element new_element = Element.get_instance new_element.value = v new_element.next = @first_element @first_element.previous = new_element @first_element = new_element else start(v) end @size += 1 return v end |