示例#1
0
        public virtual TValue GetNearest <T>(T key)
        {
            IRedBlackLeaf <TKey, TValue> leaf = Root;
            IRedBlackLeaf <TKey, TValue> last = null;
            var compareKey = (TKey)Convert.ChangeType(key, typeof(TKey));
            var compare    = CreateLeaf(compareKey, default(TValue));

            while (leaf != null)
            {
                if (leaf.GreaterThan(compare))
                {
                    leaf = leaf.Left;
                }
                else if (leaf.LessThan(compare))
                {
                    last = leaf;
                    leaf = leaf.Right;
                }
                else
                {
                    return(leaf.Value);
                }
            }
            if (last.IsEmpty())
            {
                return(GetMaxLeaf().Value);
            }
            else
            {
                return(last.Value);
            }
        }
示例#2
0
 public virtual IRedBlackLeaf <TKey, TValue> Insert(IRedBlackLeaf <TKey, TValue> root,
                                                    IRedBlackLeaf <TKey, TValue> leaf)
 {
     if (root.IsEmpty())
     {
         root = leaf;
     }
     else
     {
         leaf.Parent = root;
         var direction = leaf.GreaterThan(root);
         root[direction] = Insert(root[direction], leaf);
         root            = BalancePostInsert(root, direction);
     }
     return(root);
 }
示例#3
0
        public virtual TValue Get(TKey key)
        {
            Lock.EnterReadLock();
            IRedBlackLeaf <TKey, TValue> leaf = Root;
            var compare = CreateLeaf(key, default(TValue));

            while (leaf != null)
            {
                if (leaf.GreaterThan(compare))
                {
                    leaf = leaf.Left;
                }
                else if (leaf.LessThan(compare))
                {
                    leaf = leaf.Right;
                }
                else
                {
                    break;
                }
            }
            Lock.ExitReadLock();
            return(leaf.IsEmpty() ? default(TValue) : leaf.Value);
        }