Example #1
0
 public void Complete()
 {
     if (_targetNode == null || _targetNode.Value.Length == _digIndex)
     {
         return;
     }
     // target node is too long to specified cache string.
     // let's digging left descendants.
     _targetNode = CompleteText(_targetNode.Value, _targetNode.Left) ?? _targetNode;
 }
Example #2
0
            public bool DigNextChar(char c)
            {
                var node = DigChar(_targetNode, c);

                if (node == null)
                {
                    return(false);
                }
                _digIndex++;
                _targetNode = node;
                return(true);
            }
Example #3
0
            private KeyCacheTreeNode CompleteText(string reference, KeyCacheTreeNode node)
            {
                if (node == null)
                {
                    return(null);
                }
                var diff = String.Compare(reference, 0, node.Value, 0, _digIndex, StringComparison.Ordinal);

                if (diff == 0 && node.Value.Length == _digIndex)
                {
                    return(node);
                }
                return(CompleteText(reference, diff <= 0 ? node.Left : node.Right));
            }
Example #4
0
            private KeyCacheTreeNode DigText(KeyCacheTreeNode node, string text, char nextchar)
            {
                if (node == null)
                {
                    return(null);
                }
                var item = node.Value;
                var diff = String.Compare(text, 0, item, 0, _digIndex, StringComparison.Ordinal);

                if (diff == 0)
                {
                    return(DigChar(node, nextchar));
                }
                return(DigText(diff < 0 ? node.Left : node.Right, text, nextchar));
            }
Example #5
0
        private KeyCacheTreeNode RotateLeft(KeyCacheTreeNode node)
        {
            //    node
            //      pick
            //     |
            //     V
            //    pick
            //  node
            var pick = node.Right;
            node.Right = pick.Left;
            pick.Left = node;
            pick.IsBlack = node.IsBlack;
            node.IsBlack = false;
            return pick;

        }
Example #6
0
        private KeyCacheTreeNode RotateRight(KeyCacheTreeNode node)
        {
            //   node
            // pick
            //   |
            //   V
            //   pick
            //     node
            var pick = node.Left;

            node.Left    = pick.Right;
            pick.Right   = node;
            pick.IsBlack = node.IsBlack;
            node.IsBlack = false;
            return(pick);
        }
Example #7
0
        private KeyCacheTreeNode Add(KeyCacheTreeNode node, string key)
        {
            if (node == null)
            {
                Count++;
                return(new KeyCacheTreeNode(key));
            }

            if (IsRedNode(node.Left) && IsRedNode(node.Right))
            {
                FlipRedBlack(node);
            }

            var cr = String.Compare(node.Value, key, StringComparison.Ordinal);

            if (cr == 0)
            {
                // this node is target node -> nothing to do.
                return(node);
            }
            if (cr > 0)
            {
                node.Left = Add(node.Left, key);
            }
            else
            {
                node.Right = Add(node.Right, key);
            }

            // rotate
            if (IsRedNode(node.Right))
            {
                node = RotateLeft(node);
            }

            if (IsRedNode(node.Left) && IsRedNode(node.Left.Left))
            {
                node = RotateRight(node);
            }

            return(node);
        }
Example #8
0
        private KeyCacheTreeNode Add(KeyCacheTreeNode node, string key)
        {
            if (node == null)
            {
                Count++;
                return new KeyCacheTreeNode(key);
            }

            if (IsRedNode(node.Left) && IsRedNode(node.Right))
            {
                FlipRedBlack(node);
            }

            var cr = String.Compare(node.Value, key, StringComparison.Ordinal);
            if (cr == 0)
            {
                // this node is target node -> nothing to do.
                return node;
            }
            if (cr > 0)
            {
                node.Left = Add(node.Left, key);
            }
            else
            {
                node.Right = Add(node.Right, key);
            }

            // rotate
            if (IsRedNode(node.Right))
            {
                node = RotateLeft(node);
            }

            if (IsRedNode(node.Left) && IsRedNode(node.Left.Left))
            {
                node = RotateRight(node);
            }

            return node;
        }
Example #9
0
            private KeyCacheTreeNode DigChar(KeyCacheTreeNode node, char nextchar)
            {
                if (node == null)
                {
                    return(null);
                }
                var item = node.Value;

                if (item.Length < _digIndex + 1)
                {
                    // go to right node
                    return(DigText(node.Right, item, nextchar));
                }
                var diff = nextchar - item[_digIndex];

                if (diff == 0)
                {
                    // this node has same(or partial same) string
                    return(node);
                }
                return(DigText(diff < 0 ? node.Left : node.Right, item, nextchar));
            }
Example #10
0
 public void Add(string key)
 {
     AddRequestCount++;
     _root         = Add(_root, key);
     _root.IsBlack = true;
 }
Example #11
0
 public void Clear()
 {
     _root = null;
     Count = 0;
 }
Example #12
0
 public void Initialize()
 {
     _targetNode = _parent._root;
     _digIndex = 0;
 }
Example #13
0
 public bool DigNextChar(char c)
 {
     var node = DigChar(_targetNode, c);
     if (node == null)
     {
         return false;
     }
     _digIndex++;
     _targetNode = node;
     return true;
 }
Example #14
0
 private KeyCacheTreeNode DigChar(KeyCacheTreeNode node, char nextchar)
 {
     if (node == null) return null;
     var item = node.Value;
     if (item.Length < _digIndex + 1)
     {
         // go to right node
         return DigText(node.Right, item, nextchar);
     }
     var diff = nextchar - item[_digIndex];
     if (diff == 0)
     {
         // this node has same(or partial same) string
         return node;
     }
     return DigText(diff < 0 ? node.Left : node.Right, item, nextchar);
 }
Example #15
0
 public void Initialize()
 {
     _targetNode = _parent._root;
     _digIndex   = 0;
 }
Example #16
0
 public void Clear()
 {
     _root = null;
     Count = 0;
 }
Example #17
0
 private static void FlipRedBlack(KeyCacheTreeNode node)
 {
     node.IsBlack       = !node.IsBlack;
     node.Left.IsBlack  = !node.Left.IsBlack;
     node.Right.IsBlack = !node.Right.IsBlack;
 }
Example #18
0
 private static void FlipRedBlack(KeyCacheTreeNode node)
 {
     node.IsBlack = !node.IsBlack;
     node.Left.IsBlack = !node.Left.IsBlack;
     node.Right.IsBlack = !node.Right.IsBlack;
 }
Example #19
0
 public void Add(string key)
 {
     AddRequestCount++;
     _root = Add(_root, key);
     _root.IsBlack = true;
 }
Example #20
0
 private KeyCacheTreeNode CompleteText(string reference, KeyCacheTreeNode node)
 {
     if (node == null) return null;
     var diff = String.Compare(reference, 0, node.Value, 0, _digIndex, StringComparison.Ordinal);
     if (diff == 0 && node.Value.Length == _digIndex)
     {
         return node;
     }
     return CompleteText(reference, diff <= 0 ? node.Left : node.Right);
 }
Example #21
0
 public void Complete()
 {
     if (_targetNode == null || _targetNode.Value.Length == _digIndex) return;
     // target node is too long to specified cache string.
     // let's digging left descendants.
     _targetNode = CompleteText(_targetNode.Value, _targetNode.Left) ?? _targetNode;
 }
Example #22
0
 private KeyCacheTreeNode DigText(KeyCacheTreeNode node, string text, char nextchar)
 {
     if (node == null) return null;
     var item = node.Value;
     var diff = String.Compare(text, 0, item, 0, _digIndex, StringComparison.Ordinal);
     if (diff == 0)
     {
         return DigChar(node, nextchar);
     }
     return DigText(diff < 0 ? node.Left : node.Right, text, nextchar);
 }
Example #23
0
 private static bool IsRedNode(KeyCacheTreeNode node)
 {
     return(node != null && !node.IsBlack);
 }
Example #24
0
 private static bool IsRedNode(KeyCacheTreeNode node)
 {
     return node != null && !node.IsBlack;
 }