/src/lib/abilities/indexable.e

http://github.com/tybor/Liberty · Specman e · 205 lines · 121 code · 13 blank · 71 comment · 4 complexity · fb08fe1528d89af3d4dd56c2b6023ed3 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 INDEXABLE[E_]
  5. --
  6. -- An `INDEXABLE[E_]' is a finite readable sequence of objects of type E_.
  7. --
  8. -- A good performance should always be obtained by sequentially accessing an `INDEXABLE' with increasing
  9. -- indexes (from `lower' to `upper'), as demonstrated in the following code snippet :
  10. --
  11. -- from
  12. -- i := a_indexable.lower
  13. -- until
  14. -- i > a_indexable.upper
  15. -- loop
  16. -- do_something_with(a_indexable.item(i))
  17. -- i := i + 1
  18. -- end
  19. --
  20. -- Other accessing methods (including random access and sequential access from `upper' to `lower') may or
  21. -- may not lead to acceptable performance, depending on the particular implementation of `INDEXABLE'.
  22. --
  23. inherit
  24. HOARD[E_]
  25. redefine
  26. out_in_tagged_out_memory
  27. end
  28. feature {ANY} -- Indexing:
  29. lower: INTEGER
  30. -- Minimum index.
  31. --
  32. -- See also `upper', `valid_index', `item'.
  33. deferred
  34. end
  35. upper: INTEGER
  36. -- Maximum index.
  37. --
  38. -- See also `lower', `valid_index', `item'.
  39. deferred
  40. end
  41. valid_index (i: INTEGER): BOOLEAN
  42. -- True when `i' is valid (i.e., inside actual bounds).
  43. --
  44. -- See also `lower', `upper', `item'.
  45. do
  46. Result := lower <= i and then i <= upper
  47. ensure
  48. definition: Result = (lower <= i and i <= upper)
  49. end
  50. feature {ANY} -- Accessing:
  51. item (i: INTEGER): E_
  52. -- Item at the corresponding index `i'.
  53. --
  54. -- See also `lower', `upper', `valid_index'.
  55. require
  56. valid_index(i)
  57. deferred
  58. end
  59. first: like item
  60. -- The very `first' item.
  61. --
  62. -- See also `last', `item'.
  63. require
  64. not is_empty
  65. deferred
  66. ensure
  67. definition: Result = item(lower)
  68. end
  69. last: like item
  70. -- The `last' item.
  71. --
  72. -- See also `first', `item'.
  73. require
  74. not is_empty
  75. deferred
  76. ensure
  77. definition: Result = item(upper)
  78. end
  79. feature {ANY} -- Agent-based features:
  80. for_each (action: PROCEDURE[TUPLE[E_]])
  81. -- Apply `action' to every item of `Current'.
  82. --
  83. -- See also `for_all', `exists', `aggregate'.
  84. local
  85. i: INTEGER
  86. do
  87. from
  88. i := lower
  89. until
  90. i > upper
  91. loop
  92. action.call([item(i)])
  93. i := i + 1
  94. end
  95. end
  96. for_all (test: PREDICATE[TUPLE[E_]]): BOOLEAN
  97. -- Do all items satisfy `test'?
  98. --
  99. -- See also `for_each', `exists', `aggregate'.
  100. local
  101. i: INTEGER
  102. do
  103. from
  104. Result := True
  105. i := lower
  106. until
  107. not Result or else i > upper
  108. loop
  109. Result := test.item([item(i)])
  110. i := i + 1
  111. end
  112. end
  113. exists (test: PREDICATE[TUPLE[E_]]): BOOLEAN
  114. -- Does at least one item satisfy `test'?
  115. --
  116. -- See also `for_each', `for_all', `aggregate'.
  117. local
  118. i: INTEGER
  119. do
  120. from
  121. i := lower
  122. until
  123. Result or else i > upper
  124. loop
  125. Result := test.item([item(i)])
  126. i := i + 1
  127. end
  128. end
  129. aggregate (action: FUNCTION[TUPLE[E_, E_], E_]; initial: E_): E_
  130. -- Aggregate all the elements starting from the initial value.
  131. --
  132. -- See also `for_each', `for_all', `exists'.
  133. local
  134. i: INTEGER
  135. do
  136. from
  137. Result := initial
  138. i := lower
  139. until
  140. i > upper
  141. loop
  142. Result := action.item([Result, item(i)])
  143. i := i + 1
  144. end
  145. end
  146. feature {ANY} -- Printing:
  147. out_in_tagged_out_memory
  148. local
  149. i: INTEGER; v: like item
  150. do
  151. tagged_out_memory.extend('{')
  152. tagged_out_memory.append(generating_type)
  153. tagged_out_memory.append(once ":[")
  154. from
  155. i := lower
  156. until
  157. i > upper
  158. loop
  159. v := item(i)
  160. if v = Void then
  161. tagged_out_memory.append(once "Void")
  162. else
  163. v.out_in_tagged_out_memory
  164. end
  165. if i < upper then
  166. tagged_out_memory.extend(' ')
  167. end
  168. i := i + 1
  169. end
  170. tagged_out_memory.append(once "]}")
  171. end
  172. end -- class INDEXABLE
  173. --
  174. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  175. --
  176. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  177. -- of this software and associated documentation files (the "Software"), to deal
  178. -- in the Software without restriction, including without limitation the rights
  179. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  180. -- copies of the Software, and to permit persons to whom the Software is
  181. -- furnished to do so, subject to the following conditions:
  182. --
  183. -- The above copyright notice and this permission notice shall be included in
  184. -- all copies or substantial portions of the Software.
  185. --
  186. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  187. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  188. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  189. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  190. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  191. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  192. -- THE SOFTWARE.