low_level.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package abstract
  2. // LowLevelIterator is exposed to developers within this module for use
  3. // implemented augmented search functionality.
  4. type LowLevelIterator[K, V, A any] Iterator[K, V, A]
  5. // LowLevel converts an iterator to a LowLevelIterator. Given this package
  6. // is internal, callers outside of this module cannot construct a
  7. // LowLevelIterator.
  8. func LowLevel[K, V, A any](
  9. it *Iterator[K, V, A],
  10. ) *LowLevelIterator[K, V, A] {
  11. return it.lowLevel()
  12. }
  13. // Config returns the Map's config.
  14. func (i *LowLevelIterator[K, V, A]) Config() *Config[K, V, A] {
  15. return &i.r.cfg.Config
  16. }
  17. // IncrementPos increments the iterator's position within the current node.
  18. func (i *LowLevelIterator[K, V, A]) IncrementPos() {
  19. i.SetPos(i.pos + 1)
  20. }
  21. // SetPos sets the iterator's position within the current node.
  22. func (i *LowLevelIterator[K, V, A]) SetPos(pos int16) {
  23. i.pos = pos
  24. }
  25. // Node returns the current Node.
  26. func (i *LowLevelIterator[K, V, A]) Node() *Node[K, V, A] {
  27. return i.node
  28. }
  29. // IsLeaf returns true if the current node is a leaf.
  30. func (i *LowLevelIterator[K, V, A]) IsLeaf() bool {
  31. return i.node.IsLeaf()
  32. }
  33. // Pos returns the current position within the current node.
  34. func (i *LowLevelIterator[K, V, A]) Pos() int16 {
  35. return i.pos
  36. }
  37. // Depth returns the number of nodes above the current node in the stack.
  38. // It is illegal to call Ascend if this function returns 0.
  39. func (i *LowLevelIterator[K, V, A]) Depth() int {
  40. return i.s.len()
  41. }
  42. // Child returns the augmentation of the child node at the current position.
  43. // It is illegal to call if this is a leaf node or there is no child
  44. // node at the current position.
  45. func (i *LowLevelIterator[K, V, A]) Child() *A {
  46. return &i.node.children[i.pos].aug
  47. }
  48. // Descend pushes the current position into the iterators stack and
  49. // descends into the child node currently pointed to by the iterator.
  50. // It is illegal to call if there is no such child. The position in the
  51. // new node will be 0.
  52. func (i *LowLevelIterator[K, V, A]) Descend() {
  53. i.s.push(i.iterFrame)
  54. i.iterFrame = i.makeFrame(i.node.children[i.pos], 0)
  55. }
  56. // Ascend ascends up to the current node's parent and resets the position
  57. // to the one previously set for this parent node.
  58. func (i *LowLevelIterator[K, V, A]) Ascend() {
  59. i.iterFrame = i.s.pop()
  60. }
  61. func (i *LowLevelIterator[K, V, A]) makeFrame(
  62. n *Node[K, V, A], pos int16,
  63. ) iterFrame[K, V, A] {
  64. return iterFrame[K, V, A]{
  65. node: n,
  66. pos: pos,
  67. }
  68. }