Author:bmeyer

Date:2014-02-12T17:17:34.000000Z


git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@1260 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
This commit is contained in:
bmeyer
2014-02-12 17:17:34 +00:00
parent 10bfd79ee3
commit ab3d8466ba
@@ -5,7 +5,7 @@ The classes of the Iteration cluster encapsulate control structures representing
=Iterators and Agents= =Iterators and Agents=
The recent introduction of the agents mechanism in Eiffel offers an attractive alternative to the <eiffel>Iterator</eiffel> cluster of EiffelBase. Eiffel's agent mechanism offers an attractive alternative to the <eiffel>Iterator</eiffel> cluster of EiffelBase.
=The Notion of iterator= =The Notion of iterator=
@@ -33,16 +33,16 @@ You can perform iterations without any special iteration classes. For example if
then a class <eiffel>SPECIAL_PROMOTION</eiffel> of a text processing system may include in one of its routines a loop of the form then a class <eiffel>SPECIAL_PROMOTION</eiffel> of a text processing system may include in one of its routines a loop of the form
<code> <code>
from from
customers.start customers.start
until until
customers.exhausted customers.exhausted
loop loop
if recent_purchases.has (customers.item>) then if recent_purchases.has (customers.item>) then
target_list.put (customers.item>) target_list.put (customers.item>)
end end
customers.forth customers.forth
end</code> end</code>
Such schemes are quite common. But it is precisely because they occur frequently that it is useful to rely on library classes to handle them. One of the principal tasks of object-oriented software development is to identify recurring patterns and build reusable classes that encapsulate them, so that future developers will be able to rely on ready-made solutions. <br/> Such schemes are quite common. But it is precisely because they occur frequently that it is useful to rely on library classes to handle them. One of the principal tasks of object-oriented software development is to identify recurring patterns and build reusable classes that encapsulate them, so that future developers will be able to rely on ready-made solutions. <br/>
The classes of the Iteration library address this need. Using them offers two benefits: The classes of the Iteration library address this need. Using them offers two benefits:
@@ -51,55 +51,50 @@ The classes of the Iteration library address this need. Using them offers two be
=Simple Examples= =Simple Examples=
To get a first grasp of how one can work with the Iteration library, let us look at a typical iteration class and a typical iteration client. To get a first grasp of how one can work with the Iteration library, let us look at a typical iteration class and a typical iteration client.
==An example iterator routine== ==An example iterator routine==
Here, given with its full implementation, is a typical Iteration library routine: the procedure until_do from [[ref:libraries/base/reference/linear_iterator_chart]] , the class defining iteration mechanisms on linear (sequential) structures. Here, given with its full implementation, is a typical Iteration library routine: the procedure until_do from [[ref:libraries/base/reference/linear_iterator_chart]] , the class defining iteration mechanisms on linear (sequential) structures.
<code> <code>
until_do until_do
-- Apply action to every item of target, -- Apply action to every item of target,
-- up to but excluding first one satisfying test. -- up to but excluding first one satisfying test.
-- (Apply to full list if no item satisfies test.) -- (Apply to full list if no item satisfies test.)
require require
traversable_exists: target /= Void traversable_exists: target /= Void
do do
from from
target.start target.start
invariant invariant
''invariant_value'' ''invariant_value''
until until
target.exhausted or else test target.exhausted or else test
loop loop
action action
target.forth target.forth
end end
ensure ensure
achieved: target.exhausted or else test achieved: target.exhausted or else test
invariant_satisfied: ''invariant_value'' invariant_satisfied: ''invariant_value''
end</code> end</code>
The precise form of the procedure in the class relies on a call to another procedure, until_continue, and on inherited assertions. Here everything has been unfolded for illustration purposes. <br/> The precise form of the procedure in the class relies on a call to another procedure, until_continue, and on inherited assertions. Here everything has been unfolded for illustration purposes. <br/>
This procedure will traverse the linear structure identified by target and apply the procedure calledaction on every item up to but excluding the first one satisfying test. <br/> This procedure will traverse the linear structure identified by target and apply the procedure calledaction on every item up to but excluding the first one satisfying test. <br/>
The class similarly offers <eiffel>do_all</eiffel>, <eiffel>do_while</eiffel>, <eiffel>do_for</eiffel>, <eiffel>do_if</eiffel> and other procedures representing the common control structures. It also includes functions such as <eiffel>exists</eiffel> and <eiffel>forall</eiffel>, corresponding to the usual quantifiers. <br/> The class similarly offers <eiffel>do_all</eiffel>, <eiffel>do_while</eiffel>, <eiffel>do_for</eiffel>, <eiffel>do_if</eiffel> and other procedures representing the common control structures. It also includes functions such as <eiffel>exists</eiffel> and <eiffel>forall</eiffel>, corresponding to the usual quantifiers. <br/>
These iteration schemes depend on the procedure <eiffel>action</eiffel>, defining the action to be applied to successive elements, and on the function <eiffel>test</eiffel>, defining the boolean query to be applied to these elements. These features are declared in class [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] (the highest-level deferred class of the Iteration library); here is <eiffel>test</eiffel>: These iteration schemes depend on the procedure <eiffel>action</eiffel>, defining the action to be applied to successive elements, and on the function <eiffel>test</eiffel>, defining the boolean query to be applied to these elements. These features are declared in class [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] (the highest-level deferred class of the Iteration library); here is <eiffel>test</eiffel>:
<code> <code>
test: BOOLEAN test: BOOLEAN
-- Test to be applied to item at current position in -- Test to be applied to item at current position in
-- target (default: value of item_test on item) -- target (default: value of item_test on item)
require require
traversable_exists: target /= Void traversable_exists: target /= Void
not_off: not target.off not_off: not target.off
do do
Result := item_test (target.item>) Result := item_test (target.item>)
ensure ensure
not_off: not target.off not_off: not target.off
end</code> end</code>
This indicates that the value of the boolean function <eiffel>test</eiffel> will be obtained by applying <eiffel>item_test</eiffel> to the item at the current position in the target structure. In [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] , function <eiffel>item_test</eiffel> always return ; descendant classes will redefine it so as to describe the desired test. Similarly, <eiffel>action</eiffel> is declared in class [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] as a call to <eiffel>item_action</eiffel>. Descendants will redefine <eiffel>item_action</eiffel>, which as initially declared in [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] is a procedure with a null body. <br/> This indicates that the value of the boolean function <eiffel>test</eiffel> will be obtained by applying <eiffel>item_test</eiffel> to the item at the current position in the target structure. In [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] , function <eiffel>item_test</eiffel> always return ; descendant classes will redefine it so as to describe the desired test. Similarly, <eiffel>action</eiffel> is declared in class [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] as a call to <eiffel>item_action</eiffel>. Descendants will redefine <eiffel>item_action</eiffel>, which as initially declared in [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] is a procedure with a null body. <br/>
Going through <eiffel>item_action</eiffel> and <eiffel>item_test</eiffel> provides an extra degree of flexibility. Normally the action and test performed at each step apply to <eiffel>target</eiffel> <code> . </code><eiffel>item></eiffel>, so that it suffices to redefine the <eiffel>item_features</eiffel>. This is the case with all examples studied in this chapter. In a more general setting, however, you might need to redefine <eiffel>action</eiffel> and <eiffel>test</eiffel> themselves. Going through <eiffel>item_action</eiffel> and <eiffel>item_test</eiffel> provides an extra degree of flexibility. Normally the action and test performed at each step apply to <eiffel>target</eiffel> <code> . </code><eiffel>item></eiffel>, so that it suffices to redefine the <eiffel>item_features</eiffel>. This is the case with all examples studied in this chapter. In a more general setting, however, you might need to redefine <eiffel>action</eiffel> and <eiffel>test</eiffel> themselves.
@@ -109,50 +104,50 @@ Going through <eiffel>item_action</eiffel> and <eiffel>item_test</eiffel> provid
Here now is an example illustrating the use of these mechanisms. The result will enable us to resize all the paragraphs of a text up to the first one that has been modified - as we might need to do, in a text processing system, to process an interactive user request. Assume a class <eiffel>TEXT</eiffel> that describes lists of paragraphs with certain additional features. The example will also assume a class <eiffel>PARAGRAPH</eiffel> with a procedure <eiffel>resize</eiffel>, and a boolean-valued attribute <eiffel>modified</eiffel> which indicates whether a paragraph has been modified. Class <eiffel>TEXT</eiffel> inherits from [[ref:/libraries/base/reference/linked_list_chart|LINKED_LIST]] and so is a descendant of [[ref:/libraries/base/reference/linear_chart|LINEAR]] : Here now is an example illustrating the use of these mechanisms. The result will enable us to resize all the paragraphs of a text up to the first one that has been modified - as we might need to do, in a text processing system, to process an interactive user request. Assume a class <eiffel>TEXT</eiffel> that describes lists of paragraphs with certain additional features. The example will also assume a class <eiffel>PARAGRAPH</eiffel> with a procedure <eiffel>resize</eiffel>, and a boolean-valued attribute <eiffel>modified</eiffel> which indicates whether a paragraph has been modified. Class <eiffel>TEXT</eiffel> inherits from [[ref:/libraries/base/reference/linked_list_chart|LINKED_LIST]] and so is a descendant of [[ref:/libraries/base/reference/linear_chart|LINEAR]] :
<code> <code>
class class
TEXT TEXT
inherit inherit
LINKED_LIST [PARAGRAPH] LINKED_LIST [PARAGRAPH]
... ...
feature feature
... ...
end</code> end</code>
In a class <eiffel>TEXT_PROCESSOR</eiffel>, you can use an iteration procedure to write a very simple procedure <eiffel>resize_ paragraphs</eiffel> that will resize all paragraphs up to but excluding the first one that has been modified: In a class <eiffel>TEXT_PROCESSOR</eiffel>, you can use an iteration procedure to write a very simple procedure <eiffel>resize_ paragraphs</eiffel> that will resize all paragraphs up to but excluding the first one that has been modified:
<code> <code>
class class
TEXT_PROCESSOR TEXT_PROCESSOR
inherit inherit
LINEAR_ITERATOR [PARAGRAPH] LINEAR_ITERATOR [PARAGRAPH]
redefine redefine
item_action, item_test item_action, item_test
end end
feature feature
resize_paragraphs (t: TEXT) resize_paragraphs (t: TEXT)
-- Resize all the paragraphs of t up to but excluding -- Resize all the paragraphs of t up to but excluding
-- the first one that has been modified. -- the first one that has been modified.
do do
set (t) set (t)
until_do until_do
end end
feature {NONE} feature {NONE}
item_test (p PARAGRAPH): BOOLEAN item_test (p PARAGRAPH): BOOLEAN
-- Has p been modified? -- Has p been modified?
do do
Result := p.modified Result := p.modified
end end
item_action (p: PARAGRAPH) item_action (p: PARAGRAPH)
-- Resize p. -- Resize p.
do do
p.resize p.resize
end end
... ...
end</code> end</code>
Thanks to the iteration mechanism, the procedure <eiffel>resize_paragraphs</eiffel> simply needs two procedure calls: Thanks to the iteration mechanism, the procedure <eiffel>resize_paragraphs</eiffel> simply needs two procedure calls:
@@ -208,48 +203,48 @@ An iterator will always apply to a certain target structure. The target is intro
Both the iterator classes and the traversal classes are generic, with a formal generic parameter G. The actual generic parameters will be matched through the choice of iteration target: for a generic derivation of the form <eiffel>SOME_ITERATOR</eiffel> [<eiffel>ACTUAL_TYPE</eiffel>] the target can only be of type <eiffel>SOME_TRAVERSABLE</eiffel> [<eiffel>ACTUAL_TYPE</eiffel>] for the same <eiffel>ACTUAL_TYPE</eiffel>, where <eiffel>SOME_TRAVERSABLE</eiffel> is the traversal class matching <eiffel>SOME_ITERATOR</eiffel> according to the preceding table ([[ref:/libraries/base/reference/linear_chart|LINEAR]] for [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] and so on), or one of its proper descendants. <br/> Both the iterator classes and the traversal classes are generic, with a formal generic parameter G. The actual generic parameters will be matched through the choice of iteration target: for a generic derivation of the form <eiffel>SOME_ITERATOR</eiffel> [<eiffel>ACTUAL_TYPE</eiffel>] the target can only be of type <eiffel>SOME_TRAVERSABLE</eiffel> [<eiffel>ACTUAL_TYPE</eiffel>] for the same <eiffel>ACTUAL_TYPE</eiffel>, where <eiffel>SOME_TRAVERSABLE</eiffel> is the traversal class matching <eiffel>SOME_ITERATOR</eiffel> according to the preceding table ([[ref:/libraries/base/reference/linear_chart|LINEAR]] for [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] and so on), or one of its proper descendants. <br/>
Each of the proper descendants of [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] redefines the type of target to the matching proper descendant of [[ref:/libraries/base/reference/traversable_chart|TRAVERSABLE]] , to cover more specific variants of the iteration target, For example in [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] the feature is redefined to be of type [[ref:/libraries/base/reference/linear_chart|LINEAR]] . [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] also introduces the procedure for selecting a target: Each of the proper descendants of [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] redefines the type of target to the matching proper descendant of [[ref:/libraries/base/reference/traversable_chart|TRAVERSABLE]] , to cover more specific variants of the iteration target, For example in [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] the feature is redefined to be of type [[ref:/libraries/base/reference/linear_chart|LINEAR]] . [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] also introduces the procedure for selecting a target:
<code> <code>
set (s: like target) set (s: like target)
-- Make s the new target of iterations. -- Make s the new target of iterations.
require require
s /= Void s /= Void
do do
target := s target := s
ensure ensure
target = s target = s
target /= Void target /= Void
end</code> end</code>
Next [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] introduces the routines describing the elementary action and test that will be applied to items of the iteration targets: Next [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] introduces the routines describing the elementary action and test that will be applied to items of the iteration targets:
<code> <code>
action action
-- Action to be applied to item at current position in -- Action to be applied to item at current position in
-- target. -- target.
-- (default: item_action on item at current position.) -- (default: item_action on item at current position.)
-- Note: for iterators to work properly, redefined -- Note: for iterators to work properly, redefined
-- versions of this feature should not change the -- versions of this feature should not change the
-- traversable structure. -- traversable structure.
require require
traversable_exists: target /= Void traversable_exists: target /= Void
not_off: not target.off not_off: not target.off
invariant_satisfied: invariant_value invariant_satisfied: invariant_value
do do
item_action (target.item>) item_action (target.item>)
ensure ensure
not_off: not target.off not_off: not target.off
invariant_satisfied: invariant_value invariant_satisfied: invariant_value
end end
test: BOOLEAN test: BOOLEAN
-- Test to be applied to item at current position in -- Test to be applied to item at current position in
-- target (default: value of item_test on item) -- target (default: value of item_test on item)
require require
traversable_exists: target /= Void traversable_exists: target /= Void
not_off: not target.off not_off: not target.off
do do
Result := item_test (target.item>) Result := item_test (target.item>)
ensure ensure
not target.off not target.off
end</code> end</code>
These routines rely on two others, <eiffel>item_action</eiffel> and <eiffel>item_test</eiffel>, which both take an argument of type G, the formal generic parameter. The reason, already noted above, is that in a vast majority of cases the iterated action and test solely depend, at each step of the traversal, on the item (of type G) at the current position. To define an iteration process, then, it suffices to redefine<eiffel> item_action</eiffel> and <eiffel>item_test</eiffel> in a descendant of the appropriate iteration class. Only in complex cases will it be necessary to redefine <eiffel>action</eiffel> and <eiffel>test</eiffel> themselves. <br/> These routines rely on two others, <eiffel>item_action</eiffel> and <eiffel>item_test</eiffel>, which both take an argument of type G, the formal generic parameter. The reason, already noted above, is that in a vast majority of cases the iterated action and test solely depend, at each step of the traversal, on the item (of type G) at the current position. To define an iteration process, then, it suffices to redefine<eiffel> item_action</eiffel> and <eiffel>item_test</eiffel> in a descendant of the appropriate iteration class. Only in complex cases will it be necessary to redefine <eiffel>action</eiffel> and <eiffel>test</eiffel> themselves. <br/>
If you encounter such a case, note the caveat about action changing the target's structure. Understandably enough, an iterator that attempts to change the data structure while traversing it may engage in strange behavior. No such risk exists if you only redefine <eiffel>item_action</eiffel>, which may change the contents of items but not the structure itself. <br/> If you encounter such a case, note the caveat about action changing the target's structure. Understandably enough, an iterator that attempts to change the data structure while traversing it may engage in strange behavior. No such risk exists if you only redefine <eiffel>item_action</eiffel>, which may change the contents of items but not the structure itself. <br/>
@@ -272,38 +267,37 @@ All these features, and most of the other iteration features introduced in prope
[[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] , an effective class, refines the iteration mechanisms for cases in which the target is a linear structure, such as a list in any implementation or a circular chain. <br/> [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] , an effective class, refines the iteration mechanisms for cases in which the target is a linear structure, such as a list in any implementation or a circular chain. <br/>
The class effects all the deferred features inherited from [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] , taking advantage of the linear traversal mechanisms present in the corresponding traversal class, [[ref:/libraries/base/reference/linear_chart|LINEAR]] . Here for example is the effecting of <eiffel>do_if</eiffel>: The class effects all the deferred features inherited from [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] , taking advantage of the linear traversal mechanisms present in the corresponding traversal class, [[ref:/libraries/base/reference/linear_chart|LINEAR]] . Here for example is the effecting of <eiffel>do_if</eiffel>:
<code> <code>
do_if do_if
-- Apply action to every item of target satisfying -- Apply action to every item of target satisfying
-- test. -- test.
do do
from from
target.start
target.start invariant
invariant invariant_value
invariant_value until
until target.exhausted
target.exhausted loop
loop if test then
if test then action
action end
end forth
forth end
end end</code>
end</code>
This routine text relies on features <eiffel>start</eiffel>, <eiffel>forth</eiffel> and <eiffel>exhausted</eiffel> which, together with <eiffel>off</eiffel>, have for convenience been carried over to [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] from their counterparts in [[ref:/libraries/base/reference/linear_chart|LINEAR]] , with feature declarations such as This routine text relies on features <eiffel>start</eiffel>, <eiffel>forth</eiffel> and <eiffel>exhausted</eiffel> which, together with <eiffel>off</eiffel>, have for convenience been carried over to [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] from their counterparts in [[ref:/libraries/base/reference/linear_chart|LINEAR]] , with feature declarations such as
<code> <code>
off: BOOLEAN off: BOOLEAN
-- Is position of target off? -- Is position of target off?
require require
traversable_exists: target /= Void traversable_exists: target /= Void
do do
Result := target.off Result := target.off
end</code> end</code>
and similarly for the others. <br/> and similarly for the others. <br/>
In addition to effecting the general iteration features from [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] , class [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] introduces iteration features that apply to the specific case of linear structures: In addition to effecting the general iteration features from [[ref:/libraries/base/reference/iterator_chart|ITERATOR]] , class [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] introduces iteration features that apply to the specific case of linear structures:
* <eiffel>search </eiffel> <code> ( b :BOOLEAN) moves the iteration to the first position satisfying test if b is true, or not satisfying test if b </code> is false. This use of a boolean argument to switch between two opposite semantics is not part of the recommended style, and you will find few if any other examples in the Base libraries. Here, however, it was deemed preferable to the alternative, which would have involved four separate procedures (if together with <eiffel>search</eiffel> we consider <eiffel>continue_search</eiffel> discussed next). * <eiffel>search (b :BOOLEAN)</eiffel> moves the iteration to the first position satisfying test if <code>b</code> is true, or not satisfying test if <code>b</code> is false. This use of a boolean argument to switch between two opposite semantics is not part of the recommended style, and you will find few if any other examples in the Base libraries. Here, however, it was deemed preferable to the alternative, which would have involved four separate procedures (if together with <eiffel>search</eiffel> we consider <eiffel>continue_search</eiffel> discussed next).
* With a linear structure we can implement an iteration corresponding to the 'for' loop of traditional programming languages, defined by three integers: the starting position, the number of items to be traversed, and the step between consecutive items. This is provided by procedure <eiffel>do_for</eiffel> <code> ( starting , number , step :INTEGER). </code> * With a linear structure we can implement an iteration corresponding to the 'for' loop of traditional programming languages, defined by three integers: the starting position, the number of items to be traversed, and the step between consecutive items. This is provided by procedure <eiffel>do_for</eiffel> <code> ( starting , number , step :INTEGER). </code>
* Since with a linear target the iterator can advance the cursor step by step, the basic iteration operations are complemented by variants which pick up from the position reached by the last call: <eiffel>continue_until</eiffel>, <eiffel>until_continue</eiffel>, <eiffel>continue_while</eiffel>, <eiffel>while_continue</eiffel>, <eiffel>continue_search</eiffel>, <eiffel>continue_for</eiffel>. * Since with a linear target the iterator can advance the cursor step by step, the basic iteration operations are complemented by variants which pick up from the position reached by the last call: <eiffel>continue_until</eiffel>, <eiffel>until_continue</eiffel>, <eiffel>continue_while</eiffel>, <eiffel>while_continue</eiffel>, <eiffel>continue_search</eiffel>, <eiffel>continue_for</eiffel>.
@@ -316,70 +310,70 @@ Contrary to what one might at first imagine, class [[ref:/libraries/base/referen
The trick is to use repeated inheritance. [[ref:/libraries/base/reference/two_way_chain_iterator_chart|TWO_WAY_CHAIN_ITERATOR]] inherits twice from [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] ; the first inheritance branch yields the forward iteration features, the second yields those for backward iteration. There is no need for any explicit declaration or redeclaration of iteration features. Here is the entire class text that yields this result: The trick is to use repeated inheritance. [[ref:/libraries/base/reference/two_way_chain_iterator_chart|TWO_WAY_CHAIN_ITERATOR]] inherits twice from [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] ; the first inheritance branch yields the forward iteration features, the second yields those for backward iteration. There is no need for any explicit declaration or redeclaration of iteration features. Here is the entire class text that yields this result:
<code> <code>
class class
TWO_WAY_CHAIN_ITERATOR [G] TWO_WAY_CHAIN_ITERATOR [G]
inherit inherit
LINEAR_ITERATOR [G] LINEAR_ITERATOR [G]
redefine redefine
target target
select select
start, start,
forth, forth,
do_all, do_all,
until_do, until_do,
do_until, do_until,
do_if, do_if,
do_for, do_for,
search, search,
forall, forall,
exists, exists,
until_continue, until_continue,
continue_until, continue_until,
continue_for, continue_for,
continue_search continue_search
end end
LINEAR_ITERATOR [G] LINEAR_ITERATOR [G]
rename rename
start as finish, start as finish,
forth as back, forth as back,
do_all as do_all_back, do_all as do_all_back,
until_do as until_do_back, until_do as until_do_back,
do_until as do_until_back, do_until as do_until_back,
do_if as do_if_back, do_if as do_if_back,
do_for as do_for_back, do_for as do_for_back,
search as search_back, search as search_back,
forall as forall_back, forall as forall_back,
exists as exists_back, exists as exists_back,
until_continue as until_continue_back, until_continue as until_continue_back,
continue_until as continue_until_back, continue_until as continue_until_back,
continue_for as continue_for_back, continue_for as continue_for_back,
continue_search as continue_search_back continue_search as continue_search_back
redefine redefine
target target
end end
feature -- Status report feature -- Status report
target: BI_LINEAR [G] target: BI_LINEAR [G]
-- The structure to which iteration features will -- The structure to which iteration features will
-- apply -- apply
feature -- Cursor movement feature -- Cursor movement
finish finish
-- Move cursor of target to last position. -- Move cursor of target to last position.
do do
target.finish target.finish
end end
back back
-- Move cursor of target backward one position. -- Move cursor of target backward one position.
do do
target.back target.back
end end
end end
</code> </code>
This class provides a good example of the economy of expression that the full inheritance mechanism affords through the combination of renaming, redefinition, repeated inheritance rules and selection, without sacrificing clarity and maintainability. This class provides a good example of the economy of expression that the full inheritance mechanism affords through the combination of renaming, redefinition, repeated inheritance rules and selection, without sacrificing clarity and maintainability.
@@ -410,72 +404,72 @@ The next two techniques will remove this limitation.
One way to obtain several iteration schemes is a simple extension to the single descendant technique. You can use repeated inheritance to provide two or more variants. We have in fact already encountered the technique when studying how to derive [[ref:/libraries/base/reference/two_way_chain_iterator_chart|TWO_WAY_CHAIN_ITERATOR]] and [[ref:/libraries/base/reference/cursor_tree_iterator_chart|CURSOR_TREE_ITERATOR]] from [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] . The general pattern, applied here to just two iteration schemes but easily generalized to more, is straightforward: One way to obtain several iteration schemes is a simple extension to the single descendant technique. You can use repeated inheritance to provide two or more variants. We have in fact already encountered the technique when studying how to derive [[ref:/libraries/base/reference/two_way_chain_iterator_chart|TWO_WAY_CHAIN_ITERATOR]] and [[ref:/libraries/base/reference/cursor_tree_iterator_chart|CURSOR_TREE_ITERATOR]] from [[ref:/libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] . The general pattern, applied here to just two iteration schemes but easily generalized to more, is straightforward:
<code> <code>
class class
DUAL_PROCESSOR DUAL_PROCESSOR
inherit inherit
LINEAR_ITERATOR [SOME_TYPE] LINEAR_ITERATOR [SOME_TYPE]
rename rename
item_action as action1, item_action as action1,
item_test as test1, item_test as test1,
do_if as do_if1, do_if as do_if1,
redefine redefine
action1, test1 action1, test1
select select
action1, test1 action1, test1
end end
LINEAR_ITERATOR [SOME_TYPE] LINEAR_ITERATOR [SOME_TYPE]
rename rename
item_action as action2, item_action as action2,
item_test as test2, item_test as test2,
do_if as do_if2, do_if as do_if2,
redefine redefine
action2, test2 action2, test2
end end
feature feature
action1 action1
-- Action for the first scheme -- Action for the first scheme
do do
... ...
end end
test1: BOOLEAN test1: BOOLEAN
-- Test for the first scheme -- Test for the first scheme
do do
... ...
end end
action2 action2
-- Action for the second scheme -- Action for the second scheme
do do
... ...
end end
test2: BOOLEAN test2: BOOLEAN
-- Test for the second scheme -- Test for the second scheme
do do
... ...
end end
iterate1 iterate1
-- Execute iteration of first kind. -- Execute iteration of first kind.
do do
set (...) set (...)
do_if1 do_if1
end end
iterate2 iterate2
-- Execute iteration of second kind. -- Execute iteration of second kind.
do do
set (...) set (...)
do_if2 do_if2
end end
... ...
end end
</code> </code>
The repeated inheritance machinery takes care of the rest. The repeated inheritance machinery takes care of the rest.
@@ -485,97 +479,97 @@ To obtain maximum flexibility, classes that need iteration facilities should be
The following example illustrates the technique. Consider a deferred class <eiffel>FIGURE</eiffel> describing the notion of graphical figure, with many effective descendants ( <eiffel>POLYGON</eiffel>, <eiffel>CIRCLE</eiffel> and so on). It is useful to define <eiffel>COMPLEX_FIGURE</eiffel>, describing figures that are recursively composed of sub-figures. This is a remarkable example of multiple inheritance: The following example illustrates the technique. Consider a deferred class <eiffel>FIGURE</eiffel> describing the notion of graphical figure, with many effective descendants ( <eiffel>POLYGON</eiffel>, <eiffel>CIRCLE</eiffel> and so on). It is useful to define <eiffel>COMPLEX_FIGURE</eiffel>, describing figures that are recursively composed of sub-figures. This is a remarkable example of multiple inheritance:
<code> <code>
class class
COMPLEX_FIGURE COMPLEX_FIGURE
inherit inherit
FIGURE, FIGURE,
LINKED_LIST [FIGURE] LINKED_LIST [FIGURE]
feature feature
... ...
end end
</code> </code>
In the feature clause we want to provide the appropriate effectings for the deferred features of class <eiffel>FIGURE</eiffel>: <eiffel>display</eiffel>, <eiffel>hide</eiffel>, <eiffel>translate</eiffel> and all other basic figure operations. <br/> In the feature clause we want to provide the appropriate effectings for the deferred features of class <eiffel>FIGURE</eiffel>: <eiffel>display</eiffel>, <eiffel>hide</eiffel>, <eiffel>translate</eiffel> and all other basic figure operations. <br/>
We can use loops for that purpose, for example We can use loops for that purpose, for example
<code> <code>
display display
-- Recursively display all components of the complex -- Recursively display all components of the complex
-- figure -- figure
do do
from from
start start
until until
exhausted exhausted
loop loop
item.display item.display
forth forth
end end
end end
</code> </code>
Although acceptable and even elegant, this scheme will cause significant duplication: all the <eiffel>FIGURE</eiffel> features - not just <eiffel>display</eiffel> but also <eiffel>hide</eiffel>, <eiffel>rotate</eiffel>, <eiffel>move</eiffel> and others - will have the same structure, with a loop. We can use iterators to avoid this duplication. The repeated inheritance technique would work, but given the large number of <eiffel>FIGURE</eiffel> features the amount of repeated inheritance that would be needed seems unwieldy. It is also not very desirable to have to change the inheritance structure of the system just to add a new feature to <eiffel>FIGURE</eiffel>. The more dynamic approach using iterator objects seems preferable. <br/> Although acceptable and even elegant, this scheme will cause significant duplication: all the <eiffel>FIGURE</eiffel> features - not just <eiffel>display</eiffel> but also <eiffel>hide</eiffel>, <eiffel>rotate</eiffel>, <eiffel>move</eiffel> and others - will have the same structure, with a loop. We can use iterators to avoid this duplication. The repeated inheritance technique would work, but given the large number of <eiffel>FIGURE</eiffel> features the amount of repeated inheritance that would be needed seems unwieldy. It is also not very desirable to have to change the inheritance structure of the system just to add a new feature to <eiffel>FIGURE</eiffel>. The more dynamic approach using iterator objects seems preferable. <br/>
To implement this approach, define a class for iterating on complex figures: To implement this approach, define a class for iterating on complex figures:
<code> <code>
class class
COMPLEX_FIGURE_ITERATOR COMPLEX_FIGURE_ITERATOR
inherit inherit
LINEAR_ITERATOR LINEAR_ITERATOR
redefine redefine
target target
end end
create create
set set
feature feature
target: COMPLEX_FIGURE target: COMPLEX_FIGURE
end end
</code> </code>
Then for each operation to be iterated define a small class. For example: Then for each operation to be iterated define a small class. For example:
<code> <code>
class class
FIGURE_DISPLAYER FIGURE_DISPLAYER
inherit inherit
COMPLEX_FIGURE_ITERATOR COMPLEX_FIGURE_ITERATOR
redefine redefine
item_action item_action
end end
create create
set set
feature feature
item_action (f: FIGURE) item_action (f: FIGURE)
-- Action to be applied to each figure: display -- Action to be applied to each figure: display
-- it. -- it.
do do
f.display f.display
end end
end end
</code> </code>
Similarly, you may define <eiffel>FIGURE_HIDER</eiffel>, <eiffel>FIGURE_MOVER</eiffel> and others. Then the features of <eiffel>COMPLEX_FIGURE</eiffel> are written almost trivially, without any explicit loops; for example: Similarly, you may define <eiffel>FIGURE_HIDER</eiffel>, <eiffel>FIGURE_MOVER</eiffel> and others. Then the features of <eiffel>COMPLEX_FIGURE</eiffel> are written almost trivially, without any explicit loops; for example:
<code> <code>
display display
-- Recursively display all components of the complex -- Recursively display all components of the complex
-- figure -- figure
local local
disp: FIGURE_DISPLAYER disp: FIGURE_DISPLAYER
do do
create disp.set (Current) create disp.set (Current)
disp.do_all disp.do_all
end end
</code> </code>
and similarly for all the others. <br/> and similarly for all the others. <br/>
Note the use of <eiffel>set</eiffel> as creation procedure, which is more convenient than requiring the clients first to create an iterator object and then to call <eiffel>set</eiffel>. This is also safer, since with <eiffel>set</eiffel> as a creation procedure the client cannot forget to initialize the target. (If a class <eiffel>C</eiffel> has a creation clause, the creation instruction <code> create </code> <eiffel>C</eiffel>.) Note the use of <eiffel>set</eiffel> as creation procedure, which is more convenient than requiring the clients first to create an iterator object and then to call <eiffel>set</eiffel>. This is also safer, since with <eiffel>set</eiffel> as a creation procedure the client cannot forget to initialize the target. (If a class <eiffel>C</eiffel> has a creation clause, the creation instruction <code>create </code> <eiffel>C</eiffel>.)