/src/lib/abilities/searchable.e

http://github.com/tybor/Liberty · Specman e · 144 lines · 63 code · 12 blank · 69 comment · 0 complexity · f73d44b178e561b502b6065ab2ab1fe6 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 SEARCHABLE[E_]
  5. --
  6. -- A SEARCHABLE sequence is INDEXABLE, and the index of each
  7. -- element may be retrieved.
  8. --
  9. inherit
  10. INDEXABLE[E_]
  11. feature {ANY} -- Looking and Searching:
  12. has (x: like item): BOOLEAN
  13. -- Look for `x' using `is_equal' for comparison.
  14. --
  15. -- See also `fast_has', `index_of', `fast_index_of'.
  16. deferred
  17. ensure
  18. definition: Result = valid_index(first_index_of(x))
  19. end
  20. fast_has (x: like item): BOOLEAN
  21. -- Look for `x' using basic `=' for comparison.
  22. --
  23. -- See also `has', `fast_index_of', `index_of'.
  24. deferred
  25. ensure
  26. definition: Result = valid_index(fast_first_index_of(x))
  27. end
  28. first_index_of (element: like item): INTEGER
  29. -- Give the index of the first occurrence of `element' using `is_equal' for comparison.
  30. -- Answer `upper + 1' when `element' is not inside.
  31. --
  32. -- See also `fast_first_index_of', `index_of', `last_index_of', `reverse_index_of'.
  33. deferred
  34. ensure
  35. definition: Result = index_of(element, lower)
  36. end
  37. index_of (element: like item; start_index: INTEGER): INTEGER
  38. -- Using `is_equal' for comparison, gives the index of the first occurrence of `element' at or after
  39. -- `start_index'. Return `upper + 1' if the search for `element' failed.
  40. --
  41. -- See also `fast_index_of', `reverse_index_of', `first_index_of'.
  42. deferred
  43. ensure
  44. Result.in_range(start_index, upper + 1)
  45. valid_index(Result) implies (create {SAFE_EQUAL[E_]}).test(element, item(Result))
  46. end
  47. reverse_index_of (element: like item; start_index: INTEGER): INTEGER
  48. -- Using `is_equal' for comparison, gives the index of the first occurrence of `element' at or before
  49. -- `start_index'. Search is done in reverse direction, which means from the `start_index' down to the
  50. -- `lower' index . Answer `lower -1' when the search fail.
  51. --
  52. -- See also `fast_reverse_index_of', `last_index_of', `index_of'.
  53. require
  54. valid_index(start_index)
  55. deferred
  56. ensure
  57. Result.in_range(lower - 1, start_index)
  58. valid_index(Result) implies item(Result).is_equal(element)
  59. end
  60. last_index_of (element: like item): INTEGER
  61. -- Using `is_equal' for comparison, gives the index of the last occurrence of `element' at or before
  62. -- `upper'. Search is done in reverse direction, which means from the `upper' down to the
  63. -- `lower' index . Answer `lower -1' when the search fail.
  64. --
  65. -- See also `fast_last_index_of', `reverse_index_of', `index_of'.
  66. deferred
  67. ensure
  68. definition: Result = reverse_index_of(element, upper)
  69. end
  70. fast_first_index_of (element: like item): INTEGER
  71. -- Give the index of the first occurrence of `element' using basic `=' for comparison.
  72. -- Answer `upper + 1' when `element' is not inside.
  73. --
  74. -- See also `first_index_of', `last_index_of', `fast_last_index_of'.
  75. deferred
  76. ensure
  77. definition: Result = fast_index_of(element, lower)
  78. end
  79. fast_index_of (element: like item; start_index: INTEGER): INTEGER
  80. -- Using basic `=' for comparison, gives the index of the first occurrence of `element' at or after
  81. -- `start_index'. Answer `upper + 1' when `element' when the search fail.
  82. --
  83. -- See also `index_of', `fast_reverse_index_of', `fast_first_index_of'.
  84. deferred
  85. ensure
  86. Result.in_range(start_index, upper + 1)
  87. valid_index(Result) implies element = item(Result)
  88. end
  89. fast_reverse_index_of (element: like item; start_index: INTEGER): INTEGER
  90. -- Using basic `=' comparison, gives the index of the first occurrence of `element' at or before
  91. -- `start_index'. Search is done in reverse direction, which means from the `start_index' down to the
  92. -- `lower' index . Answer `lower -1' when the search fail.
  93. --
  94. -- See also `reverse_index_of', `fast_index_of', `fast_last_index_of'.
  95. require
  96. valid_index(start_index)
  97. deferred
  98. ensure
  99. Result.in_range(lower - 1, start_index)
  100. valid_index(Result) implies item(Result) = element
  101. end
  102. fast_last_index_of (element: like item): INTEGER
  103. -- Using basic `=' for comparison, gives the index of the last occurrence of `element' at or before
  104. -- `upper'. Search is done in reverse direction, which means from the `upper' down to the
  105. -- `lower' index . Answer `lower -1' when the search fail.
  106. --
  107. -- See also `fast_reverse_index_of', `last_index_of'.
  108. deferred
  109. ensure
  110. definition: Result = fast_reverse_index_of(element, upper)
  111. end
  112. end -- class SEARCHABLE
  113. --
  114. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  115. --
  116. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  117. -- of this software and associated documentation files (the "Software"), to deal
  118. -- in the Software without restriction, including without limitation the rights
  119. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  120. -- copies of the Software, and to permit persons to whom the Software is
  121. -- furnished to do so, subject to the following conditions:
  122. --
  123. -- The above copyright notice and this permission notice shall be included in
  124. -- all copies or substantial portions of the Software.
  125. --
  126. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  127. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  128. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  129. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  130. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  131. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  132. -- THE SOFTWARE.