You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Semantic/Trie.cs

242 lines
7.1 KiB

5 years ago
using System.Collections.Generic;
5 years ago
using System.Linq;
5 years ago
using System.Threading;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Services.Semantic
{
public class Trie
: IBinarySerializable
{
5 years ago
internal class TrieNode
: IBinarySerializable
{
5 years ago
public char? Key; // setted only with rebuild index
public uint? Value;
5 years ago
public TrieNode Parent;
5 years ago
public Dictionary<char, TrieNode> Children;
5 years ago
public TrieNode() { }
public TrieNode(TrieNode parent) { Parent = parent; }
public void Deserialize(IBinaryReader reader)
{
if (reader.ReadBoolean())
{
this.Value = reader.ReadUInt32();
}
else
{
this.Value = null;
}
5 years ago
this.Children = reader.ReadDictionary<char, TrieNode>();
}
public void Serialize(IBinaryWriter writer)
{
if (this.Value.HasValue)
{
writer.WriteBoolean(true);
writer.WriteUInt32(this.Value.Value);
}
else
{
writer.WriteBoolean(false);
}
if (this.Children == null)
{
writer.WriteInt32(0);
}
else
{
writer.WriteDictionary<char, TrieNode>(this.Children);
}
}
5 years ago
internal TrieNode Append(string word, int index)
{
5 years ago
if (word.Length == index)
{
if (!this.Value.HasValue)
{
5 years ago
return this;
}
}
else
{
if (this.Children == null)
{
5 years ago
this.Children = new Dictionary<char, TrieNode>();
}
if (!this.Children.ContainsKey(word[index]))
{
5 years ago
this.Children.Add(word[index], new TrieNode(this));
}
5 years ago
return Children[word[index]].Append(word, index + 1);
}
5 years ago
return null;
}
internal uint? GetKey(string word, int index)
{
if (word != null && (this.Children?.ContainsKey(word[index]) ?? false))
{
if (word.Length == index + 1) return this.Children[word[index]].Value;
return this.Children[word[index]].GetKey(word, index + 1);
}
return null;
}
5 years ago
5 years ago
internal void RebuildReverseIndex(TrieNode parent, char key, Dictionary<uint, TrieNode> index)
5 years ago
{
5 years ago
this.Key = key;
5 years ago
this.Parent = parent;
if (this.Value.HasValue)
{
index.Add(this.Value.Value, this);
}
if (this.Children != null)
{
foreach (var child in this.Children)
{
5 years ago
child.Value.RebuildReverseIndex(this, child.Key, index);
5 years ago
}
}
}
internal void DestroyReverseIndex()
{
this.Parent = null!;
5 years ago
this.Key = null;
5 years ago
if (this.Children != null)
{
foreach (var child in this.Children)
{
child.Value.DestroyReverseIndex();
5 years ago
}
}
}
}
internal TrieNode _root;
5 years ago
private int _word_index = 0;
private bool _use_reverse_index;
5 years ago
private Dictionary<uint, TrieNode> _reverse_index;
5 years ago
public IEnumerable<uint> Keys => _reverse_index.Keys;
public Trie() : this(false)
{
}
5 years ago
public Trie(bool reverse_index = false)
{
5 years ago
_use_reverse_index = reverse_index;
if (_use_reverse_index)
{
_reverse_index = new Dictionary<uint, TrieNode>();
}
_root = new TrieNode();
}
5 years ago
public void ToggleReverseIndex(bool enabled)
{
if (_use_reverse_index == enabled) return;
_use_reverse_index = enabled;
if (_use_reverse_index)
{
RebuildReverseIndex();
}
else
{
DestroyReverseIndex();
}
}
public void Append(string word)
{
if (word.Length == 0) return;
5 years ago
var node = _root.Append(word, 0);
if (node != null)
{
5 years ago
node.Value = (uint)Interlocked.Increment(ref _word_index);
if (_use_reverse_index)
5 years ago
{
_reverse_index.Add(node.Value.Value, node);
5 years ago
}
}
}
public uint? Key(string word)
{
if (word?.Length == 0) return null;
return _root.GetKey(word, 0);
}
5 years ago
public string Word(uint key)
{
if (_use_reverse_index)
{
if (_reverse_index.ContainsKey(key))
{
var node = _reverse_index[key];
return new string(Backward(node).Reverse().ToArray());
}
}
return null;
}
private IEnumerable<char> Backward(TrieNode node)
{
if (_use_reverse_index)
{
do
{
yield return node.Key!.Value;
5 years ago
node = node.Parent;
} while (node.Parent != null);
}
}
public bool Contains(string word)
{
if (word?.Length == 0) return false;
return _root.GetKey(word!, 0).HasValue;
}
public void Serialize(IBinaryWriter writer)
{
5 years ago
writer.WriteInt32(this._word_index);
writer.WriteBoolean(this._use_reverse_index);
writer.Write<TrieNode>(this._root);
}
public void Deserialize(IBinaryReader reader)
{
5 years ago
this._word_index = reader.ReadInt32();
this._use_reverse_index = reader.ReadBoolean();
this._root = reader.Read<TrieNode>();
5 years ago
RebuildReverseIndex();
}
private void RebuildReverseIndex()
{
if (this._use_reverse_index)
{
if (_reverse_index == null)
{
_reverse_index = new Dictionary<uint, TrieNode>();
}
_root.RebuildReverseIndex(null!, ' ', _reverse_index);
5 years ago
}
}
private void DestroyReverseIndex()
{
if (_reverse_index != null)
{
_reverse_index.Clear();
_reverse_index = null!;
5 years ago
}
_root.DestroyReverseIndex();
}
}
}

Powered by TurnKey Linux.