Exemplo n.º 1
0
 public void LogOperation(object key, object entry, OperationType type)
 {
     if (_opIndex != null)
     {
         _opIndex.Add(HPTime.Now, new OperationInfo(key, entry, type));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 初始化添加
        /// </summary>
        /// <param name="nodes">真实节点</param>
        /// <param name="nodeCopies">每个真实节点组合的虚拟节点数</param>
        public void AddNode(List <StoreNode> nodes, int nodeCopies = 0)
        {
            resetEvent.Reset();
            shards = nodes;
            if (nodeCopies != 0)
            {
                numReps = nodeCopies;
            }
            ketamaNodes.Clear();
            //对所有节点,生成nCopies个虚拟结点
            List <ulong> lst = new List <ulong>();

            foreach (StoreNode node in nodes)
            {
                //每四个虚拟结点为一组
                lst.Clear();
                for (int i = 0; i < numReps; i++)
                {
                    try
                    {
                        //直接使用MurmurHash
                        byte[] digest = MurmurHashFactory.ComputeMurmur(node.ToString() + i);
                        ulong  key    = MurmurHashFactory.Hash(digest);
                        ketamaNodes.Add(key, node);
                        lst.Add(key);//有冲突时重新产生,理论上是不会的。
                    }
                    catch (RedBlackException ex)
                    {
                        if (ex.Error == ReadBlackCode.KeyExists)
                        {
                            foreach (ulong key in lst)
                            {
                                try
                                {
                                    ketamaNodes.Remove(key);
                                }
                                catch
                                {
                                }
                            }
                        }
                        i--;
                    }
                }
            }
            resetEvent.Set();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adiciona um novo item para ser armazenado.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(object key, object value)
        {
            var key2 = key as IComparable;

            if (_rbTree != null)
            {
                _rbTree.Add(key2, value);
            }
        }
Exemplo n.º 4
0
        public object Add(object key, object value)
        {
            object node = new object();

            if (_rbTree != null)
            {
                node = _rbTree.Add((T)key, value);
            }
            return(node);
        }
Exemplo n.º 5
0
        public object Add(object key, object value)
        {
            object node = new object();

            if (_rbTree != null)
            {
                node = _rbTree.Add(key as IComparable, value);
            }
            return(node);
        }
Exemplo n.º 6
0
        private static bool isPolygonSimple(double[,] polygon, int n)
        {
            Comparer comparer = new Comparer();
            SortedDictionary <Edge, object> vertices = new SortedDictionary <Edge, object>(comparer);

            try
            {
                for (int i = 1; i <= n; i++)
                {
                    vertices.Add(new Edge(polygon[i % n, 0], polygon[i % n, 1], polygon[(i + 1) % n, 0], polygon[(i + 1) % n, 1]), null);
                    vertices.Add(new Edge(polygon[i % n, 0], polygon[i % n, 1], polygon[(i - 1) % n, 0], polygon[(i - 1) % n, 1]), null);
                }

                RedBlack tree = new RedBlack();
                foreach (var vertex in vertices)
                {
                    Edge edge = vertex.Key;
                    if (comparer.Compare(edge.A, edge.B) < 0)
                    {
                        tree.Add(edge, new object());
                        IComparable below, above;
                        tree.GetKeysDirectlyAboveAndBelow(edge, out below, out above);

                        if (below != null && ((Edge)below).Crosses(edge))
                        {
                            return(false);
                        }
                        if (above != null && ((Edge)above).Crosses(edge))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        edge = new Edge(edge.B[0], edge.B[1], edge.A[0], edge.A[1]);
                        IComparable below, above;

                        tree.GetKeysDirectlyAboveAndBelow(edge, out below, out above);
                        if (below != null && above != null && ((Edge)below).Crosses((Edge)above))
                        {
                            return(false);
                        }
                        tree.Remove(edge);
                    }
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
 public bool Offer(object key, SequentialNode <TItem> node)
 {
     lock (syncRoot)
     {
         var offerOk = false;
         if (node == null)
         {
             return(false);
         }
         if (nodeMap.Count == capacity)
         {
             return(false);
         }
         if (!nodeMap.ContainsKey(key))
         {
             object val = null;
             nodeMap[key] = node;
             if (redBlack.TryGetValue(node.SortedKey, out val))
             {
                 var alreadyExists = val as SequentialNode <TItem>;
                 while (alreadyExists.Next != null)
                 {
                     alreadyExists = alreadyExists.Next;
                 }
                 alreadyExists.Next = node;
                 node.Previous      = alreadyExists;
             }
             else
             {
                 if (redBlack.Size() > 0)
                 {
                     var minVal = redBlack.GetMinValue() as SequentialNode <TItem>;
                     if (minVal.SortedKey.CompareTo(node.SortedKey) > 0)
                     {
                         minVal.NotifySequenceChanged();
                     }
                 }
                 redBlack.Add(node.SortedKey, node);
             }
             Monitor.Pulse(syncRoot);
             offerOk = true;
         }
         return(offerOk);
     }
 }
Exemplo n.º 8
0
 public static void Main()
 {
     Console.BackgroundColor = ConsoleColor.White;
     Console.ForegroundColor = ConsoleColor.Black;
     Console.Write("Red");
     Console.ForegroundColor = ConsoleColor.Red;
     Console.WriteLine("Black");
     Console.ResetColor();
     try
     {
         Console.WriteLine("Adding items to Red-Black");
         redBlack.Add(1, "One");
         redBlack.Add(2, "Two");
         redBlack.Add(3, "Three");
         redBlack.Add(4, "Four");
         redBlack.Add(5, "Five");
         redBlack.Add(6, "Six");
         redBlack.Add(7, "Seven");
         redBlack.Add(8, "Eight");
         redBlack.Add(9, "Nine");
         redBlack.Add(10, "Ten");
         redBlack.Add(11, "Eleven");
         redBlack.Add(12, "Twelve");
         redBlack.Add(13, "Thirteen");
         Console.WriteLine("Traversing tree..");
         TraverseRedBlack(redBlack);
         Console.WriteLine("Removing 9");
         redBlack.Remove(9);
         Console.WriteLine("Traversing tree..");
         TraverseRedBlack(redBlack);
         Console.WriteLine("Removing 13");
         redBlack.Remove(13);
         Console.WriteLine("Traversing tree..");
         TraverseRedBlack(redBlack);
         Console.WriteLine("Removing 12");
         redBlack.Remove(12);
         Console.WriteLine("Traversing tree..");
         TraverseRedBlack(redBlack);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         Console.WriteLine("Press enter to terminate...");
         Console.ReadLine();
     }
     Console.WriteLine();
     Console.BackgroundColor = ConsoleColor.White;
     Console.ForegroundColor = ConsoleColor.Green;
     Console.WriteLine("AVL");
     Console.ResetColor();
     try
     {
         Console.WriteLine("Adding items to AVL");
         avlTree.Add(1, "One");
         avlTree.Add(2, "Two");
         avlTree.Add(3, "Three");
         avlTree.Add(4, "Four");
         avlTree.Add(5, "Five");
         avlTree.Add(6, "Six");
         avlTree.Add(7, "Seven");
         avlTree.Add(8, "Eight");
         avlTree.Add(9, "Nine");
         avlTree.Add(10, "Ten");
         avlTree.Add(11, "Eleven");
         avlTree.Add(12, "Twelve");
         avlTree.Add(13, "Thirteen");
         Console.WriteLine("Traversing tree..");
         TraverseAvl(avlTree);
         Console.WriteLine("Removing 9");
         avlTree.Remove(9);
         Console.WriteLine("Traversing tree..");
         TraverseAvl(avlTree);
         Console.WriteLine("Removing 13");
         avlTree.Remove(13);
         Console.WriteLine("Traversing tree..");
         TraverseAvl(avlTree);
         Console.WriteLine("Removing 12");
         avlTree.Remove(12);
         TraverseAvl(avlTree);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         Console.WriteLine("Press enter to terminate...");
         Console.ReadLine();
     }
 }