Created
July 23, 2020 08:22
-
-
Save DCCoder90/02afe5bff7b12ebee8695af06b95395e to your computer and use it in GitHub Desktop.
Very simple generic increment/decremental list
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
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