/src/lib/abilities/iterable.e

http://github.com/tybor/Liberty · Specman e · 122 lines · 72 code · 11 blank · 39 comment · 2 complexity · ecd194884408c4b8b01a5f73c4b0bb00 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 ITERABLE[E_]
  5. --
  6. -- An `ITERABLE[E_]' is a (potentially infinite) readable sequence of objects of type E_ that can be
  7. -- accessed through an ITERATOR[E]
  8. --
  9. inherit
  10. HOARD[E_]
  11. redefine
  12. out_in_tagged_out_memory
  13. end
  14. feature {ANY} -- Other features:
  15. new_iterator: ITERATOR[E_]
  16. deferred
  17. ensure
  18. Result /= Void
  19. Result.generation = generation
  20. end
  21. frozen get_new_iterator: like new_iterator
  22. obsolete "Use `new_iterator' instead. This historical SmartEiffel feature is badly named."
  23. do
  24. Result := new_iterator
  25. end
  26. feature {ANY} -- Agent-based features:
  27. for_each (action: PROCEDURE[TUPLE[E_]])
  28. -- Apply `action' to every item of `Current'.
  29. --
  30. -- See also `for_all', `exists', `aggregate'.
  31. do
  32. new_iterator.for_each(action)
  33. end
  34. for_all (test: PREDICATE[TUPLE[E_]]): BOOLEAN
  35. -- Do all items satisfy `test'?
  36. --
  37. -- See also `for_each', `exists', `aggregate'.
  38. do
  39. Result := new_iterator.for_all(test)
  40. end
  41. exists (test: PREDICATE[TUPLE[E_]]): BOOLEAN
  42. -- Does at least one item satisfy `test'?
  43. --
  44. -- See also `for_each', `for_all', `aggregate'.
  45. do
  46. Result := new_iterator.exists(test)
  47. end
  48. aggregate (action: FUNCTION[TUPLE[E_, E_], E_]; initial: E_): E_
  49. -- Aggregate all the elements starting from the initial value.
  50. --
  51. -- See also `for_each', `for_all', `exists'.
  52. do
  53. Result := new_iterator.aggregate(action, initial)
  54. end
  55. feature {ANY} -- Printing:
  56. out_in_tagged_out_memory
  57. local
  58. i: like new_iterator; v: E_
  59. do
  60. tagged_out_memory.extend('{')
  61. tagged_out_memory.append(generating_type)
  62. tagged_out_memory.append(once ":[")
  63. from
  64. i := new_iterator
  65. i.start
  66. until
  67. i.is_off
  68. loop
  69. v := i.item
  70. if v = Void then
  71. tagged_out_memory.append(once "Void")
  72. else
  73. v.out_in_tagged_out_memory
  74. end
  75. i.next
  76. if not i.is_off then
  77. tagged_out_memory.extend(' ')
  78. end
  79. end
  80. tagged_out_memory.append(once "]}")
  81. end
  82. feature {ANY}
  83. generation: INTEGER
  84. feature {}
  85. next_generation
  86. do
  87. generation := generation + 1
  88. ensure
  89. generation > old generation
  90. end
  91. end -- class ITERABLE
  92. --
  93. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  94. --
  95. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  96. -- of this software and associated documentation files (the "Software"), to deal
  97. -- in the Software without restriction, including without limitation the rights
  98. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  99. -- copies of the Software, and to permit persons to whom the Software is
  100. -- furnished to do so, subject to the following conditions:
  101. --
  102. -- The above copyright notice and this permission notice shall be included in
  103. -- all copies or substantial portions of the Software.
  104. --
  105. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  106. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  107. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  108. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  109. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  110. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  111. -- THE SOFTWARE.