public void CouldFillSeries()
        {
            var sm  = new SortedMap <DateTime, double>();
            var sm2 = new SortedMap <DateTime, double>();

            var count = 1000000;

            for (int i = 0; i < count; i++)
            {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i * 2), i);
            }

            for (int i = 0; i < count; i++)
            {
                sm2.Add(DateTime.UtcNow.Date.AddSeconds(i * 2 + 1), i);
            }

            var expected = 0.0;

            for (int i = 0; i < count; i++)
            {
                expected += i;;
            }

            var sw = new Stopwatch();

            sw.Start();
            var sum = (sm.Fill(0) + sm2).Values.Sum();

            sw.Stop();
            Assert.AreEqual(expected, sum);
            Console.WriteLine("Repeat + zip, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
        }
        public void CouldFillSeries() {
            var sm = new SortedMap<DateTime, double>();
            var sm2 = new SortedMap<DateTime, double>();

            var count = 1000000;

            for (int i = 0; i < count; i++) {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i * 2), i);
            }

            for (int i = 0; i < count; i++) {
                sm2.Add(DateTime.UtcNow.Date.AddSeconds(i * 2 + 1), i);
            }

            var expected = 0.0;
            for (int i = 0; i < count; i++) {
                expected += i; ;
            }

            var sw = new Stopwatch();
            sw.Start();
            var sum = (sm.Fill(0) + sm2).Values.Sum();
            sw.Stop();
            Assert.AreEqual(expected, sum);
            Console.WriteLine("Repeat + zip, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));

        }
        public void CouldZipOneEmptyFillSeries()
        {
            var sm  = new SortedMap <DateTime, double>();
            var sm2 = new SortedMap <DateTime, double>();

            sm2.Add(DateTime.Today, 42.0);
            var one   = sm.Fill(1.0);
            var two   = sm2.Fill(2.0);
            var three = one + two;

            var threeeCursor = three.GetCursor();

            Assert.IsTrue(threeeCursor.MoveNext());
            Assert.AreEqual(DateTime.Today, threeeCursor.CurrentKey);
            Assert.AreEqual(43.0, threeeCursor.CurrentValue);

            double v;

            Assert.IsTrue(threeeCursor.TryGetValue(DateTime.Now, out v));
            Assert.AreEqual(3.0, v);
            Assert.IsTrue(three.TryGetValue(DateTime.Now, out v));
            Assert.AreEqual(3.0, v);

            Assert.IsTrue(three.TryGetValue(DateTime.Today, out v));
            Assert.AreEqual(43.0, v);
        }
        public void CouldZipAllEmptyFillSeries()
        {
            var sm    = new SortedMap <DateTime, double>();
            var one   = sm.Fill(1.0);
            var two   = sm.Fill(2.0);
            var three = one + two;

            var threeeCursor = three.GetCursor();

            Assert.IsFalse(threeeCursor.MoveNext());
            double v;

            Assert.IsTrue(threeeCursor.TryGetValue(DateTime.Now, out v));
            Assert.AreEqual(3.0, v);
            Assert.IsTrue(three.TryGetValue(DateTime.Now, out v));
            Assert.AreEqual(3.0, v);
        }
Esempio n. 5
0
        public void CouldFillThenAddValues()
        {
            var sm = new SortedMap <int, double>
            {
                { 1, 0 },
                { 3, 2 },
                { 5, 4 }
            };



            var src     = (sm.GetEnumerator() as ISpecializedCursor <int, double, SortedMapCursor <int, double> >).Source;
            var filled  = src.Fill(0);
            var filled2 = (src as ISpecializedSeries <int, double, SortedMapCursor <int, double> >).Fill(0);
            var filled3 = sm.Fill(0);

            var fc = new Fill <int, double, Cursor <int, double> >(sm.GetSpecializedCursor(), 41).Initialize();
            var c  = (fc.Source + 1).GetEnumerator();

            Assert.True(c.MoveNext());
            Assert.AreEqual(1, c.CurrentValue);

            Assert.True(c.MoveNext());
            Assert.AreEqual(3, c.CurrentValue);

            Assert.True(c.MoveNext());
            Assert.AreEqual(5, c.CurrentValue);

            Assert.False(c.MoveNext());
            Assert.AreEqual(5, c.CurrentValue);

            Assert.True(c.MoveLast());
            Assert.AreEqual(5, c.CurrentValue);

            Assert.False(c.MoveAt(4, Lookup.EQ));
            Assert.AreEqual(5, c.CurrentValue);

            Assert.True(c.MoveAt(4, Lookup.LE));
            Assert.AreEqual(3, c.CurrentValue);

            Assert.True(c.TryGetValue(2, out var x));
            Assert.AreEqual(3, c.CurrentValue);
            Assert.AreEqual(42, x);

            var clone = c.Clone();

            Assert.True(c.MoveAt(4, Lookup.GT));
            Assert.AreEqual(5, c.CurrentValue);
            Assert.AreEqual(3, clone.CurrentValue);

            Assert.True(clone.TryGetValue(0, out var y));
            Assert.AreEqual(42, y);

            c.Dispose();
        }
Esempio n. 6
0
        public void ZipNFromLogoAndReadmeRepeatFillWorks()
        {
            var sum = (_upper.Repeat() + _lower.Fill(42));

            // Item[] like any moves are defined only on observed keys
            Assert.AreEqual(4, sum.Count());
            Assert.AreEqual(44, sum[2]);
            Assert.AreEqual(32, sum[3]);
            Assert.AreEqual(46, sum[4]);
            Assert.AreEqual(54, sum[5]);

            // To get a value from continuous series at non-existing key, we should use TryGetValue method
            int v;

            Assert.IsTrue(sum.TryGetValue(6, out v));
            Assert.AreEqual(46, v);

            // Try find is series mirrow to cursor moves: we cannot move exactly to 6 here
            KeyValuePair <int, int> kvp;

            Assert.IsFalse(sum.TryFind(6, Lookup.EQ, out kvp));
            Assert.IsFalse(sum.TryFind(6, Lookup.GE, out kvp));

            // But we can move to the last key 5 if we try find a kvp at a key that is less or equal to 6
            Assert.IsTrue(sum.TryFind(6, Lookup.LE, out kvp));
            Assert.IsTrue(sum.TryFind(6, Lookup.LT, out kvp));
            Assert.AreEqual(5, kvp.Key);
            Assert.AreEqual(54, kvp.Value);

            // Or to the first key from another side
            Assert.IsFalse(sum.TryFind(0, Lookup.EQ, out kvp));
            Assert.IsFalse(sum.TryFind(1, Lookup.EQ, out kvp));
            Assert.IsFalse(sum.TryFind(0, Lookup.LE, out kvp));

            Assert.IsTrue(sum.TryFind(0, Lookup.GE, out kvp));
            Assert.AreEqual(2, kvp.Key);
            Assert.AreEqual(44, kvp.Value);

            Assert.IsTrue(sum.TryFind(1, Lookup.GE, out kvp));
            Assert.AreEqual(2, kvp.Key);
            Assert.AreEqual(44, kvp.Value);
        }
Esempio n. 7
0
        public void ZipNFromLogoAndReadmeRepeatFillCouldMoveCursorCorrectly()
        {
            var upper = new SortedMap<int, int> { { 2, 2 }, { 4, 4 } };
            var lower = new SortedMap<int, int> { { 1, 10 }, { 3, 30 }, { 5, 50 } };
            var sum = (upper.Repeat() + lower.Fill(42));
            var cursor = sum.GetCursor();

            Assert.IsFalse(cursor.MoveAt(1, Lookup.EQ));
            Assert.IsTrue(cursor.MoveAt(1, Lookup.GE));
            Assert.AreEqual(2, cursor.CurrentKey);
            Assert.AreEqual(44, cursor.CurrentValue);

            // move forward

            Assert.IsTrue(cursor.MoveNext());
            Assert.AreEqual(3, cursor.CurrentKey);
            Assert.AreEqual(32, cursor.CurrentValue);

            Assert.IsTrue(cursor.MoveNext());
            Assert.AreEqual(4, cursor.CurrentKey);
            Assert.AreEqual(46, cursor.CurrentValue);

            Assert.IsTrue(cursor.MoveNext());
            Assert.AreEqual(5, cursor.CurrentKey);
            Assert.AreEqual(54, cursor.CurrentValue);

            // finished
            Assert.IsFalse(cursor.MoveNext());

            //// move back

            Assert.IsTrue(cursor.MovePrevious());
            Assert.AreEqual(4, cursor.CurrentKey);
            Assert.AreEqual(46, cursor.CurrentValue);

            Assert.IsTrue(cursor.MovePrevious());
            Assert.AreEqual(3, cursor.CurrentKey);
            Assert.AreEqual(32, cursor.CurrentValue);

            Assert.IsTrue(cursor.MovePrevious());
            Assert.AreEqual(2, cursor.CurrentKey);
            Assert.AreEqual(44, cursor.CurrentValue);

            // async moves
            Assert.IsTrue(cursor.MoveNext(CancellationToken.None).Result);
            Assert.AreEqual(3, cursor.CurrentKey);
            Assert.AreEqual(32, cursor.CurrentValue);


            Assert.IsTrue(cursor.MoveNext(CancellationToken.None).Result);
            Assert.AreEqual(4, cursor.CurrentKey);
            Assert.AreEqual(46, cursor.CurrentValue);

            Assert.IsTrue(cursor.MoveNext(CancellationToken.None).Result);
            Assert.AreEqual(5, cursor.CurrentKey);
            Assert.AreEqual(54, cursor.CurrentValue);

            var moved = false;
            var t = Task.Run(async () =>
            {
                moved = await cursor.MoveNext(CancellationToken.None);
            });

            // add new value
            upper.Add(6, 6);
            t.Wait();
            Assert.IsTrue(moved);
            Assert.AreEqual(6, cursor.CurrentKey);
            Assert.AreEqual(6 + 42, cursor.CurrentValue);

            // when all sources are marked as immutable/complete, MNA must return false
            var t2 = Task.Run(async () => {
                moved = await cursor.MoveNext(CancellationToken.None);
            });
            upper.Complete();
            lower.Complete();
            t2.Wait();
            Assert.IsFalse(moved);

        }
Esempio n. 8
0
        public void ZipNFromLogoAndReadmeRepeatFillCouldMoveCursorCorrectly()
        {
            var upper = new SortedMap <int, int> {
                { 2, 2 }, { 4, 4 }
            };
            var lower = new SortedMap <int, int> {
                { 1, 10 }, { 3, 30 }, { 5, 50 }
            };
            var sum    = (upper.Repeat() + lower.Fill(42));
            var cursor = sum.GetCursor();

            Assert.IsFalse(cursor.MoveAt(1, Lookup.EQ));
            Assert.IsTrue(cursor.MoveAt(1, Lookup.GE));
            Assert.AreEqual(2, cursor.CurrentKey);
            Assert.AreEqual(44, cursor.CurrentValue);

            // move forward

            Assert.IsTrue(cursor.MoveNext());
            Assert.AreEqual(3, cursor.CurrentKey);
            Assert.AreEqual(32, cursor.CurrentValue);

            Assert.IsTrue(cursor.MoveNext());
            Assert.AreEqual(4, cursor.CurrentKey);
            Assert.AreEqual(46, cursor.CurrentValue);

            Assert.IsTrue(cursor.MoveNext());
            Assert.AreEqual(5, cursor.CurrentKey);
            Assert.AreEqual(54, cursor.CurrentValue);

            // finished
            Assert.IsFalse(cursor.MoveNext());

            //// move back

            Assert.IsTrue(cursor.MovePrevious());
            Assert.AreEqual(4, cursor.CurrentKey);
            Assert.AreEqual(46, cursor.CurrentValue);

            Assert.IsTrue(cursor.MovePrevious());
            Assert.AreEqual(3, cursor.CurrentKey);
            Assert.AreEqual(32, cursor.CurrentValue);

            Assert.IsTrue(cursor.MovePrevious());
            Assert.AreEqual(2, cursor.CurrentKey);
            Assert.AreEqual(44, cursor.CurrentValue);

            // async moves
            Assert.IsTrue(cursor.MoveNext(CancellationToken.None).Result);
            Assert.AreEqual(3, cursor.CurrentKey);
            Assert.AreEqual(32, cursor.CurrentValue);


            Assert.IsTrue(cursor.MoveNext(CancellationToken.None).Result);
            Assert.AreEqual(4, cursor.CurrentKey);
            Assert.AreEqual(46, cursor.CurrentValue);

            Assert.IsTrue(cursor.MoveNext(CancellationToken.None).Result);
            Assert.AreEqual(5, cursor.CurrentKey);
            Assert.AreEqual(54, cursor.CurrentValue);

            var moved = false;
            var t     = Task.Run(async() =>
            {
                moved = await cursor.MoveNext(CancellationToken.None);
            });

            // add new value
            upper.Add(6, 6);
            t.Wait();
            Assert.IsTrue(moved);
            Assert.AreEqual(6, cursor.CurrentKey);
            Assert.AreEqual(6 + 42, cursor.CurrentValue);

            // when all sources are marked as immutable/complete, MNA must return false
            var t2 = Task.Run(async() => {
                moved = await cursor.MoveNext(CancellationToken.None);
            });

            upper.Complete();
            lower.Complete();
            t2.Wait();
            Assert.IsFalse(moved);
        }
Esempio n. 9
0
        public void CouldMoveAtPositionOfThreeDifferentSeriesAllContinuous() {

            var sm1 = new SortedMap<int, int>(new Dictionary<int, int>()
                {
                    { 1, 1},
                    //{ 2, 2}, // Fill(100)
                    { 3, 3},
                    //{ 5, 5}, // Fill(100)
                    { 7, 7}
                });
            var sm2 = new SortedMap<int, int>(new Dictionary<int, int>()
                {
                    { 1, 2},
                    { 2, 4},
                    { 3, 6},
                    { 5, 10},
                    { 7, 14}

                });
            var sm3 = new SortedMap<int, int>(new Dictionary<int, int>()
                {
                    { 1, 3},
                    { 2, 6},
                    { 3, 9},
                    { 5, 15},
                    { 7, 21}
                });

            var series = new[] { sm1.Fill(100), sm2.Repeat(), sm3.Repeat() };
            var sum = series.Zip((k, varr) => k * varr.Sum());

            var zipNCursor = sum.GetCursor();
            var movedAtEQ = zipNCursor.MoveAt(3, Lookup.EQ);
            Assert.IsTrue(movedAtEQ);
            Assert.AreEqual((3 + 6 + 9) * 3, zipNCursor.CurrentValue);

            var movedAtLE = zipNCursor.MoveAt(2, Lookup.LE);
            Assert.IsTrue(movedAtLE);
            Assert.AreEqual((100 + 4 + 6) * 2, zipNCursor.CurrentValue);

            var movedAtGE = zipNCursor.MoveAt(5, Lookup.GE);
            Assert.IsTrue(movedAtGE);
            Assert.AreEqual((100 + 10 + 15) * 5, zipNCursor.CurrentValue);

            var movedAtLT = zipNCursor.MoveAt(3, Lookup.LT);
            Assert.IsTrue(movedAtLT);
            Assert.AreEqual((100 + 4 + 6) * 2, zipNCursor.CurrentValue);
            movedAtLT = zipNCursor.MoveAt(1, Lookup.LT);
            Assert.IsTrue(!movedAtLT);

            var movedAtGT = zipNCursor.MoveAt(3, Lookup.GT);
            Assert.IsTrue(movedAtGT);
            Assert.AreEqual((100 + 10 + 15) * 5, zipNCursor.CurrentValue);
            movedAtGT = zipNCursor.MoveAt(7, Lookup.GT);
            Assert.IsTrue(!movedAtGT);
            int val;
            var hasGTValue = zipNCursor.TryGetValue(8, out val);
            Assert.IsTrue(hasGTValue);
            Assert.AreEqual((100 + 14 + 21) * 8, val);
        }
Esempio n. 10
0
        public void ContinuousZipWithEmptySeriesIsEmpty() {
            var sm1 = new SortedMap<int, int>();
            var sm2 = new SortedMap<int, int>();

            var rp1 = sm1.Repeat();
            var rp2 = sm2.Repeat();

            var zip = rp1.Zip(rp2, (l, r) => l + r);
            Assert.AreEqual(0, zip.Count());

            sm1.Add(1, 1);
            var zip2 = rp1.Zip(rp2, (l, r) => l + r);
            Assert.AreEqual(0, zip2.Count());

            var cursor = zip.GetCursor();
            Assert.IsFalse(cursor.MoveNext());

            var cursor2 = zip2.GetCursor();
            Assert.IsFalse(cursor2.MoveNext());

            var fill = sm2.Fill(0);
            var zip3 = rp1.Zip(fill, (l, r) => l + r);
            Assert.AreEqual(1, zip3.Count());
        }
Esempio n. 11
0
        public void CouldZipOneEmptyFillSeries() {
            var sm = new SortedMap<DateTime, double>();
            var sm2 = new SortedMap<DateTime, double>();
            sm2.Add(DateTime.Today, 42.0);
            var one = sm.Fill(1.0);
            var two = sm2.Fill(2.0);
            var three = one + two;

            var threeeCursor = three.GetCursor();
            
            Assert.IsTrue(threeeCursor.MoveNext());
            Assert.AreEqual(DateTime.Today, threeeCursor.CurrentKey);
            Assert.AreEqual(43.0, threeeCursor.CurrentValue);

            double v;
            Assert.IsTrue(threeeCursor.TryGetValue(DateTime.Now, out v));
            Assert.AreEqual(3.0, v);
            Assert.IsTrue(three.TryGetValue(DateTime.Now, out v));
            Assert.AreEqual(3.0, v);

            Assert.IsTrue(three.TryGetValue(DateTime.Today, out v));
            Assert.AreEqual(43.0, v);
        }
Esempio n. 12
0
        public void CouldZipAllEmptyFillSeries() {
            var sm = new SortedMap<DateTime, double>();
            var one = sm.Fill(1.0);
            var two = sm.Fill(2.0);
            var three = one + two;

            var threeeCursor = three.GetCursor();
            Assert.IsFalse(threeeCursor.MoveNext());
            double v;
            Assert.IsTrue(threeeCursor.TryGetValue(DateTime.Now, out v));
            Assert.AreEqual(3.0, v);
            Assert.IsTrue(three.TryGetValue(DateTime.Now, out v));
            Assert.AreEqual(3.0, v);
        }