diff --git a/CSharp.Nixill/src/Collections/AVLTree/AVLTreeDictionary.cs b/CSharp.Nixill/src/Collections/AVLTree/AVLTreeDictionary.cs index 617d9a8..050170a 100644 --- a/CSharp.Nixill/src/Collections/AVLTree/AVLTreeDictionary.cs +++ b/CSharp.Nixill/src/Collections/AVLTree/AVLTreeDictionary.cs @@ -17,7 +17,7 @@ public class AVLTreeDictionary : INavigableDictionary { #region Implementing IDictionary public V this[K key] { get { - AVLTreeSet>.NodeTriplet nodes = BackingSet.SearchAround(new KeyValuePair(key, DefaultValue)); + NodeTriplet> nodes = BackingSet.SearchAround(new KeyValuePair(key, DefaultValue)); if (nodes.HasEqualValue) { return nodes.EqualValue.Value; } @@ -80,6 +80,16 @@ public AVLTreeDictionary(IEnumerable> elems, Comparison co #region Public Methods /// Documented in GitHub. public bool IsEmpty() => BackingSet.IsEmpty(); + + public NodeTriplet> EntriesAround(K from) => BackingSet.SearchAround(new KeyValuePair(from, default(V))); + + public NodeTriplet KeysAround(K from) { + var entries = EntriesAround(from); + var lesser = (entries.HasLesserValue) ? new AVLTreeSet.Node { Data = entries.LesserValue.Key } : null; + var equal = (entries.HasLesserValue) ? new AVLTreeSet.Node { Data = entries.LesserValue.Key } : null; + var greater = (entries.HasLesserValue) ? new AVLTreeSet.Node { Data = entries.LesserValue.Key } : null; + return new NodeTriplet((lesser, equal, greater)); + } #endregion #region Interface Implementations @@ -239,7 +249,7 @@ public void Add(K key, V value) { // Documented in GitHub. public bool TryGetValue(K key, out V value) { - AVLTreeSet>.NodeTriplet nodes = BackingSet.SearchAround(new KeyValuePair(key, DefaultValue)); + NodeTriplet> nodes = BackingSet.SearchAround(new KeyValuePair(key, DefaultValue)); if (nodes.HasEqualValue) { value = nodes.EqualValue.Value; return true; diff --git a/CSharp.Nixill/src/Collections/AVLTree/AVLTreeSet.cs b/CSharp.Nixill/src/Collections/AVLTree/AVLTreeSet.cs index 67db0da..43852b3 100644 --- a/CSharp.Nixill/src/Collections/AVLTree/AVLTreeSet.cs +++ b/CSharp.Nixill/src/Collections/AVLTree/AVLTreeSet.cs @@ -295,8 +295,8 @@ public void Print() { /// Returns the value requested if the collection contains it, as well /// as the next higher and lower values. /// - public NodeTriplet SearchAround(T value) { - return new NodeTriplet(SearchBounded(value)); + public NodeTriplet SearchAround(T value) { + return new NodeTriplet(SearchBounded(value)); } /// @@ -1072,51 +1072,50 @@ public void Visit(VisitNodeHandler> visitor, int level) { #endregion } + #endregion + } - public class NodeTriplet { - private readonly NodeValue Lesser; - private readonly NodeValue Equal; - private readonly NodeValue Greater; + public class NodeTriplet { + internal readonly NodeValue Lesser; + internal readonly NodeValue Equal; + internal readonly NodeValue Greater; - public bool HasLesserValue => Lesser != null; - public bool HasEqualValue => Equal != null; - public bool HasGreaterValue => Greater != null; + public bool HasLesserValue => Lesser != null; + public bool HasEqualValue => Equal != null; + public bool HasGreaterValue => Greater != null; - public T LesserValue { - get { - if (HasLesserValue) return Lesser.Value; - else throw new InvalidOperationException("No lesser value exists."); - } + public T LesserValue { + get { + if (HasLesserValue) return Lesser.Value; + else throw new InvalidOperationException("No lesser value exists."); } + } - public T EqualValue { - get { - if (HasEqualValue) return Equal.Value; - else throw new InvalidOperationException("No equal value exists."); - } + public T EqualValue { + get { + if (HasEqualValue) return Equal.Value; + else throw new InvalidOperationException("No equal value exists."); } + } - public T GreaterValue { - get { - if (HasGreaterValue) return Greater.Value; - else throw new InvalidOperationException("No greater value exists."); - } + public T GreaterValue { + get { + if (HasGreaterValue) return Greater.Value; + else throw new InvalidOperationException("No greater value exists."); } + } - internal NodeTriplet((AVLTreeSet.Node L, AVLTreeSet.Node E, AVLTreeSet.Node G) nodes) { - if (nodes.L != null) Lesser = new NodeValue(nodes.L.Data); - if (nodes.E != null) Equal = new NodeValue(nodes.E.Data); - if (nodes.G != null) Greater = new NodeValue(nodes.G.Data); - } + internal NodeTriplet((AVLTreeSet.Node L, AVLTreeSet.Node E, AVLTreeSet.Node G) nodes) { + if (nodes.L != null) Lesser = new NodeValue(nodes.L.Data); + if (nodes.E != null) Equal = new NodeValue(nodes.E.Data); + if (nodes.G != null) Greater = new NodeValue(nodes.G.Data); + } - private class NodeValue { - public readonly T Value; - public NodeValue(T val) { - Value = val; - } + internal class NodeValue { + public readonly T Value; + public NodeValue(T val) { + Value = val; } } - - #endregion } }