/src/lib/abilities/internal/hoard.e
Specman e | 104 lines | 48 code | 8 blank | 48 comment | 1 complexity | be9cd8cfdb293d190cdb02112333b7b5 MD5 | raw file
1-- This file is part of a Liberty Eiffel library. 2-- See the full copyright at the end. 3-- 4deferred class HOARD[E_] 5 -- 6 -- A hoard of objects is a collation of many objects sharing the same type. This meta type defines some 7 -- traversal features using agents, as well as very simple hoard properties: `count' and `is_empty'. 8 -- 9 10feature {ANY} -- Counting: 11 count: INTEGER 12 -- Number of available items in the hoard. 13 -- 14 -- See also `is_empty' 15 deferred 16 ensure 17 Result >= 0 18 end 19 20 is_empty: BOOLEAN 21 -- Is the hoard empty ? 22 -- 23 -- See also `count'. 24 deferred 25 ensure 26 definition: Result = (count = 0) 27 end 28 29feature {ANY} -- Agent-based features: 30 for_each (action: PROCEDURE[TUPLE[E_]]) 31 -- Apply `action' to every item of `Current'. 32 -- 33 -- See also `for_all', `exists', `aggregate'. 34 require 35 action /= Void 36 deferred 37 end 38 39 frozen do_all (action: ROUTINE[TUPLE[E_]]) 40 -- Apply `action' to every item of `Current'. 41 -- 42 -- See also `for_all', `exists', `aggregate'. 43 obsolete "Use `for_each` instead. This feature is not secure because it accepts a FUNCTION, the result of which is lost." 44 require 45 action /= Void 46 local 47 p: PROCEDURE[TUPLE[E_]] 48 do 49 if p ?:= action then 50 p ::= action 51 else 52 p := agent (a: ROUTINE[TUPLE[E_]]; e: E_) do a.call([e]) end (action, ?) 53 end 54 for_each(p) 55 end 56 57 for_all (test: PREDICATE[TUPLE[E_]]): BOOLEAN 58 -- Do all items satisfy `test'? 59 -- 60 -- See also `for_each', `exists', `aggregate'. 61 require 62 test /= Void 63 deferred 64 end 65 66 exists (test: PREDICATE[TUPLE[E_]]): BOOLEAN 67 -- Does at least one item satisfy `test'? 68 -- 69 -- See also `for_each', `for_all', `aggregate'. 70 require 71 test /= Void 72 deferred 73 end 74 75 aggregate (action: FUNCTION[TUPLE[E_, E_], E_]; initial: E_): E_ 76 -- Aggregate all the elements starting from the initial value. 77 -- 78 -- See also `for_each', `for_all', `exists'. 79 require 80 action /= Void 81 deferred 82 end 83 84end -- class HOARD 85-- 86-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file. 87-- 88-- Permission is hereby granted, free of charge, to any person obtaining a copy 89-- of this software and associated documentation files (the "Software"), to deal 90-- in the Software without restriction, including without limitation the rights 91-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 92-- copies of the Software, and to permit persons to whom the Software is 93-- furnished to do so, subject to the following conditions: 94-- 95-- The above copyright notice and this permission notice shall be included in 96-- all copies or substantial portions of the Software. 97-- 98-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 99-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 100-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 101-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 102-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 103-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 104-- THE SOFTWARE.