ln.collections/ln.collections/MappingBTree.cs

69 lines
2.0 KiB
C#

// /**
// * File: MappingBTree.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Collections;
namespace ln.collections
{
public delegate K KeyMapping<K, V>(V value);
public class MappingBTree<K,V> : IEnumerable<V>
{
public KeyMapping<K,V> KeyMapping { get; }
private BTree<K, V> tree;
public MappingBTree(KeyMapping<K, V> keyMapping)
{
KeyMapping = keyMapping;
tree = new BTree<K, V>();
}
public MappingBTree(KeyMapping<K, V> keyMapping,Comparison<K> comparison)
{
KeyMapping = keyMapping;
tree = new BTree<K, V>(comparison);
}
public V this[K key]
{
get => tree[key];
set => tree[key] = value;
}
public void Clear()
{
tree.Clear();
}
public int Count => tree.Count;
public void Add(V v) => tree.Add(KeyMapping(v), v);
public void TryAdd(V v) => tree.TryAdd(KeyMapping(v), v);
public void Remove(V v) => tree.Remove(KeyMapping(v));
public bool TryRemove(V v) => tree.TryRemove(KeyMapping(v));
public bool TryGet(K key, out V v) => tree.TryGet(key, out v);
public void RemoveKey(K key) => tree.Remove(key);
public bool Contains(V v) => tree.ContainsKey(KeyMapping(v));
public bool ContainsKey(K k) => tree.ContainsKey(k);
public IEnumerable<K> Keys => tree.Keys;
public IEnumerable<V> Values => tree.Values;
public V Next(V value) => tree[tree.Next(KeyMapping(value))];
public V Previous(V value) => tree[tree.Previous(KeyMapping(value))];
public IEnumerator<V> GetEnumerator() => Values.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => Values.GetEnumerator();
}
}