public virtual T this[int xIndex, int yIndex] { get { if (xIndex < 0 || xIndex >= mWidth || yIndex < 0 || yIndex >= mWidth) { return(mEmptyValue); } int xBlockIndex = xIndex >> 4; // xIndex / mBlockWidth; int yBlockIndex = yIndex >> 4; // yIndex / mBlockHeight; if (mSparseBlocks[xBlockIndex, yBlockIndex] == null) { return(mEmptyValue); } int localX = xIndex % mBlockWidth; int localY = yIndex % mBlockHeight; return(mSparseBlocks[xBlockIndex, yBlockIndex][localX, localY]); } set { if (xIndex < 0 || xIndex >= mWidth || yIndex < 0 || yIndex >= mWidth) { return; } int xBlockIndex = xIndex >> 4; // xIndex / mBlockWidth; int yBlockIndex = yIndex >> 4; // yIndex / mBlockHeight; if (mSparseBlocks[xBlockIndex, yBlockIndex] == null) { mSparseBlocks[xBlockIndex, yBlockIndex] = new Sparse2DBlock <T>(mBlockWidth, mBlockHeight, mEmptyValue); } int localX = xIndex % mBlockWidth; int localY = yIndex % mBlockHeight; mSparseBlocks[xBlockIndex, yBlockIndex][localX, localY] = value; if (mEraseEmptyBlocksDynamically) { if (value.Equals(mEmptyValue)) { if (mSparseBlocks[xBlockIndex, yBlockIndex].isAllEqualTo(mEmptyValue)) { mSparseBlocks[xBlockIndex, yBlockIndex] = null; } } } } }
public virtual void destroy() { if (mSparseBlocks != null) { for (int x = 0; x < mNumXBlocks; x++) { for (int y = 0; y < mNumYBlocks; y++) { mSparseBlocks[x, y] = null; } } mSparseBlocks = null; } }
public Sparse2DBlockArray(int width, int height, T emptyValue, bool eraseEmptyBlocksDynamically) { mBlockWidth = 16; mBlockHeight = 16; mWidth = width; mHeight = height; mEmptyValue = emptyValue; mEraseEmptyBlocksDynamically = eraseEmptyBlocksDynamically; mNumXBlocks = mWidth / mBlockWidth; if (mNumXBlocks * mBlockWidth < mWidth) { mNumXBlocks++; } mNumYBlocks = mHeight / mBlockHeight; if (mNumYBlocks * mBlockHeight < mHeight) { mNumYBlocks++; } mSparseBlocks = new Sparse2DBlock <T> [mNumXBlocks, mNumYBlocks]; }
public override T this[int xIndex, int yIndex] { get { if (xIndex < 0 || xIndex >= mWidth || yIndex < 0 || yIndex >= mWidth) { return(mEmptyValue); } int xBlockIndex = xIndex >> 4; // xIndex / mBlockWidth; int yBlockIndex = yIndex >> 4; // yIndex / mBlockHeight; int blockIndex = xBlockIndex + yBlockIndex * mNumXBlocks; if (mIsBlockConstant.isSet(blockIndex)) { return(mBlockConstantValue[blockIndex]); } return(base[xIndex, yIndex]); } set { if (xIndex < 0 || xIndex >= mWidth || yIndex < 0 || yIndex >= mWidth) { return; } int xBlockIndex = xIndex >> 4; // xIndex / mBlockWidth; int yBlockIndex = yIndex >> 4; // yIndex / mBlockHeight; int blockIndex = xBlockIndex + yBlockIndex * mNumXBlocks; if (mIsBlockConstant.isSet(blockIndex)) { if (mBlockConstantValue[blockIndex].Equals(value)) { return; } //we had a deflated block, that we're now adding noise to. //inflate and add the data mSparseBlocks[xBlockIndex, yBlockIndex] = new Sparse2DBlock <T>(mBlockWidth, mBlockHeight, mBlockConstantValue[blockIndex]); mIsBlockConstant.setValue((uint)blockIndex, false); mBlockConstantValue[blockIndex] = mEmptyValue; } base[xIndex, yIndex] = value; //if the entire block is equal to the same value.. deflate! if (mDeflateBlocksDynamically && !value.Equals(mEmptyValue)) { if (mSparseBlocks[xBlockIndex, yBlockIndex].isAllEqualTo(value)) { mSparseBlocks[xBlockIndex, yBlockIndex] = null; mIsBlockConstant.setValue((uint)blockIndex, true); mBlockConstantValue[blockIndex] = value; } } } }