Skip to content

Commit e880d00

Browse files
committed
z-curve example added
1 parent 984075c commit e880d00

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Eocron.Algorithms.SpaceCurves;
2+
using FluentAssertions;
3+
using NUnit.Framework;
4+
5+
namespace Eocron.Algorithms.Tests
6+
{
7+
[TestFixture]
8+
public class SpaceCurveTests
9+
{
10+
[Test]
11+
public void ZCurve_Check()
12+
{
13+
var input = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
14+
var actual = ZCurve.Interleave(input, 4);
15+
actual.Should().Equal(new[] {1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12});
16+
}
17+
}
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Eocron.Algorithms.SpaceCurves
5+
{
6+
public static class ZCurve
7+
{
8+
public static T[] Interleave<T>(IReadOnlyList<T> items, int n)
9+
{
10+
if (items.Count % n != 0)
11+
{
12+
throw new ArgumentOutOfRangeException(nameof(n), "Array must be a multiple of n");
13+
}
14+
var output = new T[items.Count];
15+
var m = output.Length / n;
16+
for (var i = 0; i < output.Length; i++)
17+
{
18+
output[GetInterleavedIndex(i, n, m)] = items[i];
19+
}
20+
return output;
21+
}
22+
23+
public static int GetInterleavedIndex(int idx, int n, int m)
24+
{
25+
var i = idx / n;
26+
var j = idx % n;
27+
return j * m + i;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)