/// <summary>
        /// Adds an old value with fixed timestamp to the sample.
        /// </summary>
        /// <param name="value">The value to be added.</param>
        /// <param name="timestamp">The epoch timestamp of <paramref name="value"/>
        /// in seconds.</param>
        public void Update(long value, long timestamp)
        {
            RescaleIfNeeded();

            double priority = Priority(timestamp);

            // Fills the resevoir with the first "m" values and keep elements
            // with the greatest priorities in the resvoir.
            if (++count_ <= resevoir_size_)
            {
                priorities_[priority] = value;
                //resevoir_[count_ - 1] = value;
            }
            else
            {
                KeyValuePair <double, long> first = priorities_.First;
                if (first.Key < priority)
                {
                    // replace the element associated with the smallest key by the
                    // sampled value.
                    priorities_.Remove(first.Key);
                    priorities_[priority] = value;
                    //resevoir_[first.Value] = value;
                }
            }
        }
Example #2
0
        public void Remove()
        {
            AndersonTree <int, int> tree = new AndersonTree <int, int>();

            tree.Add(0, 0);
            tree.Add(1, 1);
            tree.Add(2, 2);
            tree.Add(3, 3);
            tree.Add(4, 4);
            tree.Add(5, 5);
            tree.Add(6, 6);
            tree.Add(7, 7);

            // removing zero will cause a break in the levels between 1 and nil,
            // so the level of 1 is decreased to 1. Then break is between 1 and 3,
            // so the level of 3 is decreased to 2.
            tree.Remove(0);
            AndersonTreeNode <int, int> node = tree.FindNode(3);

            // levels
            Assert.AreEqual(node.Level, 2);
            Assert.AreEqual(node.Left.Level, 1);
            Assert.AreEqual(node.Left.Right.Level, 1);
            Assert.AreEqual(node.Right.Level, 2);
            Assert.AreEqual(node.Right.Left.Level, 1);
            Assert.AreEqual(node.Right.Right.Level, 1);

            // values
            Assert.AreEqual(node.Value, 3);
            Assert.AreEqual(node.Left.Value, 1);
            Assert.AreEqual(node.Left.Right.Value, 2);
            Assert.AreEqual(node.Right.Value, 5);
            Assert.AreEqual(node.Right.Left.Value, 4);
            Assert.AreEqual(node.Right.Right.Value, 6);

            Assert.IsTrue(tree.Remove(1));
            Assert.IsFalse(tree.ContainsKey(1));
            Assert.Throws(typeof(KeyNotFoundException), delegate() { int val = tree[1]; });

            // the real remove test
            node = tree.FindNode(3);

            // levels
            Assert.AreEqual(node.Level, 2);
            Assert.AreEqual(node.Left.Level, 1);
            Assert.AreEqual(node.Right.Level, 2);
            Assert.AreEqual(node.Right.Left.Level, 1);
            Assert.AreEqual(node.Right.Right.Level, 1);
            Assert.AreEqual(node.Right.Right.Right.Level, 1);

            // values
            Assert.AreEqual(node.Value, 3);
            Assert.AreEqual(node.Left.Value, 2);
            Assert.AreEqual(node.Right.Value, 5);
            Assert.AreEqual(node.Right.Left.Value, 4);
            Assert.AreEqual(node.Right.Right.Value, 6);
            Assert.AreEqual(node.Right.Right.Right.Value, 7);
        }