iterator.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //@File iterator.go
  2. //@Time 2022/05/12
  3. //@Author #Suyghur,
  4. package rbtree
  5. import "ylink/comm/utils/iterator"
  6. type RbTreeIterator struct {
  7. node *Node
  8. }
  9. // NewIterator creates a RbTreeIterator from the passed node
  10. func NewIterator(node *Node) *RbTreeIterator {
  11. return &RbTreeIterator{node: node}
  12. }
  13. // IsValid returns true if the iterator is valid, otherwise returns false
  14. func (iter *RbTreeIterator) IsValid() bool {
  15. if iter.node != nil {
  16. return true
  17. }
  18. return false
  19. }
  20. // Next moves the pointer of the iterator to the next node, and returns itself
  21. func (iter *RbTreeIterator) Next() iterator.ConstIterator {
  22. if iter.IsValid() {
  23. iter.node = iter.node.Next()
  24. }
  25. return iter
  26. }
  27. // Prev moves the pointer of the iterator to the previous node, and returns itself
  28. func (iter *RbTreeIterator) Prev() iterator.ConstBidIterator {
  29. if iter.IsValid() {
  30. iter.node = iter.node.Prev()
  31. }
  32. return iter
  33. }
  34. // Key returns the node's key of the iterator point to
  35. func (iter *RbTreeIterator) Key() interface{} {
  36. return iter.node.Key()
  37. }
  38. // Value returns the node's value of the iterator point to
  39. func (iter *RbTreeIterator) Value() interface{} {
  40. return iter.node.Value()
  41. }
  42. //SetValue sets the node's value of the iterator point to
  43. func (iter *RbTreeIterator) SetValue(val interface{}) error {
  44. iter.node.SetValue(val)
  45. return nil
  46. }
  47. // Clone clones the iterator into a new RbTreeIterator
  48. func (iter *RbTreeIterator) Clone() iterator.ConstIterator {
  49. return NewIterator(iter.node)
  50. }
  51. // Equal returns true if the iterator is equal to the passed iterator
  52. func (iter *RbTreeIterator) Equal(other iterator.ConstIterator) bool {
  53. otherIter, ok := other.(*RbTreeIterator)
  54. if !ok {
  55. return false
  56. }
  57. if otherIter.node == iter.node {
  58. return true
  59. }
  60. return false
  61. }