diff --git a/ExaArray/ExaArray2D.cs b/ExaArray/ExaArray2D.cs index 53fcbad..6c61b90 100644 --- a/ExaArray/ExaArray2D.cs +++ b/ExaArray/ExaArray2D.cs @@ -1,11 +1,14 @@ using System; +using System.IO; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; namespace Exa { /// /// The two-dimensional exa-scale array. Can grow up to 18,446,744,073,709,551,615 elements in total. /// - public sealed class ExaArray2D + public sealed class ExaArray2D : ISerializable { /// /// The total number of possible elements. @@ -16,6 +19,13 @@ namespace Exa // Chunk storage: private readonly ExaArray1D> chunks = new ExaArray1D>(Strategy.MAX_PERFORMANCE); + + /// + /// Constructs a two-dimensional exa-scale array. + /// + public ExaArray2D() + { + } /// /// Returns the current total number of elements across all dimensions. @@ -72,5 +82,61 @@ namespace Exa this.chunks[indexAbscissa][indexOrdinate] = value; } } + + #region Store and load + + /// + /// Stores the exa array into a stream. + /// + /// + /// This method does not dispose the stream. + /// + public void Store(Stream outputStream) + { + var formatter = new BinaryFormatter(); + formatter.Serialize(outputStream, this); + } + + /// + /// Restores an exa array from the given stream. + /// + /// + /// This method does not dispose the stream. + /// + public static ExaArray2D Restore(Stream inputStream) + { + var formatter = new BinaryFormatter(); + return formatter.Deserialize(inputStream) as ExaArray2D; + } + + #endregion + + #region Serialization + + /// + /// This method serves for the serialization process. Do not call it manually. + /// + public void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue("version", "v1"); + info.AddValue("length", this.Length); + info.AddValue("chunks", this.chunks, typeof(ExaArray1D>)); + } + + private ExaArray2D(SerializationInfo info, StreamingContext context) + { + switch (info.GetString("version")) + { + case "v1": + this.sumLengthOrdinates = info.GetUInt64("length"); + this.chunks = info.GetValue("chunks", typeof(ExaArray1D>)) as ExaArray1D>; + break; + + default: + break; + } + } + + #endregion } } \ No newline at end of file