using System;\r
using System.Collections.Generic;\r
using System.Text;\r
+#if !MMF_DIC\r
using System.IO;\r
-#if MMF_DIC\r
-using System.IO.MemoryMappedFiles;\r
#endif\r
\r
namespace NMeCab.Core\r
/// <summary>\r
/// Double-Array Trie の実装\r
/// </summary>\r
- public class DoubleArray : IDisposable\r
+ public class DoubleArray\r
{\r
#region Array\r
\r
private struct Unit\r
{\r
- public readonly int Base;\r
- public readonly uint Check;\r
-\r
- public Unit(int b, uint c)\r
- {\r
- this.Base = b;\r
- this.Check = c;\r
- }\r
+#pragma warning disable 0649\r
+ public int Base;\r
+ public uint Check;\r
+#pragma warning restore 0649\r
}\r
\r
public const int UnitSize = sizeof(int) + sizeof(uint);\r
\r
#if MMF_DIC\r
\r
- private MemoryMappedViewAccessor accessor;\r
+ private unsafe Unit* array;\r
\r
public int Size\r
{\r
- get { return (int)(this.accessor.Capacity) / UnitSize; }\r
+ get { return this.TotalSize / UnitSize; }\r
}\r
\r
- public int TotalSize\r
- {\r
- get { return (int)(this.accessor.Capacity); }\r
- }\r
+ public int TotalSize { get; private set; }\r
\r
#else\r
\r
\r
#if MMF_DIC\r
\r
- public void Open(MemoryMappedFile mmf, long offset, long size)\r
+ public unsafe void Open(byte* ptr, int size)\r
{\r
- this.accessor = mmf.CreateViewAccessor(offset, size, MemoryMappedFileAccess.Read);\r
+ this.array = (Unit*)ptr;\r
+ this.TotalSize = size;\r
}\r
\r
#else\r
\r
- public void Open(BinaryReader reader, uint size)\r
+ public void Open(BinaryReader reader, int size)\r
{\r
this.array = new Unit[size / UnitSize];\r
\r
for (int i = 0; i < array.Length; i++)\r
{\r
- this.array[i] = new Unit(reader.ReadInt32(), reader.ReadUInt32());\r
+ this.array[i] = new Unit()\r
+ {\r
+ Base = reader.ReadInt32(),\r
+ Check = reader.ReadUInt32()\r
+ };\r
}\r
}\r
\r
public struct ResultPair\r
{\r
public int Value;\r
-\r
public int Length;\r
-\r
- public ResultPair(int r, int t)\r
- {\r
- this.Value = r;\r
- this.Length = t;\r
- }\r
- }\r
-\r
- public unsafe void ExactMatchSearch(byte* key, ResultPair* result, int len, int nodePos)\r
- {\r
- *result = this.ExactMatchSearch(key, len, nodePos);\r
}\r
\r
public unsafe ResultPair ExactMatchSearch(byte* key, int len, int nodePos)\r
{\r
- int b = this.ReadBase(nodePos);\r
- Unit p;\r
+ int b = this.array[nodePos].Base;\r
+ int p;\r
\r
for (int i = 0; i < len; i++)\r
{\r
- this.ReadUnit(b + key[i] + 1, out p);\r
- if (b == p.Check)\r
+ p = b + key[i] + 1;\r
+ if (b == this.array[p].Check)\r
{\r
- b = p.Base;\r
+ b = this.array[p].Base;\r
}\r
else\r
{\r
- return new ResultPair(-1, 0);\r
+ return new ResultPair() { Value = -1, Length = 0 };\r
}\r
}\r
\r
- this.ReadUnit(b, out p);\r
- int n = p.Base;\r
- if (b == p.Check && n < 0)\r
+ p = b;\r
+ int n = this.array[b].Base;\r
+ if (b == this.array[p].Check && n < 0)\r
{\r
- return new ResultPair(-n - 1, len);\r
+ return new ResultPair() { Value = -n - 1, Length = 0 };\r
}\r
\r
- return new ResultPair(-1, 0);\r
+ return new ResultPair() { Value = -1, Length = 0 };\r
}\r
\r
public unsafe int CommonPrefixSearch(byte* key, ResultPair* result, int resultLen, int len, int nodePos = 0)\r
{\r
- int b = this.ReadBase(nodePos);\r
+ int b = this.array[nodePos].Base;\r
int num = 0;\r
int n;\r
- Unit p;\r
+ int p;\r
\r
for (int i = 0; i < len; i++)\r
{\r
- this.ReadUnit(b, out p);\r
- n = p.Base;\r
+ p = b;\r
+ n = this.array[p].Base;\r
\r
- if (b == p.Check && n < 0)\r
+ if (b == this.array[p].Check && n < 0)\r
{\r
- if (num < resultLen) result[num] = new ResultPair(-n - 1, i);\r
+ if (num < resultLen)\r
+ result[num] = new ResultPair() { Value = -n - 1, Length = i };\r
num++;\r
}\r
\r
- this.ReadUnit(b + key[i] + 1, out p);\r
- if (b == p.Check)\r
+ p = b + key[i] + 1;\r
+ if (b == this.array[p].Check)\r
{\r
- b = p.Base;\r
+ b = this.array[p].Base;\r
}\r
else\r
{\r
}\r
}\r
\r
- this.ReadUnit(b, out p);\r
- n = p.Base;\r
+ p = b;\r
+ n = this.array[p].Base;\r
\r
- if (b == p.Check && n < 0)\r
+ if (b == this.array[p].Check && n < 0)\r
{\r
- if (num < resultLen) result[num] = new ResultPair(-n - 1, len);\r
+ if (num < resultLen)\r
+ result[num] = new ResultPair() { Value = -n - 1, Length = len };\r
num++;\r
}\r
\r
return num;\r
}\r
\r
-\r
-\r
- private int ReadBase(int pos)\r
- {\r
-#if MMF_DIC\r
- return this.accessor.ReadInt32(pos * UnitSize);\r
-#else\r
- return this.array[pos].Base;\r
-#endif\r
- }\r
-\r
- private void ReadUnit(int pos, out Unit unit)\r
- {\r
-#if MMF_DIC\r
- this.accessor.Read<Unit>(pos * UnitSize, out unit);\r
-#else\r
- unit = this.array[pos];\r
-#endif\r
- }\r
-\r
- #endregion\r
-\r
- #region Dispose\r
-\r
- private bool disposed;\r
-\r
- public void Dispose()\r
- {\r
- this.Dispose(true);\r
- GC.SuppressFinalize(this);\r
- }\r
-\r
- protected virtual void Dispose(bool disposing)\r
- {\r
- if (disposed) return;\r
-\r
- if (disposing)\r
- {\r
-#if MMF_DIC\r
- if (this.accessor != null) this.accessor.Dispose();\r
-#endif\r
- }\r
-\r
- this.disposed = true;\r
- }\r
-\r
- ~DoubleArray()\r
- {\r
- this.Dispose(false);\r
- }\r
-\r
#endregion\r
}\r
}\r