|
| 1 | +# Go Stack |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +[中文文档](./README.md) |
| 6 | + |
| 7 | +[English Document](./README_en.md) |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +# 1. What is this? Why choose it? What are the advantages? |
| 12 | +This project is a Go language implementation version of various types of stacks. |
| 13 | + |
| 14 | +- It supports generics, makes the API more convenient to use, and avoids type forced conversion |
| 15 | +- A thread safe version is provided for each type of stack. When using it, you can focus on the business and don't need to think about locks anymore |
| 16 | +- The supported stack types are more abundant. If there are more interesting stacks, please mention the issues call me implementation |
| 17 | + |
| 18 | +# 2. Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +go get github.com/CC11001100/go-stack |
| 22 | +``` |
| 23 | +# 3. Currently implemented stack |
| 24 | + |
| 25 | +- ArrayStack[T any] |
| 26 | +- SyncArrayStack[T any] |
| 27 | +- LinkedStack[T any] |
| 28 | +- SyncLinkedStack[T any] |
| 29 | +- MinStack[T any] |
| 30 | +- SyncMinStack[T any] |
| 31 | +- MaxStack[T any] |
| 32 | +- SyncMaxStack [T any] |
| 33 | + |
| 34 | +## Support Stack: |
| 35 | + |
| 36 | +|Struct name | Thread safety | Blocking | Special features| |
| 37 | +| ---------------------- | :--------: | :----: | ------------------ | |
| 38 | +| ArrayStack[T any] | × | × | | |
| 39 | +| SyncArrayStack[T any] | √ | × | | |
| 40 | +| LinkedStack[T any] | × | × | | |
| 41 | +| SyncLinkedStack[T any] | √ | × | | |
| 42 | +| MinStack[T any] | × | × | O (1) Get the minimum value in the stack| |
| 43 | +| SyncMinStack[T any] | √ | × | O (1) Get the minimum value in the stack| |
| 44 | +| MaxStack[T any] | × | × | O (1) Get the maximum value in the stack| |
| 45 | +| SyncMaxStack [T any] | √ | × | O (1) Get the maximum value in the stack| |
| 46 | +# 4. Interface: Stack |
| 47 | + The interface 'Stack [Any]' defines some APIs that all stacks have. |
| 48 | + |
| 49 | +## Stack |
| 50 | + |
| 51 | +``` |
| 52 | +Push(values ...T) |
| 53 | +``` |
| 54 | +Example: |
| 55 | +```go |
| 56 | +stack := NewArrayStack[int]() |
| 57 | +stack.Push(1) |
| 58 | +``` |
| 59 | +### Take Top of Stack Elements |
| 60 | + |
| 61 | +``` |
| 62 | +Pop() T |
| 63 | +PopE() (T, error) |
| 64 | +``` |
| 65 | +Pop Example: |
| 66 | +```go |
| 67 | +type User struct { |
| 68 | +} |
| 69 | +stack := NewArrayStack[*User]() |
| 70 | +fmt.Println(stack.Pop()) |
| 71 | +u := &User{} |
| 72 | +stack.Push(u) |
| 73 | +fmt.Println(stack.Pop()) |
| 74 | +// Output: |
| 75 | +// <nil> |
| 76 | +// &{} |
| 77 | +``` |
| 78 | +PopE Example: |
| 79 | +```go |
| 80 | +stack := NewArrayStack[int]() |
| 81 | +element, err := stack.PopE() |
| 82 | +if errors. Is(err, ErrStackEmpty) { |
| 83 | +fmt. Println("stack empty!") |
| 84 | +} |
| 85 | +stack.Push(1) |
| 86 | +element, err = stack.PopE() |
| 87 | +if err != nil { |
| 88 | +fmt.Println(err.Error()) |
| 89 | +return |
| 90 | +} |
| 91 | +fmt.Println(element) |
| 92 | +// Output: |
| 93 | +// stack empty! |
| 94 | +// 1 |
| 95 | +``` |
| 96 | +### View Top of Stack Elements |
| 97 | + |
| 98 | +``` |
| 99 | +Peek() T |
| 100 | +PeekE() (T, error) |
| 101 | +``` |
| 102 | +Peek Example: |
| 103 | +```go |
| 104 | +type User struct { |
| 105 | +} |
| 106 | +stack := NewArrayStack[*User]() |
| 107 | +fmt.Println(stack.Peek()) |
| 108 | +u := &User{} |
| 109 | +stack.Push(u) |
| 110 | +fmt.Println(stack.Peek()) |
| 111 | +// Output: |
| 112 | +// <nil> |
| 113 | +// &{} |
| 114 | +``` |
| 115 | +PeekE Example: |
| 116 | +```go |
| 117 | +stack := NewArrayStack[int]() |
| 118 | +element, err := stack.PeekE() |
| 119 | +if errors. Is(err, ErrStackEmpty) { |
| 120 | +fmt. Println("stack empty!") |
| 121 | +} |
| 122 | +stack.Push(1) |
| 123 | +element, err = stack.PeekE() |
| 124 | +if err != nil { |
| 125 | +fmt.Println(err.Error()) |
| 126 | +return |
| 127 | +} |
| 128 | +fmt.Println(element) |
| 129 | +// Output: |
| 130 | +// stack empty! |
| 131 | +// 1 |
| 132 | +``` |
| 133 | +### Determine whether the stack is empty? |
| 134 | + |
| 135 | +``` |
| 136 | +IsEmpty() bool |
| 137 | +IsNotEmpty() bool |
| 138 | +``` |
| 139 | +IsEmpty Example: |
| 140 | +```go |
| 141 | +stack := NewArrayStack[int]() |
| 142 | +fmt.Println(stack.IsEmpty()) |
| 143 | +stack.Push(1) |
| 144 | +fmt.Println(stack.IsEmpty()) |
| 145 | +// Output: |
| 146 | +// true |
| 147 | +// false |
| 148 | +``` |
| 149 | +IsNotEmpty Example: |
| 150 | +```go |
| 151 | +stack := NewArrayStack[int]() |
| 152 | +fmt.Println(stack.IsNotEmpty()) |
| 153 | +stack.Push(1) |
| 154 | +fmt.Println(stack.IsNotEmpty()) |
| 155 | +// Output: |
| 156 | +// false |
| 157 | +// true |
| 158 | +``` |
| 159 | +### Number of elements in the stack? |
| 160 | + |
| 161 | +``` |
| 162 | +Size() int |
| 163 | +``` |
| 164 | +Example: |
| 165 | +```go |
| 166 | +stack := NewArrayStack[int]() |
| 167 | +stack.Push(1) |
| 168 | +fmt.Println(stack.Size()) |
| 169 | +// Output: |
| 170 | +// 1 |
| 171 | +``` |
| 172 | +### reset stack, clear all element! |
| 173 | + |
| 174 | +``` |
| 175 | +Clear() |
| 176 | +``` |
| 177 | +Example: |
| 178 | +```go |
| 179 | +stack := NewArrayStack[int]() |
| 180 | +stack.Push(1) |
| 181 | +fmt.Println(stack.Size()) |
| 182 | +stack.Clear() |
| 183 | +fmt.Println(stack.Size()) |
| 184 | +// Output: |
| 185 | +// 1 |
| 186 | +// 0 |
| 187 | +``` |
| 188 | +### Stack to String |
| 189 | +Convert the stack to String, which is generally used to visualize all elements in the stack, such as debug. |
| 190 | +Example: |
| 191 | +```go |
| 192 | +stack := NewArrayStack[int]() |
| 193 | +stack.Push(1) |
| 194 | +fmt.Println(stack.String()) |
| 195 | +// Output: |
| 196 | +// [1] |
| 197 | +``` |
| 198 | +# ArrayStack |
| 199 | + |
| 200 | +Stack based on array implementation. |
| 201 | +Example: |
| 202 | + |
| 203 | +```go |
| 204 | +stack := NewArrayStack[int]() |
| 205 | +fmt.Println(stack.String()) |
| 206 | +// Output: |
| 207 | +// [] |
| 208 | +``` |
| 209 | +# LinkedStack |
| 210 | +Stack based on linked list. |
| 211 | +Example: |
| 212 | + |
| 213 | +```go |
| 214 | +stack := NewLinkedStack[int]() |
| 215 | +fmt.Println(stack.String()) |
| 216 | +// Output: |
| 217 | +// [] |
| 218 | +``` |
| 219 | +# 5. Maximum stack & minimum stack |
| 220 | + |
| 221 | +## MinStack & SyncMinStack |
| 222 | +Compared with the Stack interface, two methods are added for O (1) to obtain the minimum value of all elements in the stack: |
| 223 | +```go |
| 224 | +GetMin() T |
| 225 | +GetMinE() (T, error) |
| 226 | +``` |
| 227 | +GetMin Example: |
| 228 | +```go |
| 229 | +stack := NewSyncMinStack[int](func(a, b int) int { return a - b }) |
| 230 | +_, err := stack.GetMinE() |
| 231 | +assert. ErrorIs(t, err, ErrStackEmpty) |
| 232 | +stack.Push(10) |
| 233 | +stack.Push(7) |
| 234 | +stack.Push(9) |
| 235 | +element, err := stack.GetMinE() |
| 236 | +assert. Nil(t, err) |
| 237 | +assert. Equal(t, 7, element) |
| 238 | +``` |
| 239 | +GetMinE Example: |
| 240 | +```go |
| 241 | +stack := NewSyncMinStack[int](func(a, b int) int { return a - b }) |
| 242 | +_, err := stack.GetMinE() |
| 243 | +if errors. Is(err, ErrStackEmpty) { |
| 244 | +fmt. Println("stack empty!") |
| 245 | +} |
| 246 | +stack.Push(10) |
| 247 | +stack.Push(7) |
| 248 | +stack.Push(9) |
| 249 | +element, err := stack.GetMinE() |
| 250 | +if err != nil { |
| 251 | +fmt.Println(err.Error()) |
| 252 | +return |
| 253 | +} |
| 254 | +fmt.Println(element) |
| 255 | +// Output: |
| 256 | +// stack empty! |
| 257 | +// 7 |
| 258 | +``` |
| 259 | +## MaxStack & SyncMaxStack |
| 260 | +Compared with the Stack interface, two methods are added for O (1) to obtain the minimum value of all elements in the stack: |
| 261 | +```go |
| 262 | +GetMax() T |
| 263 | +GetMaxE() (T, error) |
| 264 | +``` |
| 265 | +GetMax Example: |
| 266 | +```go |
| 267 | +stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 268 | +_, err := stack.GetMaxE() |
| 269 | +assert. ErrorIs(t, err, ErrStackEmpty) |
| 270 | +stack.Push(10) |
| 271 | +stack.Push(7) |
| 272 | +stack.Push(9) |
| 273 | +element, err := stack.GetMaxE() |
| 274 | +assert. Nil(t, err) |
| 275 | +assert. Equal(t, 10, element) |
| 276 | +``` |
| 277 | +GetMaxE Example: |
| 278 | +```go |
| 279 | +stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 280 | +_, err := stack.GetMaxE() |
| 281 | +if errors. Is(err, ErrStackEmpty) { |
| 282 | +fmt. Println("stack empty!") |
| 283 | +} |
| 284 | +stack.Push(10) |
| 285 | +stack.Push(7) |
| 286 | +stack.Push(9) |
| 287 | +element, err := stack.GetMaxE() |
| 288 | +if err != nil { |
| 289 | +fmt.Println(err.Error()) |
| 290 | +return |
| 291 | +} |
| 292 | +fmt.Println(element) |
| 293 | +// Output: |
| 294 | +// stack empty! |
| 295 | +// 10 |
| 296 | +``` |
| 297 | +# TODO |
| 298 | +Implement the blocking stack, because only multiple coroutine operations |
0 commit comments