Created
November 18, 2020 04:42
-
-
Save celechii/b4372ff4c795a9b11c90f1893fd778c0 to your computer and use it in GitHub Desktop.
This file contains 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 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