/*
* DoubleLargeArray.cs
*
* Copyright © 2009
* Diversified Technical Systems, Inc.
* All Rights Reserved
*/
using System;
using System.Diagnostics;
// ReSharper disable once CheckNamespace
namespace DTS.Common.Utilities.IO.MemoryMap
{
///
/// A of doubles
///
public class DoubleLargeArray : LargeArray
{
///
/// Initialize an instance of a .
///
///
///
/// The size of the collection.
///
///
public DoubleLargeArray(ulong size)
: this(size, null, null)
{
}
///
/// Initialize an instance of a .
///
///
///
/// The size of the collection.
///
///
///
/// The name of the directory where this LargeArray will store the temporary
/// serializations of its items. If null, then the default location will be used.
///
///
///
/// The file name prefix under which this LargeArray will store temporary
/// serializations of its items. Initialized to DefaultFilePrefix's value at object
/// creation, unless an alternate is specified by the user. If null, then the default prefix
/// will be used.
///
///
public DoubleLargeArray(ulong size, string scratchFileDirectory, string scratchFilePrefix)
: base(size, scratchFileDirectory, scratchFilePrefix)
{
}
///
/// Get the size of the datum handled by this class.
///
public override uint DatumSize => sizeof(double);
///
///
/// Get the "zero" value for this class' datum type.
///
protected override double DatumClearValue => 0.0;
///
///
/// Get the datum value at the specified index.
///
///
/// The index of the datum to be returned.
///
///
/// The value of the datum at the specified index.
///
protected override double GetDatumAtIndex(ulong index)
{
try
{
if (index > long.MaxValue)
throw new ApplicationException("overflowed an Int64 variable with a UInt64 value");
var bytes = new byte[DatumSize];
for (var i = 0; i < DatumSize; i++)
bytes[i] = ViewArray[(long)(DatumSize * index) + i];
return BitConverter.ToDouble(bytes, 0);
}
catch (System.Exception ex)
{
throw new Exception("encountered problem getting datum at index " + index, ex);
}
}
///
///
/// Set the datum value at the specified index.
///
///
/// The datum to be written to the specified index.
///
///
/// The index at which the specified datum value will be written.
///
protected override void SetDatumAtIndex(double datum, ulong index)
{
try
{
if (index > long.MaxValue)
throw new ApplicationException("overflowed an Int64 variable with a UInt64 value");
var bytes = BitConverter.GetBytes(datum);
Debug.Assert(bytes.Length == DatumSize);
for (var i = 0; i < DatumSize; i++)
ViewArray[(long)(DatumSize * index) + i] = bytes[i];
}
catch (System.Exception ex)
{
throw new Exception("encountered problem setting datum (" + datum + ") at index " + index, ex);
}
}
///
///
/// Creates a new object that is a copy of the current instance.
///
///
/// A new that is a copy of the current instance.
///
public override object Clone()
{
try
{
var clone = new DoubleLargeArray(Size);
for (var i = 0UL; i < clone.LargeCount; i++)
clone[i] = this[i];
return clone;
}
catch (System.Exception ex)
{
throw new Exception("encountered problem cloning " + GetType().FullName, ex);
}
}
///
/// Get/set the value at the specified index.
///
///
///
/// The index of the datum to be referenced.
///
///
///
/// The value of the datum at the specified index.
///
///
public new double this[ulong index]
{
get => (short)base[index];
set => base[index] = value;
}
}
}