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