/src/lib/abilities/internal/hoard.e

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