Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a random access container type that accepts a prefilled & preallocated buffer?

I'm about to write something myself since I didn't find what I was looking for, but figured I should ask the crowd before diving in.

  • Is there a boost or stl random access container type that allows passing in of prefilled buffer?

The imaginary(?) container type would operate something like this:

  const int blobSize = unpackBlobSize( msg );
  int *     blob     = unpackBlobData( msg );

  SpecialVector<int> mySpecialIntVector( blob, blobSize );

Basically I'm interfacing an old library with c-style raw pointers-to-buffers, but would like to use C++ style container semantics without requiring a copy step. What I would hope to have is std::vector plus preallocated & prefilled buffer constructor and, minus resize.

Libeigen has this sort of functionality with their Eigen::Map which allows things like the following:

  int array[9];
  for(int i = 0; i < 9; ++i) 
      array[i] = i;
  stl::cout << Eigen::Map<Eigen::Matrix3i>(array) << stl::endl;

Anyone know of a boost or stl template that covers these constraints?

like image 645
Catskul Avatar asked Jul 28 '11 03:07

Catskul


People also ask

Which of the container has random access on the container?

An iterator that reads, writes, and allows random access to a container.

Which of the following container does not support random access iterators?

All STL containers do not support all 5 types of iterators. For instance, random-access iterators are supported by the container “vector”, whereas bidirectional iterators are supported by the container “list”.

Which of the following is supported by random access iterator?

The RandomAccessIterator concept adds support for constant-time advancement with +=, +, -=, and -, as well as the computation of distance in constant time with -. Random access iterators also support array notation via subscripting.

Are set iterators random access?

So, as we can see here we can both access as well as assign value to the iterator, therefore the iterator is a random Access iterator.


2 Answers

Going from Dennis Zickenfoose's comment, I looked up Boost.range seems to offer the perfect solution:

  #include <boost/range.hpp>
  #include <boost/foreach.hpp>
  #include <iostream>

  int main()
  {
    const int blobSize = 100;
    int blob[blobSize];

    for( int i = 0; i < blobSize; ++i )
      blob[i] = i;

    boost::iterator_range<int*> blobPsudoContainer( blob, blob + blobSize );

    BOOST_FOREACH( int & i, blobPsudoContainer )
      i = i + 1;

    std::cout << "Size is:" << blobPsudoContainer.size() << "\n";
    std::cout << "value at 0 is:" << blobPsudoContainer[0] << "\n";
    return 0;
  }

Thanks Dennis! : )

like image 187
Catskul Avatar answered Nov 03 '22 19:11

Catskul


There's nothing like you want, this is just a shortcoming of the language. If you were satisfied with having the allocator statically guess a good expected maximum element count, you could just embed a normal C array inside an allocator and defer to it as long as possible; this is usually called an auto buffer.


Old Answer:

All of the standard library containers allow you to specify an allocator. You can make a buffer allocator.

like image 20
GManNickG Avatar answered Nov 03 '22 19:11

GManNickG