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

84 lines
2.8KB

  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 arraylist
  5. import "github.com/emirpasic/gods/containers"
  6. func assertIteratorImplementation() {
  7. var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil)
  8. }
  9. // Iterator holding the iterator's state
  10. type Iterator struct {
  11. list *List
  12. index int
  13. }
  14. // Iterator returns a stateful iterator whose values can be fetched by an index.
  15. func (list *List) Iterator() Iterator {
  16. return Iterator{list: list, 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.list.size {
  24. iterator.index++
  25. }
  26. return iterator.list.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.list.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. return iterator.list.elements[iterator.index]
  41. }
  42. // Index returns the current element's index.
  43. // Does not modify the state of the iterator.
  44. func (iterator *Iterator) Index() int {
  45. return iterator.index
  46. }
  47. // Begin resets the iterator to its initial state (one-before-first)
  48. // Call Next() to fetch the first element if any.
  49. func (iterator *Iterator) Begin() {
  50. iterator.index = -1
  51. }
  52. // End moves the iterator past the last element (one-past-the-end).
  53. // Call Prev() to fetch the last element if any.
  54. func (iterator *Iterator) End() {
  55. iterator.index = iterator.list.size
  56. }
  57. // First moves the iterator to the first element and returns true if there was a first element in the container.
  58. // If First() returns true, then first element's index and value can be retrieved by Index() and Value().
  59. // Modifies the state of the iterator.
  60. func (iterator *Iterator) First() bool {
  61. iterator.Begin()
  62. return iterator.Next()
  63. }
  64. // Last moves the iterator to the last element and returns true if there was a last element in the container.
  65. // If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
  66. // Modifies the state of the iterator.
  67. func (iterator *Iterator) Last() bool {
  68. iterator.End()
  69. return iterator.Prev()
  70. }
上海开阖软件有限公司 沪ICP备12045867号-1