Example #1
0
        /**
         * Initialize this cell union to the union or intersection of the two given
         * cell unions. Requires: x != this and y != this.
         */

        public void GetIntersection(S2CellUnion x, S2CellUnion y)
        {
            // assert (x != this && y != this);

            // This is a fairly efficient calculation that uses binary search to skip
            // over sections of both input vectors. It takes constant time if all the
            // cells of "x" come before or after all the cells of "y" in S2CellId order.

            _cellIds.Clear();

            var i = 0;
            var j = 0;

            while (i < x._cellIds.Count && j < y._cellIds.Count)
            {
                var imin = x.CellId(i).RangeMin;
                var jmin = y.CellId(j).RangeMin;
                if (imin > jmin)
                {
                    // Either j->contains(*i) or the two cells are disjoint.
                    if (x.CellId(i) <= y.CellId(j).RangeMax)
                    {
                        _cellIds.Add(x.CellId(i++));
                    }
                    else
                    {
                        // Advance "j" to the first cell possibly contained by *i.
                        j = IndexedBinarySearch(y._cellIds, imin, j + 1);
                        // The previous cell *(j-1) may now contain *i.
                        if (x.CellId(i) <= y.CellId(j - 1).RangeMax)
                        {
                            --j;
                        }
                    }
                }
                else if (jmin >= imin)
                {
                    // Identical to the code above with "i" and "j" reversed.
                    if (y.CellId(j) <= x.CellId(i).RangeMax)
                    {
                        _cellIds.Add(y.CellId(j++));
                    }
                    else
                    {
                        i = IndexedBinarySearch(x._cellIds, jmin, i + 1);
                        if (y.CellId(j) <= x.CellId(i - 1).RangeMax)
                        {
                            --i;
                        }
                    }
                }
                else
                {
                    // "i" and "j" have the same range_min(), so one contains the other.
                    if (x.CellId(i) < y.CellId(j))
                    {
                        _cellIds.Add(x.CellId(i++));
                    }
                    else
                    {
                        _cellIds.Add(y.CellId(j++));
                    }
                }
            }
            // The output is generated in sorted order, and there should not be any
            // cells that can be merged (provided that both inputs were normalized).
            // assert (!normalize());
        }