Added possibility to clone an existing ExaArray.
Closes thorsten/ExaArray#2 (comment)
This commit is contained in:
parent
8c0722be44
commit
7f56894efe
35
ExaArray/ExaArray1D.Factories.cs
Normal file
35
ExaArray/ExaArray1D.Factories.cs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Exa
|
||||||
|
{
|
||||||
|
public partial class ExaArray1D<T>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new ExaArray1D from another.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// When <c>T</c> is a value type, data gets copied as values. When <c>T</c> is a reference type, the pointers
|
||||||
|
/// to the original objects are copied. Thus, this factory method does not create a deep copy.
|
||||||
|
///
|
||||||
|
/// Performance: O(n)
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="other">The instance from which the new instance is to be created.</param>
|
||||||
|
/// <returns>The new instance</returns>
|
||||||
|
public static ExaArray1D<T> CreateFrom(ExaArray1D<T> other)
|
||||||
|
{
|
||||||
|
var next = new ExaArray1D<T>(other.OptimizationStrategy)
|
||||||
|
{
|
||||||
|
Length = other.Length,
|
||||||
|
chunks = new T[other.chunks.Length][]
|
||||||
|
};
|
||||||
|
|
||||||
|
for (var n = 0; n < other.chunks.Length; n++)
|
||||||
|
{
|
||||||
|
next.chunks[n] = new T[other.chunks[n].Length];
|
||||||
|
Array.Copy(other.chunks[n], next.chunks[n], other.chunks[n].Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -8,7 +8,7 @@ namespace Exa
|
|||||||
/// to 4.6 quintillion (4,607,183,514,018,780,000) or 4.6 exa elements.
|
/// to 4.6 quintillion (4,607,183,514,018,780,000) or 4.6 exa elements.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The desired type to use, e.g. byte, int, etc.</typeparam>
|
/// <typeparam name="T">The desired type to use, e.g. byte, int, etc.</typeparam>
|
||||||
public sealed class ExaArray1D<T>
|
public sealed partial class ExaArray1D<T>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Unfortunately, this seems to be the maximal number of entries an
|
/// Unfortunately, this seems to be the maximal number of entries an
|
||||||
|
@ -180,7 +180,7 @@ namespace ExaArrayTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
[Category("normal")]
|
[Category("intensive")]
|
||||||
public void CountingHugeSize01()
|
public void CountingHugeSize01()
|
||||||
{
|
{
|
||||||
var exaA = new ExaArray1D<byte>();
|
var exaA = new ExaArray1D<byte>();
|
||||||
@ -194,6 +194,114 @@ namespace ExaArrayTests
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
[Category("normal")]
|
||||||
|
[Category("cover")]
|
||||||
|
public void CreateFrom001()
|
||||||
|
{
|
||||||
|
var exPerf = new ExaArray1D<byte>(Strategy.MAX_PERFORMANCE);
|
||||||
|
exPerf.Extend(2);
|
||||||
|
exPerf[0] = 0x01;
|
||||||
|
exPerf[1] = 0x02;
|
||||||
|
|
||||||
|
var next = ExaArray1D<byte>.CreateFrom(exPerf);
|
||||||
|
Assert.That(next.Length, Is.EqualTo(exPerf.Length));
|
||||||
|
Assert.That(next.OptimizationStrategy, Is.EqualTo(exPerf.OptimizationStrategy));
|
||||||
|
Assert.That(next.Items(), Is.EquivalentTo(exPerf.Items()));
|
||||||
|
|
||||||
|
exPerf = null;
|
||||||
|
next = null;
|
||||||
|
|
||||||
|
var exElem = new ExaArray1D<byte>(Strategy.MAX_ELEMENTS);
|
||||||
|
exElem.Extend(2);
|
||||||
|
exElem[0] = 0x03;
|
||||||
|
exElem[1] = 0x04;
|
||||||
|
|
||||||
|
next = ExaArray1D<byte>.CreateFrom(exElem);
|
||||||
|
Assert.That(next.Length, Is.EqualTo(exElem.Length));
|
||||||
|
Assert.That(next.OptimizationStrategy, Is.EqualTo(exElem.OptimizationStrategy));
|
||||||
|
Assert.That(next.Items(), Is.EquivalentTo(exElem.Items()));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
[Category("normal")]
|
||||||
|
[Category("cover")]
|
||||||
|
public void CreateFrom002Objects()
|
||||||
|
{
|
||||||
|
var exPerf = new ExaArray1D<object>(Strategy.MAX_PERFORMANCE);
|
||||||
|
exPerf.Extend(2);
|
||||||
|
exPerf[0] = new object();
|
||||||
|
exPerf[1] = new object();
|
||||||
|
|
||||||
|
var next = ExaArray1D<object>.CreateFrom(exPerf);
|
||||||
|
Assert.That(next.Length, Is.EqualTo(exPerf.Length));
|
||||||
|
Assert.That(next.OptimizationStrategy, Is.EqualTo(exPerf.OptimizationStrategy));
|
||||||
|
Assert.That(next[0], Is.SameAs(exPerf[0]));
|
||||||
|
Assert.That(next[1], Is.SameAs(exPerf[1]));
|
||||||
|
Assert.That(next[0], Is.Not.SameAs(next[1]));
|
||||||
|
|
||||||
|
exPerf = null;
|
||||||
|
next = null;
|
||||||
|
|
||||||
|
var exElem = new ExaArray1D<object>(Strategy.MAX_ELEMENTS);
|
||||||
|
exElem.Extend(2);
|
||||||
|
exElem[0] = new object();
|
||||||
|
exElem[1] = new object();
|
||||||
|
|
||||||
|
next = ExaArray1D<object>.CreateFrom(exElem);
|
||||||
|
Assert.That(next.Length, Is.EqualTo(exElem.Length));
|
||||||
|
Assert.That(next.OptimizationStrategy, Is.EqualTo(exElem.OptimizationStrategy));
|
||||||
|
Assert.That(next[0], Is.SameAs(exElem[0]));
|
||||||
|
Assert.That(next[1], Is.SameAs(exElem[1]));
|
||||||
|
Assert.That(next[0], Is.Not.SameAs(next[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
[Category("normal")]
|
||||||
|
[Category("cover")]
|
||||||
|
public void CreateFrom003()
|
||||||
|
{
|
||||||
|
var exPerf = new ExaArray1D<byte>(Strategy.MAX_PERFORMANCE);
|
||||||
|
exPerf.Extend(5_000_000_000); // more than one chunk
|
||||||
|
|
||||||
|
var next = ExaArray1D<byte>.CreateFrom(exPerf);
|
||||||
|
Assert.That(next.Length, Is.EqualTo(exPerf.Length));
|
||||||
|
Assert.DoesNotThrow(() =>
|
||||||
|
{
|
||||||
|
next[4_999_999_999] = 0xab;
|
||||||
|
});
|
||||||
|
|
||||||
|
exPerf = null;
|
||||||
|
next = null;
|
||||||
|
|
||||||
|
var exElem = new ExaArray1D<byte>(Strategy.MAX_ELEMENTS);
|
||||||
|
exElem.Extend(5_000_000_000);
|
||||||
|
|
||||||
|
next = ExaArray1D<byte>.CreateFrom(exElem);
|
||||||
|
Assert.That(next.Length, Is.EqualTo(exElem.Length));
|
||||||
|
Assert.DoesNotThrow(() =>
|
||||||
|
{
|
||||||
|
next[4_999_999_999] = 0xab;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
[Category("normal")]
|
||||||
|
[Category("cover")]
|
||||||
|
public void CreateFrom004()
|
||||||
|
{
|
||||||
|
var exPerf = new ExaArray1D<byte>(Strategy.MAX_PERFORMANCE);
|
||||||
|
var next = ExaArray1D<byte>.CreateFrom(exPerf);
|
||||||
|
Assert.That(next.Length, Is.EqualTo(0));
|
||||||
|
|
||||||
|
exPerf = null;
|
||||||
|
next = null;
|
||||||
|
|
||||||
|
var exElem = new ExaArray1D<byte>(Strategy.MAX_ELEMENTS);
|
||||||
|
next = ExaArray1D<byte>.CreateFrom(exElem);
|
||||||
|
Assert.That(next.Length, Is.EqualTo(0));
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
[Category("intensive")]
|
[Category("intensive")]
|
||||||
public void Adding5Billion01()
|
public void Adding5Billion01()
|
||||||
|
Loading…
Reference in New Issue
Block a user