Add a container

This commit is contained in:
Andreas Fabri 2014-12-11 20:33:53 +01:00
parent ddf4b932b0
commit 1bc624ee30
3 changed files with 97 additions and 0 deletions

View File

@ -36,6 +36,7 @@ if ( CGAL_FOUND )
create_single_source_cgal_program( "nn3cgal.cpp" )
create_single_source_cgal_program( "nn3nanoflan.cpp" )
create_single_source_cgal_program( "sizeof.cpp" )
create_single_source_cgal_program( "deque.cpp" )
else()

View File

@ -0,0 +1,38 @@
#include <vector>
#include <deque>
#include <CGAL/Memory_sizer.h>
#include <CGAL/array.h>
#include <CGAL/Block_list.h>
#include <CGAL/Timer.h>
struct X {
double a,b,c,d,e,f,g,h,i,j,k,l;
};
int main()
{
CGAL::Memory_sizer ms;
CGAL::Timer t;
char c;
t.start();
#if 0
CGAL::Block_list<X, 256> de;
#elif 1
std::deque<X> de;
#else
std::vector<X> de;
de.reserve(100000000);
#endif
for(int i=0; i < 100000000; i++){
de.push_back(X());
}
t.stop();
std::cout << t.time() << "sec"<< std::endl;
std::cout << ms.virtual_size() << " " << ms.resident_size() << std::endl;
std::cout << "cont>" << std::endl;
std::cin >> c;
return 0;
}

View File

@ -0,0 +1,58 @@
#ifndef CGAL_BLOCKLIST_H
#define CGAL_BLOCKLIST_H
#include <list>
#include <CGAL/array.h>
namespace CGAL {
template < typename T, int N>
class Block_list{
typedef CGAL::cpp11::array<T,N> Block;
int index;
std::list<Block> blocks;
public:
Block_list()
: index(N)
{}
T* push_back(const T& t)
{
if(index==N){
Block b;
blocks.push_back(b);
index = 0;
}
Block& b = blocks.back();
b[index] = t;
T* ptr = &b[index];
++index;
return ptr;
}
void clear()
{
blocks.clear();
index = N;
}
void print(std::ostream& os)
{
std::size_t s = blocks.size();
std::size_t i = 0;
for(std::list<Block>::iterator it = blocks.begin(); it!= blocks.end(); ++it, ++i){
Block& b = *it;
os << "Block "<< i << std::endl;
std::size_t n = (i+1 == s)? index : N;
for(std::size_t j=0; j < n; j++){
os << b[j] << std::endl;
}
}
}
};
} // namespace CGAL
#endif