jsonpath/queue.go

61 lines
886 B
Go
Raw Permalink Normal View History

2019-02-26 13:05:15 +04:00
package jsonpath
type Results struct {
nodes []*Result
head int
tail int
count int
}
func newResults() *Results {
return &Results{
nodes: make([]*Result, 3),
2019-02-26 13:05:15 +04:00
}
}
func (q *Results) push(n *Result) {
if q.head == q.tail && q.count > 0 {
2019-10-19 01:00:37 +04:00
nodes := make([]*Result, len(q.nodes)*2)
2019-02-26 13:05:15 +04:00
copy(nodes, q.nodes[q.head:])
copy(nodes[len(q.nodes)-q.head:], q.nodes[:q.head])
2019-10-19 02:14:19 +04:00
2019-02-26 13:05:15 +04:00
q.head = 0
q.tail = len(q.nodes)
q.nodes = nodes
}
2019-10-19 02:14:19 +04:00
2019-02-26 13:05:15 +04:00
q.nodes[q.tail] = n
q.tail = (q.tail + 1) % len(q.nodes)
q.count++
}
func (q *Results) Pop() *Result {
if q.count == 0 {
return nil
}
2019-10-19 02:14:19 +04:00
2019-02-26 13:05:15 +04:00
node := q.nodes[q.head]
q.head = (q.head + 1) % len(q.nodes)
q.count--
2019-10-19 02:14:19 +04:00
2019-02-26 13:05:15 +04:00
return node
}
2019-10-19 01:00:37 +04:00
func (q *Results) Peek() *Result {
2019-02-26 13:05:15 +04:00
if q.count == 0 {
return nil
}
2019-10-19 02:14:19 +04:00
2019-02-26 13:05:15 +04:00
return q.nodes[q.head]
}
func (q *Results) len() int {
return q.count
}
func (q *Results) clear() {
q.head = 0
q.count = 0
q.tail = 0
}