Class: Hyperactive::List::Head

Inherits:
Record::Bass show all
Defined in:
lib/hyperactive/list.rb

Overview

A List head.

Constant Summary

Constants inherited from Record::Bass

Record::Bass::HOST

Instance Attribute Summary collapse

Attributes inherited from Record::Bass

#record_id

Instance Method Summary collapse

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

#initializeHead

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_elementObject (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_elementObject (readonly)

Returns the value of attribute last_element.



40
41
42
# File 'lib/hyperactive/list.rb', line 40

def last_element
  @last_element
end

#sizeObject (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

#firstObject

Return the first value of the list.



55
56
57
# File 'lib/hyperactive/list.rb', line 55

def first
  @first_element.value
end

#lastObject

Return the last value of the list.



62
63
64
# File 'lib/hyperactive/list.rb', line 62

def last
  @last_element.value
end

#popObject

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

#shiftObject

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