diff --git a/build.ln b/build.ln index afa2528..2a0641a 100644 --- a/build.ln +++ b/build.ln @@ -3,7 +3,7 @@ "dotnet" ], "env": { - "NUGET_SOURCE": "https://nexus.niclas-thobaben.de/repository/l--n.de/", + "NUGET_SOURCE": "https://nexus.l--n.de/repository/ln.net/", "CONFIGURATION": "Release" }, "stages": [ diff --git a/ln.collections.test/ln.collections.test.csproj b/ln.collections.test/ln.collections.test.csproj index c64a042..d363dd0 100644 --- a/ln.collections.test/ln.collections.test.csproj +++ b/ln.collections.test/ln.collections.test.csproj @@ -4,7 +4,7 @@ false - net5.0;netcoreapp3.1 + net5.0 diff --git a/ln.collections/BTree.cs b/ln.collections/BTree.cs index ae613f2..ca535eb 100644 --- a/ln.collections/BTree.cs +++ b/ln.collections/BTree.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using System.Linq; namespace ln.collections { - public class BTree : IDict + public class BTree : IDict, IEnumerable> { public Comparison Comparison { get; } public int Count => count; @@ -629,6 +629,22 @@ namespace ln.collections IEnumerable IDict.Keys => Keys; IEnumerable IDict.Values => Values; + public IEnumerable> GetInterval(K start, K end) => GetInterval(start, end, null); + + public IEnumerable> GetInterval(K start, K end, Func filter) + { + TreeNode currentNode = (start is null) ? First(headNode) : FindFirstGE(start); + if (currentNode != null) + { + while ((currentNode is not null) && ((end is null) || (Comparison(currentNode.Key, end) <= 0))) + { + if (filter is null || filter(currentNode.Key, currentNode.Value)) + yield return new KeyValuePair(currentNode.Key, currentNode.Value); + currentNode = Next(currentNode); + } + } + } + class TreeNode { public BTree Tree { get; } @@ -720,6 +736,20 @@ namespace ln.collections } + public IEnumerator> GetEnumerator() + { + TreeNode node = First(headNode); + while (node != null) + { + yield return new KeyValuePair(node.Key, node.Value); + node = Next(node); + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } } public class BTree : BTree diff --git a/ln.collections/BTreeValueList.cs b/ln.collections/BTreeValueList.cs index f13da40..3c77924 100644 --- a/ln.collections/BTreeValueList.cs +++ b/ln.collections/BTreeValueList.cs @@ -1,9 +1,10 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; namespace ln.collections { - public class BTreeValueList + public class BTreeValueList : IEnumerable> { public bool Empty => bTree.Empty; @@ -143,15 +144,16 @@ namespace ln.collections public IEnumerable Keys => bTree.Keys; public IEnumerable Values => bTree.Values.SelectMany(vl => vl); - public IEnumerable> GetKeyValuePairs() + public IEnumerator> GetEnumerator() { - foreach (K key in Keys) + foreach (KeyValuePair> vl in this.bTree) + foreach (V v in vl.Value) { - List lv = bTree[key]; - foreach (V value in lv) - yield return new KeyValuePair(key, value); + yield return new KeyValuePair(vl.Key, v); } } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + public void AddRange(IEnumerable> keyValuePairs) { foreach (KeyValuePair keyValuePair in keyValuePairs) diff --git a/ln.collections/BTreeValueSet.cs b/ln.collections/BTreeValueSet.cs index 4140f13..f240e9d 100644 --- a/ln.collections/BTreeValueSet.cs +++ b/ln.collections/BTreeValueSet.cs @@ -1,11 +1,14 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; + namespace ln.collections { - public class BTreeValueSet + public class BTreeValueSet : IEnumerable> { public bool Empty => bTree.Empty; + public Comparison Comparison => bTree.Comparison; BTree> bTree; @@ -13,6 +16,7 @@ namespace ln.collections { bTree = new BTree>(); } + public BTreeValueSet(Comparison comparison) { bTree = new BTree>(comparison); @@ -22,10 +26,11 @@ namespace ln.collections { get { - if (TryGet(key,out IEnumerable values)) + if (TryGet(key, out IEnumerable values)) { return values; } + throw new KeyNotFoundException(); } } @@ -35,41 +40,48 @@ namespace ln.collections if (!TryAdd(key, value)) throw new ArgumentException("duplicate key"); } + public void Remove(K key, V value) { if (!TryRemove(key, value)) throw new KeyNotFoundException(); } + public void Remove(K key) { if (!TryRemove(key)) throw new KeyNotFoundException(); } - public bool TryAdd(K key,V value) + public bool TryAdd(K key, V value) { if (!bTree.TryGet(key, out HashSet values)) { values = new HashSet(); bTree.Add(key, values); } + return values.Add(value); } - public bool TryGet(K key,out IEnumerable values) + + public bool TryGet(K key, out IEnumerable values) { if ((!bTree.TryGet(key, out HashSet v)) || (v.Count == 0)) { values = v; return false; } + values = v; return true; } + public bool TryRemove(K key) { return bTree.TryRemove(key); } - public bool TryRemove(K key,V value) + + public bool TryRemove(K key, V value) { if (bTree.TryGet(key, out HashSet values)) { @@ -78,14 +90,17 @@ namespace ln.collections bTree.Remove(key); return success; } + return false; } + public bool ContainsKey(K key) { if (!bTree.TryGet(key, out HashSet _values)) return false; return _values.Count > 0; } + public bool ContainsValue(V value) { foreach (HashSet _values in bTree.Values) @@ -94,6 +109,7 @@ namespace ln.collections if (v.Equals(value)) return true; } + return false; } @@ -104,17 +120,17 @@ namespace ln.collections return _values.Count; } - public void Clear() - { - bTree.Clear(); - } + public void Clear() + { + bTree.Clear(); + } public K First => bTree.First(); public K Last => bTree.Last(); public IEnumerable Keys => bTree.Keys; - public IEnumerable Values => bTree.Values.SelectMany(vl => vl); - + public IEnumerable Values => bTree.Values.SelectMany(vl => vl); + public IEnumerable> GetKeyValuePairs() { foreach (K key in Keys) @@ -124,11 +140,36 @@ namespace ln.collections yield return new KeyValuePair(key, value); } } + public void AddRange(IEnumerable> keyValuePairs) { foreach (KeyValuePair keyValuePair in keyValuePairs) Add(keyValuePair.Key, keyValuePair.Value); } + public IEnumerable> GetInterval(K start, K end) => GetInterval(start, end, null); + public IEnumerable> GetInterval(K start, K end, Func filter) + { + foreach (KeyValuePair> vl in this.bTree.GetInterval(start, end)) + foreach (V v in vl.Value) + { + if (filter is null || filter(vl.Key, v)) + yield return new KeyValuePair(vl.Key, v); + } + } + + public IEnumerator> GetEnumerator() + { + foreach (KeyValuePair> vl in this.bTree) + foreach (V v in vl.Value) + { + yield return new KeyValuePair(vl.Key, v); + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } } } diff --git a/ln.collections/ln.collections.csproj b/ln.collections/ln.collections.csproj index 7c9ac1c..086d267 100644 --- a/ln.collections/ln.collections.csproj +++ b/ln.collections/ln.collections.csproj @@ -2,13 +2,13 @@ true - 0.1.5 + 0.1.6 Harald Wolff-Thobaben l--n.de 0.0.1.1 0.0.1.1 - 0.1.5 - net5.0;netcoreapp3.1 + 0.1.7 + net5.0