Skip to content

Instantly share code, notes, and snippets.

@bityob
Last active December 25, 2017 20:25
Show Gist options
  • Save bityob/6eba0ae5e1f437ff5045fb919df71af8 to your computer and use it in GitHub Desktop.
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#)
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();
}
}
}
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);
}
}
}
}
@bityob
Copy link
Author

bityob commented Dec 25, 2017

Output from Program.cs

a:0
a:0|b:1
a:0|b:1|c:2
a:0|b:1|c:2|d:3
a:0|b:1|c:2|d:3|e:4
b:1|c:2|d:3|e:4|f:5
c:2|d:3|e:4|f:5|g:6
d:3|e:4|f:5|g:6|h:7
e:4|f:5|g:6|h:7|i:8
f:5|g:6|h:7|i:8|j:9
g:6|h:7|i:8|j:9|k:10
...
E:30|F:31|G:32|H:33|I:34
F:31|G:32|H:33|I:34|J:35
G:32|H:33|I:34|J:35|K:36
H:33|I:34|J:35|K:36|L:37
I:34|J:35|K:36|L:37|M:38
J:35|K:36|L:37|M:38|N:39
K:36|L:37|M:38|N:39|O:40
L:37|M:38|N:39|O:40|P:41
M:38|N:39|O:40|P:41|Q:42
N:39|O:40|P:41|Q:42|R:43
O:40|P:41|Q:42|R:43|S:44
P:41|Q:42|R:43|S:44|T:45
Q:42|R:43|S:44|T:45|U:46
R:43|S:44|T:45|U:46|V:47
S:44|T:45|U:46|V:47|W:48
T:45|U:46|V:47|W:48|X:49
U:46|V:47|W:48|X:49|Y:50
V:47|W:48|X:49|Y:50|Z:51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment