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.



143
144
145
146
147
148
# File 'lib/innodb/list.rb', line 143

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

Instance Method Details

#each_nodeObject



195
196
197
198
199
200
201
# File 'lib/innodb/list.rb', line 195

def each_node
  return enum_for(:each_node) unless block_given?

  while (n = node)
    yield n
  end
end

#goto_node(node) ⇒ Object



175
176
177
# File 'lib/innodb/list.rb', line 175

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

#initial_node(node) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/innodb/list.rb', line 150

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.



191
192
193
# File 'lib/innodb/list.rb', line 191

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

#nodeObject



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/innodb/list.rb', line 161

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.



183
184
185
# File 'lib/innodb/list.rb', line 183

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