iterator - Eiffel Iterable implementation on MAP? -
iterator - Eiffel Iterable implementation on MAP? -
i have class called map :
class map [key,val] inherit iterable [key] i implemented new cursor within map cursor returns ,
map_iterator_cursor [key] and passes iterable cursor array of keys iterate through
i implemented map_iterator_cursor [key] class
class map_iterator_cursor [key] inherit iteration_cursor [key] for class implemented feature item: val because class defined key won't recognize val how map_iterator_cursor [key] item feature homecoming val associated key on @ moment ?
knowing map has function called item take key , returns val associated key
item (k: key): val
as map [key, val] inherits iterable [key], generic parameter of iteration_cursor bound key. {iteration_cursor}.item normal feature, subject redeclaration, renaming, etc. hence several approaches can fit need:
declare map_iterator_cursor have 2 formal generics , declare {map}.new_cursor follows:
class map [key, val] inherit iterable [key] feature new_cursor: map_iterator_cursor [key, val] create result.make (current) end end class map_iterator_cursor [key, val] inherit iteration_cursor [key] create create feature create (t: target) target := t end target: map [key, val] item: key ... value: val result := target.item (item) end end then client code can as
across map c loop -- utilize `c.item` of type key. -- utilize `c.value` of type val. end if required {map_iterator_cursor}.item of type val, first way utilize same code above rename feature item comes iterable:
class map_iterator_cursor [key, val] inherit iteration_cursor [key] rename item key end ... key: key ... item: val result := target.item (key) end end then client code can as
across map c loop -- utilize `c.item` of type val. -- utilize `c.key` of type key. end the iteration can performed on items of type val beginning. in case actual generic of iterable should val:
class map [key, val] inherit iterable [val] feature new_cursor: map_iterator_cursor [key, val] create result.make (current) end end class map_iterator_cursor [key, val] inherit iteration_cursor [val] create create feature create (t: target) target := t end target: map [key, val] item: val result := target.item (key) end key: key -- feature can not exported, or not nowadays -- `item` can implemented. end the client code similar case 2, key may unavailable:
across map c loop -- utilize `c.item` of type val. end in 3 formal generic key in map_iteration_cursor kept convenience. can removed provided there way access items of map, may raise other issues related access map, conformance , cat-calls. hence though it's potentially feasible, not go it.
iterator iterable eiffel
Comments
Post a Comment