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

110 lines
4.5KB

  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 containers
  5. // IteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.
  6. type IteratorWithIndex interface {
  7. // Next moves the iterator to the next element and returns true if there was a next element in the container.
  8. // If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
  9. // If Next() was called for the first time, then it will point the iterator to the first element if it exists.
  10. // Modifies the state of the iterator.
  11. Next() bool
  12. // Value returns the current element's value.
  13. // Does not modify the state of the iterator.
  14. Value() interface{}
  15. // Index returns the current element's index.
  16. // Does not modify the state of the iterator.
  17. Index() int
  18. // Begin resets the iterator to its initial state (one-before-first)
  19. // Call Next() to fetch the first element if any.
  20. Begin()
  21. // First moves the iterator to the first element and returns true if there was a first element in the container.
  22. // If First() returns true, then first element's index and value can be retrieved by Index() and Value().
  23. // Modifies the state of the iterator.
  24. First() bool
  25. }
  26. // IteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.
  27. type IteratorWithKey interface {
  28. // Next moves the iterator to the next element and returns true if there was a next element in the container.
  29. // If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
  30. // If Next() was called for the first time, then it will point the iterator to the first element if it exists.
  31. // Modifies the state of the iterator.
  32. Next() bool
  33. // Value returns the current element's value.
  34. // Does not modify the state of the iterator.
  35. Value() interface{}
  36. // Key returns the current element's key.
  37. // Does not modify the state of the iterator.
  38. Key() interface{}
  39. // Begin resets the iterator to its initial state (one-before-first)
  40. // Call Next() to fetch the first element if any.
  41. Begin()
  42. // First moves the iterator to the first element and returns true if there was a first element in the container.
  43. // If First() returns true, then first element's key and value can be retrieved by Key() and Value().
  44. // Modifies the state of the iterator.
  45. First() bool
  46. }
  47. // ReverseIteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.
  48. //
  49. // Essentially it is the same as IteratorWithIndex, but provides additional:
  50. //
  51. // Prev() function to enable traversal in reverse
  52. //
  53. // Last() function to move the iterator to the last element.
  54. //
  55. // End() function to move the iterator past the last element (one-past-the-end).
  56. type ReverseIteratorWithIndex interface {
  57. // Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
  58. // If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
  59. // Modifies the state of the iterator.
  60. Prev() bool
  61. // End moves the iterator past the last element (one-past-the-end).
  62. // Call Prev() to fetch the last element if any.
  63. End()
  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. Last() bool
  68. IteratorWithIndex
  69. }
  70. // ReverseIteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.
  71. //
  72. // Essentially it is the same as IteratorWithKey, but provides additional:
  73. //
  74. // Prev() function to enable traversal in reverse
  75. //
  76. // Last() function to move the iterator to the last element.
  77. type ReverseIteratorWithKey interface {
  78. // Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
  79. // If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value().
  80. // Modifies the state of the iterator.
  81. Prev() bool
  82. // End moves the iterator past the last element (one-past-the-end).
  83. // Call Prev() to fetch the last element if any.
  84. End()
  85. // Last moves the iterator to the last element and returns true if there was a last element in the container.
  86. // If Last() returns true, then last element's key and value can be retrieved by Key() and Value().
  87. // Modifies the state of the iterator.
  88. Last() bool
  89. IteratorWithKey
  90. }
上海开阖软件有限公司 沪ICP备12045867号-1