Implemented store and load methods

This commit is contained in:
Thorsten Sommer 2020-08-04 20:51:13 +02:00
parent 42b7d4349d
commit 0d6c719148
2 changed files with 123 additions and 1 deletions

View File

@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace Exa
{
@ -8,7 +11,8 @@ namespace Exa
/// to 4.6 quintillion (4,607,183,514,018,780,000) or 4.6 exa elements.
/// </summary>
/// <typeparam name="T">The desired type to use, e.g. byte, int, etc.</typeparam>
public sealed partial class ExaArray1D<T>
[Serializable]
public sealed partial class ExaArray1D<T> : ISerializable
{
/// <summary>
/// Unfortunately, this seems to be the maximal number of entries an
@ -218,5 +222,64 @@ namespace Exa
for (ulong n = 0; n < this.Length; n++)
yield return this[n];
}
#region Store and load
/// <summary>
/// Stores the exa array into a stream.
/// </summary>
/// <remarks>
/// This method does not dispose the stream.
/// </remarks>
public void Store(Stream outputStream)
{
var formatter = new BinaryFormatter();
formatter.Serialize(outputStream, this);
}
/// <summary>
/// Restores an exa array from the given stream.
/// </summary>
/// <remarks>
/// This method does not dispose the stream.
/// </remarks>
public static ExaArray1D<T> Restore(Stream inputStream)
{
var formatter = new BinaryFormatter();
return formatter.Deserialize(inputStream) as ExaArray1D<T>;
}
#endregion
#region Serialization
/// <summary>
/// This method serves for the serialization process. Do not call it manually.
/// </summary>
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("version", "v1");
info.AddValue("strategy", this.OptimizationStrategy, typeof(Strategy));
info.AddValue("length", this.Length);
info.AddValue("chunks", this.chunks, typeof(T[][]));
}
private ExaArray1D(SerializationInfo info, StreamingContext context)
{
switch (info.GetString("version"))
{
case "v1":
this.Length = info.GetUInt64("length");
this.chunks = info.GetValue("chunks", typeof(T[][])) as T[][];
this.OptimizationStrategy = (Strategy) info.GetValue("strategy", typeof(Strategy));
break;
}
this.chunks[0] ??= new T[0];
this.maxElements = this.OptimizationStrategy == Strategy.MAX_PERFORMANCE ? MAX_NUMBER_ELEMENTS_PERFORMANCE : MAX_NUMBER_ELEMENTS;
this.maxArrayCapacity = this.OptimizationStrategy == Strategy.MAX_PERFORMANCE ? MAX_CAPACITY_ARRAY_PERFORMANCE : MAX_CAPACITY_ARRAY;
}
#endregion
}
}

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Exa;
@ -12,6 +13,7 @@ namespace ExaArrayTests
[ExcludeFromCodeCoverage]
public class ExaArray1DTests
{
[Serializable]
private class TestClass
{
public int Age { get; set; }
@ -734,5 +736,62 @@ namespace ExaArrayTests
TestContext.WriteLine($"Performing 100M assignments took {t2Times.Average()} ms (average) by means of the max. performance strategy (min={t2Times.Min()} ms, max={t2Times.Max()} ms)");
}
[Test]
[Category("cover")]
[Category("normal")]
public void StoreAndLoad01()
{
var exaA = new ExaArray1D<byte>();
exaA.Extend(5_000_000);
exaA[4_483_124] = 0xff;
var filename = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
using (var file = File.OpenWrite(filename))
{
exaA.Store(file);
}
using (var file = File.OpenRead(filename))
{
var exaB = ExaArray1D<byte>.Restore(file);
Assert.That(exaA.Length, Is.EqualTo(exaB.Length));
Assert.That(exaA[4_483_124], Is.EqualTo(0xff));
Assert.That(exaB[4_483_124], Is.EqualTo(0xff));
}
File.Delete(filename);
}
[Test]
[Category("cover")]
[Category("normal")]
public void StoreAndLoad02()
{
var exaA = new ExaArray1D<TestClass>();
exaA.Extend(100);
exaA[66] = new TestClass
{
Age = 55,
};
var filename = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
using (var file = File.OpenWrite(filename))
{
exaA.Store(file);
}
using (var file = File.OpenRead(filename))
{
var exaB = ExaArray1D<TestClass>.Restore(file);
Assert.That(exaA.Length, Is.EqualTo(exaB.Length));
Assert.That(exaA[66].Age, Is.EqualTo(55));
Assert.That(exaB[66].Age, Is.EqualTo(55));
}
File.Delete(filename);
}
}
}