Beispiel #1
0
        public void Test_SubGridTreeSparseCellRecord_Creation()
        {
            SubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>());

            ISubGrid leafSubgrid = new SubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels);

            SubGridTreeSparseCellRecord sparseCell = new SubGridTreeSparseCellRecord(15, 15, leafSubgrid);

            Assert.True(sparseCell.CellX == 15 && sparseCell.CellY == 15 && sparseCell.Cell == leafSubgrid,
                        "Sparse subgrid tree cell record failed to initialise");
        }
Beispiel #2
0
        /// <summary>
        /// Calculate the memory used by this node subgrid. Assume the sub grid reference is the size of a long (8 bytes)
        /// </summary>
        public int SizeOf()
        {
            var sum = 0;

            if (_cells != null)
            {
                sum += SubGridTreeConsts.SubGridTreeDimension * SubGridTreeConsts.SubGridTreeDimension * sizeof(long);
            }
            if (_sparseCells != null)
            {
                sum += _sparseCells.Length * SubGridTreeSparseCellRecord.SizeOf();
            }

            sum += sizeof(short); // For sparse cell count

            return(sum);
        }
Beispiel #3
0
        /// <summary>
        /// Set a child sub grid at the X, Y location into the set of sub grids that are contained in this sub grid.
        /// </summary>
        public override void SetSubGrid(int x, int y, ISubGrid value)
        {
            // Set the origin position and level for the sub grid as these quantities are
            // relative to the location of the sub grid in the tree. Throw an exception if the
            // level of the sub grid is not 0 (null), and is not the same as this.Level + 1
            // (ie: the caller is trying to be too clever!)
            if (value != null)
            {
                if (value.Level != 0 && value.Level != level + 1)
                {
                    throw new ArgumentException("Level of sub grid being added is non-null and is not set correctly for the level it is being added to", nameof(value.Level));
                }

                value.Parent = this;
                value.SetOriginPosition(x, y);
                value.Level = (byte)(level + 1);
            }

            if (_cells != null)
            {
                _cells[x, y] = value;
                return;
            }

            if (value != null)
            {
                // Add it to the sparse list
                if (_sparseCellCount < SubGridTreeNodeCellSparcityLimit)
                {
                    _sparseCells[_sparseCellCount++] = new SubGridTreeSparseCellRecord((byte)x, (byte)y, value);
                }
                else
                {
                    // Create the full array of sub grid references now the number of sub grids is too large to
                    // fit into the sparcity constraint
                    _cells = new ISubGrid[SubGridTreeConsts.SubGridTreeDimension, SubGridTreeConsts.SubGridTreeDimension];

                    for (var I = 0; I < _sparseCellCount; I++)
                    {
                        var sparseCell = _sparseCells[I];
                        _cells[sparseCell.CellX, sparseCell.CellY] = sparseCell.Cell;
                    }

                    _sparseCellCount = 0;
                    _sparseCells     = null; // Release the sparse cells array

                    // Add the new sub grid into the Cells array
                    _cells[x, y] = value;
                }
            }
            else
            {
                for (var I = 0; I < _sparseCellCount; I++)
                {
                    if (_sparseCells[I].CellX == x && _sparseCells[I].CellY == y)
                    {
                        if (I < _sparseCellCount - 1)
                        {
                            Array.Copy(_sparseCells, I + 1, _sparseCells, I, _sparseCellCount - I - 1);
                        }

                        _sparseCellCount--;

                        // Clear the spare cell entry
                        _sparseCells[_sparseCellCount] = new SubGridTreeSparseCellRecord();

                        break;
                    }
                }
            }
        }