summaryrefslogtreecommitdiff
path: root/lib/containers
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-08-29 22:42:12 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-08-30 21:29:21 -0600
commitecb21f33362367d44215af73f060f32485155893 (patch)
tree73e69e771c65a1d26586ce24ad9c5d51123ec7ec /lib/containers
parent03bd0222a8ef360c332f78f9e6a3762bde0aea58 (diff)
wip
Diffstat (limited to 'lib/containers')
-rw-r--r--lib/containers/set.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/containers/set.go b/lib/containers/set.go
new file mode 100644
index 0000000..e20b5be
--- /dev/null
+++ b/lib/containers/set.go
@@ -0,0 +1,60 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package containers
+
+import (
+ "io"
+
+ "git.lukeshu.com/go/lowmemjson"
+ "golang.org/x/exp/constraints"
+
+ "git.lukeshu.com/btrfs-progs-ng/lib/maps"
+)
+
+type Set[T constraints.Ordered] map[T]struct{}
+
+var (
+ _ lowmemjson.Encodable = Set[int]{}
+ _ lowmemjson.Decodable = (*Set[int])(nil)
+)
+
+func (o Set[T]) EncodeJSON(w io.Writer) error {
+ return lowmemjson.Encode(w, maps.SortedKeys(o))
+}
+
+func (o *Set[T]) DecodeJSON(r io.RuneScanner) error {
+ c, _, _ := r.ReadRune()
+ if c == 'n' {
+ _, _, _ = r.ReadRune() // u
+ _, _, _ = r.ReadRune() // l
+ _, _, _ = r.ReadRune() // l
+ *o = nil
+ return nil
+ }
+ _ = r.UnreadRune()
+ *o = Set[T]{}
+ return lowmemjson.DecodeArray(r, func(r io.RuneScanner) error {
+ var val T
+ if err := lowmemjson.Decode(r, &val); err != nil {
+ return err
+ }
+ (*o)[val] = struct{}{}
+ return nil
+ })
+}
+
+func (o *Set[T]) Insert(v T) {
+ if o == nil {
+ *o = Set[T]{}
+ }
+ (*o)[v] = struct{}{}
+}
+
+func (o *Set[T]) Delete(v T) {
+ if o == nil {
+ return
+ }
+ delete(*o, v)
+}