blob: 1768a96ae1b91ed521018e3697da4794d141f1c3 (
plain) (
blame)
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
|
// This file is part of gnunet-go, a GNUnet-implementation in Golang.
// Copyright (C) 2019-2022 Bernd Fix >Y<
//
// gnunet-go is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// gnunet-go is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later
package util
import (
"strings"
)
//----------------------------------------------------------------------
// Count occurrence of multiple instance at the same time.
//----------------------------------------------------------------------
// Counter is a metric with single key
type Counter[T comparable] map[T]int
// Add one to themetric for a given key and return current value
func (cm Counter[T]) Add(i T) int {
count, ok := cm[i]
if !ok {
count = 1
} else {
count++
}
cm[i] = count
return count
}
// Num returns the metric for a given key
func (cm Counter[T]) Num(i T) int {
count, ok := cm[i]
if !ok {
count = 0
}
return count
}
//----------------------------------------------------------------------
// Parameter set with string keys and variable value types
//----------------------------------------------------------------------
// ParameterSet with string keys and variable value types
type ParameterSet map[string]any
// Get a parameter value with given type 'V'
func GetParam[V any](params ParameterSet, key string) (i V, ok bool) {
var v any
if v, ok = params[key]; ok {
if i, ok = v.(V); ok {
return
}
}
return
}
//----------------------------------------------------------------------
// additional helpers
//----------------------------------------------------------------------
// StripPathRight returns a dot-separated path without
// its last (right-most) element.
func StripPathRight(s string) string {
if idx := strings.LastIndex(s, "."); idx != -1 {
return s[:idx]
}
return s
}
|