diff --git a/ZeroLevel/Services/Trees/Generic.cs b/ZeroLevel/Services/Trees/Generic.cs index 756b062..a24f0f0 100644 --- a/ZeroLevel/Services/Trees/Generic.cs +++ b/ZeroLevel/Services/Trees/Generic.cs @@ -1,83 +1,56 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; namespace ZeroLevel.Services.Trees { - public class Tree + public class GNode { - private readonly Dictionary> _rootNodes = new Dictionary>(); - - public IEnumerable> RootNodes - { - get - { - return _rootNodes.Values.ToArray(); - } - } + public string Id { get; set; } + public T Value { get; set; } + public readonly Dictionary> Nodes = new Dictionary>(); - public bool TryAdd(string id, T value, bool override_if_exists) + public void Add(string[] path, int index, T value) { - if (_rootNodes.ContainsKey(id)) + if (path.Length > index) { - if (override_if_exists) + if (false == Nodes.ContainsKey(path[index])) { - (_rootNodes[id].Value as IDisposable)?.Dispose(); - _rootNodes[id] = new TreeNode { Value = value }; - return true; + Nodes[path[index]] = new GNode(); + } + if (path.Length == (index + 1)) + { + Nodes[path[index]].Value = value; + } + else + { + Nodes[path[index]].Add(path, index + 1, value); } - return false; } - _rootNodes[id] = new TreeNode { Value = value }; - return true; - } - - private bool __TryAdd(string[] path, int index, T value, bool override_if_exists) - { - /* if (_rootNodes.ContainsKey(path[index])) - { - }*/ - return false; - } - - public T Find(string[] path) - { - return default(T); - } - - public bool TryAdd(string[] path, T value, bool override_if_exists) - { - if (path.Length == 0) return false; - return __TryAdd(path, 0, value, override_if_exists); } + } - public bool TryRemove(string id) - { - if (_rootNodes.ContainsKey(id)) - { - (_rootNodes[id].Value as IDisposable)?.Dispose(); - _rootNodes[id].SubNodes?.Clear(); - _rootNodes.Remove(id); - return true; - } - return false; - } + public class GTree + { + private readonly Dictionary> _rootNodes = new Dictionary>(); + public IEnumerable> RootNodes => _rootNodes.Values.ToArray(); - public void Clear() + public void Add(string[] path, T value) { - foreach (var key in _rootNodes.Keys) + if (path.Length > 0) { - (_rootNodes[key].Value as IDisposable)?.Dispose(); - _rootNodes[key].SubNodes?.Clear(); + if (false == _rootNodes.ContainsKey(path[0])) + { + _rootNodes[path[0]] = new GNode(); + } + if (path.Length == 1) + { + _rootNodes[path[0]].Value = value; + } + else + { + _rootNodes[path[0]].Add(path, 1, value); + } } - _rootNodes.Clear(); } } - - public class TreeNode - { - public string Id { get; set; } - public T Value { get; set; } - public Tree SubNodes = null; - } } \ No newline at end of file