forked from ef-ds/stack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testvaluestack.go
144 lines (130 loc) · 4.66 KB
/
testvaluestack.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
// Copyright (c) 2018 ef-ds
//
// 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.
// Package stack implements a very fast and efficient general purpose
// Last-In-First-Out (LIFO) stack data structure that is specifically
// optimized to perform when used by Microservices and serverless services
// running in production environments. This stack operates on *TestValueStack
// struct.
package stack
// TestValueStack implements an unbounded, dynamically growing Last-In-First-Out (LIFO)
// stack data structure.
// The zero value for stack is an empty stack ready to use.
type TestValueStack struct {
// Head points to the first node of the linked list.
head *testValueNode
// Tail points to the last node of the linked list.
// In an empty stack, head and tail points to the same node.
tail *testValueNode
// Len holds the current stack values length.
len int
}
// TestValue is used as the value added in each push call to the queues.
// A struct is being used as structs should be more representative of real
// world uses of a queue. A second f2 field was added as the users structs
// are likely to contain more than one field.
type TestValue struct {
F1 int
F2 int
}
// Node represents a stack node.
// Each node holds a slice of user managed values.
type testValueNode struct {
// v holds the list of user added values in this node.
v []*TestValue
// n points to the next node in the linked list.
n *testValueNode
// p points to the previous node in the linked list.
p *testValueNode
}
// NewTestValueStack returns an initialized TestValueStack.
func NewTestValueStack() *TestValueStack {
return new(TestValueStack)
}
// Init initializes or clears stack s.
func (s *TestValueStack) Init() *TestValueStack {
*s = TestValueStack{}
return s
}
// Len returns the number of elements of stack s.
// The complexity is O(1).
func (s *TestValueStack) Len() int { return s.len }
// Back returns the last element of stack d or nil if the stack is empty.
// The second, bool result indicates whether a valid value was returned;
// if the stack is empty, false will be returnes.
// The complexity is O(1).
func (s *TestValueStack) Back() (*TestValue, bool) {
if s.len == 0 {
return nil, false
}
return s.tail.v[len(s.tail.v)-1], true
}
// Push adds value v to the the back of the stack.
// The complexity is O(1).
func (s *TestValueStack) Push(v *TestValue) {
switch {
case s.head == nil:
// No nodes present yet.
h := &testValueNode{v: make([]*TestValue, 0, firstSliceSize)}
h.p = h
s.head = h
s.tail = h
case len(s.tail.v) < cap(s.tail.v):
// There's room in the tail slice.
case cap(s.tail.v) < maxInternalSliceSize:
// We're on the first slice and it hasn't grown large enough yet.
l := len(s.tail.v)
nv := make([]*TestValue, l, l*sliceGrowthFactor)
copy(nv, s.tail.v)
s.tail.v = nv
case s.tail.n != nil:
// There's at least one unused slice between head and tail nodes.
n := s.tail.n
s.tail = n
default:
// No available nodes, so make one.
n := &testValueNode{v: make([]*TestValue, 0, maxInternalSliceSize)}
n.p = s.tail
s.tail.n = n
s.tail = n
}
s.len++
s.tail.v = append(s.tail.v, v)
}
// Pop retrieves and removes the current element from the back of the stack.
// The second, bool result indicates whether a valid value was returned;
// if the stack is empty, false will be returnes.
// The complexity is O(1).
func (s *TestValueStack) Pop() (*TestValue, bool) {
if s.len == 0 {
return nil, false
}
s.len--
tp := len(s.tail.v) - 1
vp := &s.tail.v[tp]
v := *vp
*vp = nil // Avoid memory leaks
s.tail.v = s.tail.v[:tp]
if tp <= 0 {
// Move to the previous slice as all elements
// in the current one were removed.
s.tail = s.tail.p
}
return v, true
}