Ejemplo n.º 1
0
        internal bool TryGetIndex(ref Int2 cellIndex, out int index, out int sortingHash)
        {
            sortingHash = cellIndex.GetSortingHash();
            int minIndex = 0;     //inclusive
            int maxIndex = count; //exclusive

            index = 0;
            while (maxIndex - minIndex > 0
                   ) //If the testing interval has a length of zero, we've done as much as we can.
            {
                index = (maxIndex + minIndex) / 2;
                if (cells.Elements[index].sortingHash > sortingHash)
                {
                    maxIndex = index;
                }
                else if (cells.Elements[index].sortingHash < sortingHash)
                {
                    minIndex = ++index;
                }
                else
                {
                    //Found an equal sorting hash!
                    //The hash can collide, and we cannot add an entry to
                    //an incorrect index.  It would break the 'cell responsibility'
                    //used by the cell update process to avoid duplicate overlaps.
                    //So, check if the index we found is ACTUALLY correct.
                    if (cells.Elements[index].cellIndex.Y == cellIndex.Y &&
                        cells.Elements[index].cellIndex.Z == cellIndex.Z)
                    {
                        return(true);
                    }

                    //If it was not the correct index, let it continue searching.
                }
            }

            return(false);
        }