Additional Methods

master
Harald Wolff 2022-06-03 13:39:05 +02:00
parent b99b7dcac8
commit 61a010c666
6 changed files with 96 additions and 23 deletions

View File

@ -3,7 +3,7 @@
"dotnet" "dotnet"
], ],
"env": { "env": {
"NUGET_SOURCE": "https://nexus.niclas-thobaben.de/repository/l--n.de/", "NUGET_SOURCE": "https://nexus.l--n.de/repository/ln.net/",
"CONFIGURATION": "Release" "CONFIGURATION": "Release"
}, },
"stages": [ "stages": [

View File

@ -4,7 +4,7 @@
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks> <TargetFrameworks>net5.0</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -4,7 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace ln.collections namespace ln.collections
{ {
public class BTree<K,V> : IDict<K,V> public class BTree<K,V> : IDict<K,V>, IEnumerable<KeyValuePair<K,V>>
{ {
public Comparison<K> Comparison { get; } public Comparison<K> Comparison { get; }
public int Count => count; public int Count => count;
@ -629,6 +629,22 @@ namespace ln.collections
IEnumerable IDict.Keys => Keys; IEnumerable IDict.Keys => Keys;
IEnumerable IDict.Values => Values; IEnumerable IDict.Values => Values;
public IEnumerable<KeyValuePair<K, V>> GetInterval(K start, K end) => GetInterval(start, end, null);
public IEnumerable<KeyValuePair<K, V>> GetInterval(K start, K end, Func<K,V,bool> 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<K, V>(currentNode.Key, currentNode.Value);
currentNode = Next(currentNode);
}
}
}
class TreeNode class TreeNode
{ {
public BTree<K, V> Tree { get; } public BTree<K, V> Tree { get; }
@ -720,6 +736,20 @@ namespace ln.collections
} }
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{
TreeNode node = First(headNode);
while (node != null)
{
yield return new KeyValuePair<K, V>(node.Key, node.Value);
node = Next(node);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
} }
public class BTree<K> : BTree<K,object> public class BTree<K> : BTree<K,object>

View File

@ -1,9 +1,10 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace ln.collections namespace ln.collections
{ {
public class BTreeValueList<K, V> public class BTreeValueList<K, V> : IEnumerable<KeyValuePair<K,V>>
{ {
public bool Empty => bTree.Empty; public bool Empty => bTree.Empty;
@ -143,15 +144,16 @@ namespace ln.collections
public IEnumerable<K> Keys => bTree.Keys; public IEnumerable<K> Keys => bTree.Keys;
public IEnumerable<V> Values => bTree.Values.SelectMany(vl => vl); public IEnumerable<V> Values => bTree.Values.SelectMany(vl => vl);
public IEnumerable<KeyValuePair<K, V>> GetKeyValuePairs() public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{ {
foreach (K key in Keys) foreach (KeyValuePair<K, List<V>> vl in this.bTree)
foreach (V v in vl.Value)
{ {
List<V> lv = bTree[key]; yield return new KeyValuePair<K, V>(vl.Key, v);
foreach (V value in lv)
yield return new KeyValuePair<K, V>(key, value);
} }
} }
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void AddRange(IEnumerable<KeyValuePair<K, V>> keyValuePairs) public void AddRange(IEnumerable<KeyValuePair<K, V>> keyValuePairs)
{ {
foreach (KeyValuePair<K, V> keyValuePair in keyValuePairs) foreach (KeyValuePair<K, V> keyValuePair in keyValuePairs)

View File

@ -1,11 +1,14 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace ln.collections namespace ln.collections
{ {
public class BTreeValueSet<K, V> public class BTreeValueSet<K, V> : IEnumerable<KeyValuePair<K, V>>
{ {
public bool Empty => bTree.Empty; public bool Empty => bTree.Empty;
public Comparison<K> Comparison => bTree.Comparison;
BTree<K, HashSet<V>> bTree; BTree<K, HashSet<V>> bTree;
@ -13,6 +16,7 @@ namespace ln.collections
{ {
bTree = new BTree<K, HashSet<V>>(); bTree = new BTree<K, HashSet<V>>();
} }
public BTreeValueSet(Comparison<K> comparison) public BTreeValueSet(Comparison<K> comparison)
{ {
bTree = new BTree<K, HashSet<V>>(comparison); bTree = new BTree<K, HashSet<V>>(comparison);
@ -22,10 +26,11 @@ namespace ln.collections
{ {
get get
{ {
if (TryGet(key,out IEnumerable<V> values)) if (TryGet(key, out IEnumerable<V> values))
{ {
return values; return values;
} }
throw new KeyNotFoundException(); throw new KeyNotFoundException();
} }
} }
@ -35,41 +40,48 @@ namespace ln.collections
if (!TryAdd(key, value)) if (!TryAdd(key, value))
throw new ArgumentException("duplicate key"); throw new ArgumentException("duplicate key");
} }
public void Remove(K key, V value) public void Remove(K key, V value)
{ {
if (!TryRemove(key, value)) if (!TryRemove(key, value))
throw new KeyNotFoundException(); throw new KeyNotFoundException();
} }
public void Remove(K key) public void Remove(K key)
{ {
if (!TryRemove(key)) if (!TryRemove(key))
throw new KeyNotFoundException(); throw new KeyNotFoundException();
} }
public bool TryAdd(K key,V value) public bool TryAdd(K key, V value)
{ {
if (!bTree.TryGet(key, out HashSet<V> values)) if (!bTree.TryGet(key, out HashSet<V> values))
{ {
values = new HashSet<V>(); values = new HashSet<V>();
bTree.Add(key, values); bTree.Add(key, values);
} }
return values.Add(value); return values.Add(value);
} }
public bool TryGet(K key,out IEnumerable<V> values)
public bool TryGet(K key, out IEnumerable<V> values)
{ {
if ((!bTree.TryGet(key, out HashSet<V> v)) || (v.Count == 0)) if ((!bTree.TryGet(key, out HashSet<V> v)) || (v.Count == 0))
{ {
values = v; values = v;
return false; return false;
} }
values = v; values = v;
return true; return true;
} }
public bool TryRemove(K key) public bool TryRemove(K key)
{ {
return bTree.TryRemove(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<V> values)) if (bTree.TryGet(key, out HashSet<V> values))
{ {
@ -78,14 +90,17 @@ namespace ln.collections
bTree.Remove(key); bTree.Remove(key);
return success; return success;
} }
return false; return false;
} }
public bool ContainsKey(K key) public bool ContainsKey(K key)
{ {
if (!bTree.TryGet(key, out HashSet<V> _values)) if (!bTree.TryGet(key, out HashSet<V> _values))
return false; return false;
return _values.Count > 0; return _values.Count > 0;
} }
public bool ContainsValue(V value) public bool ContainsValue(V value)
{ {
foreach (HashSet<V> _values in bTree.Values) foreach (HashSet<V> _values in bTree.Values)
@ -94,6 +109,7 @@ namespace ln.collections
if (v.Equals(value)) if (v.Equals(value))
return true; return true;
} }
return false; return false;
} }
@ -104,17 +120,17 @@ namespace ln.collections
return _values.Count; return _values.Count;
} }
public void Clear() public void Clear()
{ {
bTree.Clear(); bTree.Clear();
} }
public K First => bTree.First(); public K First => bTree.First();
public K Last => bTree.Last(); public K Last => bTree.Last();
public IEnumerable<K> Keys => bTree.Keys; public IEnumerable<K> Keys => bTree.Keys;
public IEnumerable<V> Values => bTree.Values.SelectMany(vl => vl); public IEnumerable<V> Values => bTree.Values.SelectMany(vl => vl);
public IEnumerable<KeyValuePair<K, V>> GetKeyValuePairs() public IEnumerable<KeyValuePair<K, V>> GetKeyValuePairs()
{ {
foreach (K key in Keys) foreach (K key in Keys)
@ -124,11 +140,36 @@ namespace ln.collections
yield return new KeyValuePair<K, V>(key, value); yield return new KeyValuePair<K, V>(key, value);
} }
} }
public void AddRange(IEnumerable<KeyValuePair<K, V>> keyValuePairs) public void AddRange(IEnumerable<KeyValuePair<K, V>> keyValuePairs)
{ {
foreach (KeyValuePair<K, V> keyValuePair in keyValuePairs) foreach (KeyValuePair<K, V> keyValuePair in keyValuePairs)
Add(keyValuePair.Key, keyValuePair.Value); Add(keyValuePair.Key, keyValuePair.Value);
} }
public IEnumerable<KeyValuePair<K, V>> GetInterval(K start, K end) => GetInterval(start, end, null);
public IEnumerable<KeyValuePair<K, V>> GetInterval(K start, K end, Func<K,V,bool> filter)
{
foreach (KeyValuePair<K, HashSet<V>> 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<K, V>(vl.Key, v);
}
}
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{
foreach (KeyValuePair<K, HashSet<V>> vl in this.bTree)
foreach (V v in vl.Value)
{
yield return new KeyValuePair<K, V>(vl.Key, v);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
} }
} }

View File

@ -2,13 +2,13 @@
<PropertyGroup> <PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.1.5</Version> <Version>0.1.6</Version>
<Authors>Harald Wolff-Thobaben</Authors> <Authors>Harald Wolff-Thobaben</Authors>
<Company>l--n.de</Company> <Company>l--n.de</Company>
<AssemblyVersion>0.0.1.1</AssemblyVersion> <AssemblyVersion>0.0.1.1</AssemblyVersion>
<FileVersion>0.0.1.1</FileVersion> <FileVersion>0.0.1.1</FileVersion>
<PackageVersion>0.1.5</PackageVersion> <PackageVersion>0.1.7</PackageVersion>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks> <TargetFramework>net5.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>