The jsonpath library for Go with low-memory footprint
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
886B

  1. package jsonpath
  2. type Results struct {
  3. nodes []*Result
  4. head int
  5. tail int
  6. count int
  7. }
  8. func newResults() *Results {
  9. return &Results{
  10. nodes: make([]*Result, 3),
  11. }
  12. }
  13. func (q *Results) push(n *Result) {
  14. if q.head == q.tail && q.count > 0 {
  15. nodes := make([]*Result, len(q.nodes)*2)
  16. copy(nodes, q.nodes[q.head:])
  17. copy(nodes[len(q.nodes)-q.head:], q.nodes[:q.head])
  18. q.head = 0
  19. q.tail = len(q.nodes)
  20. q.nodes = nodes
  21. }
  22. q.nodes[q.tail] = n
  23. q.tail = (q.tail + 1) % len(q.nodes)
  24. q.count++
  25. }
  26. func (q *Results) Pop() *Result {
  27. if q.count == 0 {
  28. return nil
  29. }
  30. node := q.nodes[q.head]
  31. q.head = (q.head + 1) % len(q.nodes)
  32. q.count--
  33. return node
  34. }
  35. func (q *Results) Peek() *Result {
  36. if q.count == 0 {
  37. return nil
  38. }
  39. return q.nodes[q.head]
  40. }
  41. func (q *Results) len() int {
  42. return q.count
  43. }
  44. func (q *Results) clear() {
  45. q.head = 0
  46. q.count = 0
  47. q.tail = 0
  48. }