orderedmap.go 2.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Package orderedmap provides a generic ordered map that maintains insertion order.
// It wraps github.com/wk8/go-ordered-map/v2 to encapsulate the dependency.
package orderedmap

import (
	"encoding/json"
	"iter"

	orderedmap "github.com/wk8/go-ordered-map/v2"
)

// Map is a generic ordered map that maintains insertion order.
type Map[K comparable, V any] struct {
	om *orderedmap.OrderedMap[K, V]
}

// New creates a new empty ordered map.
func New[K comparable, V any]() *Map[K, V] {
	return &Map[K, V]{
		om: orderedmap.New[K, V](),
	}
}

// Get retrieves a value by key.
func (m *Map[K, V]) Get(key K) (V, bool) {
	if m == nil || m.om == nil {
		var zero V
		return zero, false
	}
	return m.om.Get(key)
}

// Set sets a key-value pair. If the key already exists, its value is updated
// but its position in the iteration order is preserved. If the key is new,
// it is appended to the end.
func (m *Map[K, V]) Set(key K, value V) {
	if m == nil {
		return
	}
	if m.om == nil {
		m.om = orderedmap.New[K, V]()
	}
	m.om.Set(key, value)
}

// Len returns the number of entries.
func (m *Map[K, V]) Len() int {
	if m == nil || m.om == nil {
		return 0
	}
	return m.om.Len()
}

// All returns an iterator over all key-value pairs in insertion order.
func (m *Map[K, V]) All() iter.Seq2[K, V] {
	return func(yield func(K, V) bool) {
		if m == nil || m.om == nil {
			return
		}
		for pair := m.om.Oldest(); pair != nil; pair = pair.Next() {
			if !yield(pair.Key, pair.Value) {
				return
			}
		}
	}
}

// ToMap converts to a regular Go map.
// Note: The resulting map does not preserve order.
func (m *Map[K, V]) ToMap() map[K]V {
	if m == nil || m.om == nil {
		return nil
	}
	result := make(map[K]V, m.om.Len())
	for pair := m.om.Oldest(); pair != nil; pair = pair.Next() {
		result[pair.Key] = pair.Value
	}
	return result
}

// MarshalJSON implements json.Marshaler. The JSON output preserves key order.
func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
	if m == nil || m.om == nil {
		return []byte("null"), nil
	}
	return json.Marshal(m.om)
}

// UnmarshalJSON implements json.Unmarshaler. The insertion order matches the
// order of keys in the JSON input.
func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
	m.om = orderedmap.New[K, V]()
	return json.Unmarshal(data, &m.om)
}