summaryrefslogtreecommitdiff
path: root/dict
diff options
context:
space:
mode:
authorJonas Knobloch <jonas.knobloch@t-online.de>2026-09-10 01:59:34 +0200
committerJonas Knobloch <jonas.knobloch@t-online.de>2026-09-10 01:59:34 +0200
commitf998fee7427403698f9262d1e92d1a307c652094 (patch)
tree88fa03f0a13045d9e13ab4235475a6ff8512de5d /dict
parent123a37a04b0076c569cbed7c2669ab1087e2cba7 (diff)
WIP
Diffstat (limited to 'dict')
-rw-r--r--dict/dict.go4
-rw-r--r--dict/serialize.go55
2 files changed, 59 insertions, 0 deletions
diff --git a/dict/dict.go b/dict/dict.go
index bc0f8bd..f153932 100644
--- a/dict/dict.go
+++ b/dict/dict.go
@@ -21,6 +21,10 @@ func NewDict[V any]() *Dict[V] {
}
}
+func (d *Dict[V]) Len() int {
+ return len(d.s)
+}
+
func (d *Dict[V]) Set(key string, val V) {
if i, ok := d.m[key]; ok {
d.s[i].Val = val
diff --git a/dict/serialize.go b/dict/serialize.go
new file mode 100644
index 0000000..da97c06
--- /dev/null
+++ b/dict/serialize.go
@@ -0,0 +1,55 @@
+package dict
+
+import (
+ "bufio"
+ "io"
+ "strings"
+)
+
+func LoadDict[V any](r io.Reader, f func(l string) (string, V)) (*Dict[V], error) {
+ //var d *Dict[V]
+ //
+ //if l, err := linesNotEmpty(r); err != nil {
+ // return nil, err
+ //} else {
+ // d = NewDict[V](l)
+ //}
+
+ d := NewDict[V]()
+
+ scanner := bufio.NewScanner(r)
+
+ for scanner.Scan() {
+ line := scanner.Text()
+
+ if err := scanner.Err(); err != nil {
+ return nil, err
+ }
+
+ k, v := f(line)
+
+ d.Set(k, v)
+ }
+
+ return d, nil
+}
+
+func linesNotEmpty(r io.Reader) (int, error) {
+ scanner := bufio.NewScanner(r)
+
+ count := 0
+
+ for scanner.Scan() {
+ line := scanner.Text()
+
+ if strings.TrimSpace(line) != "" {
+ count++
+ }
+ }
+
+ if err := scanner.Err(); err != nil {
+ return 0, err
+ }
+
+ return count, nil
+}