From 0f1c6970c9f0b3e05ba7ca8be1b838435ba7c1d5 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 8 May 2011 08:17:20 +0000 Subject: [PATCH] More preparation towards refactoring --- Substrate/SubstrateCS/Source/ChunkRef.cs | 4 +- .../SubstrateCS/Source/Utility/ByteArray.cs | 168 ++++++++++++++++++ .../Source/Utility/IndexedLinkedList.cs | 4 +- .../SubstrateCS/Source/Utility/LRUCache.cs | 42 ++++- .../SubstrateCS/Source/Utility/NibbleArray.cs | 87 +++++++-- Substrate/SubstrateCS/Substrate.csproj | 2 + 6 files changed, 292 insertions(+), 15 deletions(-) create mode 100644 Substrate/SubstrateCS/Source/Utility/ByteArray.cs diff --git a/Substrate/SubstrateCS/Source/ChunkRef.cs b/Substrate/SubstrateCS/Source/ChunkRef.cs index a82c06b..f37a8f9 100644 --- a/Substrate/SubstrateCS/Source/ChunkRef.cs +++ b/Substrate/SubstrateCS/Source/ChunkRef.cs @@ -653,12 +653,12 @@ namespace Substrate cc.SetBlockSkyLight(x, y, z, dimStr); if (info.TransmitsLight) { - spread.Enqueue(new LightRecord(rec.x - 1, rec.y, rec.z, dimStr - 1)); + /*spread.Enqueue(new LightRecord(rec.x - 1, rec.y, rec.z, dimStr - 1)); spread.Enqueue(new LightRecord(rec.x + 1, rec.y, rec.z, dimStr - 1)); spread.Enqueue(new LightRecord(rec.x, rec.y - 1, rec.z, dimStr - 1)); spread.Enqueue(new LightRecord(rec.x, rec.y + 1, rec.z, dimStr - 1)); spread.Enqueue(new LightRecord(rec.x, rec.y, rec.z - 1, dimStr - 1)); - spread.Enqueue(new LightRecord(rec.x, rec.y, rec.z + 1, dimStr - 1)); + spread.Enqueue(new LightRecord(rec.x, rec.y, rec.z + 1, dimStr - 1));*/ if (heightMap[xi, zi] > rec.y - 1) { spread.Enqueue(new LightRecord(rec.x, rec.y - 1, rec.z, dimStr - 1)); diff --git a/Substrate/SubstrateCS/Source/Utility/ByteArray.cs b/Substrate/SubstrateCS/Source/Utility/ByteArray.cs new file mode 100644 index 0000000..3332ed0 --- /dev/null +++ b/Substrate/SubstrateCS/Source/Utility/ByteArray.cs @@ -0,0 +1,168 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Substrate.Utility +{ + public class ByteArray : ICopyable + { + protected readonly byte[] _data; + + public ByteArray (byte[] data) + { + _data = data; + } + + public byte this[int i] + { + get { return _data[i]; } + set { _data[i] = value; } + } + + public int Length + { + get { return _data.Length; } + } + + public void Clear () + { + for (int i = 0; i < _data.Length; i++) + { + _data[i] = 0; + } + } + + #region ICopyable Members + + public virtual ByteArray Copy () + { + byte[] data = new byte[_data.Length]; + _data.CopyTo(data, 0); + + return new ByteArray(data); + } + + #endregion + } + + public sealed class XZYByteArray : ByteArray + { + private readonly int _xdim; + private readonly int _ydim; + private readonly int _zdim; + + private readonly byte[] _data; + + public XZYByteArray (int xdim, int ydim, int zdim, byte[] data) + : base(data) + { + _xdim = xdim; + _ydim = ydim; + _zdim = zdim; + + if (xdim * ydim * zdim != data.Length) + { + throw new ArgumentException("Product of dimensions must equal length of data"); + } + } + + public byte this[int x, int y, int z] + { + get + { + int index = _ydim * (x * _zdim + z) + y; + return _data[index]; + } + + set + { + int index = _ydim * (x * _zdim + z) + y; + _data[index] = value; + } + } + + public int XDim + { + get { return _xdim; } + } + + public int YDim + { + get { return _ydim; } + } + + public int ZDim + { + get { return _zdim; } + } + + #region ICopyable Members + + public override ByteArray Copy () + { + byte[] data = new byte[_data.Length]; + _data.CopyTo(data, 0); + + return new XZYByteArray(_xdim, _ydim, _zdim, data); + } + + #endregion + } + + public sealed class ZXByteArray : ByteArray + { + private readonly int _xdim; + private readonly int _zdim; + + private readonly byte[] _data; + + public ZXByteArray (int xdim, int zdim, byte[] data) + : base(data) + { + _xdim = xdim; + _zdim = zdim; + + if (xdim * zdim != data.Length) + { + throw new ArgumentException("Product of dimensions must equal length of data"); + } + } + + public byte this[int x, int z] + { + get + { + int index = z * _xdim + x; + return _data[index]; + } + + set + { + int index = z * _xdim + x; + _data[index] = value; + } + } + + public int XDim + { + get { return _xdim; } + } + + public int ZDim + { + get { return _zdim; } + } + + #region ICopyable Members + + public override ByteArray Copy () + { + byte[] data = new byte[_data.Length]; + _data.CopyTo(data, 0); + + return new ZXByteArray(_xdim, _zdim, data); + } + + #endregion + } +} diff --git a/Substrate/SubstrateCS/Source/Utility/IndexedLinkedList.cs b/Substrate/SubstrateCS/Source/Utility/IndexedLinkedList.cs index 2aee393..dca1d31 100644 --- a/Substrate/SubstrateCS/Source/Utility/IndexedLinkedList.cs +++ b/Substrate/SubstrateCS/Source/Utility/IndexedLinkedList.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; namespace Substrate.Utility { - class IndexedLinkedList : ICollection, ICollection + public class IndexedLinkedList : ICollection, ICollection { private LinkedList _list; private Dictionary> _index; @@ -111,7 +111,7 @@ namespace Substrate.Utility object ICollection.SyncRoot { - get { return this; } + get { return (_list as ICollection).SyncRoot; } } #endregion diff --git a/Substrate/SubstrateCS/Source/Utility/LRUCache.cs b/Substrate/SubstrateCS/Source/Utility/LRUCache.cs index bb0e220..c1889e4 100644 --- a/Substrate/SubstrateCS/Source/Utility/LRUCache.cs +++ b/Substrate/SubstrateCS/Source/Utility/LRUCache.cs @@ -4,8 +4,35 @@ using System.Collections.Generic; namespace Substrate.Utility { - class LRUCache : IDictionary + + public class LRUCache : IDictionary { + public delegate void RemoveCacheValueHandler (Object o, CacheValueArgs e); + + public class CacheValueArgs : EventArgs + { + private TKey _key; + private TValue _value; + + public TKey Key + { + get { return _key; } + } + + public TValue Value + { + get { return _value; } + } + + public CacheValueArgs (TKey key, TValue value) + { + _key = key; + _value = value; + } + } + + public event RemoveCacheValueHandler RemoveCacheValue; + private Dictionary _data; private IndexedLinkedList _index; @@ -38,6 +65,8 @@ namespace Substrate.Utility if (_data.Count > _capacity) { + OnRemoveCacheValue(new CacheValueArgs(_index.First, _data[_index.First])); + _data.Remove(_index.First); _index.RemoveFirst(); } @@ -99,6 +128,8 @@ namespace Substrate.Utility if (_data.Count > _capacity) { + OnRemoveCacheValue(new CacheValueArgs(_index.First, _data[_index.First])); + _data.Remove(_index.First); _index.RemoveFirst(); } @@ -170,5 +201,14 @@ namespace Substrate.Utility } #endregion + + private void OnRemoveCacheValue (CacheValueArgs e) + { + if (RemoveCacheValue != null) + { + RemoveCacheValue(this, e); + } + } } + } diff --git a/Substrate/SubstrateCS/Source/Utility/NibbleArray.cs b/Substrate/SubstrateCS/Source/Utility/NibbleArray.cs index b07e8fc..49b2442 100644 --- a/Substrate/SubstrateCS/Source/Utility/NibbleArray.cs +++ b/Substrate/SubstrateCS/Source/Utility/NibbleArray.cs @@ -1,10 +1,10 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Text; namespace Substrate.Utility { + public class NibbleArray : ICopyable { protected readonly byte[] _data = null; @@ -14,26 +14,30 @@ namespace Substrate.Utility _data = data; } - public int this[int index] + public byte this[int index] { get { int subs = index >> 1; - if ((index & 1) == 0) { - return _data[subs] & 0x0F; + if ((index & 1) == 0) + { + return (byte)(_data[subs] & 0x0F); } - else { - return (_data[subs] >> 4) & 0x0F; + else + { + return (byte)((_data[subs] >> 4) & 0x0F); } } set { int subs = index >> 1; - if ((index & 1) == 0) { + if ((index & 1) == 0) + { _data[subs] = (byte)((_data[subs] & 0xF0) | (value & 0x0F)); } - else { + else + { _data[subs] = (byte)((_data[subs] & 0x0F) | ((value & 0x0F) << 4)); } } @@ -49,14 +53,15 @@ namespace Substrate.Utility public void Clear () { - for (int i = 0; i < _data.Length; i++) { + for (int i = 0; i < _data.Length; i++) + { _data[i] = 0; } } #region ICopyable Members - public NibbleArray Copy () + public virtual NibbleArray Copy () { byte[] data = new byte[_data.Length]; _data.CopyTo(data, 0); @@ -66,4 +71,66 @@ namespace Substrate.Utility #endregion } + + public sealed class XZYNibbleArray : NibbleArray + { + private readonly int _xdim; + private readonly int _ydim; + private readonly int _zdim; + + public XZYNibbleArray (int xdim, int ydim, int zdim, byte[] data) + : base(data) + { + _xdim = xdim; + _ydim = ydim; + _zdim = zdim; + + if (xdim * ydim * zdim != data.Length / 2) + { + 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 Members + + public override NibbleArray Copy () + { + byte[] data = new byte[_data.Length]; + _data.CopyTo(data, 0); + + return new XZYNibbleArray(_xdim, _ydim, _zdim, data); + } + + #endregion + } } diff --git a/Substrate/SubstrateCS/Substrate.csproj b/Substrate/SubstrateCS/Substrate.csproj index 34e4ed5..6f0ff2b 100644 --- a/Substrate/SubstrateCS/Substrate.csproj +++ b/Substrate/SubstrateCS/Substrate.csproj @@ -72,6 +72,7 @@ + @@ -139,6 +140,7 @@ +