Skip to content

Instantly share code, notes, and snippets.

@DCCoder90
Created July 23, 2020 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DCCoder90/02afe5bff7b12ebee8695af06b95395e to your computer and use it in GitHub Desktop.
Save DCCoder90/02afe5bff7b12ebee8695af06b95395e to your computer and use it in GitHub Desktop.
Very simple generic increment/decremental list
public class IncList<T>
{
private readonly IDictionary<T,int> _incList;
public IncList()
{
_incList = new Dictionary<T, int>();
}
public void Add(T item)
{
AlterCount(Movement.Inc,item);
}
public void Dec(T item)
{
AlterCount(Movement.Dec,item);
}
public int Get(T item) => _incList[item];
public void Delete(T item)
{
_incList.Remove(item);
}
public int Total => _incList.Count;
public bool Exists(T item) => _incList.ContainsKey(item);
private void AlterCount(Movement movement, T item)
{
if (!Exists(item))
{
_incList.Add(item,0);
}
if (movement == Movement.Inc)
{
_incList[item] += 1;
}
else
{
_incList[item] -= 1;
if (_incList[item] < 0)
_incList[item] = 0;
}
}
private enum Movement
{
Inc,
Dec
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment