-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
414 lines (375 loc) · 10.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Package stack implements a persistent stack.
package stack // import "jsouthworth.net/go/immutable/stack"
import (
"errors"
"fmt"
"reflect"
"strings"
"jsouthworth.net/go/dyn"
"jsouthworth.net/go/immutable/vector"
"jsouthworth.net/go/seq"
)
var errRangeSig = errors.New("Range requires a function: func(v vT) bool or func(v vT)")
var errReduceSig = errors.New("Reduce requires a function: func(init iT, v vT) oT")
// Stack is a persistent stack.
type Stack struct {
backingVector *vector.Vector
}
var empty = Stack{
backingVector: vector.Empty(),
}
// Empty returns the empty stack.
func Empty() *Stack {
return &empty
}
// New converts as list of elements to a persistent stack.
func New(elems ...interface{}) *Stack {
out := Empty().AsTransient()
for _, elem := range elems {
out = out.Push(elem)
}
return out.AsPersistent()
}
// From will convert many go types to an immutable map.
// Converting some types is more efficient than others and the
// mechanisms are described below.
//
// *Stack:
// Used directly as it is already immutable.
// *TStack:
// AsPersistent is called on the value and the result used for the stack.
// *Vector:
// Used directly as the stack as it is already immutable.
// *TVector:
// AsPersistent is called on the value and the result used for the stack.
// []interface{}:
// New is called with the elements.
// seq.Sequable:
// Seq is called on the value and the stack is built from the resulting sequence.
// seq.Sequence:
// The stack is built from the sequence. Care should be taken to provide finite sequences or the vector will grow without bound.
// []T:
// The slice is converted to a vector using reflection.
func From(value interface{}) *Stack {
switch v := value.(type) {
case *Stack:
return v
case *TStack:
return v.AsPersistent()
default:
vec := vector.From(value)
if vec.Length() == 0 {
return Empty()
}
return &Stack{
backingVector: vec,
}
}
}
// Push returns a new stack with the element as the top of the stack.
func (s *Stack) Push(elem interface{}) *Stack {
return &Stack{
backingVector: s.backingVector.Append(elem),
}
}
// Conj returns a new stack with the element as the top of the stack.
// Conj implements a generic mechanism for building collections.
func (s *Stack) Conj(elem interface{}) interface{} {
return s.Push(elem)
}
// Pop returns a new stack without the top element
func (s *Stack) Pop() *Stack {
v := s.backingVector.Pop()
if v.Length() == 0 {
return Empty()
}
return &Stack{
backingVector: v,
}
}
// Top returns the top of the stack
func (s *Stack) Top() interface{} {
return s.backingVector.At(s.backingVector.Length() - 1)
}
// Find whether the value exists in the stack by walking every value.
// Returns the value and whether or not it was found.
func (s *Stack) Find(value interface{}) (interface{}, bool) {
var out interface{}
var found bool
s.Range(func(v interface{}) bool {
if v == value {
out = v
found = true
return false
}
return true
})
return out, found
}
// Length returns the number of elements in the stack.
func (s *Stack) Length() int {
return s.backingVector.Length()
}
// Reverse returns the elements of the stack in FIFO order as a vector.
func (s *Stack) Reverse() *vector.Vector {
return s.backingVector
}
// AsTransient will return a mutable copy on write version of the stack.
func (s *Stack) AsTransient() *TStack {
return &TStack{
backingVector: s.backingVector.AsTransient(),
}
}
// Range calls the passed in function on each element of the stack.
// The function passed in may be of many types:
//
// func(value interface{}) bool:
// Takes a value of any type and returns if the loop should continue.
// Useful to avoid reflection where not needed and to support
// heterogenous stacks.
// func(value interface{})
// Takes a value of any type.
// Useful to avoid reflection where not needed and to support
// heterogenous stacks.
// func(value T) bool:
// Takes a value of the type of element stored in the stack and
// returns if the loop should continue. Useful for homogeneous stacks.
// Is called with reflection and will panic if the type is incorrect.
// func(value T)
// Takes a value of the type of element stored in the stack and
// returns if the loop should continue. Useful for homogeneous stacks.
// Is called with reflection and will panic if the type is incorrect.
// Range will panic if passed anything that doesn't match one of these signatures
func (s *Stack) Range(do interface{}) {
cont := true
fn := genRangeFunc(do)
for stack := s; stack != Empty() && cont; stack = stack.Pop() {
value := stack.Top()
cont = fn(value)
}
}
func genRangeFunc(do interface{}) func(value interface{}) bool {
switch fn := do.(type) {
case func(value interface{}) bool:
return fn
case func(value interface{}):
return func(value interface{}) bool {
fn(value)
return true
}
default:
rv := reflect.ValueOf(do)
if rv.Kind() != reflect.Func {
panic(errRangeSig)
}
rt := rv.Type()
if rt.NumIn() != 1 || rt.NumOut() > 1 {
panic(errRangeSig)
}
if rt.NumOut() == 1 &&
rt.Out(0).Kind() != reflect.Bool {
panic(errRangeSig)
}
return func(value interface{}) bool {
out := dyn.Apply(do, value)
if out != nil {
return out.(bool)
}
return true
}
}
}
// Reduce is a fast mechanism for reducing a Stack. Reduce can take
// the following types as the fn:
//
// func(init interface{}, value interface{}) interface{}
// func(init iT, v vT) oT
//
// Reduce will panic if given any other function type.
func (s *Stack) Reduce(fn interface{}, init interface{}) interface{} {
res := init
rFn := genReduceFunc(fn)
for i := s.backingVector.Length() - 1; i >= 0; i-- {
res = rFn(res, s.backingVector.At(i))
}
return res
}
func genReduceFunc(fn interface{}) func(r, v interface{}) interface{} {
switch f := fn.(type) {
case func(res, val interface{}) interface{}:
return func(r, v interface{}) interface{} {
return f(r, v)
}
default:
rv := reflect.ValueOf(fn)
if rv.Kind() != reflect.Func {
panic(errReduceSig)
}
rt := rv.Type()
if rt.NumIn() != 2 {
panic(errReduceSig)
}
if rt.NumOut() != 1 {
panic(errReduceSig)
}
return func(r, v interface{}) interface{} {
return dyn.Apply(f, r, v)
}
}
}
// Seq returns a representation of the stack as a sequence
// corresponding to the elements of the stack.
func (s *Stack) Seq() seq.Sequence {
return &stackSequence{
stack: s,
}
}
// String returns a representation of the stack as a string.
func (s *Stack) String() string {
b := new(strings.Builder)
fmt.Fprint(b, "[ ")
s.Range(func(item interface{}) {
fmt.Fprintf(b, "%v ", item)
})
fmt.Fprint(b, "]")
return b.String()
}
// Transform takes a set of actions and performs them
// on the persistent stack. It does this by making a transient
// stack and calling each action on it, then converting it back
// to a persistent vector.
func (s *Stack) Transform(actions ...func(*TStack) *TStack) *Stack {
out := s.AsTransient()
for _, action := range actions {
out = action(out)
}
return out.AsPersistent()
}
// Equal tests if two sets are Equal by comparing the entries of each.
// Equal implements the Equaler which allows for deep
// comparisons.
func (s *Stack) Equal(o interface{}) bool {
other, ok := o.(*Stack)
if !ok {
return ok
}
return s.backingVector.Equal(other.backingVector)
}
type stackSequence struct {
stack *Stack
}
func (s *stackSequence) First() interface{} {
return s.stack.Top()
}
func (s *stackSequence) Next() seq.Sequence {
new := s.stack.Pop()
if new.backingVector.Length() == 0 {
return nil
}
return &stackSequence{
stack: new,
}
}
func (s *stackSequence) String() string {
return seq.ConvertToString(s)
}
type TStack struct {
backingVector *vector.TVector
}
// Push places an element at the top of the stack. s is returned
func (s *TStack) Push(elem interface{}) *TStack {
s.backingVector = s.backingVector.Append(elem)
return s
}
// Pop removes the top element of the stack. s is returned.
func (s *TStack) Pop() *TStack {
s.backingVector = s.backingVector.Pop()
return s
}
// Top returns the top element of the stack.
func (s *TStack) Top() interface{} {
return s.backingVector.At(s.backingVector.Length() - 1)
}
// Find whether the value exists in the stack by walking every value.
// Returns the value and whether or not it was found.
func (s *TStack) Find(value interface{}) (interface{}, bool) {
var out interface{}
var found bool
s.Range(func(v interface{}) bool {
if v == value {
out = v
found = true
return false
}
return true
})
return out, found
}
// AsPersistent returns the an immutable version of the stack. Any
// transient operations performed after this will cause a panic.
func (s *TStack) AsPersistent() *Stack {
v := s.backingVector.AsPersistent()
if v.Length() == 0 {
return Empty()
}
return &Stack{
backingVector: v,
}
}
// Range calls the passed in function on each element of the stack.
// The function passed in may be of many types:
//
// func(value interface{}) bool:
// Takes a value of any type and returns if the loop should continue.
// Useful to avoid reflection where not needed and to support
// heterogenous stacks.
// func(value interface{})
// Takes a value of any type.
// Useful to avoid reflection where not needed and to support
// heterogenous stacks.
// func(value T) bool:
// Takes a value of the type of element stored in the stack and
// returns if the loop should continue. Useful for homogeneous stacks.
// Is called with reflection and will panic if the type is incorrect.
// func(value T)
// Takes a value of the type of element stored in the stack and
// returns if the loop should continue. Useful for homogeneous stacks.
// Is called with reflection and will panic if the type is incorrect.
// Range will panic if passed anything that doesn't match one of these signatures
func (s *TStack) Range(do interface{}) {
cont := true
fn := genRangeFunc(do)
for i := s.backingVector.Length() - 1; i >= 0 && cont; i-- {
value := s.backingVector.At(i)
cont = fn(value)
}
}
// Reduce is a fast mechanism for reducing a Stack. Reduce can take
// the following types as the fn:
//
// func(init interface{}, value interface{}) interface{}
// func(init iT, v vT) oT
//
// Reduce will panic if given any other function type.
func (s *TStack) Reduce(fn interface{}, init interface{}) interface{} {
res := init
rFn := genReduceFunc(fn)
for i := s.backingVector.Length() - 1; i >= 0; i-- {
res = rFn(res, s.backingVector.At(i))
}
return res
}
// Length returns the number of elements in the stack.
func (s *TStack) Length() int {
return s.backingVector.Length()
}
// String returns a representation of the stack as a string.
func (s *TStack) String() string {
b := new(strings.Builder)
fmt.Fprint(b, "[ ")
s.Range(func(item interface{}) {
fmt.Fprintf(b, "%v ", item)
})
fmt.Fprint(b, "]")
return b.String()
}