Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "DetectorDescription/Core/interface/DDsvalues.h"
0002 
0003 #include <iostream>
0004 
0005 void merge(DDsvalues_type &target, DDsvalues_type const &sv, bool sortit /* =true */) {
0006   if (target.empty()) {
0007     target = sv;
0008     return;
0009   }
0010   DDsvalues_type::const_iterator sit = sv.begin();
0011   DDsvalues_type::const_iterator sed = sv.end();
0012   // fast merge
0013   if (target.back() < sv.front()) {
0014     target.insert(target.end(), sit, sed);
0015     return;
0016   }
0017   if (sv.back() < target.front()) {
0018     target.insert(target.begin(), sit, sed);
0019     return;
0020   }
0021   {
0022     DDsvalues_type::iterator it = std::lower_bound(target.begin(), target.end(), sv.front());
0023     if (it == std::lower_bound(target.begin(), target.end(), sv.back())) {
0024       target.insert(it, sit, sed);
0025       return;
0026     }
0027   }
0028   // it nevers arrives here...
0029   target.reserve(target.size() + sv.size());
0030   DDsvalues_type::const_iterator ted = target.end();
0031   for (; sit != sed; ++sit) {
0032     DDsvalues_type::const_iterator it = find(target.begin(), ted, (*sit).first);
0033     if (it != ted)
0034       const_cast<DDsvalues_Content_type &>(*it).second = (*sit).second;
0035     else
0036       target.emplace_back(*sit);
0037   }
0038   if (sortit)
0039     std::sort(target.begin(), target.end());
0040 }
0041 
0042 std::ostream &operator<<(std::ostream &os, const DDsvalues_type &s) {
0043   DDsvalues_type::const_iterator it = s.begin();
0044   for (; it != s.end(); ++it) {
0045     os << it->second;
0046     /*
0047     os << DDValue(it->first).name() << " = ";
0048     for (unsigned int i=0; i<it->second.size(); ++i) {
0049        os << it->second[i] << ' ';
0050     }    
0051     os << std::endl;
0052     */
0053   }
0054   return os;
0055 }
0056 
0057 std::ostream &operator<<(std::ostream &os, const std::vector<const DDsvalues_type *> &v) {
0058   for (const auto &i : v) {
0059     os << *i;  // << std::endl;
0060   }
0061 
0062   return os;
0063 }
0064 
0065 /** Example:
0066    \code
0067    std::vector<std::string> myFunction(const DDsvalues_type * svalptr, const std::string & name) 
0068    {
0069       static std::vector<std::string> v; // empty std::vector, for zero result
0070       DDValue val(name);
0071       if (DDfetch(svalptr,val)) {
0072         return val.std::strings(); 
0073       } else {
0074        return v;
0075       }      
0076    }
0077    \endcode
0078 */
0079 bool DDfetch(const DDsvalues_type *p, DDValue &v) {
0080   bool result = false;
0081   DDsvalues_type::const_iterator it = find(*p, v);
0082   if (it != p->end()) {
0083     result = true;
0084     v = it->second;
0085   }
0086   return result;
0087 }
0088 
0089 unsigned int DDfetch(const std::vector<const DDsvalues_type *> &sp, DDValue &toFetch, std::vector<DDValue> &result) {
0090   unsigned int count = 0;
0091   for (const auto &it : sp) {
0092     if (DDfetch(it, toFetch)) {
0093       result.emplace_back(toFetch);
0094       ++count;
0095     }
0096   }
0097   return count;
0098 }