Files
DP44/Common/DTS.Common.Utilities/ShortLargeArray.cs
2026-04-17 14:55:32 -04:00

178 lines
6.0 KiB
C#

/*
* ShortLargeArray.cs
*
* Copyright © 2009
* Diversified Technical Systems, Inc.
* All Rights Reserved
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Text;
using DTS.Common.Utilities;
using DTS.Common.Utilities.DotNetProgrammingConstructs;
namespace DTS.Common.Utilities.IO.MemoryMap
{
/// <summary>
/// a <see cref="T:IO.MemoryMap.LargeArray" /> of
/// <see cref="short" />
/// </summary>
public class ShortLargeArray : LargeArray<short>
{
/// <summary>
/// Initialize an instance of a <see cref="IO.MemoryMap.ShortLargeArray"/>.
/// </summary>
///
/// <param name="size">
/// The <see cref="ulong"/> size of the collection.
/// </param>
public ShortLargeArray(ulong size)
: this(size, null, null)
{
}
/// <summary>
/// Initialize an instance of a <see cref="IO.MemoryMap.ShortLargeArray"/>.
/// </summary>
///
/// <param name="size">
/// The <see cref="ulong"/> size of the collection.
/// </param>
///
/// <param name="scratchFileDirectory">
/// The <see cref="string"/> name of the directory where this LargeArray will store the temporary
/// serializations of its items. If null, then the default location will be used.
/// </param>
///
/// <param name="scratchFilePrefix">
/// The <see cref="string"/> 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.
/// </param>
///
public ShortLargeArray(ulong size, string scratchFileDirectory, string scratchFilePrefix)
: base(size, scratchFileDirectory, scratchFilePrefix)
{
}
/// <summary>
/// Get the <see cref="uint"/> size of the datum handled by this class.
/// </summary>
public override uint DatumSize => sizeof(short);
/// <summary>
/// Get the "zero" value for this class' datum type.
/// </summary>
protected override short DatumClearValue => 0;
/// <summary>
/// Get the datum value at the specified index.
/// </summary>
///
/// <param name="index">
/// The <see cref="ulong"/> index of the datum to be returned.
/// </param>
///
/// <returns>
/// The value of the <see cref="short"/> datum at the specified index.
/// </returns>
///
protected override short GetDatumAtIndex(ulong index)
{
try
{
short rv;
if (index > long.MaxValue)
throw new ApplicationException("overflowed an Int64 variable with a UInt64 value");
rv = (short)(ViewArray[(long)((2 * index))] & 0xFFFF);
rv += (short)((ViewArray[(long)((2 * index) + 1)] & 0xFFFF) << 8);
return rv;
}
catch (System.Exception ex)
{
throw new ShortLargeArray.Exception("encountered problem getting datum at index " + index.ToString(), ex);
}
}
/// <summary>
/// Set the datum value at the specified index.
/// </summary>
///
/// <param name="datum">
/// The <see cref="short"/> datum to be written to the specified index.
/// </param>
///
/// <param name="index">
/// The <see cref="ulong"/> index at which the specified datum value will be written.
/// </param>
///
protected override void SetDatumAtIndex(short datum, ulong index)
{
try
{
if (index > long.MaxValue)
throw new ApplicationException("overflowed an Int64 variable with a UInt64 value");
(ViewArray[(long)((2 * index))]) = (byte)((datum) & 0xFFFF);
(ViewArray[(long)((2 * index) + 1)]) = (byte)((datum >> 8) & 0xFFFF);
}
catch (System.Exception ex)
{
throw new ShortLargeArray.Exception("encountered problem setting datum (" + datum.ToString() + ") at index " + index.ToString(), ex);
}
}
/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
///
/// <returns>
/// A new <see cref="System.Object"/> that is a copy of the current instance.
/// </returns>
///
public override object Clone()
{
try
{
var clone = new ShortLargeArray(Size);
for (ulong i = 0; i < clone.LargeCount; i++)
clone[i] = this[i];
return clone;
}
catch (System.Exception ex)
{
throw new Exception("encountered problem cloning " + GetType().FullName, ex);
}
}
/// <summary>
/// Get/set the value at the specified index.
/// </summary>
///
/// <param name="index">
/// The <see cref="ulong"/> index of the datum to be referenced.
/// </param>
///
/// <returns>
/// The <see cref="short"/> value of the datum at the specified index.
/// </returns>
///
public new short this[ulong index]
{
get => base[index];
set => base[index] = value;
}
}
}