BLaZeKiLL / VloxyEngine

Performance oriented voxel engine for unity.
MIT License
100 stars 9 forks source link

IndexOutOfRangeException in UnsafeIntervalList #127

Closed dexsper closed 5 months ago

dexsper commented 6 months ago

Set method encounters an IndexOutOfRangeException because the left_node_index returned by the LeftOf method is -1. This happens when the index is 0 and the node is the first node in the list.

https://github.com/BLaZeKiLL/VloxyEngine/blob/0f2f6fb843591ba7ad59a2b587b4f24cd265f9f3/Packages/io.codeblaze.vloxyengine/Runtime/Engine/Utils/Collections/UnsafeIntervalList.cs#L113C1-L114C77

dexsper commented 6 months ago

Solution: Check for the first and last nodes before accessing their indices. Modify the Set method to handle cases when left_node_index or right_node_index is -1.

var (left_item, left_node_index) = LeftOf(index, node_index);
var (right_item, right_node_index) = RightOf(index, node_index);

// Handle cases where left_node_index or right_node_index is -1
if (left_node_index == -1 && right_node_index == -1)
{
    // Special case: single element list
    Internal[node_index] = new Node(id, 1);
    return true;
}
else if (left_node_index == -1)
{
    // Handle case when left_node_index is -1
    if (id == right_item)
    {
        // Merge with the right node
        Internal[node_index] = new Node(id, 1);
        Internal.RemoveRange(node_index + 1, 1);
    }
    else
    {
        Internal.InsertRange(node_index, 1);
        Internal[node_index] = new Node(id, 1);
    }
}
else if (right_node_index == -1)
{
    // Handle case when right_node_index is -1
    if (id == left_item)
    {
        var left_node = Internal[left_node_index];
        left_node.Count++;
        Internal[left_node_index] = left_node;
        Internal.RemoveRange(node_index, 1);
    }
    else
    {
        Internal.InsertRange(node_index, 1);
        Internal[node_index] = new Node(id, 1);
    }
}
else
{
        ...
}
BLaZeKiLL commented 5 months ago

Thanks, I guess I missed these cases in the unit tests