forked from mirrors/NBTExplorer
Added explicit caching support classes ahead of potential block collection refactoring.
This commit is contained in:
parent
fe0e71694e
commit
c8cdf32eb2
5 changed files with 324 additions and 14 deletions
|
@ -73,14 +73,6 @@ namespace Substrate.NBT
|
||||||
return Verify(_root, _schema);
|
return Verify(_root, _schema);
|
||||||
}
|
}
|
||||||
|
|
||||||
static NBTCompoundNode inventorySchema = new NBTCompoundNode("")
|
|
||||||
{
|
|
||||||
new NBTScalerNode("id", TagType.TAG_SHORT),
|
|
||||||
new NBTScalerNode("Damage", TagType.TAG_SHORT),
|
|
||||||
new NBTScalerNode("Count", TagType.TAG_BYTE),
|
|
||||||
new NBTScalerNode("Slot", TagType.TAG_BYTE),
|
|
||||||
};
|
|
||||||
|
|
||||||
private bool Verify (TagValue tag, NBTSchemaNode schema)
|
private bool Verify (TagValue tag, NBTSchemaNode schema)
|
||||||
{
|
{
|
||||||
if (tag == null) {
|
if (tag == null) {
|
||||||
|
|
137
Substrate/SubstrateCS/Source/Utility/IndexedLinkedList.cs
Normal file
137
Substrate/SubstrateCS/Source/Utility/IndexedLinkedList.cs
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Substrate.Utility
|
||||||
|
{
|
||||||
|
class IndexedLinkedList<T> : ICollection<T>, ICollection
|
||||||
|
{
|
||||||
|
private LinkedList<T> _list;
|
||||||
|
private Dictionary<T, LinkedListNode<T>> _index;
|
||||||
|
|
||||||
|
public T First
|
||||||
|
{
|
||||||
|
get { return _list.First.Value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Last
|
||||||
|
{
|
||||||
|
get { return _list.Last.Value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public IndexedLinkedList ()
|
||||||
|
{
|
||||||
|
_list = new LinkedList<T>();
|
||||||
|
_index = new Dictionary<T, LinkedListNode<T>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddFirst (T value)
|
||||||
|
{
|
||||||
|
LinkedListNode<T> node = _list.AddFirst(value);
|
||||||
|
_index.Add(value, node);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddLast (T value)
|
||||||
|
{
|
||||||
|
LinkedListNode<T> node = _list.AddLast(value);
|
||||||
|
_index.Add(value, node);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveFirst ()
|
||||||
|
{
|
||||||
|
_index.Remove(_list.First.Value);
|
||||||
|
_list.RemoveFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveLast ()
|
||||||
|
{
|
||||||
|
_index.Remove(_list.First.Value);
|
||||||
|
_list.RemoveLast();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region ICollection<T> Members
|
||||||
|
|
||||||
|
public void Add (T item)
|
||||||
|
{
|
||||||
|
AddLast(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear ()
|
||||||
|
{
|
||||||
|
_index.Clear();
|
||||||
|
_list.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Contains (T item)
|
||||||
|
{
|
||||||
|
return _index.ContainsKey(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CopyTo (T[] array, int arrayIndex)
|
||||||
|
{
|
||||||
|
_list.CopyTo(array, arrayIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsReadOnly
|
||||||
|
{
|
||||||
|
get { return false; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove (T value)
|
||||||
|
{
|
||||||
|
LinkedListNode<T> node;
|
||||||
|
if (_index.TryGetValue(value, out node))
|
||||||
|
{
|
||||||
|
_index.Remove(value);
|
||||||
|
_list.Remove(node);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region ICollection Members
|
||||||
|
|
||||||
|
void ICollection.CopyTo (Array array, int index)
|
||||||
|
{
|
||||||
|
(_list as ICollection).CopyTo(array, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Count
|
||||||
|
{
|
||||||
|
get { return _list.Count; }
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ICollection.IsSynchronized
|
||||||
|
{
|
||||||
|
get { return false; }
|
||||||
|
}
|
||||||
|
|
||||||
|
object ICollection.SyncRoot
|
||||||
|
{
|
||||||
|
get { return this; }
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region IEnumerable<T> Members
|
||||||
|
|
||||||
|
public IEnumerator<T> GetEnumerator ()
|
||||||
|
{
|
||||||
|
return _list.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region IEnumerable Members
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator ()
|
||||||
|
{
|
||||||
|
return _list.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
174
Substrate/SubstrateCS/Source/Utility/LRUCache.cs
Normal file
174
Substrate/SubstrateCS/Source/Utility/LRUCache.cs
Normal file
|
@ -0,0 +1,174 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Substrate.Utility
|
||||||
|
{
|
||||||
|
class LRUCache<TKey, TValue> : IDictionary<TKey, TValue>
|
||||||
|
{
|
||||||
|
private Dictionary<TKey, TValue> _data;
|
||||||
|
private IndexedLinkedList<TKey> _index;
|
||||||
|
|
||||||
|
private int _capacity;
|
||||||
|
|
||||||
|
public LRUCache (int capacity)
|
||||||
|
{
|
||||||
|
if (_capacity <= 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Cache capacity must be positive");
|
||||||
|
}
|
||||||
|
|
||||||
|
_capacity = capacity;
|
||||||
|
|
||||||
|
_data = new Dictionary<TKey, TValue>();
|
||||||
|
_index = new IndexedLinkedList<TKey>();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region IDictionary<TKey,TValue> Members
|
||||||
|
|
||||||
|
public void Add (TKey key, TValue value)
|
||||||
|
{
|
||||||
|
if (_data.ContainsKey(key))
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Attempted to insert a duplicate key");
|
||||||
|
}
|
||||||
|
|
||||||
|
_data[key] = value;
|
||||||
|
_index.Add(key);
|
||||||
|
|
||||||
|
if (_data.Count > _capacity)
|
||||||
|
{
|
||||||
|
_data.Remove(_index.First);
|
||||||
|
_index.RemoveFirst();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ContainsKey (TKey key)
|
||||||
|
{
|
||||||
|
return _data.ContainsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ICollection<TKey> Keys
|
||||||
|
{
|
||||||
|
get { return _data.Keys; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove (TKey key)
|
||||||
|
{
|
||||||
|
if (_data.Remove(key))
|
||||||
|
{
|
||||||
|
_index.Remove(key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetValue (TKey key, out TValue value)
|
||||||
|
{
|
||||||
|
if (!_data.TryGetValue(key, out value))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_index.Remove(key);
|
||||||
|
_index.Add(key);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ICollection<TValue> Values
|
||||||
|
{
|
||||||
|
get { return _data.Values; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public TValue this[TKey key]
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
TValue value = _data[key];
|
||||||
|
_index.Remove(key);
|
||||||
|
_index.Add(key);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_data[key] = value;
|
||||||
|
_index.Remove(key);
|
||||||
|
_index.Add(key);
|
||||||
|
|
||||||
|
if (_data.Count > _capacity)
|
||||||
|
{
|
||||||
|
_data.Remove(_index.First);
|
||||||
|
_index.RemoveFirst();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region ICollection<KeyValuePair<TKey,TValue>> Members
|
||||||
|
|
||||||
|
public void Add (KeyValuePair<TKey, TValue> item)
|
||||||
|
{
|
||||||
|
Add(item.Key, item.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear ()
|
||||||
|
{
|
||||||
|
_data.Clear();
|
||||||
|
_index.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Contains (KeyValuePair<TKey, TValue> item)
|
||||||
|
{
|
||||||
|
return ((ICollection<KeyValuePair<TKey, TValue>>)_data).Contains(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CopyTo (KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||||
|
{
|
||||||
|
((ICollection<KeyValuePair<TKey, TValue>>)_data).CopyTo(array, arrayIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Count
|
||||||
|
{
|
||||||
|
get { return _data.Count; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsReadOnly
|
||||||
|
{
|
||||||
|
get { return false; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove (KeyValuePair<TKey, TValue> item)
|
||||||
|
{
|
||||||
|
if (((ICollection<KeyValuePair<TKey, TValue>>)_data).Remove(item))
|
||||||
|
{
|
||||||
|
_index.Remove(item.Key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region IEnumerable<KeyValuePair<TKey,TValue>> Members
|
||||||
|
|
||||||
|
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator ()
|
||||||
|
{
|
||||||
|
return _data.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region IEnumerable Members
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator ()
|
||||||
|
{
|
||||||
|
return _data.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
@ -13,10 +13,13 @@
|
||||||
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
|
||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
<IsWebBootstrapper>false</IsWebBootstrapper>
|
<IsWebBootstrapper>false</IsWebBootstrapper>
|
||||||
<TargetFrameworkSubset>
|
|
||||||
</TargetFrameworkSubset>
|
|
||||||
<StartupObject>
|
<StartupObject>
|
||||||
</StartupObject>
|
</StartupObject>
|
||||||
|
<FileUpgradeFlags>
|
||||||
|
</FileUpgradeFlags>
|
||||||
|
<OldToolsVersion>3.5</OldToolsVersion>
|
||||||
|
<UpgradeBackupLocation />
|
||||||
|
<TargetFrameworkProfile />
|
||||||
<PublishUrl>publish\</PublishUrl>
|
<PublishUrl>publish\</PublishUrl>
|
||||||
<Install>true</Install>
|
<Install>true</Install>
|
||||||
<InstallFrom>Disk</InstallFrom>
|
<InstallFrom>Disk</InstallFrom>
|
||||||
|
@ -67,6 +70,8 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Source\ChunkCache.cs" />
|
<Compile Include="Source\ChunkCache.cs" />
|
||||||
<None Include="Source\Experimental\BlockInterface.cs" />
|
<None Include="Source\Experimental\BlockInterface.cs" />
|
||||||
|
<Compile Include="Source\Entities\EntitySquid.cs" />
|
||||||
|
<None Include="Source\Experimental\Interface2.cs" />
|
||||||
<Compile Include="Source\Level.cs" />
|
<Compile Include="Source\Level.cs" />
|
||||||
<Compile Include="Source\PlayerManager.cs" />
|
<Compile Include="Source\PlayerManager.cs" />
|
||||||
<Compile Include="Source\PlayerFile.cs" />
|
<Compile Include="Source\PlayerFile.cs" />
|
||||||
|
@ -134,14 +139,16 @@
|
||||||
<Compile Include="Source\TileEntity.cs" />
|
<Compile Include="Source\TileEntity.cs" />
|
||||||
<Compile Include="Source\TileEntityFactory.cs" />
|
<Compile Include="Source\TileEntityFactory.cs" />
|
||||||
<Compile Include="Source\Utility\Base.cs" />
|
<Compile Include="Source\Utility\Base.cs" />
|
||||||
|
<Compile Include="Source\Utility\IndexedLinkedList.cs" />
|
||||||
<Compile Include="Source\Utility\Interface.cs" />
|
<Compile Include="Source\Utility\Interface.cs" />
|
||||||
|
<Compile Include="Source\Utility\LRUCache.cs" />
|
||||||
<Compile Include="Source\Utility\NibbleArray.cs" />
|
<Compile Include="Source\Utility\NibbleArray.cs" />
|
||||||
<Compile Include="Source\World.cs" />
|
<Compile Include="Source\World.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
|
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
|
||||||
<Visible>False</Visible>
|
<Visible>False</Visible>
|
||||||
<ProductName>.NET Framework Client Profile</ProductName>
|
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
|
||||||
<Install>false</Install>
|
<Install>false</Install>
|
||||||
</BootstrapperPackage>
|
</BootstrapperPackage>
|
||||||
<BootstrapperPackage Include="Microsoft.Net.Framework.2.0">
|
<BootstrapperPackage Include="Microsoft.Net.Framework.2.0">
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 10.00
|
Microsoft Visual Studio Solution File, Format Version 11.00
|
||||||
# Visual C# Express 2008
|
# Visual C# Express 2010
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Substrate", "Substrate.csproj", "{AFE30E14-3F2F-4461-9F7D-147AB4DCA4C3}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Substrate", "Substrate.csproj", "{AFE30E14-3F2F-4461-9F7D-147AB4DCA4C3}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NBToolkit", "..\..\NBToolkit\NBToolkit.csproj", "{68207314-C080-4823-97F1-A6623145AA00}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NBToolkit", "..\..\NBToolkit\NBToolkit.csproj", "{68207314-C080-4823-97F1-A6623145AA00}"
|
||||||
|
|
Loading…
Reference in a new issue