Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:05:21

0001 #include "DataFormats/TrackingRecHit/interface/mayown_ptr.h"
0002 
0003 #include <vector>
0004 #include <iostream>
0005 #include <algorithm>
0006 
0007 int main() {
0008   using PD = mayown_ptr<double>;
0009   using VD = std::vector<PD>;
0010 
0011   double d1 = 2;
0012   PD p1;
0013   assert(p1.empty());
0014   assert(!p1.isOwn());
0015   p1.reset();
0016   p1.reset(d1);
0017   assert(&d1 == &(*p1));
0018 
0019   std::vector<double> dd(10, 3.14);
0020   VD vd;
0021   for (int i = 0; i < 10; ++i) {
0022     if (i % 2 == 0)
0023       vd.push_back(PD(dd[i]));
0024     else
0025       vd.push_back(PD(new double(-1.2)));
0026   }
0027 
0028   VD vd2;
0029   std::swap(vd, vd2);
0030   assert(10 == vd2.size());
0031   for (auto i = 0U; i < vd2.size(); ++i)
0032     std::cout << *vd2[i] << ' ';
0033   std::cout << std::endl;
0034   for (auto i = 0U; i < vd2.size(); ++i)
0035     if (i % 2 == 0)
0036       assert(!vd2[i].isOwn());
0037     else
0038       assert(vd2[i].isOwn());
0039 
0040   std::cout << "reset" << std::endl;
0041   vd2[3].reset();
0042   vd2[6].reset();
0043   assert(vd2[3].empty());
0044   assert(vd2[6].empty());
0045 
0046   std::cout << "remove" << std::endl;
0047   auto last = std::remove_if(vd2.begin(), vd2.end(), [](PD const& p) { return p.empty(); });
0048   for (auto i = vd2.begin(); i != last; ++i)
0049     std::cout << **i << ' ';
0050   std::cout << std::endl;
0051   vd2.resize(last - vd2.begin());
0052   assert(8 == vd2.size());
0053   for (auto i = 0U; i < vd2.size(); ++i)
0054     std::cout << *vd2[i] << ' ';
0055   std::cout << std::endl;
0056 
0057   return 0;
0058 }