|
|
@ -1,83 +1,56 @@
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZeroLevel.Services.Trees
|
|
|
|
namespace ZeroLevel.Services.Trees
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class Tree<T>
|
|
|
|
public class GNode<T>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly Dictionary<string, TreeNode<T>> _rootNodes = new Dictionary<string, TreeNode<T>>();
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
|
|
|
public T Value { get; set; }
|
|
|
|
|
|
|
|
public readonly Dictionary<string, GNode<T>> Nodes = new Dictionary<string, GNode<T>>();
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<TreeNode<T>> RootNodes
|
|
|
|
public void Add(string[] path, int index, T value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
get
|
|
|
|
if (path.Length > index)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return _rootNodes.Values.ToArray();
|
|
|
|
if (false == Nodes.ContainsKey(path[index]))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool TryAdd(string id, T value, bool override_if_exists)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (_rootNodes.ContainsKey(id))
|
|
|
|
Nodes[path[index]] = new GNode<T>();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.Length == (index + 1))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (override_if_exists)
|
|
|
|
Nodes[path[index]].Value = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
(_rootNodes[id].Value as IDisposable)?.Dispose();
|
|
|
|
Nodes[path[index]].Add(path, index + 1, value);
|
|
|
|
_rootNodes[id] = new TreeNode<T> { Value = value };
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_rootNodes[id] = new TreeNode<T> { 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)
|
|
|
|
public class GTree<T>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return default(T);
|
|
|
|
private readonly Dictionary<string, GNode<T>> _rootNodes = new Dictionary<string, GNode<T>>();
|
|
|
|
}
|
|
|
|
public IEnumerable<GNode<T>> RootNodes => _rootNodes.Values.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
public bool TryAdd(string[] path, T value, bool override_if_exists)
|
|
|
|
public void Add(string[] path, T value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (path.Length == 0) return false;
|
|
|
|
if (path.Length > 0)
|
|
|
|
return __TryAdd(path, 0, value, override_if_exists);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool TryRemove(string id)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (_rootNodes.ContainsKey(id))
|
|
|
|
if (false == _rootNodes.ContainsKey(path[0]))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
(_rootNodes[id].Value as IDisposable)?.Dispose();
|
|
|
|
_rootNodes[path[0]] = new GNode<T>();
|
|
|
|
_rootNodes[id].SubNodes?.Clear();
|
|
|
|
|
|
|
|
_rootNodes.Remove(id);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
if (path.Length == 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
foreach (var key in _rootNodes.Keys)
|
|
|
|
_rootNodes[path[0]].Value = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
(_rootNodes[key].Value as IDisposable)?.Dispose();
|
|
|
|
_rootNodes[path[0]].Add(path, 1, value);
|
|
|
|
_rootNodes[key].SubNodes?.Clear();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_rootNodes.Clear();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class TreeNode<T>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
|
|
|
public T Value { get; set; }
|
|
|
|
|
|
|
|
public Tree<T> SubNodes = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|