-
Notifications
You must be signed in to change notification settings - Fork 52
/
sort.go
66 lines (56 loc) · 1.14 KB
/
sort.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
//go:build go1.18
// +build go1.18
package zarray
type SortMaper[K hashable, V any] struct {
values *Maper[K, V]
keys []K
}
func NewSortMap[K hashable, V any](size ...uintptr) *SortMaper[K, V] {
return &SortMaper[K, V]{
keys: make([]K, 0),
values: NewHashMap[K, V](size...),
}
}
func (s *SortMaper[K, V]) Set(key K, value V) {
if !s.values.Has(key) {
s.keys = append(s.keys, key)
}
s.values.Set(key, value)
}
func (s *SortMaper[K, V]) Get(key K) (value V, ok bool) {
return s.values.Get(key)
}
func (s *SortMaper[K, V]) Has(key K) (ok bool) {
for _, v := range s.keys {
if v == key {
return true
}
}
return false
}
func (s *SortMaper[K, V]) Delete(key ...K) {
for i, v := range s.keys {
for _, k := range key {
if v == k {
s.keys = append(s.keys[:i], s.keys[i+1:]...)
break
}
}
}
s.values.Delete(key...)
}
func (s *SortMaper[K, V]) Len() int {
return len(s.keys)
}
func (s *SortMaper[K, V]) Keys() []K {
return s.keys
}
func (s *SortMaper[K, V]) ForEach(lambda func(K, V) bool) {
for i := range s.keys {
v, ok := s.values.Get(s.keys[i])
if ok && lambda(s.keys[i], v) {
continue
}
break
}
}