// AFTER public static RangeSeries <TKey, TValue, SortedMapCursor <TKey, TValue> > After <TKey, TValue>( this SortedMap <TKey, TValue> series, TKey startKey, bool startInclusive = true) { return(new RangeSeries <TKey, TValue, SortedMapCursor <TKey, TValue> >(series, new Opt <TKey>(startKey), Opt <TKey> .Missing, startInclusive, true)); }
public void CouldMoveNextAsyncWhenChangingOrder_NoSemaphore() { var cts = new CancellationTokenSource(); var ct = cts.Token; var sm = new SortedMap <int, int>(); sm.IsSynchronized = true; var tcs = new TaskCompletionSource <bool>(); var sumTask = Task.Run(async() => { var c = sm.GetCursor(); tcs.SetResult(true); Assert.IsTrue(await c.MoveNext(ct)); // here we change order Assert.IsTrue(await c.MoveNext(ct)); Assert.IsFalse(await c.MoveNext(ct)); }); tcs.Task.Wait(ct); sm.Add(1, 1); Thread.Sleep(100); //sm.Add(0, 0); // will through OOO sm.Add(2, 2); //sm.Add(3, 3); sm.Complete(); sumTask.Wait(ct); }
public void CouldRepeatMapSeries() { 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 * 2 + 1 + 1; } OptimizationSettings.CombineFilterMapDelegates = false; var sw = new Stopwatch(); sw.Start(); var sum = (sm.Repeat().Map(x => x + 1.0).Repeat().Map(x => x + 1.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 CouldScanSeries() { OptimizationSettings.CombineFilterMapDelegates = true; var data = new SortedMap <DateTime, double>(); var count = 5000; for (int i = 0; i < count; i++) { data.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } var sign = 1; var runnningSum = data.Zip(data, (d1, d2) => d1 + d2).Scan(0.0, (st, k, v) => { if (st > 100) { sign = -1; } if (st < -100) { sign = 1; } return(st += sign * v); }); var runnningSumSm = runnningSum.ToSortedMap(); Assert.AreEqual(runnningSum.Count(), count); }
public unsafe void CouldSerializeRegularSortedMapWithZstd() { var rng = new Random(); var dest = (Memory <byte>) new byte[1000000]; var buffer = dest; var handle = buffer.Pin(); var ptr = (IntPtr)handle.Pointer; var sm = new SortedMap <DateTime, decimal>(); for (var i = 0; i < 1000; i++) { sm.Add(DateTime.Today.AddSeconds(i), (decimal)Math.Round(i + rng.NextDouble(), 2)); } var sizeOf = BinarySerializer.SizeOf(sm, out var tmp); var written = BinarySerializer.Write(sm, dest.Span, tmp); Assert.AreEqual(sizeOf, written); Console.WriteLine($"Useful: {sm.Count * 24}"); Console.WriteLine($"Total: {written}"); // NB interesting that with converting double to decimal savings go from 65% to 85%, // even calculated from (8+8) base size not decimal's 16 size Console.WriteLine($"Savings: {1.0 - ((written * 1.0) / (sm.Count * 24.0))}"); SortedMap <DateTime, decimal> sm2 = null; var len2 = BinarySerializer.Read(buffer.Span, out sm2); Assert.AreEqual(written, len2); Assert.IsTrue(sm2.Keys.SequenceEqual(sm.Keys)); Assert.IsTrue(sm2.Values.SequenceEqual(sm.Values)); }
public void CouldMoveOnFirstAndLastPositionOfThreeSeries() { var sm1 = new SortedMap<int, int>(new Dictionary<int, int>() { //{ 1, 1}, { 2, 2}, //{ 3, 3} }); var sm2 = new SortedMap<int, int>(new Dictionary<int, int>() { { 1, 2}, { 2, 4}, { 3, 6} }); var sm3 = new SortedMap<int, int>(new Dictionary<int, int>() { { 1, 3}, { 2, 6}, { 3, 9} }); var series = new[] { sm1, sm2, sm3 }; var sum = series.Zip((k, varr) => k * varr.Sum()); var zipNCursor = sum.GetCursor(); var movedFirst = zipNCursor.MoveFirst(); Assert.IsTrue(movedFirst); Assert.AreEqual((2 + 4 + 6) * 2, zipNCursor.CurrentValue); var movedLast = zipNCursor.MoveLast(); Assert.IsTrue(movedLast); Assert.AreEqual((2 + 4 + 6) * 2, zipNCursor.CurrentValue); }
public static RangeSeries <TKey, TValue, SortedMapCursor <TKey, TValue> > Range <TKey, TValue>( this SortedMap <TKey, TValue> series, Opt <TKey> startKey, Opt <TKey> endKey, bool startInclusive, bool endInclusive) { return(new RangeSeries <TKey, TValue, SortedMapCursor <TKey, TValue> >(series, startKey, endKey, startInclusive, endInclusive)); }
public void ZipLagIssue11Test() { OptimizationSettings.CombineFilterMapDelegates = true; var data = new SortedMap <DateTime, double>(); var count = 5000; for (int i = 0; i < count; i++) { data.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } var sma = data.SMA(20, true).Lag(1u); Console.WriteLine($"laggedSma count: {sma.Count()}"); var deviation = sma; // this line or any other intermediate enumeration affect the last line Console.WriteLine($"deviation count: {deviation.Count()}"); var direction = deviation;//.Map(x => (Math.Sign(x))); Console.WriteLine($"direction count: {direction.Count()}"); var diff = direction.ZipLag(1u, (c, p) => c - p); //.ToSortedMap(); Console.WriteLine($"Count: {diff.Count()}"); Assert.IsTrue(diff.Count() > 0); }
public void CouldNotEnumerateChangingSM() { var count = 1000000; var sw = new Stopwatch(); sw.Start(); var sm = new SortedMap <DateTime, double>(); var c = sm.GetCursor(); Assert.Throws <OutOfOrderKeyException <DateTime> >(() => { for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); var version = sm.version; if (i > 10) { sm[DateTime.UtcNow.Date.AddSeconds(i - 10)] = i - 10 + 1; Assert.IsTrue(sm.version > version); } c.MoveNext(); Assert.AreEqual(i, c.CurrentValue); } }); sw.Stop(); Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds - 50); Console.WriteLine("Ops: {0}", Math.Round(0.000001 * count * 1000.0 / (sw.ElapsedMilliseconds * 1.0), 2)); }
public void CouldCalculateAverageOnMovingWindowWithStep() { var sm = new SortedMap <DateTime, double>(); var count = 100000; for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } // slow implementation var sw = new Stopwatch(); sw.Start(); var ma = sm.Window(20, 2);//.ToSortedMap(); var c = 1; foreach (var m in ma) { var innersm = m.Value;//.ToSortedMap(); if (innersm.Values.Average() != c + 8.5) { Console.WriteLine(m.Value.Values.Average()); throw new ApplicationException("Invalid value"); } c++; c++; } sw.Stop(); Console.WriteLine($"Final c: {c}"); Console.WriteLine("Window MA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0))); Console.WriteLine("Calculation ops: {0}", (int)((double)count * 20.0 / (sw.ElapsedMilliseconds / 1000.0))); }
public void CouldMoveAtWithBatching() { var sm = new SortedMap <DateTime, double>(); var count = 1000; for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } sm.Complete(); var bmvc = new BatchMapValuesCursor <DateTime, double, double>(sm.GetCursor, (v) => v + 1.0, this.IncrementMap); var c = 0; while (c < 500 && bmvc.MoveNext()) { Assert.AreEqual(c + 1.0, bmvc.CurrentValue); c++; } c--; while (bmvc.MoveAt(DateTime.UtcNow.Date.AddSeconds(c), Lookup.EQ)) { Assert.AreEqual(c + 1.0, bmvc.CurrentValue); c--; } Assert.AreEqual(-1, c); }
public void CouldCalculateSMAInRealTime() { var sm = new SortedMap<int, double>(); Task.Run(async () => { for (int i = 0; i < 20; i++) { sm.Add(i, i); } await Task.Delay(100); for (int i = 20; i < 100; i++) { await Task.Delay(1); // 15 msec sm.Add(i, i); } sm.IsMutable = false; }); var sma = sm.SMA(10, true); var c = sma.GetCursor(); while (c.MoveNext(CancellationToken.None).Result) { Console.WriteLine("Key: {0}, value: {1}", c.CurrentKey, c.CurrentValue); } }
public void CouldSerializeRegularSortedMapWithZstd() { BloscSettings.CompressionMethod = CompressionMethod.Zstd; var rng = new Random(); var ptr = Marshal.AllocHGlobal(10000); var db = new DirectBuffer(10000, ptr); var sm = new SortedMap <DateTime, decimal>(); for (var i = 0; i < 1; i++) { sm.Add(DateTime.Today.AddSeconds(i), (decimal)Math.Round(i + rng.NextDouble(), 2)); } MemoryStream tmp; var size = BinarySerializer.SizeOf(sm, out tmp); var len = BinarySerializer.Write(sm, ref db, 0, tmp); Console.WriteLine($"Useful: {sm.Count * 24}"); Console.WriteLine($"Total: {len}"); // NB interesting that with converting double to decimal savings go from 65% to 85%, // even calculated from (8+8) base size not decimal's 16 size Console.WriteLine($"Savings: {1.0 - ((len * 1.0) / (sm.Count * 24.0))}"); SortedMap <DateTime, decimal> sm2 = null; var len2 = BinarySerializer.Read(db, 0, ref sm2); Assert.AreEqual(len, len2); Assert.IsTrue(sm2.Keys.SequenceEqual(sm.Keys)); Assert.IsTrue(sm2.Values.SequenceEqual(sm.Values)); }
public void CouldLagSeries() { var sm = new SortedMap<double, double>(); var count = 1000000; for (int i = 0; i < count; i++) { sm.Add(i, i); } // slow implementation var sw = new Stopwatch(); sw.Start(); var lag = sm.Lag(1);//.ToSortedMap(); var c = 1; foreach (var zl in lag) { if (c - 1 != zl.Value) { throw new ApplicationException(); } c++; } sw.Stop(); Console.WriteLine($"Final c: {c}"); Console.WriteLine("ZipLag, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0))); var repeated = lag.Repeat(); for (int i = 1; i < 1000; i++) { double v; Assert.IsTrue(repeated.TryGetValue(i+1.5, out v)); Assert.AreEqual(i, v); } }
public void CouldSerializeSortedMap2() { var rng = new Random(); var ptr = Marshal.AllocHGlobal(1000000); var db = new DirectBuffer(1000000, ptr); var sm = new SortedMap <int, int>(); for (var i = 0; i < 10000; i++) { sm.Add(i, i); } MemoryStream temp; var len = BinarySerializer.SizeOf(sm, out temp); var len2 = BinarySerializer.Write(sm, ref db, 0, temp); Assert.AreEqual(len, len2); Console.WriteLine($"Useful: {sm.Count * 8}"); Console.WriteLine($"Total: {len}"); // NB interesting that with converting double to decimal savings go from 65% to 85%, // even calculated from (8+8) base size not decimal's 16 size Console.WriteLine($"Savings: {1.0 - ((len * 1.0) / (sm.Count * 8.0))}"); SortedMap <int, int> sm2 = null; var len3 = BinarySerializer.Read(db, 0, ref sm2); Assert.AreEqual(len, len3); Assert.IsTrue(sm2.Keys.SequenceEqual(sm.Keys)); Assert.IsTrue(sm2.Values.SequenceEqual(sm.Values)); }
public void CouldZipLagSeries() { var sm = new SortedMap<DateTime, double>(); var count = 10000000; for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } // slow implementation var sw = new Stopwatch(); sw.Start(); var zipLag = sm.ZipLag(1, (cur, prev) => cur + prev); //.ToSortedMap(); var c = 1; foreach (var zl in zipLag) { if (c + (c - 1) != zl.Value) { throw new ApplicationException(); } c++; } sw.Stop(); Console.WriteLine($"Final c: {c}"); Console.WriteLine("ZipLag, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0))); }
public IReadOnlyOrderedMap<DateTime, double> MultiplyMap(IReadOnlyOrderedMap<DateTime, double> batch) { var sm = new SortedMap<DateTime, double>(); foreach (var kvp in batch) { sm.Add(kvp.Key, kvp.Value * 10.0); } return sm; }
/// <summary> /// Very straighforward batch operation for testing /// </summary> public IReadOnlyOrderedMap<DateTime, double> IncrementMap(IReadOnlyOrderedMap<DateTime, double> batch) { var sm = new SortedMap<DateTime, double>(); foreach (var kvp in batch) { sm.Add(kvp.Key, kvp.Value + 1.0); } return sm; }
public unsafe void CouldSerializeSortedMap2() { var rng = new Random(); var dest = (Memory <byte>) new byte[1000000]; var buffer = dest; var handle = buffer.Pin(); var ptr = (IntPtr)handle.Pointer; var sm = new SortedMap <int, int>(); for (var i = 0; i < 10000; i++) { sm.Add(i, i); } var len = BinarySerializer.SizeOf(sm, out var temp); var len2 = BinarySerializer.Write(sm, buffer.Span, temp); Assert.AreEqual(len, len2); Console.WriteLine($"Useful: {sm.Count * 8}"); Console.WriteLine($"Total: {len}"); // NB interesting that with converting double to decimal savings go from 65% to 85%, // even calculated from (8+8) base size not decimal's 16 size Console.WriteLine($"Savings: {1.0 - ((len * 1.0) / (sm.Count * 8.0))}"); SortedMap <int, int> sm2 = null; var len3 = BinarySerializer.Read(buffer.Span, out sm2); Assert.AreEqual(len, len3); Assert.IsTrue(sm2.Keys.SequenceEqual(sm.Keys)); Assert.IsTrue(sm2.Values.SequenceEqual(sm.Values)); }
public bool AddBatch <K>(double scalar, IReadOnlyOrderedMap <K, double> batch, out IReadOnlyOrderedMap <K, double> value) { var sm = batch as SortedMap <K, double>; if (!ReferenceEquals(sm, null)) { double[] newValues = OptimizationSettings.ArrayPool.Take <double>(sm.size); double[] buffer = new double[Vector <double> .Count]; for (int c = 0; c < Vector <double> .Count; c++) { buffer[c] = scalar; } var tempVector = new System.Numerics.Vector <double>(buffer); int i; for (i = 0; i < newValues.Length; i = i + Vector <double> .Count) { var vec = new Vector <double>(sm.values, i); vec = Vector.Add(vec, tempVector); vec.CopyTo(newValues, i); } for (; i < newValues.Length; i++) { newValues[i] = sm.values[i] + scalar; } var newKeys = sm.keys.ToArray(); var newSm = SortedMap <K, double> .OfSortedKeysAndValues(newKeys, newValues, sm.size, sm.Comparer, false, sm.IsRegular); value = newSm; return(true); } throw new NotImplementedException(); }
public void CouldRepeatMapSeries() { 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 * 2 + 1 + 1; } OptimizationSettings.CombineFilterMapDelegates = false; var sw = new Stopwatch(); sw.Start(); var sum = (sm.Repeat().Map(x => x + 1.0).Repeat().Map(x => x + 1.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 CouldZipLagSeries() { var sm = new SortedMap <DateTime, double>(); var count = 10000000; for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } // slow implementation var sw = new Stopwatch(); sw.Start(); var zipLag = sm.ZipLag(1, (cur, prev) => cur + prev); //.ToSortedMap(); var c = 1; foreach (var zl in zipLag) { if (c + (c - 1) != zl.Value) { throw new ApplicationException(); } c++; } sw.Stop(); Console.WriteLine($"Final c: {c}"); Console.WriteLine("ZipLag, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0))); }
public void CouldReadVariantSeries() { var sm = new SortedMap <int, string>(); for (int i = 0; i < 100; i++) { sm.Add(i, (i * 100).ToString()); } var vs = new VariantSeries <int, string>(sm); foreach (var item in vs) { System.Console.WriteLine(item.Key.Get <int>() + ": " + item.Value.Get <string>()); } Assert.AreEqual(Variant.Create(0), vs.First.Key); Assert.AreEqual(Variant.Create("0"), vs.First.Value); Assert.AreEqual(Variant.Create(99), vs.Last.Key); Assert.AreEqual(Variant.Create("9900"), vs.Last.Value); var cursorSeries = new CursorSeries <int, string>(sm); Assert.AreEqual(0, cursorSeries.First.Key); }
/// <summary> /// Get history of offsets with keys as zoned time. Used to convert from zoned to UTC time. /// </summary> /// <returns></returns> public static SortedMap <DateTime, long> GetOffsetsFromZoned(string tzFrom, bool standardOffsetOnly = false) { string tz; if (!Normalizer.TryGetValue(tzFrom.ToLowerInvariant(), out tz)) { tz = tzFrom; } var sortedMap = new SortedMap <DateTime, long>(); if (tz.ToLowerInvariant() == "utc") { sortedMap.Set(new DateTime(0L, DateTimeKind.Unspecified), 0); } else { var givenTz = DateTimeZoneProviders.Tzdb[tz]; var intervals = givenTz.GetZoneIntervals(Instant.FromDateTimeUtc( // https://en.wikipedia.org/wiki/International_Meridian_Conference new DateTime(1884, 10, 22, 12, 0, 0, DateTimeKind.Utc) ), Instant.MaxValue); foreach (var interval in intervals) { var localStart = interval.IsoLocalStart.ToDateTimeUnspecified(); var offset = standardOffsetOnly ? interval.StandardOffset : interval.WallOffset; var offsetTicks = offset.Ticks; sortedMap.TryAddLast(localStart, offsetTicks); } } sortedMap.Complete(); return(sortedMap); }
public async Task MNATest() { var sm = new SortedMap <int, int>(); var count = 1_0; var sum = 0; using (Benchmark.Run("MNA")) { var _ = Task.Run(() => { for (int i = 0; i < count; i++) { sm.TryAddLast(i, i); } sm.Complete(); }); var c = sm.GetCursor(); while (await c.MoveNextAsync()) { sum += c.CurrentValue; } } Assert.IsTrue(sum > 0); Benchmark.Dump(); }
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 CouldAddVariantSeries() { var sm = new SortedMap <int, double>(); for (int i = 0; i < 100; i++) { sm.Add(i, (i * 100)); } var vs = new VariantSeries <int, double>(sm); var doubled = vs + vs; foreach (var item in doubled) { System.Console.WriteLine(item.Key.Get <int>() + ": " + item.Value.Get <double>()); } Assert.AreEqual(Variant.Create(0), doubled.First.Key); Assert.AreEqual(Variant.Create(0.0), doubled.First.Value); Assert.AreEqual(Variant.Create(99), doubled.Last.Key); Assert.AreEqual(Variant.Create(9900.0 * 2), doubled.Last.Value); var cursorSeries = doubled.ReadOnly(); Assert.AreEqual(Variant.Create(0), cursorSeries.First.Key); }
public async Task CouldCancelCursor() { var sm = new SortedMap <int, int>(); var cursor = sm.GetAsyncEnumerator(); var completable = cursor as IAsyncCompletable; var t = Task.Run(async() => { try { await cursor.MoveNextAsync(); Assert.Fail(); } catch (Exception ex) { Assert.True(true); } }); Thread.Sleep(100); completable?.TryComplete(true); t.Wait(); }
public void CouldCalculateSMAWithWidthEQ() { Assert.Throws <NotImplementedException>(() => { var count = 20; var sm = new SortedMap <int, double>(); sm.Add(0, 0); for (int i = 2; i <= count; i++) { sm.Add(i, i); } sm.Remove(11); sm.Remove(12); var onlineOp = new SumAvgOnlineOp <int, double, SortedMapCursor <int, double> >(); var smaOp = new SpanOpWidth <int, double, double, SortedMapCursor <int, double>, SumAvgOnlineOp <int, double, SortedMapCursor <int, double> > > (2, Lookup.EQ, onlineOp); var smaSeries = new SpanOpImpl <int, double, double, SpanOpWidth <int, double, double, SortedMapCursor <int, double>, SumAvgOnlineOp <int, double, SortedMapCursor <int, double> > >, SortedMapCursor <int, double> >(sm.GetEnumerator(), smaOp).Source; foreach (var keyValuePair in smaSeries) { Trace.WriteLine($"{keyValuePair.Key} - {keyValuePair.Value}"); } }); }
public void RangeCursorMovesAfterAwating() { var sm = new SortedMap <int, int>(); sm.Add(1, 1); var range = sm.Range(int.MinValue, 2, true, true); //Assert.AreEqual(1, range.First.Value); var cursor = range.GetAsyncEnumerator(); var source = cursor.Source; var cts = new CancellationTokenSource(); var t = Task.Run(async() => { var moved = await cursor.MoveNextAsync(); Assert.True(moved); moved = await cursor.MoveNextAsync(); Assert.True(moved); moved = await cursor.MoveNextAsync(); Assert.False(moved); Assert.AreEqual(2, cursor.CurrentKey); Assert.AreEqual(2, cursor.CurrentValue); }); Thread.Sleep(100); sm.Add(2, 2); sm.Add(3, 3); t.Wait(); }
public void CouldCalculateOnlineMovingRegression() { var rng = new Random(); var y = new SortedMap <DateTime, double>(); var xx = new SortedMap <DateTime, double[]>(); var dt = DateTime.Today; for (int i = 0; i < 10000; i++) { var xrow = new double[3]; xrow[0] = 1; xrow[1] = rng.NextDouble() * 5 * i; xrow[2] = rng.NextDouble() * 10 * i; var yrow = 0.33 + 0.25 * xrow[1] + 1.5 * xrow[2] + (rng.NextDouble() - 0.5); y.Add(dt, yrow); xx.Add(dt, xrow); dt = dt.AddSeconds(1); } var movingRegression = y.MovingRegression(xx, 1000, 100); foreach (var kvp in movingRegression) { Console.WriteLine($"{kvp.Value[0, 0]} - {kvp.Value[1, 0]} - {kvp.Value[2, 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 static RangeSeries <TKey, TValue, SortedMapCursor <TKey, TValue> > Range <TKey, TValue>( this SortedMap <TKey, TValue> series, TKey startKey, TKey endKey, bool startInclusive = true, bool endInclusive = true) { return(new RangeSeries <TKey, TValue, SortedMapCursor <TKey, TValue> >(series, new Opt <TKey>(startKey), new Opt <TKey>(endKey), startInclusive, endInclusive)); }
// BEFORE public static RangeSeries <TKey, TValue, SortedMapCursor <TKey, TValue> > Before <TKey, TValue>( this SortedMap <TKey, TValue> series, TKey endKey, bool endInclusive = true) { return(new RangeSeries <TKey, TValue, SortedMapCursor <TKey, TValue> >(series, Opt <TKey> .Missing, new Opt <TKey>(endKey), true, endInclusive)); }
public void CouldWriteToStorage() { var repo = new SeriesStorage(SeriesStorage.GetDefaultConnectionString("../StorageTests.db")); var test = new SortedMap <DateTime, double>(); for (int i = 0; i < 10; i++) { test.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } test.Complete(); foreach (var kvp in test.Map(x => (decimal)x)) { Console.WriteLine($"{kvp.Key} - {kvp.Key.Kind} - {kvp.Value}"); } var storageSeries = repo.GetPersistentOrderedMap <DateTime, decimal>("test_series_CouldWriteToStorage"); var test2 = storageSeries.ToSortedMap(); foreach (var kvp in test2) { Console.WriteLine($"{kvp.Key} - {kvp.Key.Kind} - {kvp.Value}"); } storageSeries.Append(test.Map(x => (decimal)x), AppendOption.RequireEqualOverlap); storageSeries.Flush(); }
private static void MoveNextBenchSM(SortedMap <int, int> sm, int count, int mult) { // warm up for (int _ = 0; _ < 1; _++) { var cSM = sm.GetCursor(); for (int i = 0; i < count; i++) { cSM.MoveNext(); } } using (Benchmark.Run("SM", count * mult)) { long sum = 1L; for (int _ = 0; _ < mult; _++) { var cSM = sm.GetCursor(); for (int i = 0; i < count; i++) { cSM.MoveNext(); sum = cSM.CurrentValue; } } Assert.IsTrue(sum != 0); } }
public void DeadCursorDoesNotCauseEndlessLoopInNotifyUpdate() { var sm = new SortedMap <int, int>(); sm.Add(1, 1); var cursor = sm.GetAsyncEnumerator(); Assert.True(cursor.MoveNext()); Assert.False(cursor.MoveNext()); cursor.MoveNextAsync(); cursor = default; GC.Collect(2, GCCollectionMode.Forced, true); GC.Collect(2, GCCollectionMode.Forced, true); //var t = Task.Run(() => cursor.MoveNextAsync(cts.Token)); sm.Add(2, 2); sm.Add(3, 3); Assert.True(sm.Count == 3); }
public void CouldMoveAsyncOnEmptySM() { var sm = new SortedMap<DateTime, double>(); var c = sm.GetCursor(); var moveTask = c.MoveNext(CancellationToken.None); sm.Add(DateTime.UtcNow.Date.AddSeconds(0), 0); var result = moveTask.Result; Assert.IsTrue(result); }
public void UpdateEventIsTriggered() { var sm = new SortedMap<DateTime, double>(); (sm as IObservableEvents<DateTime, double>).OnNext += (kvp) => { Console.WriteLine("Added {0} : {1}", kvp.Key, kvp.Value); }; sm.Add(DateTime.UtcNow.Date.AddSeconds(0), 0); }
public void ZipNFromLogoAndReadmeRepeatCouldMoveCursorCorrectly() { 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); var cursor = sum.GetCursor(); Assert.AreEqual(32, sum[3]); Assert.AreEqual(54, sum[5]); Assert.IsFalse(cursor.MoveAt(1, Lookup.EQ)); Assert.IsTrue(cursor.MoveAt(1, Lookup.GE)); Assert.AreEqual(3, cursor.CurrentKey); Assert.AreEqual(32, cursor.CurrentValue); // move forward 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(3, cursor.CurrentKey); Assert.AreEqual(32, cursor.CurrentValue); // async moves 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 lower.Add(6, 60); t.Wait(); Assert.IsTrue(moved); Assert.AreEqual(6, cursor.CurrentKey); Assert.AreEqual(4 + 60, 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); }
public void UpdateEventIsTriggered() { var sm = new SortedMap<DateTime, double>(); (sm as IUpdateable<DateTime, double>).OnData += (s, x) => { Console.WriteLine("Added {0} : {1}", x.Key, x.Value); }; sm.Add(DateTime.UtcNow.Date.AddSeconds(0), 0); }
public override void readFromSortedMap(SortedMap<Integer, String> sortedAreaCodeMap) { numOfEntries = sortedAreaCodeMap.size(); phoneNumberPrefixes = new int[numOfEntries]; descriptions = new String[numOfEntries]; int index = 0; foreach (int prefix in sortedAreaCodeMap.keySet()) { phoneNumberPrefixes[index++] = prefix; possibleLengths.add((int) Math.log10(prefix) + 1); } sortedAreaCodeMap.values().toArray(descriptions); }
public void CouldEnumerateGrowingSM() { var count = 1000000; var sw = new Stopwatch(); sw.Start(); var sm = new SortedMap<DateTime, double>(); var c = sm.GetCursor(); for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); c.MoveNext(); Assert.AreEqual(i, c.CurrentValue); } sw.Stop(); Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds - 50); Console.WriteLine("Ops: {0}", Math.Round(0.000001 * count * 1000.0 / (sw.ElapsedMilliseconds * 1.0), 2)); }
public void CouldMoveAtGE() { var scm = new SortedMap<int, int>(50); for (int i = 0; i < 100; i++) { scm[i] = i; } var cursor = scm.GetCursor(); cursor.MoveAt(-100, Lookup.GE); Assert.AreEqual(0, cursor.CurrentKey); Assert.AreEqual(0, cursor.CurrentValue); var shouldBeFalse = cursor.MoveAt(-100, Lookup.LE); Assert.IsFalse(shouldBeFalse); }
public void CouldCalculateIncompleteMovingAverage() { var sm = new SortedMap<int, double>(); for (int i = 0; i < 20; i++) { sm.Add(i, i); } var sma = sm.SMA(2, true).ToSortedMap(); var c = 0; foreach (var kvp in sma) { if (c == 0) { Assert.AreEqual(c, kvp.Value); } else { Assert.AreEqual(0.5 * (c + (double)(c - 1)), kvp.Value); } c++; } }
public void CouldCalculateComplexGraph() { // TODO! need real complex data to test properly var sm = new SortedMap<DateTime, double>(); var dataTask = Task.Run(async () => { for (int i = 0; i < 1000; i++) { sm.Add(DateTime.Today.AddSeconds(i), i+10000); await Task.Delay(25); } sm.IsMutable = false; }); Thread.Sleep(50); var closeSeries = sm; var baseLeverage = 1.0; var sma = closeSeries.SMA(20, true); var deviation = sma / closeSeries - 1.0; var leverage = (baseLeverage * (-(5.0 * (deviation.Map(x => Math.Abs(x)))) + 1.0)); var smaSignal = deviation.Map(x => (double)(Math.Sign(x))); var smaPositionMultiple = ((smaSignal * leverage).Map(x => 0.25 * (Math.Round(x / 0.25)))); var smaPositionMultipleMap = smaPositionMultiple.ToSortedMap(); var traderTask = Task.Run(async () => { var positionCursor = smaPositionMultiple.GetCursor(); while (await positionCursor.MoveNext(CancellationToken.None)) // { await Task.Delay(15); Console.WriteLine("Time: {0}, position: {1}", positionCursor.CurrentKey, positionCursor.CurrentValue); } }); dataTask.Wait(); traderTask.Wait(); }
public void CouldMoveNextWithoutBatching() { var sm = new SortedMap<DateTime, double>(); var count = 1000; for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } sm.IsMutable = false; var bmvc = new BatchMapValuesCursor<DateTime, double, double>(sm.GetCursor, (v) => v + 1.0); var c = 0; while (c < 500 && bmvc.MoveNext()) { Assert.AreEqual(c + 1.0, bmvc.CurrentValue); c++; } while (bmvc.MoveNext(CancellationToken.None).Result) { // Setting IsMutable to false allows us to skip this check: c < 1000 && Assert.AreEqual(c + 1.0, bmvc.CurrentValue); c++; } Assert.AreEqual(count, c); }
public void CouldMovePreviousWithoutBatching() { var sm = new SortedMap<DateTime, double>(); var count = 1000; for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } sm.IsMutable = false; var bmvc = new BatchMapValuesCursor<DateTime, double, double>(sm.GetCursor, (v) => v + 1.0); var c = 0; while (c < 500 && bmvc.MoveNext()) { Assert.AreEqual(c + 1.0, bmvc.CurrentValue); c++; } c--; while (bmvc.MovePrevious()) { c--; Assert.AreEqual(c + 1.0, bmvc.CurrentValue); } Assert.AreEqual(0, c); }
public void CouldCloneZipLagSeries() { var count = 1000; var sm = new SortedMap<int, double>(); for (int i = 0; i < count; i++) { sm.Add(i, i); } // slow implementation var sw = new Stopwatch(); sw.Start(); var zipLag = sm.ZipLag(1, (cur, prev) => cur + prev); //.ToSortedMap(); var zc = zipLag.GetCursor(); zc.MoveNext(); var zc2 = zc.Clone(); Assert.AreEqual(zc.CurrentKey, zc2.CurrentKey); zc.MoveNext(); zc2.MoveNext(); Assert.AreEqual(zc.CurrentKey, zc2.CurrentKey); zc.MovePrevious(); zc2.MovePrevious(); Assert.AreEqual(zc.CurrentKey, zc2.CurrentKey); for (int i = 1; i < count; i++) { var expected = i + i - 1; double actual; var ok = zc.TryGetValue(i, out actual); Assert.AreEqual(expected, actual); } var sm2 = new SortedMap<int, double>(); var zc3 = sm2.ZipLag(1, (cur, prev) => cur + prev).GetCursor(); var t = Task.Run(async () => { var c = 1; // first key is missing because we cannot create state at it while (await zc3.MoveNext(CancellationToken.None)) { var expected = c + c - 1; Assert.AreEqual(expected, zc3.CurrentValue); c++; } }); for (int i = 0; i < count; i++) { sm2.Add(i, i); } sm2.IsMutable = false; // without it MoveNextAsync will wait forever t.Wait(); }
public void ZipLagIssue11Test() { OptimizationSettings.CombineFilterMapDelegates = true; var data = new SortedMap<DateTime, double>(); var count = 5000; for (int i = 0; i < count; i++) { data.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } var sma = data.SMA(20, true).Lag(1u); Console.WriteLine($"laggedSma count: {sma.Count()}"); var deviation = sma; // this line or any other intermediate enumeration affect the last line Console.WriteLine($"deviation count: {deviation.Count()}"); var direction = deviation;//.Map(x => (Math.Sign(x))); Console.WriteLine($"direction count: {direction.Count()}"); var diff = direction.ZipLag(1u, (c, p) => c - p); //.ToSortedMap(); Console.WriteLine($"Count: {diff.Count()}"); Assert.IsTrue(diff.Count() > 0); }
public void CouldCalculateMovingStDevIncomlete() { var sm = new SortedMap<DateTime, double>(); var count = 1000000; for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } // slow implementation var sw = new Stopwatch(); sw.Start(); var ma = sm.StDev(20, true); //.ToSortedMap(); var c = 1; foreach (var m in ma) { if (c < 30) { Console.WriteLine(m.Value); } else // TODO on c = 9490618 we have a different value: 5.91740018927231 // Excel value 5.91607978309962 if (Math.Abs(m.Value - 5.9160797830996161) > 0.0000001) { Console.WriteLine(m.Value); Console.WriteLine($"Error c: {c}"); throw new ApplicationException("Invalid value"); } c++; } sw.Stop(); Console.WriteLine($"Final c: {c}"); Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0))); // }
public void CouldCalculateMovingAverageIncomplete() { var sm = new SortedMap<DateTime, double>(); var count = 100000; for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } // slow implementation var sw = new Stopwatch(); sw.Start(); var ma = sm.SMA(20, true); //.ToSortedMap(); var c = 1; foreach (var m in ma) { if (c <= 20) { //Console.WriteLine(m.Value); } else if (m.Value != c - 19 + 8.5) { Console.WriteLine(m.Value); throw new ApplicationException("Invalid value"); } c++; } sw.Stop(); Console.WriteLine($"Final c: {c}"); Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0))); // }
public void CouldCalculateMovingAverage() { var sm = new SortedMap<DateTime, double>(); var count = 1000000; for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } // slow implementation var sw = new Stopwatch(); sw.Start(); var ma = sm.SMA(20); //.ToSortedMap(); var cursor = ma.GetCursor(); cursor.MoveNext(); //var cc = 0; //while (cursor.MoveNext()) //{ // cc++; //} //Console.WriteLine(cc); //if (cursor.MoveNext()) //{ // throw new ApplicationException("Moved next after MoveNext() returned false"); //} //cursor.MoveFirst(); var c = 1; //foreach (var m in ma) { cursor.Reset(); while (cursor.MoveNext()) { if (cursor.CurrentValue != c + 8.5) { Console.WriteLine(cursor.CurrentValue);// m.Value); Console.WriteLine($"Error c: {c}"); throw new ApplicationException("Invalid value"); } c++; if (c == 999982) { Console.WriteLine("Catch me"); } } sw.Stop(); Console.WriteLine($"Final c: {c}"); Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0))); ma = null; GC.Collect(3, GCCollectionMode.Forced, true); Thread.Sleep(2000); // NB! In release mode this must print that ToSortedMap task exited, in Debug mode GC does not collect SM and weak reference stays alive }
public void CouldCalculateAverageOnMovingWindowWithStep() { var sm = new SortedMap<DateTime, double>(); var count = 100000; for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } // slow implementation var sw = new Stopwatch(); sw.Start(); var ma = sm.Window(20, 2);//.ToSortedMap(); var c = 1; foreach (var m in ma) { var innersm = m.Value;//.ToSortedMap(); if (innersm.Values.Average() != c + 8.5) { Console.WriteLine(m.Value.Values.Average()); throw new ApplicationException("Invalid value"); } c++; c++; } sw.Stop(); Console.WriteLine($"Final c: {c}"); Console.WriteLine("Window MA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0))); Console.WriteLine("Calculation ops: {0}", (int)((double)count * 20.0 / (sw.ElapsedMilliseconds / 1000.0))); }
/// <summary> /// Returns a synchronized (thread-safe) sorted map backed by the specified sorted map. /// </summary> public static SortedMap synchronizedSortedMap(SortedMap m) { return default(SortedMap); }
/// <summary> /// Returns an unmodifiable view of the specified sorted map. /// </summary> public static SortedMap unmodifiableSortedMap(SortedMap m) { return default(SortedMap); }
public void SmaDeviationTest() { var data = new SortedMap<DateTime, double>(); for (int i = 1; i < 101; i++) { data.Add(DateTime.UtcNow.Date.AddMinutes(i), i); } var sma = data.SMA(20, true); var deviation = (data/sma - 1); var deviationSm = deviation; var smaDirection = deviation.Map(Math.Sign); Assert.AreEqual(100, smaDirection.Count()); Assert.AreEqual(100, deviation.Count()); }
private TreeSet(SortedMap m) { mSortedMap = m; mKeySet = m.keySet(); }
public Dictionary<string, IReadOnlyOrderedMap<DateTime, double>> GetImplementation() { var implemetations = new Dictionary<string, IReadOnlyOrderedMap<DateTime, double>>(); var sm_irregular_small = new SortedMap<DateTime, double>(); var sm_irregular_big = new SortedMap<DateTime, double>(); var sm_regular_small = new SortedMap<DateTime, double>(); var sm_regular_big = new SortedMap<DateTime, double>(); var scm_irregular_small = new SortedChunkedMap<DateTime, double>(); var scm_irregular_big = new SortedChunkedMap<DateTime, double>(); var scm_regular_small = new SortedChunkedMap<DateTime, double>(); var scm_regular_big = new SortedChunkedMap<DateTime, double>(); sm_irregular_small.Add(DateTime.Today.AddDays(-2), -2.0); sm_irregular_big.Add(DateTime.Today.AddDays(-2), -2.0); scm_irregular_small.Add(DateTime.Today.AddDays(-2), -2.0); scm_irregular_big.Add(DateTime.Today.AddDays(-2), -2.0); for (int i = 0; i < _big; i++) { if (i < _small) { sm_irregular_small.Add(DateTime.Today.AddDays(i), i); sm_regular_small.Add(DateTime.Today.AddDays(i), i); scm_irregular_small.Add(DateTime.Today.AddDays(i), i); scm_regular_small.Add(DateTime.Today.AddDays(i), i); } sm_irregular_big.Add(DateTime.Today.AddDays(i), i); sm_regular_big.Add(DateTime.Today.AddDays(i), i); scm_irregular_big.Add(DateTime.Today.AddDays(i), i); scm_regular_big.Add(DateTime.Today.AddDays(i), i); } // SM regular implemetations.Add("sm_irregular_small", sm_irregular_small); implemetations.Add("sm_regular_small", sm_regular_small); implemetations.Add("scm_irregular_small", scm_irregular_small); implemetations.Add("scm_regular_small", scm_regular_small); implemetations.Add("sm_irregular_big", sm_irregular_big); implemetations.Add("sm_regular_big", sm_regular_big); implemetations.Add("scm_irregular_big", scm_irregular_big); implemetations.Add("scm_regular_big", scm_regular_big); return implemetations; }