From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1042f393323e22da X-Google-Attributes: gid103376,public From: gauthier@alphainfo.unilim.fr (Michel Gauthier) Subject: Re: Iterator Syntax [was: Re: STL in Ada95] Date: 1997/05/23 Message-ID: X-Deja-AN: 243357723 References: <337813DF.598C@dynamite.com.au> <337D3AE4.28F7@dynamite.com.au> <337E5854.1366@sprintmail.com> <12871CEBFAB00ABE.93483F73373D0261.D1086334F6EF8ED8@library-proxy.airnews.net> <3380F4A5.1EBB@sprintmail.com> <33838C37.29C0@sprintmail.com> <33859890.5587@spam.innocon.com> Organization: Universite de Limoges Newsgroups: comp.lang.ada Date: 1997-05-23T00:00:00+00:00 List-Id: In article <33859890.5587@spam.innocon.com>, carter@spam.innocon.com wrote: >> Brian Rogoff wrote: >> > >> > John Volan wrote: >> ... >> > > generic >> > > type Iterator_Type is private; >> > > type Item_Type is private; >> > > with procedure Advance (Iterator : in out Iterator_Type); >> > > with function Get_Item (Iterator : in Iterator_Type) return >> > > Item_Type; >> > > ... -- whatever other formals >> > > procedure Do_Whatever -- whatever the algorithm is >> > > (Start, Stop : in Iterator_Type; ... ); -- whatever parameters >> >> I'm curious. What is the advantage of "exposed" iterators like these >> over encapsulated iterators for a structure: >> >> generic -- Iterate >> with procedure Action (Item : in out Element; Continue : out >> Boolean); >> procedure Iterate (Over : in Structure); >> >> [...] A personal answer : "file-like" iterators can be put together into more complex ones like iterators for tuples, for abstract types (for instances dates from years, months and days), or like merges of various kinds. Generic iterators can't (to be honest ; all specific solutions exist, but they require copying and editing, whereas merges, tuple and abstract composers are components). Additionally, I prefer richer specifications like the following : with ADA . EXCEPTIONS ; with NO_OPERATION ; with CONSTANT_UNARY ; generic type ITEM_TYPE is private ; package SCANS . SIMPLE is type SCAN_ROOT is abstract tagged limited null record ; type SCAN_CLASS is access all SCAN_ROOT ' CLASS ; type SCAN_ARRAY is array ( POSITIVE range <> ) of SCAN_CLASS ; -- for non-binary merges --## polymorphic subprograms for normal usage ##-- -- building SCAN_CLASS values is always specific procedure RESET ( THE_SCAN : in out SCAN_CLASS ) ; procedure CLOSE ( THE_SCAN : in out SCAN_CLASS ) ; procedure FORCE ( THE_SCAN : in out SCAN_CLASS ) ; -- CLOSE without exception procedure READ ( THE_SCAN : in SCAN_CLASS ; THE_ITEM : out ITEM_TYPE ) ; procedure SKIP ( THE_SCAN : in SCAN_CLASS ) ; function NEXT ( OF_SCAN : SCAN_CLASS ) return ITEM_TYPE ; function ITEM ( OF_SCAN : SCAN_CLASS ) return ITEM_TYPE ; function END_OF_SCAN ( OF_SCAN : SCAN_CLASS ) return BOOLEAN ; function NEW_OBJECT ( LIKE : SCAN_CLASS ) return SCAN_CLASS ; function OBJECT_NAME ( OF_SCAN : SCAN_CLASS ) return STRING ; function CLASS_NAME ( OF_SCAN : SCAN_CLASS ) return STRING ; function CLASS_NAME return STRING ; NAME_OF_CLASS : STRING := "simple-scans" ; --## default generic parameters with no other usage (not private because of technical reasons) ##-- function TRUE is new CONSTANT_UNARY ( ITEM_TYPE , BOOLEAN , STANDARD . TRUE ) ; --## polymorphic iterator for normal use ##-- generic with procedure REPEAT ( USING : in ITEM_TYPE ) ; with procedure BEFORE_SCANNING is NO_OPERATION ; with procedure AFTER_SCANNING is NO_OPERATION ; with procedure ON_EMPTY_SET is NO_OPERATION ; with procedure FOR_REPEAT_ERROR ( REPORT : in ADA . EXCEPTIONS . EXCEPTION_OCCURRENCE ) is ADA . EXCEPTIONS . EXCEPTION_OCCURRENCE.RERAISE_OCCURRENCE ; with procedure FOR_SCAN_ERROR ( REPORT : in ADA . EXCEPTIONS . EXCEPTION_OCCURRENCE ) is ADA . EXCEPTIONS . EXCEPTION_OCCURRENCE.RERAISE_OCCURRENCE ; with function CONDITION ( USING : ITEM_TYPE ) return BOOLEAN is TRUE ; procedure SIMPLE_APPLYER ( SCAN : in out SCAN_CLASS ) ; -- the scan is assumed to be opened and reset before the call and is returned opened and reset --## abstract subprograms for exceptional cases (not private because of technical reasons) ##-- -- no OPEN can be specified at this root point since the set type is always specific procedure RESET ( THE_SCAN : in out SCAN_ROOT ) is abstract ; procedure CLOSE ( THE_SCAN : in out SCAN_ROOT ) is abstract ; procedure FORCE ( THE_SCAN : in out SCAN_ROOT ) is abstract ; procedure READ ( THE_SCAN : in SCAN_ROOT ; THE_ITEM : out ITEM_TYPE ) is abstract ; procedure SKIP ( THE_SCAN : in SCAN_ROOT ) is abstract ; function NEXT ( OF_SCAN : SCAN_ROOT ) return ITEM_TYPE is abstract ; function ITEM ( OF_SCAN : SCAN_ROOT ) return ITEM_TYPE is abstract ; function END_OF_SCAN ( OF_SCAN : SCAN_ROOT ) return BOOLEAN is abstract ; function OBJECT_NAME ( OF_SCAN : SCAN_ROOT ) return STRING ; function CLASS_NAME ( OF_SCAN : SCAN_ROOT ) return STRING ; private function CREATE ( LIKE : access SCAN_ROOT ) return SCAN_CLASS is abstract ; -- suitable to build specific bodies end SCANS . SIMPLE ; How to use it ? First ; declare and create the iterator object : SCAN : constant SPECIFIC_SCAN . SCAN_TYPE := SPECIFIC_SCAN . NEW_OBJECT ( with possible parameters defining the set to be scanned) ; Then either use it exactly like a file or use it in the call of an instantiation : procedure REAL_SCAN is new SIMPLE_APPLYER ( REPEAT => to_be_repeated ) ; ... REAL_SCAN ( SCAN ) ; A complex specification, but with a very versatile extensibility and combinability, and also with a very simple use in both file-like and generic forms. ---------- ---------- ---------- ---------- Michel Gauthier / Laboratoire d'informatique 123 avenue Albert Thomas / F-87060 Limoges telephone + 33 5 55 43 69 73 fax +33 5 55 43 69 77 ---------- ---------- ---------- ---------- Si l'an 2000 est pour vous un mysticisme stupide, utilisez la base 9 If you feel year 2000 a stupid mystic craze, use numeration base 9 ---------- ---------- ---------- ----------