lib/perobs/BigArrayNode.rb in perobs-4.1.0 vs lib/perobs/BigArrayNode.rb in perobs-4.2.0
- old
+ new
@@ -145,11 +145,13 @@
node.values[index] = value
return
else
# Descend into the right child node to add the value to.
cidx = node.search_child_index(index)
- index -= node.offsets[cidx]
+ if (index -= node.offsets[cidx]) < 0
+ node.fatal "Index (#{index}) became negative"
+ end
node = node.children[cidx]
end
end
node.fatal "Could not find proper node to set the value while " +
@@ -390,11 +392,11 @@
last_offset = nil
@offsets.each_with_index do |offset, i|
if i > 0
if offset < last_offset
- error "Offset are not strictly monotoneously " +
+ error "Offsets are not strictly monotoneously " +
"increasing: #{@offsets.inspect}"
return false
end
expected_offset = last_offset + @children[i - 1].values_count
if offset != expected_offset
@@ -469,15 +471,16 @@
str += "@@@@@@@@@@: #{e.message}\n"
end
else
begin
if node.is_leaf?
- if node.values[position - 1]
+ if position <= node.size
str += "#{node.tree_prefix} " +
"#{position == node.size ? '-' : '|'} " +
"[ #{node.value_index(position - 1)}: " +
- "#{node.values[position - 1]} ]\n"
+ "#{node.values[position - 1].nil? ?
+ 'nil' : node.values[position - 1]} ]\n"
end
end
rescue => e
str += "@@@@@@@@@@: #{e.message}\n"
end
@@ -611,11 +614,11 @@
# @return [Integer] Index of the matching offset or the insert position.
def search_child_index(offset)
# Handle special case for empty offsets list.
return 0 if @offsets.empty? || offset <= @offsets.first
- (@offsets.bsearch_index { |o| o >= offset } || @offsets.length) - 1
+ (@offsets.bsearch_index { |o| o > offset } || @offsets.length) - 1
end
# @return The index of the current node in the children list of the parent
# node. If the node is the root node, nil is returned.
def index_in_parent_node
@@ -654,11 +657,11 @@
idx
end
# This method takes care of adjusting the offsets in tree in case elements
# were inserted or removed. All nodes that hold children after the
- # insert/remove operation needs to be adjusted. Since child nodes get their
+ # insert/remove operation need to be adjusted. Since child nodes get their
# offsets via their parents, only the parent node and the direct ancestor
# followers need to be adjusted.
# @param after_child [BigArrayNode] specifies the modified leaf node
# @param delta [Integer] specifies how many elements were inserted or
# removed.
@@ -908,11 +911,11 @@
# After:
#
# Root Node +--------------------------------+
# Offsets | 0 11 |
# Children | |
- # prepd v child v
+ # pred v child v
# Level 1 +--------------------------++--------------------------+
# Offsets | 0 4 7 || 0 2 5 |
# Children | | | | | |
# v v v v v v
# Leaves +---------++-------++----------++-------++----------++-------+
@@ -920,11 +923,12 @@
#
# Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# Remove the last predecessor offset and update the child offset with
# it
- delta = @offsets[child_index] - pred.offsets.last
- @offsets[child_index] = pred.offsets.pop
+ delta = pred.children.last.values_count
+ @offsets[child_index] -= delta
+ pred.offsets.pop
# Adjust all the offsets of the child
child.offsets.map! { |o| o += delta }
# And prepend the 0 offset
child.offsets.unshift(0)
# Move the child reference