Jurassic.Library.SparseArray.FindOrCreateArray C# (CSharp) Method

FindOrCreateArray() private method

private FindOrCreateArray ( uint index, bool writeAccess ) : object[]
index uint
writeAccess bool
return object[]
        private object[] FindOrCreateArray(uint index, bool writeAccess)
        {
            if (index < 0)
                return null;

            // Check if the index is out of bounds.
            if ((index & this.mask) != index || this.depth == 0)
            {
                if (writeAccess == false)
                    return null;

                // Create one or more new root nodes.
                do
                {
                    var newRoot = new Node();
                    newRoot.array[0] = this.root;
                    this.root = newRoot;
                    this.depth++;
                    this.mask = NodeShift * this.depth >= 32 ? -1 : (1 << NodeShift * this.depth) - 1;
                } while ((index & this.mask) != index);
            }

            // Find the node.
            Node current = this.root;
            for (int depth = this.depth - 1; depth > 0; depth--)
            {
                uint currentIndex = (index >> (depth * NodeShift)) & NodeMask;
                var newNode = (Node)current.array[currentIndex];
                if (newNode == null)
                {
                    if (writeAccess == false)
                        return null;
                    newNode = new Node();
                    current.array[currentIndex] = newNode;
                }
                current = newNode;
            }

            // Populate the MRU members.
            this.recent = current.array;
            this.recentStart = index & NodeInverseMask;

            return current.array;
        }