2011-04-06 04:43:54 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2011-06-29 02:58:34 +00:00
|
|
|
|
namespace Substrate.Core
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-05-08 08:17:20 +00:00
|
|
|
|
|
2011-04-06 04:43:54 +00:00
|
|
|
|
public class NibbleArray : ICopyable<NibbleArray>
|
|
|
|
|
{
|
2011-07-24 18:47:52 +00:00
|
|
|
|
private readonly byte[] _data = null;
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
2011-07-04 19:47:19 +00:00
|
|
|
|
public NibbleArray (int length)
|
|
|
|
|
{
|
2011-07-24 06:08:31 +00:00
|
|
|
|
_data = new byte[(int)Math.Ceiling(length / 2.0)];
|
2011-07-04 19:47:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-06 04:43:54 +00:00
|
|
|
|
public NibbleArray (byte[] data)
|
|
|
|
|
{
|
|
|
|
|
_data = data;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-08 08:17:20 +00:00
|
|
|
|
public byte this[int index]
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
int subs = index >> 1;
|
2011-05-08 08:17:20 +00:00
|
|
|
|
if ((index & 1) == 0)
|
|
|
|
|
{
|
|
|
|
|
return (byte)(_data[subs] & 0x0F);
|
2011-04-06 04:43:54 +00:00
|
|
|
|
}
|
2011-05-08 08:17:20 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (byte)((_data[subs] >> 4) & 0x0F);
|
2011-04-06 04:43:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
int subs = index >> 1;
|
2011-05-08 08:17:20 +00:00
|
|
|
|
if ((index & 1) == 0)
|
|
|
|
|
{
|
2011-04-06 04:43:54 +00:00
|
|
|
|
_data[subs] = (byte)((_data[subs] & 0xF0) | (value & 0x0F));
|
|
|
|
|
}
|
2011-05-08 08:17:20 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2011-04-06 04:43:54 +00:00
|
|
|
|
_data[subs] = (byte)((_data[subs] & 0x0F) | ((value & 0x0F) << 4));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Length
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _data.Length << 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-24 18:47:52 +00:00
|
|
|
|
protected byte[] Data
|
|
|
|
|
{
|
|
|
|
|
get { return _data; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-16 01:46:46 +00:00
|
|
|
|
public void Clear ()
|
|
|
|
|
{
|
2011-05-08 08:17:20 +00:00
|
|
|
|
for (int i = 0; i < _data.Length; i++)
|
|
|
|
|
{
|
2011-04-16 01:46:46 +00:00
|
|
|
|
_data[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-06 04:43:54 +00:00
|
|
|
|
#region ICopyable<NibbleArray> Members
|
|
|
|
|
|
2011-05-08 08:17:20 +00:00
|
|
|
|
public virtual NibbleArray Copy ()
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
byte[] data = new byte[_data.Length];
|
|
|
|
|
_data.CopyTo(data, 0);
|
|
|
|
|
|
|
|
|
|
return new NibbleArray(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
2011-05-08 08:17:20 +00:00
|
|
|
|
|
|
|
|
|
public sealed class XZYNibbleArray : NibbleArray
|
|
|
|
|
{
|
|
|
|
|
private readonly int _xdim;
|
|
|
|
|
private readonly int _ydim;
|
|
|
|
|
private readonly int _zdim;
|
|
|
|
|
|
2011-07-04 19:47:19 +00:00
|
|
|
|
public XZYNibbleArray (int xdim, int ydim, int zdim)
|
|
|
|
|
: base(xdim * ydim * zdim)
|
|
|
|
|
{
|
|
|
|
|
_xdim = xdim;
|
|
|
|
|
_ydim = ydim;
|
|
|
|
|
_zdim = zdim;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-08 08:17:20 +00:00
|
|
|
|
public XZYNibbleArray (int xdim, int ydim, int zdim, byte[] data)
|
|
|
|
|
: base(data)
|
|
|
|
|
{
|
|
|
|
|
_xdim = xdim;
|
|
|
|
|
_ydim = ydim;
|
|
|
|
|
_zdim = zdim;
|
|
|
|
|
|
2011-05-13 03:09:57 +00:00
|
|
|
|
if (xdim * ydim * zdim != data.Length * 2)
|
2011-05-08 08:17:20 +00:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Product of dimensions must equal half length of raw data");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte this[int x, int y, int z]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
int index = _ydim * (x * _zdim + z) + y;
|
|
|
|
|
return this[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
int index = _ydim * (x * _zdim + z) + y;
|
|
|
|
|
this[index] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int XDim
|
|
|
|
|
{
|
|
|
|
|
get { return _xdim; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int YDim
|
|
|
|
|
{
|
|
|
|
|
get { return _ydim; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int ZDim
|
|
|
|
|
{
|
|
|
|
|
get { return _zdim; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region ICopyable<NibbleArray> Members
|
|
|
|
|
|
|
|
|
|
public override NibbleArray Copy ()
|
|
|
|
|
{
|
2011-07-24 18:47:52 +00:00
|
|
|
|
byte[] data = new byte[Data.Length];
|
|
|
|
|
Data.CopyTo(data, 0);
|
2011-05-08 08:17:20 +00:00
|
|
|
|
|
|
|
|
|
return new XZYNibbleArray(_xdim, _ydim, _zdim, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
2011-04-06 04:43:54 +00:00
|
|
|
|
}
|