-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
133 lines (118 loc) · 2.8 KB
/
stack.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
// Copyright (c) 2024 The Go-CoreLibs Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package slices
import (
"sync"
)
// Stack is a concurrency-safe manager for a stack of generic items
type Stack[V interface{}] struct {
d []V // data
sync.RWMutex
}
// NewStack creates a new Stack instance
func NewStack[V interface{}](items ...V) *Stack[V] {
return &Stack[V]{
d: items,
}
}
// Len returns the size of the Stack
func (s *Stack[V]) Len() (size int) {
s.RLock()
defer s.RUnlock()
return len(s.d)
}
// First returns the first item in the Stack
func (s *Stack[V]) First() (item V, ok bool) {
s.RLock()
defer s.RUnlock()
if ok = len(s.d) > 0; ok {
item = s.d[0]
}
return
}
// Last returns the last item in the Stack
func (s *Stack[V]) Last() (item V, ok bool) {
s.RLock()
defer s.RUnlock()
last := len(s.d) - 1
if ok = last > -1; ok {
item = s.d[last]
}
return
}
// Get returns the item at the index given
func (s *Stack[V]) Get(idx int) (item V, ok bool) {
s.RLock()
defer s.RUnlock()
if ok = s.Valid(idx); ok {
item = s.d[idx]
}
return
}
// Set overwrites the item at the index given
func (s *Stack[V]) Set(idx int, item V) (ok bool) {
s.Lock()
defer s.Unlock()
if ok = idx > -1 && idx < len(s.d); ok {
s.d[idx] = item
}
return
}
// Push appends the given item to the end of the Stack
func (s *Stack[V]) Push(item V) {
s.Lock()
defer s.Unlock()
s.d = append(s.d, item)
}
// Pop removes and returns the last item in the Stack
func (s *Stack[V]) Pop() (item V, ok bool) {
s.Lock()
defer s.Unlock()
last := len(s.d) - 1
if ok = last > -1; ok {
item = s.d[last]
s.d = s.d[:last]
}
return
}
// Shift prepends the given item to the start of the Stack
func (s *Stack[V]) Shift(item V) {
s.Lock()
defer s.Unlock()
s.d = append([]V{item}, s.d...)
}
// Unshift removes and returns the first item in the Stack
func (s *Stack[V]) Unshift() (item V, ok bool) {
s.Lock()
defer s.Unlock()
if ok = len(s.d) > 0; ok {
item = s.d[0]
s.d = s.d[1:]
}
return
}
// Slice returns the stack as a slice
func (s *Stack[V]) Slice() (items []V) {
s.RLock()
defer s.RUnlock()
items = s.d[:] // new slice?
return
}
// Valid returns true if the given index is within bounds
func (s *Stack[V]) Valid(idx int) (valid bool) {
s.RLock()
defer s.RUnlock()
valid = idx > -1 && idx < len(s.d)
return
}