-
Notifications
You must be signed in to change notification settings - Fork 3
/
slice.go
150 lines (137 loc) · 3.14 KB
/
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
package threadsafe
import (
"reflect"
"sync"
)
// Slice represents a thread-safe slice.
// It uses a mutex to ensure that all operations are thread-safe.
type Slice[T any] struct {
data []T
mu sync.RWMutex
}
// NewSlice creates a new thread-safe slice.
// Example:
//
// slice := threadsafe.NewSlice[int]()
func NewSlice[T any]() *Slice[T] {
return &Slice[T]{data: []T{}}
}
// Append appends a value to the slice.
// Example:
//
// slice.Append(10)
func (s *Slice[T]) Append(value T) {
s.mu.Lock()
defer s.mu.Unlock()
s.data = append(s.data, value)
}
// Get retrieves the value at the given index.
// It returns the value and a boolean indicating whether the index was valid.
// Example:
//
// value, ok := slice.Get(2)
func (s *Slice[T]) Get(index int) (T, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
if index < 0 || index >= len(s.data) {
var zero T
return zero, false
}
return s.data[index], true
}
// Set sets the value at the given index.
// It returns a boolean indicating whether the operation was successful.
// Example:
//
// ok := slice.Set(2, 100)
func (s *Slice[T]) Set(index int, value T) bool {
s.mu.Lock()
defer s.mu.Unlock()
if index < 0 || index >= len(s.data) {
return false
}
s.data[index] = value
return true
}
// Length returns the length of the slice.
// Example:
//
// length := slice.Length()
func (s *Slice[T]) Length() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.data)
}
// Values returns a copy of the slice's data as a regular slice.
// Example:
//
// values := slice.Values()
func (s *Slice[T]) Values() []T {
s.mu.RLock()
defer s.mu.RUnlock()
dataCopy := make([]T, len(s.data))
copy(dataCopy, s.data)
return dataCopy
}
// Remove removes the element at the given index.
// It returns a boolean indicating whether the operation was successful.
// Example:
//
// ok := slice.Remove(2)
func (s *Slice[T]) Remove(index int) bool {
s.mu.Lock()
defer s.mu.Unlock()
if index < 0 || index >= len(s.data) {
return false
}
s.data = append(s.data[:index], s.data[index+1:]...)
return true
}
// Contains checks if the slice contains the specified value.
// Example:
//
// contains := slice.Contains(10)
func (s *Slice[T]) Contains(value T) bool {
s.mu.RLock()
defer s.mu.RUnlock()
for _, v := range s.data {
if reflect.DeepEqual(v, value) {
return true
}
}
return false
}
// Clear removes all elements from the slice.
// Example:
//
// slice.Clear()
func (s *Slice[T]) Clear() {
s.mu.Lock()
defer s.mu.Unlock()
s.data = []T{}
}
// Insert inserts a value at the specified index.
// It returns a boolean indicating whether the operation was successful.
// Example:
//
// ok := slice.Insert(2, 10)
func (s *Slice[T]) Insert(index int, value T) bool {
s.mu.Lock()
defer s.mu.Unlock()
if index < 0 || index > len(s.data) {
return false
}
s.data = append(s.data[:index], append([]T{value}, s.data[index:]...)...)
return true
}
// Copy returns a new thread-safe slice that is a copy of the current slice.
// Example:
//
// copySlice := slice.Copy()
func (s *Slice[T]) Copy() *Slice[T] {
s.mu.RLock()
defer s.mu.RUnlock()
dataCopy := make([]T, len(s.data))
copy(dataCopy, s.data)
return &Slice[T]{data: dataCopy}
}