Documentation
¶
Index ¶
- type QS
- func (s *QS[T]) Copy() *QS[T]
- func (s *QS[T]) Current() T
- func (s *QS[T]) Items() []T
- func (s *QS[T]) JSON() ([]byte, error)
- func (s QS[T]) Log()
- func (s QS[T]) MarshalJSON() ([]byte, error)
- func (s *QS[T]) Peek() T
- func (s *QS[T]) Pop() T
- func (s *QS[T]) Print()
- func (s *QS[T]) Push(these ...T)
- func (s *QS[T]) Scan() bool
- func (s *QS[T]) Shift() T
- func (s QS[T]) String() string
- func (s *QS[T]) Unshift(these ...T)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type QS ¶
QS ("queue stack") implements a combination of the traditional queue and stack data structures using a linked list with Len field for high performance. Sometimes you want more than what standard slices provide, for example, when needing to prepend to a slice, which is very inefficient using standard Go slices.
Example (Invalid_JSON_Types) ¶
package main
import (
"log"
"os"
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
defer log.SetOutput(os.Stderr)
defer log.SetFlags(log.Flags())
log.SetOutput(os.Stdout)
log.SetFlags(0)
// QS can be used to store any type,
// but log an error (no panic) when
// attempting to use the stack item
// in a string context.
s := qstack.New[any]()
s.Push(func() {})
s.Print()
}
Output: json: unsupported type: func()
Example (References) ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
type Some struct {
Val string
}
s := qstack.New[*Some]()
// just one
x := &Some{"some"}
s.Push(x)
y := s.Peek()
// log.Printf("Is %p == %p?", x, y)
fmt.Println(x == y)
// two
xx := &Some{"other"}
s.Push(xx)
yy := s.Peek()
// log.Printf("Is %p == %p?", xx, yy)
fmt.Println(xx == yy)
}
Output: true true
func Fields ¶
Fields is exactly the same as strings.Fields (returning strings separated by unicode.IsSpace) except it returns a qstack.QS instead.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
text := `
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
`
fields := qstack.Fields(text)
fmt.Println(fields.Len)
fmt.Println(fields.Items()[0])
fmt.Println(fields.Items()[34])
fmt.Println(fields.Items()[68])
}
Output: 69 Lorem commodo laborum.
Example (Scan) ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
fields := qstack.Fields("some thing")
fmt.Println(fields)
for fields.Scan() {
fmt.Print(fields.Current())
}
}
Output: ["some","thing"] something
func (*QS[T]) Copy ¶
Copy returns a duplicate of the qstack and its relations. Values are copied using simple assignment. Copy is useful for preserving state in order to revert or to allow independent processing with concurrency on individual copies. Note that QS[<ref>] types will not produce deep copies of values.
Example ¶
package main
import (
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
s := qstack.New[any]()
s.Push("some")
s.Push("thing")
c := s.Copy()
c.Pop()
s.Print()
c.Print()
}
Output: ["some","thing"] ["some"]
func (*QS[T]) Items ¶
func (s *QS[T]) Items() []T
Items returns the items of the stack as a slice with newest items on the next. To apply an iterator function over the items consider using the rwxrob/bonzai/fn package with Map/Filter/Reduce/Each functions.
Example ¶
package main
import (
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
s := qstack.New[any]()
s.Push(1)
s.Push(true)
s.Push("foo")
s.Push(map[string]int{"ten": 10, "twenty": 20})
s.Print()
}
Output: [1,true,"foo",{"ten":10,"twenty":20}]
func (QS[T]) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler and it needed to fulfill the list of nodes since they are internally stored as a linked list.
func (*QS[T]) Peek ¶
func (s *QS[T]) Peek() T
Peek (stack) returns the current top value of the stack. Prefer Len to check for emptiness.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
s := qstack.New[any]()
s.Print()
s.Push("it")
s.Print()
fmt.Println(s.Peek())
}
Output: [] ["it"] it
func (*QS[T]) Pop ¶
func (s *QS[T]) Pop() T
Pop removes most recently pushed item from top of stack and returns it.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
s := qstack.New[any]()
s.Print()
p := s.Pop()
fmt.Println(p)
s.Push("it")
s.Push("again")
s.Print()
fmt.Println(s.Len)
p = s.Pop()
s.Print()
fmt.Println(p)
fmt.Println(s.Len)
}
Output: [] <nil> ["it","again"] 2 ["it"] again 1
func (*QS[T]) Push ¶
func (s *QS[T]) Push(these ...T)
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
s := qstack.New[any]()
fmt.Println(s.Len)
s.Print()
s.Push("one")
fmt.Println(s.Len)
s.Print()
s.Push("two")
fmt.Println(s.Len)
s.Print()
}
Output: 0 [] 1 ["one"] 2 ["one","two"]
func (*QS[T]) Scan ¶
Scan advances to the next item each time it is called returning false when there are no more items. Use Current to retrieve the value of the current item.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
s := qstack.New[any]()
s.Push("foo")
s.Push("bar")
s.Push("and")
s.Push("one")
for s.Scan() {
fmt.Print(s.Current())
}
}
Output: foobarandone
func (*QS[T]) Shift ¶
func (s *QS[T]) Shift() T
Shift removes an item from the bottom of the stack and returns it.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
s := qstack.New[any]()
s.Push(1)
s.Push(true)
s.Push("foo")
v := s.Shift()
fmt.Println(v, s.Len)
s.Print()
v = s.Shift()
fmt.Println(v, s.Len)
s.Print()
v = s.Shift()
fmt.Println(v, s.Len)
s.Print()
}
Output: 1 2 [true,"foo"] true 1 ["foo"] foo 0 []
func (*QS[T]) Unshift ¶
func (s *QS[T]) Unshift(these ...T)
Unshift adds items to the bottom of the stack.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai/ds/qstack"
)
func main() {
s := qstack.New[any]()
s.Push(1)
s.Push(true)
s.Push("foo")
s.Print()
fmt.Println(s.Len)
s.Unshift(0, 34, 2)
s.Print()
fmt.Println(s.Len)
}
Output: [1,true,"foo"] 3 [0,34,2,1,true,"foo"] 6