Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:20

0001 #include "CommonTools/Utils/interface/DynArray.h"
0002 
0003 struct A {
0004   A() {}
0005   A(int ii) : i(ii) {}
0006   int i = -3;
0007   double k = 0.1;
0008 
0009   virtual ~A() {}
0010 };
0011 
0012 #include <cassert>
0013 #include <iostream>
0014 #include <queue>
0015 
0016 int main(int s, char **) {
0017   using T = A;
0018 
0019   unsigned n = 4;
0020   if (s > 1)
0021     n = 4 * s;
0022 
0023   //  alignas(alignof(T)) unsigned char a_storage[sizeof(T)*n];
0024   //  DynArray<T> a(a_storage,n);
0025 
0026   declareDynArray(T, n, a);
0027 
0028   // T b[n];
0029   declareDynArray(T, n, b);
0030 
0031   b[0].i = 42;
0032   b[n - 1].i = -42;
0033 
0034   auto pa = [&](auto i) {
0035     a[1].k = 0.3;
0036     return a[i].k;
0037   };
0038   auto pb = [&](auto i) {
0039     b[1].k = 0.5;
0040     return b[i].k;
0041   };
0042 
0043   auto loop = [&](auto k) {
0044     for (auto const &q : a)
0045       k = std::min(k, q.k);
0046     return k;
0047   };
0048 
0049   std::cout << a[n - 1].k << ' ' << pa(1) << ' ' << loop(2.3) << std::endl;
0050   std::cout << b[n - 1].k << ' ' << pb(1) << std::endl;
0051 
0052   assert(b.back().i == -42);
0053   assert(b.front().i == 42);
0054 
0055   initDynArray(bool, n, q, true);
0056   if (q[n - 1])
0057     std::cout << "ok" << std::endl;
0058 
0059   auto sn = 2 * n;
0060   unInitDynArray(T, sn + n, c);
0061   assert(c.size() == 0);
0062   for (int i = 0; i < int(sn); ++i)
0063     c.push_back(i);
0064   assert(c.size() == sn);
0065   assert(c.front().i == 0);
0066   assert(c.back().i == int(sn - 1));
0067   c[1].k = 3.14;
0068 
0069   a = std::move(c);
0070 
0071   assert(a.size() == sn);
0072   assert(c.empty());
0073   assert(a[1].k == 3.14);
0074 
0075   std::swap(a, b);
0076   assert(b.size() == sn);
0077   assert(a.size() == n);
0078 
0079   unInitDynArray(int, sn, qst);  // queue storage
0080   auto cmp = [](int i, int j) { return i < j; };
0081   std::priority_queue<int, DynArray<int>, decltype(cmp)> qq(cmp, std::move(qst));
0082   assert(qq.empty());
0083   for (int i = 0; i < int(sn); ++i)
0084     qq.push(i + 1);
0085   assert(qq.size() == sn);
0086   for (int i = 0; i < int(sn); ++i) {
0087     assert(qq.size() == sn - i);
0088     assert(qq.top() == int(sn - i));
0089     qq.pop();
0090   }
0091 
0092   assert(qq.empty());
0093   qq.push(3);
0094   qq.push(7);
0095   qq.push(-3);
0096   assert(qq.top() == 7);
0097 
0098   return 0;
0099 };