Skip to content

Commit

Permalink
make DynArray std compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
VinInn authored and fwyzard committed Jul 22, 2016
1 parent 59b6299 commit 47cf66d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
30 changes: 30 additions & 0 deletions CommonTools/Utils/interface/DynArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,54 @@ class DynArray {
T * a=nullptr;
unsigned int s=0;
public :
using size_type = unsigned int;
using value_type = T;
using reference = T&;
using const_reference = T const&;

DynArray(){}

explicit DynArray(unsigned char * storage) : a((T*)(storage)), s(0){}

DynArray(unsigned char * storage, unsigned int isize) : a((T*)(storage)), s(isize){
for (auto i=0U; i<s; ++i) new((begin()+i)) T();
}
DynArray(unsigned char * storage, unsigned int isize, T const& it) : a((T*)(storage)), s(isize){
for (auto i=0U; i<s; ++i) new((begin()+i)) T(it);
}

DynArray(DynArray const&) = delete;
DynArray & operator=(DynArray const &) = delete;

DynArray(DynArray && other) { a=other.a; s=other.s; other.s=0; other.a=nullptr;}
DynArray & operator=(DynArray && other) { a=other.a; s=other.s; other.s=0; other.a=nullptr; return *this;}


~DynArray() { for (auto i=0U; i<s; ++i) a[i].~T(); }

T & operator[](unsigned int i) { return a[i];}
T * begin() { return a;}
T * end() { return a+s;}
T & front() { return a[0];}
T & back() { return a[s-1];}

T const & operator[](unsigned int i) const { return a[i];}
T const * begin() const { return a;}
T const * end() const { return a+s;}
unsigned int size() const { return s;}
bool empty() const { return 0==s;}

T const &front() const { return a[0];}
T const & back() const { return a[s-1];}

void pop_back() {back().~T(); --s;}
void push_back(T const& t) {new((begin()+s)) T(t);++s;}
void push_back(T && t) {new((begin()+s)) T(t);++s;}


};

#define unInitDynArray(T,n,x) alignas(alignof(T)) unsigned char x ## _storage[sizeof(T)*n]; DynArray<T> x(x ## _storage)
#define declareDynArray(T,n,x) alignas(alignof(T)) unsigned char x ## _storage[sizeof(T)*n]; DynArray<T> x(x ## _storage,n)
#define initDynArray(T,n,x,i) alignas(alignof(T)) unsigned char x ## _storage[sizeof(T)*n]; DynArray<T> x(x ## _storage,n,i)

Expand Down
5 changes: 5 additions & 0 deletions CommonTools/Utils/test/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@
<use name="CommonTools/Utils"/>
</bin>


<bin file="testDynArray.cpp">
</bin>


40 changes: 39 additions & 1 deletion CommonTools/Utils/test/testDynArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@


struct A {

A(){}
A(int ii) : i(ii){}
int i=-3;
double k=0.1;

Expand All @@ -14,6 +15,7 @@ virtual ~A(){}

#include<cassert>
#include<iostream>
#include<queue>

int main(int s, char **) {

Expand All @@ -29,6 +31,8 @@ int main(int s, char **) {
// T b[n];
declareDynArray(T,n,b);

b[0].i=42;
b[n-1].i=-42;

auto pa = [&](auto i) { a[1].k=0.3; return a[i].k; };
auto pb = [&](auto i) { b[1].k=0.5; return b[i].k; };
Expand All @@ -38,8 +42,42 @@ int main(int s, char **) {
std::cout << a[n-1].k << ' ' << pa(1) << ' ' << loop(2.3) << std::endl;
std::cout << b[n-1].k << ' ' << pb(1) << std::endl;

assert(b.back().i==-42);
assert(b.front().i==42);

initDynArray(bool,n,q,true);
if (q[n-1]) std::cout << "ok" << std::endl;

auto sn = 2*n;
unInitDynArray(T,sn+n,c);
assert(c.size()==0);
for(int i=0;i<int(sn);++i) c.push_back(i);
assert(c.size()==sn);
assert(c.front().i==0);
assert(c.back().i==int(sn-1));

c = std::move(a);

assert(c.size()==n);
assert(a.empty());
assert(c[1].k==0.3);

auto cmp = [](int i, int j){return i<j;};

unInitDynArray(int,sn,qst); // queue storage
std::priority_queue<int,DynArray<int>,decltype(cmp)> qq(cmp,std::move(qst));
assert(qq.empty());
for(int i=0;i<int(sn);++i) qq.push(i+1);
assert(qq.size()==sn);
for(int i=0;i<int(sn);++i) {
assert(qq.size()==sn-i);
assert(qq.top()==int(sn-i));
qq.pop();
}

assert(qq.empty());
qq.push(3); qq.push(7); qq.push(-3);
assert(qq.top()==7);

return 0;
};
3 changes: 3 additions & 0 deletions TrackingTools/GsfTools/interface/CloseComponentsMerger.icc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <algorithm>
#include <queue>
#include <cfloat>
#include "CommonTools/Utils/interface/DynArray.h"



template <unsigned int N>
CloseComponentsMerger<N>::CloseComponentsMerger (int maxNumberOfComponents,
Expand Down

0 comments on commit 47cf66d

Please sign in to comment.