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