Klaus
Klaus

Reputation: 25623

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 ).

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.

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. 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.

c++14 is also available, if there is something I search for.

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:

While using std::vector I need also new/delete which results in having malloc/free for avr. I also found that on avr the initialization of the vector itself takes arround 350 bytes code for each template instance I use. The access functions like operator[] and also the iterators are very small.

Upvotes: 0

Views: 511

Answers (3)

Nir Friedman
Nir Friedman

Reputation: 17704

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. You could replace the map with a tuple, but then all of your keys would have to be known at compile time.

Upvotes: 0

Jarod42
Jarod42

Reputation: 217775

To avoid the overhead of std::vector, you may use instead std::initializer_list

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

Upvotes: 2

Diego Sevilla
Diego Sevilla

Reputation: 29021

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. 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.
  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...

Upvotes: 1

Related Questions