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
144
145
146
147
148
149
150
151
# File 'lib/innodb/list.rb', line 138

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

  case node
  when :min
    @node = @list.first
  when :max
    @node = @list.last
  else
    @node = node
  end
end

Instance Method Details

#each_nodeObject



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

def each_node
  unless block_given?
    return enum_for(:each_node)
  end

  while n = node
    yield n
  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.



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

def next_node
  if node = @list.next(@node)
    @node = node
  end
end

#nodeObject



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/innodb/list.rb', line 153

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.



171
172
173
174
175
# File 'lib/innodb/list.rb', line 171

def prev_node
  if node = @list.prev(@node)
    @node = node
  end
end