Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

insert keys automatically dictionary in c#

internal class AutoKeyDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
{
    private readonly Dictionary<TKey, TValue> inner;
    private readonly Func<TKey, TKey> incrementor;
    private readonly Stack<TKey> freeKeys;
    private readonly TKey keySeed;
    private TKey currentKey;

    public AutoKeyDictionary(TKey keySeed, Func<TKey, TKey> incrementor) 
    {
        if (keySeed == null)
            throw new ArgumentNullException("keySeed");

        if (incrementor == null)
            throw new ArgumentNullException("incrementor");

        inner = new Dictionary<TKey, TValue>();
        freeKeys = new Stack<TKey>();
        currentKey = keySeed;
    }

    public TKey Add(TValue value) //returns the used key
    {
        TKey usedKey;

        if (freeKeys.Count > 0)
        {
            usedKey = freeKeys.Pop();
            inner.Add(usedKey, value);
        }
        else
        {
            usedKey = currentKey;
            inner.Add(usedKey, value);
            currentKey = incrementor(currentKey);
        }

        return usedKey;
    }

    public void Clear()
    {
        inner.Clear();
        freeKeys.Clear();
        currentKey = keySeed;
    }

    public bool Remove(TKey key)
    {
        if (inner.Remove(key))
        {
            if (inner.Count > 0)
            {
                freeKeys.Push(key);
            }
            else
            {
                freeKeys.Clear();
                currentKey = keySeed;
            }

            return true;
        }

        return false;
    }

    public bool TryGetValue(TKey key, out TValue value) { return inner.TryGetValue(key, out value); }
    public TValue this[TKey key] { get {return inner[key];} set{inner[key] = value;} }
    public bool ContainsKey(TKey key) { return inner.ContainsKey(key); }
    public bool ContainsValue(TValue value) { return inner.ContainsValue (value); }
    public int Count { get{ return inner.Count; } }
    public Dictionary<TKey,TValue>.KeyCollection Keys { get { return inner.Keys; } }
    public Dictionary<TKey, TValue>.ValueCollection Values { get { return inner.Values; } }
    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return inner.GetEnumerator(); }
    IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)inner).GetEnumerator(); }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to start commvault services on linux 
Csharp :: asp net identity add a unique fields to user 
Csharp :: c# default parameter 
Csharp :: index was out of the bound array in c# 
Csharp :: unity move in x seconds to pos 
Csharp :: 403 forbidden error using Windows Forms 
Csharp :: how to add serilog to your asp.net project 
Csharp :: How to add a dynamically created form to a new tab in Syncfusion WinForms TabControlAdv? 
Csharp :: c# decimal literal 
Csharp :: telerik mvc grid unbound column 
Csharp :: make character move upward forever unity 2d 
Csharp :: notification platform not available c# 
Csharp :: c# sprintf equivalent 
Csharp :: c# unzip all archive files inside directory 
Csharp :: add file to combobox c# 
Csharp :: if statement to check if a time is between two times c# 
Csharp :: c# call constructor from constructor 
Csharp :: C# list of unique values with group and counts 
Csharp :: split a datatable based on number of rows 
Csharp :: how to make your player movr the way you are rotated in unity 
Csharp :: dotnet core vue in subdirectory 
Csharp :: c# odd or even 
Csharp :: Visual Studio - Summary Tag Comments - Optional Params 
Csharp :: c# show hidden window wpf 
Csharp :: dfgf 
Csharp :: C# parallel for loop specify cores 
Csharp :: .net SaveChanges vs update difference 
Csharp :: C# create delegate type at runtime 
Csharp :: c# remove 0 from string 
Csharp :: ow-to-return-http-500-from-asp-net-core-rc2-web-api 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =