容器迭代器上的抽象 [英] Abstraction over container iterators

查看:124
本文介绍了容器迭代器上的抽象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里谈论Ada 2012。

我会先让代码发言:

with Ada.Containers.Hashed_Maps;                                                
with Ada.Strings.Unbounded;                                                     
with Ada.Strings.Unbounded.Hash_Case_Insensitive;                               
with Ada.Strings.Unbounded.Equal_Case_Insensitive;                              

package Environments is                                                         

   type Environment is tagged private;                                          

   function Variable (                                                          
      E    : in Environment;                                                    
      Name : in Ada.Strings.Unbounded.Unbounded_String                          
   )                                                                            
      return Ada.Strings.Unbounded.Unbounded_String                             
      with Inline;                                                              

   procedure Set_Variable (                                                     
      E     : in out Environment;                                               
      Name  : in Ada.Strings.Unbounded.Unbounded_String;                        
      Value : in Ada.Strings.Unbounded.Unbounded_String                         
   )                                                                            
      with Inline;                                                              

private                                                                         

   package Variable_Maps is new Ada.Containers.Hashed_Maps (                    
      Key_Type        => Ada.Strings.Unbounded.Unbounded_String,                
      Element_Type    => Ada.Strings.Unbounded.Unbounded_String,                
      Hash            => Ada.Strings.Unbounded.Hash_Case_Insensitive,           
      Equivalent_Keys => Ada.Strings.Unbounded.Equal_Case_Insensitive,          
      "="             => Ada.Strings.Unbounded."="                              
   );                                                                           

   type Environment is tagged record                                            
      Variables : Variable_Maps.Map;                                            
   end record;                                                                  

end Environments;

我们这里有一个示例包,很好地说明了我的问题。我在 Hashed_Map 中存储了一些环境变量,但是我想在标准容器上构建一个抽象层,所以我将来可以在不更改任何代码的情况下更改底层容器我的包的客户。

What we have here is an example package fairly well illustrating my problem. I'm storing some environment variables in Hashed_Map, but I want to build a abstraction layer over the standard container, so I can in future change the underlaying container without changing any code in my package's customers.

获取和设置变量很容易 - 如上所述。真正的问题是迭代。我想让我的软件包的客户遍历环境,轻松获得每个元素的关键和价值。

Getting and setting variables is easy - as declared above. The real problem is iterating. I'd like to let my package's customers to iterate over the environment and get both key and value for each element easily.

因为我正在使用Ada 2012最好的方式会是使用迭代器,但如何?我可以将光标返回到底层容器,但同样,这个光标的界面将依赖于容器。

As I'm using Ada 2012 the best way would be to use iterators, but how? I could return a cursor to the underlaying container, but again, this cursor's interface would be container-dependent.

实现这种抽象的最佳方式是什么?容器迭代?

推荐答案

看看Ada Gems #127 #128 ,Ada 2012中的迭代器,第1部分和第2部分获得指导如何创建自己的迭代器。

Take a look at Ada Gems #127 and #128, "Iterators in Ada 2012, Parts 1 & 2" for guidance on how to create your own iterators.

这篇关于容器迭代器上的抽象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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