Skip to content

Instantly share code, notes, and snippets.

@ertugrulozcan
Created December 13, 2016 13:21
Show Gist options
  • Save ertugrulozcan/fe789ad0bc07de69a8ede15b140c24de to your computer and use it in GitHub Desktop.
Save ertugrulozcan/fe789ad0bc07de69a8ede15b140c24de to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Eigen.Infrastructure.Search
{
public class Node
{
#region Fields
private char nodeKey;
private HashSet<Node> children;
private readonly HashSet<object> objects;
#endregion
#region Properties
public char NodeKey
{
get
{
return nodeKey;
}
private set
{
nodeKey = value;
}
}
public HashSet<Node> Children
{
get
{
return children;
}
private set
{
children = value;
}
}
public HashSet<object> Objects
{
get
{
return objects;
}
}
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public Node(char key, HashSet<KeyValuePair<string, object>> source)
{
this.NodeKey = key;
this.Children = new HashSet<Node>();
this.objects = new HashSet<object>();
foreach (var data in source)
{
if (!this.Objects.Contains(data.Value))
this.Objects.Add(data.Value);
}
this.SetChildSources(source);
}
#endregion
#region Methods
private void SetChildSources(HashSet<KeyValuePair<string, object>> source)
{
var doneList = new Dictionary<string, int>();
foreach (var vp in source)
{
if (string.IsNullOrEmpty(vp.Key) || string.IsNullOrWhiteSpace(vp.Key) || doneList.ContainsKey(vp.Key))
{
continue;
}
else
{
char key = char.ToLower(vp.Key[0], CultureInfo.DefaultThreadCurrentUICulture);
if (!this.Children.Any(x => char.ToLower(x.NodeKey, CultureInfo.DefaultThreadCurrentUICulture) == key))
{
// Düğüm oluştur.
var subNodes = this.CreateSubNodeSource(source, key, ref doneList);
this.Children.Add(new Node(key, subNodes));
}
else
{
// Düğüm zaten oluşturulmuş.
}
}
}
}
private HashSet<KeyValuePair<string, object>> CreateSubNodeSource(HashSet<KeyValuePair<string, object>> mainSource, char key, ref Dictionary<string, int> doneList)
{
HashSet<KeyValuePair<string, object>> subNodeSource = new HashSet<KeyValuePair<string, object>>();
foreach (var vp in mainSource)
{
if (!string.IsNullOrEmpty(vp.Key))
{
if (char.ToLower(vp.Key[0], CultureInfo.DefaultThreadCurrentUICulture) == key)
{
subNodeSource.Add(new KeyValuePair<string, object>(vp.Key.Substring(1, vp.Key.Length - 1), vp.Value));
if(!doneList.ContainsKey(vp.Key))
doneList.Add(vp.Key, 1);
}
}
}
return subNodeSource;
}
public Node GetChildNode(char c)
{
var invar = char.ToLower(c, CultureInfo.DefaultThreadCurrentUICulture);
if (this.Children.Any(x => char.ToLower(x.NodeKey) == char.ToLower(c, CultureInfo.DefaultThreadCurrentUICulture)))
return this.Children.First(x => char.ToLower(x.NodeKey) == char.ToLower(c, CultureInfo.DefaultThreadCurrentUICulture));
else
return null;
}
public void AddNewItem(string path, object item)
{
if (string.IsNullOrEmpty(path) || string.IsNullOrWhiteSpace(path))
return;
char keyChar = char.ToLower(path[0], CultureInfo.DefaultThreadCurrentUICulture);
if (!this.Children.Any(x => x.NodeKey == keyChar))
{
// Düğüm oluştur.
if (!this.Objects.Contains(item))
this.Objects.Add(item);
this.Children.Add(new Node(keyChar, new HashSet<KeyValuePair<string, object>>() { new KeyValuePair<string, object>(path.Substring(1, path.Length - 1), item) }));
}
else
{
// Düğüm zaten oluşturulmuş.
if (!this.Objects.Contains(item))
this.Objects.Add(item);
Node child = this.GetChildNode(keyChar);
child.AddNewItem(path.Substring(1, path.Length - 1), item);
}
}
public void RemoveItem(string path, object item)
{
if (string.IsNullOrEmpty(path) || string.IsNullOrWhiteSpace(path))
return;
char keyChar = path[0];
Node child = this.GetChildNode(keyChar);
if (child != null)
{
child.RemoveItem(path.Substring(1, path.Length - 1), item);
if (child.Objects.Count == 0)
this.Children.Remove(child);
}
if (this.Objects.Contains(item))
this.Objects.Remove(item);
}
public override string ToString()
{
return "[" + this.NodeKey + "]";
}
#endregion
#region Dispose
public void Destroy()
{
foreach (var child in this.Children)
child.Destroy();
this.Objects.Clear();
this.Children.Clear();
}
#endregion
public override bool Equals(object obj)
{
if (obj == null || !(obj is Node))
return false;
char c1 = char.ToLower((obj as Node).NodeKey, CultureInfo.DefaultThreadCurrentUICulture);
char c2 = char.ToLower(this.NodeKey, CultureInfo.DefaultThreadCurrentUICulture);
return c1 == c2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment