Skip to content

Instantly share code, notes, and snippets.

@celechii
Created November 18, 2020 04:42
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 celechii/b4372ff4c795a9b11c90f1893fd778c0 to your computer and use it in GitHub Desktop.
Save celechii/b4372ff4c795a9b11c90f1893fd778c0 to your computer and use it in GitHub Desktop.
public struct MultiBool {
private int count;
private bool hasMin;
private bool hasMax;
private int max;
private int min;
public static MultiBool WithMin(int min) {
MultiBool b = new MultiBool();
b.hasMin = true;
b.min = min;
return b;
}
public static MultiBool WithMax(int max) {
MultiBool b = new MultiBool();
b.hasMax = true;
b.max = max;
return b;
}
public static MultiBool WithMinAndMax(int min, int max) {
MultiBool b = new MultiBool();
b.hasMin = true;
b.hasMax = true;
if (min <= max) {
b.min = min;
b.max = max;
} else {
b.min = max;
b.max = min;
}
return b;
}
public void Set(int amount) {
count = amount;
ClampCount();
}
public void Reset() {
count = 0;
ClampCount();
}
public void AddTrue() {
count++;
ClampCount();
}
public void AddFalse() {
count++;
ClampCount();
}
public void Add(int amount) {
count += amount;
ClampCount();
}
private void ClampCount() {
if (hasMin && count < min)
count = min;
else if (hasMax && count > max)
count = max;
}
public static implicit operator bool(MultiBool b) => b.count >= 0;
public static MultiBool operator ++(MultiBool b) {
b.AddTrue();
return b;
}
public static MultiBool operator --(MultiBool b) {
b.AddFalse();
return b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment