Last active
December 25, 2017 20:25
-
-
Save bityob/6eba0ae5e1f437ff5045fb919df71af8 to your computer and use it in GitHub Desktop.
Dictionary with maximun length, keep last N keys on dictionary, using Queue object (C#)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace DictMaxLength | |
{ | |
class DictWithMaxKeys : Dictionary<string, int>, IDictionary | |
{ | |
private Dictionary<string, int> _dict; | |
private Queue<string> _queue; | |
private int _maxLength; | |
public DictWithMaxKeys(int maxLength) | |
{ | |
_dict = new Dictionary<string, int>(); | |
_queue = new Queue<string>(); | |
_maxLength = maxLength; | |
} | |
public int this[string key] { | |
get => _dict[key]; | |
set | |
{ | |
if (!_dict.Keys.Contains(key)) | |
{ | |
if (_queue.Count >= _maxLength) | |
{ | |
var lastKey = _queue.Dequeue(); | |
_dict.Remove(lastKey); | |
} | |
_queue.Enqueue(key.ToString()); | |
} | |
_dict.Add(key, value); | |
} | |
} | |
public object this[object key] { get => ((IDictionary)_dict)[key]; set => ((IDictionary)_dict)[key] = value; } | |
public ICollection Keys => ((IDictionary)_dict).Keys; | |
public ICollection Values => ((IDictionary)_dict).Values; | |
public bool IsReadOnly => ((IDictionary)_dict).IsReadOnly; | |
public bool IsFixedSize => ((IDictionary)_dict).IsFixedSize; | |
public int Count => ((IDictionary)_dict).Count; | |
public object SyncRoot => ((IDictionary)_dict).SyncRoot; | |
public bool IsSynchronized => ((IDictionary)_dict).IsSynchronized; | |
public void Add(object key, object value) | |
{ | |
((IDictionary)_dict).Add(key, value); | |
} | |
public void Clear() | |
{ | |
((IDictionary)_dict).Clear(); | |
} | |
public bool Contains(object key) | |
{ | |
return ((IDictionary)_dict).Contains(key); | |
} | |
public void CopyTo(Array array, int index) | |
{ | |
((IDictionary)_dict).CopyTo(array, index); | |
} | |
public IDictionaryEnumerator GetEnumerator() | |
{ | |
return ((IDictionary)_dict).GetEnumerator(); | |
} | |
public void Remove(object key) | |
{ | |
((IDictionary)_dict).Remove(key); | |
} | |
public override string ToString() | |
{ | |
// Output dictionary sorted by values | |
var pairsList = _dict.ToList(); | |
pairsList.Sort((p1, p2) => p1.Value.CompareTo(p2.Value)); | |
return string.Join("|", pairsList.Select(x => string.Format("{0}{1}{2}", x.Key, ':', x.Value))); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return ((IDictionary)_dict).GetEnumerator(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace DictMaxLength | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
var d = new DictWithMaxKeys(5); | |
Console.WriteLine(d); | |
for (int i=0; i < letters.Length; i++) | |
{ | |
d[letters[i].ToString()] = i; | |
Console.WriteLine(d); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output from Program.cs