-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
75 lines (63 loc) · 1.23 KB
/
utils.go
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
package compiler
type stack[T any] []T
func (s *stack[T]) push(item T) {
*s = append(*s, item)
}
func (s *stack[T]) pop() (item T) {
if len(*s) == 0 {
return
}
item = (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
return item
}
func (s *stack[T]) peek() (item T) {
if len(*s) == 0 {
return
}
return (*s)[len(*s)-1]
}
type OrderedMap[T any] struct {
keys []string
values map[string]T
}
func NewOrderedMap[T any]() *OrderedMap[T] {
return &OrderedMap[T]{
keys: []string{},
values: map[string]T{},
}
}
func (m *OrderedMap[T]) Len() int {
return len(m.keys)
}
func (m *OrderedMap[T]) Set(key string, value T) {
if _, ok := m.values[key]; ok {
m.values[key] = value
return
}
m.keys = append(m.keys, key)
m.values[key] = value
}
func (m *OrderedMap[T]) Get(key string) (value T, ok bool) {
value, ok = m.values[key]
return
}
func (m *OrderedMap[T]) Delete(key string) {
delete(m.values, key)
for i, k := range m.keys {
if k == key {
m.keys = append(m.keys[:i], m.keys[i+1:]...)
return
}
}
}
func (m *OrderedMap[T]) Range(f func(key string, value T) (bool, error)) error {
for _, key := range m.keys {
if ok, err := f(key, m.values[key]); err != nil {
return err
} else if !ok {
break
}
}
return nil
}