本文整理汇总了C#中SortedMap.Complete方法的典型用法代码示例。如果您正苦于以下问题:C# SortedMap.Complete方法的具体用法?C# SortedMap.Complete怎么用?C# SortedMap.Complete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SortedMap
的用法示例。
在下文中一共展示了SortedMap.Complete方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CouldCalculateSMAInRealTime
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.Complete();
});
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);
}
}
示例2: CouldCalculateComplexGraph
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.Complete();
});
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();
}
示例3: ZipNFromLogoAndReadmeRepeatCouldMoveCursorCorrectly
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);
}
示例4: MoveNextAsyncBenchmark
public void MoveNextAsyncBenchmark() {
// this benchmark shows that simple async enumeration gives 13+ mops,
// this means than we should use parallel enumeration on joins.
// the idea is that a chain of calculations could be somewhat heavy, e.g. rp(ma(log(zipLag(c p -> c/p))))
// but they are optimized for single thread: when movenext is called on the outer cursor,
// the whole chain enumerates synchronously.
// During joins, we must make these evaluations parallel. The net overhead of tasks over existing data is visible
// but not too big, while on real-time stream there is no alternative at all.
// Join algos should be paralell and task-based by default
var count = 10000000;
var sw = new Stopwatch();
var sm = new SortedMap<DateTime, double>();
//sm.IsSynchronized = true;
for (int i = 0; i < count; i++) {
sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
}
sm.Complete();
sw.Start();
double sum = 0.0;
var c = sm.GetCursor();
Task.Run(async () => {
while (await c.MoveNext(CancellationToken.None)) {
sum += c.CurrentValue;
}
}).Wait();
sw.Stop();
double expectedSum = 0.0;
for (int i = 0; i < count; i++) {
expectedSum += i;
}
Assert.AreEqual(expectedSum, sum);
Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
Console.WriteLine("Ops: {0}", Math.Round(0.000001 * count * 1000.0 / (sw.ElapsedMilliseconds * 1.0), 2));
}
示例5: CouldCloneZipLagSeries
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.Complete(); // without it MoveNextAsync will wait forever
t.Wait();
}
示例6: CouldNotMoveAsyncContinuousOnEmptyZip
public void CouldNotMoveAsyncContinuousOnEmptyZip() {
var sm1 = new SortedMap<int, int>();
var sm2 = new SortedMap<int, int>();
sm1.Complete();
sm2.Complete();
var zipped = sm1.Repeat() + sm2.Repeat();
var c1 = zipped.GetCursor();
Assert.IsFalse(sm1.GetCursor().MoveNext(CancellationToken.None).Result);
Assert.IsFalse(sm2.GetCursor().MoveNext(CancellationToken.None).Result);
Assert.IsFalse(c1.MoveNext());
Assert.IsFalse(c1.MoveFirst());
var task = c1.MoveNext(CancellationToken.None);
task.Wait();
Assert.AreEqual(TaskStatus.RanToCompletion, task.Status);
Assert.IsFalse(task.Result);
}
示例7: CouldZipManyNonContinuousInRealTime
public void CouldZipManyNonContinuousInRealTime() {
var sm1 = new SortedMap<DateTime, double>();
var sm2 = new SortedMap<DateTime, double>();
var count = 100000;
for (int i = 0; i < count; i++) {
sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
}
Task.Run(() => {
Thread.Sleep(1000);
for (int i = count; i < count * 2; i++) {
sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
//Thread.Sleep(50);
}
sm1.Complete(); // stop mutating
//Console.WriteLine("Set immutable");
});
Task.Run(() => {
Thread.Sleep(950);
for (int i = count; i < count * 2; i++) {
sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
//Thread.Sleep(50);
}
sm2.Complete(); // stop mutating
//Console.WriteLine("Set immutable");
});
// this test measures isolated performance of ZipN, without ToSortedMap
var sw = new Stopwatch();
var series = new[] { sm1, sm2 };
sw.Start();
var totalSum = 0.0;
var sumCursor = series.Zip((k, varr) => varr.Sum()).GetCursor();
var c = 0;
while (c < 5 && sumCursor.MoveNext()) {
//Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
totalSum += sumCursor.CurrentValue;
c++;
}
while (sumCursor.MoveNext(CancellationToken.None).Result) {
//Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
//Console.WriteLine("Value: " + sumCursor.CurrentValue);
totalSum += sumCursor.CurrentValue;
c++;
}
sw.Stop();
Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
Console.WriteLine("Total sum: {0}", totalSum);
}
示例8: ContinuousZipIsCorrectByRandomCheck
public void ContinuousZipIsCorrectByRandomCheck() {
var sw = new Stopwatch();
var sm1 = new SortedMap<int, int>();
var sm2 = new SortedMap<int, int>();
var rng = new System.Random(31415926); //31415926
var prev1 = 0;
var prev2 = 0;
for (int i = 0; i < 100000; i = i + 1) {
prev1 = prev1 + rng.Next(1, 11);
sm1.Add(prev1, prev1);
prev2 = prev2 + rng.Next(1, 11);
sm2.Add(prev2, prev2);
}
sm1.Complete();
sm2.Complete();
//Console.WriteLine("First map:");
//foreach (var kvp in sm1)
//{
// Console.WriteLine(kvp.Key);
//}
//Console.WriteLine("Second map:");
//foreach (var kvp in sm2) {
// Console.WriteLine(kvp.Key);
//}
var series = new[] { sm1.Repeat(), sm2.Repeat(), };
sw.Start();
var allKeys = sm1.keys.Union(sm2.keys).OrderBy(x => x).ToArray();
int[] expectedKeys = new int[allKeys.Length];
int[] expectedValues = new int[allKeys.Length];
var size = 0;
for (int i = 0; i < allKeys.Length; i++) {
var val = 0;
KeyValuePair<int, int> temp;
var hasFirst = sm1.TryFind(allKeys[i], Lookup.LE, out temp);
if (hasFirst) {
val += temp.Value;
var hasSecond = sm2.TryFind(allKeys[i], Lookup.LE, out temp);
if (hasSecond) {
val += temp.Value;
expectedKeys[size] = allKeys[i];
expectedValues[size] = val;
size++;
}
}
}
var expectedMap = SortedMap<int, int>.OfSortedKeysAndValues(expectedKeys, expectedValues, size);
sw.Stop();
//Console.WriteLine("Expected map:");
//foreach (var kvp in expectedMap) {
// Console.WriteLine(kvp.Key + " ; " + kvp.Value);
//}
Console.WriteLine("Manual join, elapsed msec: {0}", sw.ElapsedMilliseconds);
SortedMap<int, int> sum = new SortedMap<int, int>();
for (int round = 0; round < 1; round++) {
sw.Restart();
var ser = series.Zip((k, varr) => varr.Sum());
var cur = ser.GetCursor();
while (cur.MoveNext()) {
sum.AddLast(cur.CurrentKey, cur.CurrentValue);
}
sw.Stop();
Console.WriteLine("Zip join, elapsed msec: {0}", sw.ElapsedMilliseconds);
//Console.WriteLine("StateCreation: {0}", RepeatCursor<int, int>.StateCreation);
//Console.WriteLine("StateHit: {0}", RepeatCursor<int, int>.StateHit);
//Console.WriteLine("StateMiss: {0}", RepeatCursor<int, int>.StateMiss);
}
//Console.WriteLine("Sync zip map:");
//foreach (var kvp in sum) {
// Console.WriteLine(kvp.Key + " ; " + kvp.Value);
//}
Assert.AreEqual(expectedMap.Count, sum.Count, "Results of sync and expected must be equal");
foreach (var kvp in expectedMap) {
Assert.AreEqual(kvp.Value, sum[kvp.Key]);
}
for (int round = 0; round < 1; round++) {
sw.Restart();
var ser = series.Zip((k, varr) => varr.Sum());
//.........这里部分代码省略.........
示例9: ContinuousZipIsCorrectByConstrcution
public void ContinuousZipIsCorrectByConstrcution() {
var count = 10;
var sw = new Stopwatch();
var sm1 = new SortedMap<int, int>();
var sm2 = new SortedMap<int, int>();
sm1.Add(0, 0);
sm2.Add(0, 0);
for (int i = 2; i < count; i = i + 2) {
sm1.Add(i, i);
sm2.Add(i + 1, i);
}
sm1.Complete();
sm2.Complete();
var series = new[] { sm1.Repeat(), sm2.Repeat(), };
sw.Start();
var ser = series.Zip((k, varr) => varr.Sum());
var sum = ser.ToSortedMap();
sw.Stop();
Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
for (int i = 2; i < count; i = i + 2) {
Assert.AreEqual(i * 2 - 2, sum[i]);
}
var cur = ser.GetCursor();
var cur2 = cur.Clone();
var sum2 = new SortedMap<int, int>();
while (cur2.MoveNext(CancellationToken.None).Result) {
sum2.Add(cur2.CurrentKey, cur2.CurrentValue);
}
Assert.AreEqual(sum.Count, sum2.Count, "Results of sync and async moves must be equal");
Assert.IsTrue(cur.MoveNext(CancellationToken.None).Result);
Assert.AreEqual(0, cur.CurrentValue);
var c = 2;
while (cur.MoveNext(CancellationToken.None).Result) {
Assert.AreEqual(c * 2 - 2, cur.CurrentValue);
var x = cur.MoveNext(CancellationToken.None).Result;
c += 2;
}
}
示例10: CouldZipManyContinuousInRealTime
public void CouldZipManyContinuousInRealTime() {
//Assert.Inconclusive();
//Trace.TraceWarning("volkswagening: this test hangs when started together with ZipN tests");
//return;
var sm1 = new SortedMap<DateTime, double>();
var sm2 = new SortedMap<DateTime, double>();
var count = 100000;
for (int i = 0; i < count; i++) {
sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
}
var t1 = Task.Run(() => {
try {
Thread.Sleep(1000);
for (int i = count; i < count * 2; i++) {
sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
}
} finally {
sm1.Complete();
Console.WriteLine("sm1.Complete()");
}
});
var t2 = Task.Run(() => {
try {
Thread.Sleep(950);
for (int i = count; i < count * 2; i++) {
sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
}
} finally {
sm2.Complete();
Console.WriteLine("sm2.Complete()");
}
});
// this test measures isolated performance of ZipN, without ToSortedMap
var sw = new Stopwatch();
var series = new[] { sm1.Repeat(), sm2.Repeat() };
sw.Start();
var totalSum = 0.0;
var sumCursor = series.Zip((k, varr) => varr.Sum()).GetCursor();
var c = 0;
while (c < 5 && sumCursor.MoveNext()) {
//Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
totalSum += sumCursor.CurrentValue;
c++;
}
var t3 = Task.Run(async () =>
{
var previous = sumCursor.CurrentKey;
while (await sumCursor.MoveNext(CancellationToken.None)) {
//Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
//Console.WriteLine("Value: " + sumCursor.CurrentValue);
totalSum += sumCursor.CurrentValue;
c++;
Assert.IsTrue(sumCursor.CurrentKey > previous, "Next key is less than previous");
previous = sumCursor.CurrentKey;
}
});
Task.WaitAll(t1, t2, t3);
sw.Stop();
Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
Console.WriteLine("Total sum: {0}", totalSum);
Assert.AreEqual(count * 2, sm1.Count);
Assert.AreEqual(count * 2, sm2.Count);
}
示例11: CouldZipContinuousInRealTime
public void CouldZipContinuousInRealTime() {
var sm1 = new SortedMap<DateTime, double>();
var sm2 = new SortedMap<DateTime, double>();
var count = 100;
for (int i = 0; i < count; i++) {
sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
}
Task.Run(() => {
Thread.Sleep(1000);
for (int i = count; i < count * 2; i++) {
sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
Thread.Sleep(50);
}
sm1.Complete(); // stop mutating
//Console.WriteLine("Set immutable");
});
Task.Run(() => {
Thread.Sleep(950);
for (int i = count; i < count * 2; i++) {
sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
Thread.Sleep(50);
}
sm2.Complete(); // stop mutating
//Console.WriteLine("Set immutable");
});
// this test measures isolated performance of ZipN, without ToSortedMap
Thread.Sleep(1050);
var sw = new Stopwatch();
var series = new[] { sm1.Repeat(), sm2.Repeat() };
sw.Start();
var totalSum = 0.0;
var sumCursor = series.Zip((k, varr) => varr.Sum()).GetCursor();
var c = 0;
while (c < 5 && sumCursor.MoveNext()) {
Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
totalSum += sumCursor.CurrentValue;
c++;
}
Task.Run(async () => {
while (await sumCursor.MoveNext(CancellationToken.None)) {
if (Math.Abs(c * 4.0 - sumCursor.CurrentValue) <= 3.0) { // NB VolksWagening
// TODO deal with it somehow, e.g. with recalc of the last value, and explicitly document
Trace.TraceWarning("Zipping continuous series in real-time is inherently non-deterministic");
} else {
Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
}
Console.WriteLine("Value: " + sumCursor.CurrentValue);
totalSum += sumCursor.CurrentValue;
c++;
}
sw.Stop();
Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
Console.WriteLine("Total sum: {0}", totalSum);
}).Wait();
Thread.Sleep(100);
sumCursor.Dispose();
}
示例12: CouldZipManyContinuousInRealTime3
public void CouldZipManyContinuousInRealTime3() {
var sm1 = new SortedMap<DateTime, double>();
var sm2 = new SortedMap<DateTime, double>();
var count = 100000;
for (int i = 0; i < count; i++) {
sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
}
sm1.Complete(); // will mutate after the first batch
sm2.Complete();
// this test measures isolated performance of ZipN, without ToSortedMap
var sw = new Stopwatch();
var series = new[] { sm1.Repeat(), sm2.Repeat(), sm1.Repeat(), sm2.Repeat(), sm1.Repeat(), sm2.Repeat(), sm1.Repeat(), sm2.Repeat(), sm1.Repeat(), sm2.Repeat() };
sw.Start();
var totalSum = 0.0;
var sumCursor = series.Zip((k, varr) => varr.Sum()).GetCursor();
var c = 0;
//while (sumCursor.MoveNext()) {
// //Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
// totalSum += sumCursor.CurrentValue;
// c++;
//}
Task.Run(async () => {
while (await sumCursor.MoveNext(CancellationToken.None)) {
//Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
//Console.WriteLine("Value: " + sumCursor.CurrentValue);
totalSum += sumCursor.CurrentValue;
c++;
}
sw.Stop();
Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
Console.WriteLine("Total sum: {0}", totalSum);
}).Wait();
}