diff options
| author | Will Hawkins <[email protected]> | 2021-12-10 22:14:26 -0500 |
|---|---|---|
| committer | Will Hawkins <[email protected]> | 2021-12-10 22:14:26 -0500 |
| commit | 339a162877a78641a45338983de2192353fe7a5a (patch) | |
| tree | 84ea105cd0533e0cc7c67904d78d495845d008b1 /saturating | |
| parent | 5927d8aca8df26bf770ca061cdd3e00794847b27 (diff) | |
More work.
Diffstat (limited to 'saturating')
| -rw-r--r-- | saturating/saturating.go | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/saturating/saturating.go b/saturating/saturating.go new file mode 100644 index 0000000..10a0c87 --- /dev/null +++ b/saturating/saturating.go @@ -0,0 +1,27 @@ +package saturating + +type SaturatingInt struct { + max int + value int + saturated bool +} + +func NewSaturatingInt(max int) *SaturatingInt { + return &SaturatingInt{max: max, value: 0, saturated: false} +} + +func (s *SaturatingInt) Value() int { + if s.saturated { + return s.max + } + return s.value +} + +func (s *SaturatingInt) Add(operand int) { + if !s.saturated { + s.value += operand + if s.value >= s.max { + s.saturated = true + } + } +} |
