-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
103 lines (86 loc) · 2.89 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package wesplot
import (
"container/ring"
"golang.org/x/exp/constraints"
)
type Number interface {
constraints.Float | constraints.Integer
}
func Filter[T any](slice []T, predicate func(T) bool) []T {
filtered := make([]T, 0, len(slice))
for _, elem := range slice {
if predicate(elem) {
filtered = append(filtered, elem)
}
}
return filtered
}
func Min[T Number](a T, b T) T {
if a > b {
return b
}
return a
}
// Ring taken from https://github.com/Shopify/mybench/blob/main/ring.go
// This is not a particularly efficient implementation (as it allocates on
// read), but a good first starting point.
//
// The mutex has been removed from the upstream implementation because the
// DataBroadcaster has a single mutex governing both data buffering and channel
// management. Having another mutex here is both unnecessary and a potential for
// deadlocks.
//
// Copyright 2022-present, Shopify Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// A terrible implementation of a ring, based on the Golang ring which is not
// thread-safe nor offers a nice API.
//
// I can't believe there are no simple ring buffer data structure in Golang,
// with generics.
type ThreadUnsafeRing[T any] struct {
capacity int
ring *ring.Ring
}
func NewRing[T any](capacity int) *ThreadUnsafeRing[T] {
return &ThreadUnsafeRing[T]{
capacity: capacity,
ring: ring.New(capacity),
}
}
func (r *ThreadUnsafeRing[T]) Push(data T) {
r.ring = r.ring.Next()
r.ring.Value = data
}
func (r *ThreadUnsafeRing[T]) ReadAllOrdered() []T {
arr := make([]T, 0, r.capacity)
earliest := r.ring
for earliest.Prev() != nil && earliest.Prev() != r.ring && earliest.Prev().Value != nil {
earliest = earliest.Prev()
}
for earliest != r.ring {
arr = append(arr, earliest.Value.(T))
earliest = earliest.Next()
}
if earliest.Value == nil {
return arr
}
arr = append(arr, earliest.Value.(T))
return arr
}