-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructures.go
More file actions
161 lines (131 loc) · 2.58 KB
/
Copy pathstructures.go
File metadata and controls
161 lines (131 loc) · 2.58 KB
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
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"errors"
"slices"
"sync"
)
type Node struct {
Value string
Parent *Node
}
// Concurrency Queue Implementation
type CQueue struct {
items []Node
lock sync.Mutex
}
func (q *CQueue) Add(item Node) {
q.lock.Lock()
defer q.lock.Unlock()
q.items = append(q.items, item)
}
func (q *CQueue) AddAll(items ...Node) {
q.lock.Lock()
defer q.lock.Unlock()
q.items = append(q.items, items...)
}
func (q *CQueue) Pop() Node {
q.lock.Lock()
defer q.lock.Unlock()
if len(q.items) == 0 {
return Node{}
}
item := q.items[0]
q.items = q.items[1:]
return item
}
func (q *CQueue) IsEmpty() bool {
q.lock.Lock()
defer q.lock.Unlock()
return len(q.items) == 0
}
// Stack Implementation
type Stack struct {
items []string
}
func (s *Stack) Push(items ...string) {
slices.Reverse(items)
s.items = append(s.items, items...)
}
func (s *Stack) Pop() (string, error) {
if len(s.items) == 0 {
return "", errors.New("Stack is empty")
}
index := len(s.items) - 1
item := s.items[index]
s.items = s.items[:index]
return item, nil
}
func (s *Stack) IsEmpty() bool {
return len(s.items) == 0
}
// Set Implementation
type Set map[string]struct{}
func (s Set) Add(item string) {
s[item] = struct{}{}
}
func (s Set) IsIn(item string) bool {
_, exists := s[item]
return exists
}
func (s Set) Delete(item string) {
delete(s, item)
}
func (s Set) ToList() []string {
var slice []string
for key := range s {
slice = append(slice, key)
}
return slice
}
// Queue Implementation
type Queue struct {
elements []string
}
func (q *Queue) Add(elements ...string) {
q.elements = append(q.elements, elements...)
}
func (q *Queue) Pop() string {
element := q.elements[0]
q.elements = q.elements[1:]
return element
}
func (q *Queue) Peek() string {
element := q.elements[0]
return element
}
func (q *Queue) IsEmpty() bool {
return len(q.elements) == 0
}
func (q *Queue) Size() int {
return len(q.elements)
}
// // Concurrency Queue Implementation
// type CQueue struct {
// items []string
// lock sync.Mutex
// }
// func (q *CQueue) Add(item string) {
// q.lock.Lock()
// defer q.lock.Unlock()
// q.items = append(q.items, item)
// }
// func (q *CQueue) AddAll(items ...string) {
// q.lock.Lock()
// defer q.lock.Unlock()
// q.items = append(q.items, items...)
// }
// func (q *CQueue) Pop() string {
// q.lock.Lock()
// defer q.lock.Unlock()
// if len(q.items) == 0 {
// return ""
// }
// item := q.items[0]
// q.items = q.items[1:]
// return item
// }
// func (q *CQueue) IsEmpty() bool {
// q.lock.Lock()
// defer q.lock.Unlock()
// return len(q.items) == 0
// }