PageRenderTime 53ms CodeModel.GetById 50ms app.highlight 0ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/abilities/iterable.e

http://github.com/tybor/Liberty
Specman e | 122 lines | 72 code | 11 blank | 39 comment | 2 complexity | ecd194884408c4b8b01a5f73c4b0bb00 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4deferred class ITERABLE[E_]
  5   --
  6   -- An `ITERABLE[E_]' is a (potentially infinite) readable sequence of objects of type E_ that can be
  7   -- accessed through an ITERATOR[E]
  8   --
  9
 10inherit
 11   HOARD[E_]
 12      redefine
 13         out_in_tagged_out_memory
 14      end
 15
 16feature {ANY} -- Other features:
 17   new_iterator: ITERATOR[E_]
 18      deferred
 19      ensure
 20         Result /= Void
 21         Result.generation = generation
 22      end
 23
 24   frozen get_new_iterator: like new_iterator
 25      obsolete "Use `new_iterator' instead. This historical SmartEiffel feature is badly named."
 26      do
 27         Result := new_iterator
 28      end
 29
 30feature {ANY} -- Agent-based features:
 31   for_each (action: PROCEDURE[TUPLE[E_]])
 32         -- Apply `action' to every item of `Current'.
 33         --
 34         -- See also `for_all', `exists', `aggregate'.
 35      do
 36         new_iterator.for_each(action)
 37      end
 38
 39   for_all (test: PREDICATE[TUPLE[E_]]): BOOLEAN
 40         -- Do all items satisfy `test'?
 41         --
 42         -- See also `for_each', `exists', `aggregate'.
 43      do
 44         Result := new_iterator.for_all(test)
 45      end
 46
 47   exists (test: PREDICATE[TUPLE[E_]]): BOOLEAN
 48         -- Does at least one item satisfy `test'?
 49         --
 50         -- See also `for_each', `for_all', `aggregate'.
 51      do
 52         Result := new_iterator.exists(test)
 53      end
 54
 55   aggregate (action: FUNCTION[TUPLE[E_, E_], E_]; initial: E_): E_
 56         -- Aggregate all the elements starting from the initial value.
 57         --
 58         -- See also `for_each', `for_all', `exists'.
 59      do
 60         Result := new_iterator.aggregate(action, initial)
 61      end
 62
 63feature {ANY} -- Printing:
 64   out_in_tagged_out_memory
 65      local
 66         i: like new_iterator; v: E_
 67      do
 68         tagged_out_memory.extend('{')
 69         tagged_out_memory.append(generating_type)
 70         tagged_out_memory.append(once ":[")
 71         from
 72            i := new_iterator
 73            i.start
 74         until
 75            i.is_off
 76         loop
 77            v := i.item
 78            if v = Void then
 79               tagged_out_memory.append(once "Void")
 80            else
 81               v.out_in_tagged_out_memory
 82            end
 83            i.next
 84            if not i.is_off then
 85               tagged_out_memory.extend(' ')
 86            end
 87         end
 88         tagged_out_memory.append(once "]}")
 89      end
 90
 91feature {ANY}
 92   generation: INTEGER
 93
 94feature {}
 95   next_generation
 96      do
 97         generation := generation + 1
 98      ensure
 99         generation > old generation
100      end
101
102end -- class ITERABLE
103--
104-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
105--
106-- Permission is hereby granted, free of charge, to any person obtaining a copy
107-- of this software and associated documentation files (the "Software"), to deal
108-- in the Software without restriction, including without limitation the rights
109-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
110-- copies of the Software, and to permit persons to whom the Software is
111-- furnished to do so, subject to the following conditions:
112--
113-- The above copyright notice and this permission notice shall be included in
114-- all copies or substantial portions of the Software.
115--
116-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
117-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
118-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
119-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
120-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
121-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
122-- THE SOFTWARE.