iterator.go 1.6 KB

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