示例#1
0
 internal static int BinarySearch <TItem, TKey>(this ReferenceNode <TItem, TKey>[] self, TKey key)
 {
     return(ListMixins.BinarySearch(self,
                                    n =>
                                    n != null ? n.CompareTo(key): 0));
     //Array.BinarySearch(node.References, self.Key);
 }
示例#2
0
        internal static Coordinates[] Locate <TItem, TKey>(
            this UniqueKeyQuery <TItem, TKey> self, ref ReferenceNode <TItem, TKey> node, Coordinates[] coordinateSet, int lvlIndex = 1, int lastOverallIndex = 0)
        {
            if (node == null)
            {
                node = self.Root;
            }

            if (object.Equals(node.Key, default(TKey)))
            {
                node.Key = self.Key;
            }

            var keyComparison =
                self.KeyComparer(node.Key, self.Key);

            //the key in the node is lower than the new key
            if (keyComparison < 0 && node.References != null)
            {
                /* set the rest of the coordinates to the right
                 * if the node has the maximum length, go to the parent and move one to the right
                 * all in the first position
                 * return coordinates set
                 */

                return(GetCoordinates <TItem, TKey>(
                           self, ref node, coordinateSet, lvlIndex, lastOverallIndex, node[node.Length - 1], node.Length - 1));
            }
            //the key in the node is higher than the new key
            else //if (rootComparison > 0)
            {
                /*
                 * if it is a reference one, search inside the child node,
                 * else search inside the values and return the coordinates set
                 */

                var selectKey =
                    self.SelectKey;

                var comparer =
                    self.KeyComparer;

                int rawIndex =
                    ListMixins.BinarySearch(node.Values,
                                            v => v != null ? comparer(self.Key, selectKey(v.Value)):0);

                var found = node[rawIndex < 0 ? ~rawIndex : rawIndex];

                return(GetCoordinates <TItem, TKey>(
                           self, ref node, coordinateSet, lvlIndex, lastOverallIndex, found, rawIndex));
            }
        }