Skip to content

Commit 17cb8dc

Browse files
committed
fix curve
1 parent e880d00 commit 17cb8dc

2 files changed

Lines changed: 46 additions & 9 deletions

File tree

Eocron.Algorithms.Tests/SpaceCurveTests.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,28 @@ namespace Eocron.Algorithms.Tests
88
public class SpaceCurveTests
99
{
1010
[Test]
11-
public void ZCurve_Check()
11+
public void ZCurve_Check_Base3()
1212
{
1313
var input = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
14-
var actual = ZCurve.Interleave(input, 4);
14+
var actual = ZCurve.Interleave(input, 3);
1515
actual.Should().Equal(new[] {1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12});
1616
}
17+
18+
[Test]
19+
public void ZCurve_Check_Base2()
20+
{
21+
var input = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
22+
var actual = ZCurve.Interleave(input, 2);
23+
actual.Should().Equal(new[] {1, 7, 2, 8, 3, 9, 4, 10, 5, 11, 6, 12});
24+
}
25+
26+
[Test]
27+
public void ZCurve_Check_Multiple()
28+
{
29+
var input1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
30+
var input2 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
31+
var actual = ZCurve.Interleave(input1, input2);
32+
actual.Should().Equal(new[] {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12});
33+
}
1734
}
1835
}

Eocron.Algorithms/SpaceCurves/ZCurve.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
namespace Eocron.Algorithms.SpaceCurves
56
{
67
public static class ZCurve
78
{
8-
public static T[] Interleave<T>(IReadOnlyList<T> items, int n)
9+
public static T[] Interleave<T>(IReadOnlyList<T> items, int m)
910
{
10-
if (items.Count % n != 0)
11+
return Interleave<T>(items, items.Count, m);
12+
}
13+
14+
15+
public static T[] Interleave<T>(IEnumerable<T> items, int size, int m)
16+
{
17+
if (size % m != 0)
1118
{
12-
throw new ArgumentOutOfRangeException(nameof(n), "Array must be a multiple of n");
19+
throw new ArgumentOutOfRangeException(nameof(m), "Array must be a multiple of " + m);
1320
}
14-
var output = new T[items.Count];
15-
var m = output.Length / n;
16-
for (var i = 0; i < output.Length; i++)
21+
var output = new T[size];
22+
var n = size / m;
23+
var i = 0;
24+
foreach (var item in items)
1725
{
18-
output[GetInterleavedIndex(i, n, m)] = items[i];
26+
output[GetInterleavedIndex(i, n, m)] = item;
27+
i++;
1928
}
2029
return output;
2130
}
2231

32+
public static T[] Interleave<T>(params IReadOnlyList<T>[] itemSequences)
33+
{
34+
if (itemSequences.Select(x=> x.Count).Distinct().Count()>1)
35+
{
36+
throw new ArgumentOutOfRangeException(nameof(itemSequences), "Sequences should have same size");
37+
}
38+
39+
var m = itemSequences.Length;
40+
return Interleave(itemSequences.SelectMany(x => x), itemSequences.Sum(x => x.Count), m);
41+
}
42+
2343
public static int GetInterleavedIndex(int idx, int n, int m)
2444
{
2545
var i = idx / n;

0 commit comments

Comments
 (0)