在MAP上的Eiffel可迭代实现? [英] Eiffel Iterable implementation on MAP?

查看:56
本文介绍了在MAP上的Eiffel可迭代实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫做MAP的类:

I have a class called MAP :

 class MAP [KEY,VAL]
       inherit ITERABLE [KEY]

我在返回的地图游标中实现了新游标

I implemented the new cursor inside the map cursor which returns and

MAP_ITERATOR_CURSOR [KEY]

并将该可迭代游标传递给KEYS数组以迭代

and passes that iterable cursor an array of KEYS to iterate through

我实现了MAP_ITERATOR_CURSOR [KEY]

 class MAP_ITERATOR_CURSOR [KEY]
       inherit ITERATION_CURSOR [KEY]

对于该类,我实现了功能项:VAL,但是由于该类是用KEY定义的,因此无法识别VAL如何获取MAP_ITERATOR_CURSOR [KEY]项功能以返回与之关联的VAL我们目前所处的钥匙?

for this class I implemented the feature item: VAL but because the class was defined with KEY it won't recognize VAL how do I get MAP_ITERATOR_CURSOR [KEY] item feature to return the VAL associated with the key that we are on at the moment ?

知道MAP具有一个称为item的函数,该函数获取密钥并返回与该密钥相关联的VAL

Knowing that MAP has a function called item which take key and returns VAL associated with that key

item (k: KEY): VAL 

推荐答案

MAP [KEY, VAL]继承ITERABLE [KEY]后,ITERATION_CURSOR的通用参数将绑定到KEY.但是{ITERATION_CURSOR}.item只是正常功能,可以重新声明,重命名等.因此,有几种方法可以满足您的需求:

As soon as MAP [KEY, VAL] inherits ITERABLE [KEY], the generic parameter of ITERATION_CURSOR is bound to KEY. However {ITERATION_CURSOR}.item is just a normal feature, that is subject to redeclaration, renaming, etc. Therefore several approaches can fit your need:

  1. 声明MAP_ITERATOR_CURSOR具有两个正式的泛型,并声明{MAP}.new_cursor如下:

  1. Declare MAP_ITERATOR_CURSOR to have two formal generics and declare {MAP}.new_cursor as follows:

class MAP [KEY, VAL] inherit ITERABLE [KEY] feature
    new_cursor: MAP_ITERATOR_CURSOR [KEY, VAL]
        do
            create Result.make (Current)
        end
end

class MAP_ITERATOR_CURSOR [KEY, VAL] inherit ITERATION_CURSOR [KEY]
create make
feature
    make (t: like target)
        do
            target := t
        end
    target: MAP [KEY, VAL]
    item: KEY ...
    value: VAL
        do
            Result := target.item (item)
        end
end

然后客户端代码可以看起来像

Then the client code can look as

across map as c loop
    -- Use `c.item` of type KEY.
    -- Use `c.value` of type VAL.
end

  • 如果要求{MAP_ITERATOR_CURSOR}.item的类型为VAL,第一种方法是使用与上面完全相同的代码,但重命名来自ITERABLE的功能item:

  • If it is required that {MAP_ITERATOR_CURSOR}.item is of type VAL, the first way is to use exactly the same code as above but rename feature item that comes from ITERABLE:

    class MAP_ITERATOR_CURSOR [KEY, VAL] inherit
        ITERATION_CURSOR [KEY] rename item as key end
    ...
        key: KEY ...
        item: VAL
            do
                Result := target.item (key)
            end
    end
    

    然后客户端代码可以看起来像

    Then the client code can look as

    across map as c loop
        -- Use `c.item` of type VAL.
        -- Use `c.key` of type KEY.
    end
    

  • 可以从一开始就对VAL类型的项目执行迭代.在这种情况下,ITERABLE的实际泛型应为VAL:

  • The iteration can be performed over items of type VAL from the very beginning. In that case the actual generic of ITERABLE should be VAL:

    class MAP [KEY, VAL] inherit ITERABLE [VAL] feature
        new_cursor: MAP_ITERATOR_CURSOR [KEY, VAL]
            do
                create Result.make (Current)
            end
    end
    
    class MAP_ITERATOR_CURSOR [KEY, VAL] inherit ITERATION_CURSOR [VAL]
    create make
    feature
        make (t: like target)
            do
                target := t
            end
        target: MAP [KEY, VAL]
        item: VAL
            do
                Result := target.item (key)
            end
        key: KEY
                -- This feature can be not exported, or even not present
                -- as soon as `item` can be implemented.
    end
    

    客户端代码类似于情况2,但是key可能不可用:

    The client code is similar to case 2, but key may be unavailable:

    across map as c loop
        -- Use `c.item` of type VAL.
    end
    

  • 在3中,为了方便起见,保留了MAP_ITERATION_CURSOR中的形式泛型KEY.如果可以通过某种方式访问​​MAP的项目,则可以将其删除,但这可能会引起其他一些与访问MAP,一致性和CAT调用有关的问题.因此,尽管这可能可行,但我不会这么做.

  • In 3 the formal generic KEY in MAP_ITERATION_CURSOR is kept for convenience. It can be removed provided that there is some way to access items of MAP, but this may raise some other issues related to access to MAP, conformance and CAT-calls. Therefore though it's potentially feasible, I would not go for it.

    这篇关于在MAP上的Eiffel可迭代实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆