本站源代码
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.

85 lines
2.9KB

  1. // Copyright (c) 2015, Emir Pasic. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package binaryheap
  5. import "github.com/emirpasic/gods/containers"
  6. func assertIteratorImplementation() {
  7. var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil)
  8. }
  9. // Iterator returns a stateful iterator whose values can be fetched by an index.
  10. type Iterator struct {
  11. heap *Heap
  12. index int
  13. }
  14. // Iterator returns a stateful iterator whose values can be fetched by an index.
  15. func (heap *Heap) Iterator() Iterator {
  16. return Iterator{heap: heap, index: -1}
  17. }
  18. // Next moves the iterator to the next element and returns true if there was a next element in the container.
  19. // If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
  20. // If Next() was called for the first time, then it will point the iterator to the first element if it exists.
  21. // Modifies the state of the iterator.
  22. func (iterator *Iterator) Next() bool {
  23. if iterator.index < iterator.heap.Size() {
  24. iterator.index++
  25. }
  26. return iterator.heap.withinRange(iterator.index)
  27. }
  28. // Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
  29. // If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
  30. // Modifies the state of the iterator.
  31. func (iterator *Iterator) Prev() bool {
  32. if iterator.index >= 0 {
  33. iterator.index--
  34. }
  35. return iterator.heap.withinRange(iterator.index)
  36. }
  37. // Value returns the current element's value.
  38. // Does not modify the state of the iterator.
  39. func (iterator *Iterator) Value() interface{} {
  40. value, _ := iterator.heap.list.Get(iterator.index)
  41. return value
  42. }
  43. // Index returns the current element's index.
  44. // Does not modify the state of the iterator.
  45. func (iterator *Iterator) Index() int {
  46. return iterator.index
  47. }
  48. // Begin resets the iterator to its initial state (one-before-first)
  49. // Call Next() to fetch the first element if any.
  50. func (iterator *Iterator) Begin() {
  51. iterator.index = -1
  52. }
  53. // End moves the iterator past the last element (one-past-the-end).
  54. // Call Prev() to fetch the last element if any.
  55. func (iterator *Iterator) End() {
  56. iterator.index = iterator.heap.Size()
  57. }
  58. // First moves the iterator to the first element and returns true if there was a first element in the container.
  59. // If First() returns true, then first element's index and value can be retrieved by Index() and Value().
  60. // Modifies the state of the iterator.
  61. func (iterator *Iterator) First() bool {
  62. iterator.Begin()
  63. return iterator.Next()
  64. }
  65. // Last moves the iterator to the last element and returns true if there was a last element in the container.
  66. // If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
  67. // Modifies the state of the iterator.
  68. func (iterator *Iterator) Last() bool {
  69. iterator.End()
  70. return iterator.Prev()
  71. }
上海开阖软件有限公司 沪ICP备12045867号-1