Ejemplo n.º 1
0
        private void Build(KV <T, K>[] KeysAndValues, int Low, int High, HyperRect <T> Region, int Depth)
        {
            int Dimension = Depth % KeysAndValues[Low].Dimensions;
            int N         = Ninther(KeysAndValues, Low, High, Dimension);

            _Value  = KeysAndValues[N];
            _Region = Region;

            Swap(KeysAndValues, N, High);
            int j = Low;

            for (int i = Low; i < High; ++i)
            {
                if (KeysAndValues[i].CompareTo(KeysAndValues[High], Dimension) < 0)
                {
                    Swap(KeysAndValues, i, j++);
                }
            }
            Pair <HyperRect <T>, HyperRect <T> > Subregions = _Region.Split(_Value.First, Dimension);

            if (Low <= j - 1)
            {
                _Left = new KDTree <T, K>(KeysAndValues, Low, j - 1, Subregions.First, Depth + 1);
            }
            if (j <= High - 1)
            {
                _Right = new KDTree <T, K>(KeysAndValues, j, High - 1, Subregions.Second, Depth + 1);
            }
        }
Ejemplo n.º 2
0
 public bool Contains(HyperRect <T> Rect)
 {
     for (int i = 0; i < _Left.Dimensions; ++i)
     {
         if (Rect._Left.CompareTo(_Left, i) < 0 || Rect._Right.CompareTo(_Right, i) > 0)
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 3
0
        public HyperRect <T> Join(HyperRect <T> Second)
        {
            T L = Left;
            T R = Second.Right;

            for (int i = 0; i < L.Dimensions; ++i)
            {
                L.SetDimension(i,
                               Left.CompareTo(Second.Left, i) < 0 ? Left.GetDimension(i) : Second.Left.GetDimension(i));
                R.SetDimension(i,
                               Right.CompareTo(Second.Left, i) > 0 ? Right.GetDimension(i) : Second.Right.GetDimension(i));
            }
            return(new HyperRect <T>(L, R));
        }
Ejemplo n.º 4
0
 public KDTree(KV <T, K>[] KeysAndValues, int Low, int High, HyperRect <T> Region, int Depth)
 {
     Build(KeysAndValues, Low, High, Region, Depth);
 }
Ejemplo n.º 5
0
 public KDTree(IEnumerable <KV <T, K> > KeysAndValues)
 {
     KV <T, K>[] V = KeysAndValues.ToArray();
     Shuffle(V);
     Build(V, 0, V.Length - 1, HyperRect <T> .Bound(V.Select(i => i.First)), 0);
 }