Class: Innodb::List::ListCursor

Inherits:
Object
  • Object
show all
Defined in:
lib/innodb/list.rb

Overview

A list iteration cursor used primarily by the Innodb::List #cursor method implicitly. Keeps its own state for iterating through lists efficiently.

Instance Method Summary collapse

Constructor Details

#initialize(list, node = :min, direction = :forward) ⇒ ListCursor

Returns a new instance of ListCursor.



138
139
140
141
142
143
# File 'lib/innodb/list.rb', line 138

def initialize(list, node = :min, direction = :forward)
  @initial = true
  @list = list
  @direction = direction
  @node = initial_node(node)
end

Instance Method Details

#each_nodeObject



190
191
192
193
194
195
196
# File 'lib/innodb/list.rb', line 190

def each_node
  return enum_for(:each_node) unless block_given?

  while (n = node)
    yield n
  end
end

#goto_node(node) ⇒ Object



170
171
172
# File 'lib/innodb/list.rb', line 170

def goto_node(node)
  @node = node if node
end

#initial_node(node) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/innodb/list.rb', line 145

def initial_node(node)
  case node
  when :min
    @list.first
  when :max
    @list.last
  else
    node
  end
end

#next_nodeObject

Return the next entry from the current position, and advance the cursor position to the returned entry. If the cursor is currently nil, return the first entry in the list and adjust the cursor position to that entry.



186
187
188
# File 'lib/innodb/list.rb', line 186

def next_node
  goto_node(@list.next(@node))
end

#nodeObject



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/innodb/list.rb', line 156

def node
  if @initial
    @initial = false
    return @node
  end

  case @direction
  when :forward
    next_node
  when :backward
    prev_node
  end
end

#prev_nodeObject

Return the previous entry from the current position, and advance the cursor position to the returned entry. If the cursor is currently nil, return the last entry in the list and adjust the cursor position to that entry.



178
179
180
# File 'lib/innodb/list.rb', line 178

def prev_node
  goto_node(@list.prev(@node))
end