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