简体   繁体   English

带有编译时初始化的常量数据的容器类

[英]container class for constant data with compile time initialization

I search something which is std::vector but without the big overhead and a bit more than std::array, because with std::array I did not have the size stored anyway ( it is only known from the type itself ). 我搜索的东西是std :: vector,但没有太大的开销,而且比std :: array还要多,因为使用std :: array时,我无论如何都没有存储大小(只能从类型本身知道)。

What I want to achieve: 我要实现的目标:

Written with "dynamic" containers it is like: 用“动态”容器编写,就像:

std::map< int, std::vector< std::pair<int,int>>>;

I need no modification during runtime, but I need the size information during runtime. 运行时不需要修改,但是运行时需要尺寸信息。 Replacing std::vector with std::array could not work, because the array must be the same size for all map entries which is not what I need. 用std :: array替换std :: vector无法正常工作,因为所有地图条目的数组大小都必须相同,这不是我所需要的。

I only! 我只是! want to ask if there is already an available implementation around. 想问一下周围是否已有可用的实现。 If the answer is simply "No", there is no need for a suggestion how to do the job. 如果答案仅仅是“否”,则无需建议如何完成这项工作。 I only want to not reinvent the wheel again :-) 我只想不再重新发明轮子:-)

Background: I can use the stl on my small avr controllers, but the overhead is "a bit" to high. 背景:我可以在小型AVR控制器上使用stl,但是开销“有点”高。 So I search for a hopefully standard implementation which fit the needs for compile time constant representation with implemented features like begin()/end() and iterators to fulfill the minimum container requirements to get them used with range based for and others. 因此,我希望寻找一种标准的实现方式,该实现方式应符合begin()/ end()和迭代器等已实现功能的编译时常数表示形式的需要,以满足最低的容器要求,以使其与基于范围的for和其他类型一起使用。

c++14 is also available, if there is something I search for. 如果我有搜索,也可以使用c ++ 14。

All what I found is full template implemented where the access to the data is also compile time constant like: 我发现的全部是实现的完整模板,其中对数据的访问也可以是编译时间常数,例如:

container.get<2>() 

which I also could not use, because I need runtime vars to access my data. 我也无法使用它,因为我需要运行时变量来访问我的数据。

EDIT: Which problem/overhead comes up while using std::vector: 编辑:使用std :: vector时出现哪个问题/开销:

While using std::vector I need also new/delete which results in having malloc/free for avr. 在使用std :: vector时,我还需要new / delete,这会导致avr具有malloc / free。 I also found that on avr the initialization of the vector itself takes arround 350 bytes code for each template instance I use. 我还发现,在avr上,向量本身的初始化对于我使用的每个模板实例都需要大约350个字节的代码。 The access functions like operator[] and also the iterators are very small. operator[]这样的访问函数以及迭代器都非常小。

为了避免std::vector开销 ,您可以改用std::initializer_list

const std::map<int, std::initializer_list<std::pair<int, int>>>

Not entirely clear to me what you're looking for, but I think you could achieve what you need with the following: 我不太清楚您要寻找的是什么,但我认为您可以通过以下方法实现您所需要的:

  1. You don't know the exact size of each contained element (say N elements), and you will know it when program starts. 您不知道每个包含的元素(例如N个元素)的确切大小,并且在程序启动时会知道它的大小。 Then, whenever you know the size of the containers, reserve just one contiguous memory block to hold all the inner elements of all the containers. 然后,只要您知道容器的大小,就只保留一个连续的内存块来容纳所有容器的所有内部元素。 (one dynamic allocation) (一个动态分配)
  2. Create the map with vectors constructed via range: std::vector(start, end) , where start and end are calculated via blocks of N elements. 使用通过范围构造的矢量创建地图: std::vector(start, end) ,其中start和end通过N个元素的块进行计算。
  3. Then populate the map. 然后填充地图。 If you don't strictly need a map, you can also calculate where each vector will begin and end, and just create an initial array with indexes of the positions of each vector... 如果您不需要严格的地图,还可以计算每个向量的开始和结束位置,只需创建一个包含每个向量位置索引的初始数组即可。

What you are asking seems to be impossible in principle. 从原则上讲您要问的似乎是不可能的。 You say you want to avoid heap allocation of your vector like type, but the size of a type on the stack must be known at compile time, and the same for all members of that type. 您说过要避免像矢量一样分配向量的堆分配,但是必须在编译时知道堆栈上类型的大小,并且该类型的所有成员的大小都应相同。 Since map is a homogeneous container, the value type must be a single type with a constant size. 由于map是同质容器,因此值类型必须是具有恒定大小的单个类型。 You could replace the map with a tuple, but then all of your keys would have to be known at compile time. 您可以用一个元组替换该映射,但随后必须在编译时知道所有键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM