/src/lib/iterator/iterator.e

http://github.com/tybor/Liberty · Specman e · 178 lines · 109 code · 15 blank · 54 comment · 2 complexity · c025dc9e498ef94fb878329cea63725a MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. deferred class ITERATOR[E_]
  5. --
  6. -- The iterator pattern at work: this abstract class defines a
  7. -- traversal interface for any kind of aggregates data structure.
  8. -- An iterator can be used when you need to do something on all
  9. -- elements in the data structure, but there is no order concept.
  10. --
  11. -- To create a new iterator, use `new_iterator' in the
  12. -- corresponding data structure.
  13. --
  14. -- See examples in directory tutorial/iterator.
  15. --
  16. feature {ANY}
  17. frozen is_valid: BOOLEAN
  18. do
  19. Result := generation = iterable_generation
  20. end
  21. start
  22. -- Positions the iterator to the first object in the
  23. -- aggregate to be traversed.
  24. deferred
  25. ensure
  26. is_valid
  27. end
  28. is_off: BOOLEAN
  29. -- Returns True when there are no more objects in the
  30. -- sequence.
  31. require
  32. is_valid
  33. deferred
  34. ensure
  35. generation = old generation
  36. is_valid
  37. end
  38. item: E_
  39. -- Returns the object at the current position in the
  40. -- sequence.
  41. require
  42. is_valid
  43. not is_off
  44. deferred
  45. ensure
  46. generation = old generation
  47. is_valid
  48. end
  49. next
  50. -- Positions the iterator to the next object in the
  51. -- sequence.
  52. require
  53. is_valid
  54. not is_off
  55. deferred
  56. ensure
  57. generation = old generation
  58. is_valid
  59. end
  60. feature {ANY} -- Check that the underlying traversable has not changed
  61. iterable_generation: INTEGER
  62. deferred
  63. end
  64. generation: INTEGER
  65. deferred
  66. end
  67. feature {ANY} -- Agent-based features:
  68. for_each (action: PROCEDURE[TUPLE[E_]])
  69. -- Apply `action' to every item of `Current'.
  70. --
  71. -- See also `for_all', `exists', `aggregate'.
  72. do
  73. from
  74. start
  75. invariant
  76. is_valid
  77. until
  78. is_off
  79. loop
  80. action.call([item])
  81. next
  82. end
  83. end
  84. for_all (test: PREDICATE[TUPLE[E_]]): BOOLEAN
  85. -- Do all items satisfy `test'?
  86. --
  87. -- See also `for_each', `exists', `aggregate'.
  88. do
  89. from
  90. Result := True
  91. start
  92. invariant
  93. is_valid
  94. until
  95. not Result or else is_off
  96. loop
  97. Result := test.item([item])
  98. next
  99. end
  100. end
  101. exists (test: PREDICATE[TUPLE[E_]]): BOOLEAN
  102. -- Does at least one item satisfy `test'?
  103. --
  104. -- See also `for_each', `for_all', `aggregate'.
  105. do
  106. from
  107. start
  108. invariant
  109. is_valid
  110. until
  111. Result or else is_off
  112. loop
  113. Result := test.item([item])
  114. next
  115. end
  116. end
  117. aggregate (action: FUNCTION[TUPLE[E_, E_], E_]; initial: E_): E_
  118. -- Aggregate all the elements starting from the initial value.
  119. --
  120. -- See also `for_each', `for_all', `exists'.
  121. do
  122. from
  123. Result := initial
  124. start
  125. invariant
  126. is_valid
  127. until
  128. is_off
  129. loop
  130. Result := action.item([Result, item])
  131. next
  132. end
  133. end
  134. feature {} -- Invariant on `generation` dynamics
  135. generation_for_invariant: INTEGER
  136. generation_only_grows: BOOLEAN
  137. do
  138. Result := generation >= generation_for_invariant
  139. generation_for_invariant := generation
  140. end
  141. invariant
  142. generation_only_grows
  143. end -- class ITERATOR
  144. --
  145. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  146. --
  147. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  148. -- of this software and associated documentation files (the "Software"), to deal
  149. -- in the Software without restriction, including without limitation the rights
  150. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  151. -- copies of the Software, and to permit persons to whom the Software is
  152. -- furnished to do so, subject to the following conditions:
  153. --
  154. -- The above copyright notice and this permission notice shall be included in
  155. -- all copies or substantial portions of the Software.
  156. --
  157. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  158. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  159. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  160. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  161. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  162. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  163. -- THE SOFTWARE.