Update Generic.cs

pull/1/head
a.bozhenov 5 years ago
parent 1c3328173f
commit 0fad8ca7f5

@ -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 IEnumerable<TreeNode<T>> RootNodes public readonly Dictionary<string, GNode<T>> Nodes = new Dictionary<string, GNode<T>>();
{
get
{
return _rootNodes.Values.ToArray();
}
}
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(); Nodes[path[index]] = new GNode<T>();
_rootNodes[id] = new TreeNode<T> { Value = value }; }
return true; 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<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)
{
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) public class GTree<T>
{ {
if (_rootNodes.ContainsKey(id)) private readonly Dictionary<string, GNode<T>> _rootNodes = new Dictionary<string, GNode<T>>();
{ public IEnumerable<GNode<T>> RootNodes => _rootNodes.Values.ToArray();
(_rootNodes[id].Value as IDisposable)?.Dispose();
_rootNodes[id].SubNodes?.Clear();
_rootNodes.Remove(id);
return true;
}
return false;
}
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(); if (false == _rootNodes.ContainsKey(path[0]))
_rootNodes[key].SubNodes?.Clear(); {
_rootNodes[path[0]] = new GNode<T>();
}
if (path.Length == 1)
{
_rootNodes[path[0]].Value = value;
}
else
{
_rootNodes[path[0]].Add(path, 1, value);
}
} }
_rootNodes.Clear();
} }
} }
public class TreeNode<T>
{
public string Id { get; set; }
public T Value { get; set; }
public Tree<T> SubNodes = null;
}
} }
Loading…
Cancel
Save

Powered by TurnKey Linux.