-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmutex_slice.go
153 lines (121 loc) · 3 KB
/
mutex_slice.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package nex
import "sync"
// TODO - This currently only properly supports Go native types, due to the use of == for comparisons. Can this be updated to support custom types?
// MutexSlice implements a slice type with go routine safe accessors through mutex locks.
//
// Embeds sync.RWMutex.
type MutexSlice[V comparable] struct {
*sync.RWMutex
real []V
}
// Add adds a value to the slice
func (m *MutexSlice[V]) Add(value V) {
m.Lock()
defer m.Unlock()
m.real = append(m.real, value)
}
// Delete removes the first instance of the given value from the slice.
//
// Returns true if the value existed and was deleted, otherwise returns false.
func (m *MutexSlice[V]) Delete(value V) bool {
m.Lock()
defer m.Unlock()
for i, v := range m.real {
if v == value {
m.real = append(m.real[:i], m.real[i+1:]...)
return true
}
}
return false
}
// DeleteAll removes all instances of the given value from the slice.
//
// Returns true if the value existed and was deleted, otherwise returns false.
func (m *MutexSlice[V]) DeleteAll(value V) bool {
m.Lock()
defer m.Unlock()
newSlice := make([]V, 0)
oldLength := len(m.real)
for _, v := range m.real {
if v != value {
newSlice = append(newSlice, v)
}
}
m.real = newSlice
return len(newSlice) < oldLength
}
// Has checks if the slice contains the given value.
func (m *MutexSlice[V]) Has(value V) bool {
m.Lock()
defer m.Unlock()
for _, v := range m.real {
if v == value {
return true
}
}
return false
}
// GetIndex checks if the slice contains the given value and returns it's index.
//
// Returns -1 if the value does not exist in the slice.
func (m *MutexSlice[V]) GetIndex(value V) int {
m.Lock()
defer m.Unlock()
for i, v := range m.real {
if v == value {
return i
}
}
return -1
}
// At returns value at the given index.
//
// Returns a bool indicating if the value was found successfully.
func (m *MutexSlice[V]) At(index int) (V, bool) {
m.Lock()
defer m.Unlock()
if index >= len(m.real) {
return *new(V), false
}
return m.real[index], true
}
// Values returns the internal slice.
func (m *MutexSlice[V]) Values() []V {
m.Lock()
defer m.Unlock()
return m.real
}
// Size returns the length of the internal slice
func (m *MutexSlice[V]) Size() int {
m.RLock()
defer m.RUnlock()
return len(m.real)
}
// Each runs a callback function for every item in the slice.
//
// The slice cannot be modified inside the callback function.
//
// Returns true if the loop was terminated early.
func (m *MutexSlice[V]) Each(callback func(index int, value V) bool) bool {
m.RLock()
defer m.RUnlock()
for i, value := range m.real {
if callback(i, value) {
return true
}
}
return false
}
// Clear removes all items from the slice.
func (m *MutexSlice[V]) Clear() {
m.Lock()
defer m.Unlock()
m.real = make([]V, 0)
}
// NewMutexSlice returns a new instance of MutexSlice with the provided value type
func NewMutexSlice[V comparable]() *MutexSlice[V] {
return &MutexSlice[V]{
RWMutex: &sync.RWMutex{},
real: make([]V, 0),
}
}