mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-21 12:35:33 +03:00
fede297942
Squashed commit of the following:
commit 37e33ec761cfa30164125af2c5bb40789412355e
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Feb 14 15:25:25 2024 +0300
aghalg: imp code
commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea
Merge: b8ea924aa 37736289e
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Feb 14 15:04:59 2024 +0300
Merge branch 'master' into AG-27492-client-persistent-storage
commit b8ea924aa7ed4c052760a6068f945d83d184e7e3
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Feb 13 19:07:52 2024 +0300
home: imp tests
commit aa6fec03b1a1ead96bc76919b7ad51ae19626633
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Feb 13 14:54:28 2024 +0300
home: imp docs
commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Feb 8 20:16:11 2024 +0300
all: imp code
commit b45c7d868ddb1be73e119b3260e2a866d57baa91
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Feb 7 19:15:11 2024 +0300
aghalg: add tests
commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Feb 6 20:50:22 2024 +0300
all: imp code, tests
commit 4a44e993c9bd393d2cb9853108eae1ad91e64402
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Feb 1 14:59:11 2024 +0300
all: persistent client index
commit 66b16e216e03e9f3d5e69496a89b18a9d732b564
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Jan 31 15:06:05 2024 +0300
aghalg: ordered map
86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
package aghalg
|
|
|
|
import (
|
|
"slices"
|
|
)
|
|
|
|
// SortedMap is a map that keeps elements in order with internal sorting
|
|
// function. Must be initialised by the [NewSortedMap].
|
|
type SortedMap[K comparable, V any] struct {
|
|
vals map[K]V
|
|
cmp func(a, b K) (res int)
|
|
keys []K
|
|
}
|
|
|
|
// NewSortedMap initializes the new instance of sorted map. cmp is a sort
|
|
// function to keep elements in order.
|
|
//
|
|
// TODO(s.chzhen): Use cmp.Compare in Go 1.21.
|
|
func NewSortedMap[K comparable, V any](cmp func(a, b K) (res int)) SortedMap[K, V] {
|
|
return SortedMap[K, V]{
|
|
vals: map[K]V{},
|
|
cmp: cmp,
|
|
}
|
|
}
|
|
|
|
// Set adds val with key to the sorted map. It panics if the m is nil.
|
|
func (m *SortedMap[K, V]) Set(key K, val V) {
|
|
m.vals[key] = val
|
|
|
|
i, has := slices.BinarySearchFunc(m.keys, key, m.cmp)
|
|
if has {
|
|
m.keys[i] = key
|
|
} else {
|
|
m.keys = slices.Insert(m.keys, i, key)
|
|
}
|
|
}
|
|
|
|
// Get returns val by key from the sorted map.
|
|
func (m *SortedMap[K, V]) Get(key K) (val V, ok bool) {
|
|
if m == nil {
|
|
return
|
|
}
|
|
|
|
val, ok = m.vals[key]
|
|
|
|
return val, ok
|
|
}
|
|
|
|
// Del removes the value by key from the sorted map.
|
|
func (m *SortedMap[K, V]) Del(key K) {
|
|
if m == nil {
|
|
return
|
|
}
|
|
|
|
if _, has := m.vals[key]; !has {
|
|
return
|
|
}
|
|
|
|
delete(m.vals, key)
|
|
i, _ := slices.BinarySearchFunc(m.keys, key, m.cmp)
|
|
m.keys = slices.Delete(m.keys, i, i+1)
|
|
}
|
|
|
|
// Clear removes all elements from the sorted map.
|
|
func (m *SortedMap[K, V]) Clear() {
|
|
if m == nil {
|
|
return
|
|
}
|
|
|
|
m.keys = nil
|
|
clear(m.vals)
|
|
}
|
|
|
|
// Range calls cb for each element of the map, sorted by m.cmp. If cb returns
|
|
// false it stops.
|
|
func (m *SortedMap[K, V]) Range(cb func(K, V) (cont bool)) {
|
|
if m == nil {
|
|
return
|
|
}
|
|
|
|
for _, k := range m.keys {
|
|
if !cb(k, m.vals[k]) {
|
|
return
|
|
}
|
|
}
|
|
}
|