Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:41

0001 #pragma once
0002 /**
0003 SNameOrder.h : Ordering vectors of objects with GetName methods
0004 =================================================================
0005 
0006 After x4/X4NameOrder.hh
0007 
0008 **/
0009 
0010 #include <string>
0011 #include <iostream>
0012 #include <iomanip>
0013 #include <vector>
0014 #include "sstr.h"
0015 
0016 
0017 template <typename T>
0018 struct SNameOrder
0019 {
0020     static void Sort( std::vector<T*>& a, bool reverse=false, const char* tail="0x" ); 
0021     static std::string Desc(const std::vector<T*>& a, const char* tail="0x", int w=50  );  
0022 
0023     SNameOrder(bool reverse_, const char* tail_);
0024     bool operator() (const T* a, const T* b) const  ;
0025 
0026     bool reverse ; 
0027     const char* tail ; 
0028 };
0029 
0030 template <typename T>
0031 inline void SNameOrder<T>::Sort( std::vector<T*>& a, bool reverse, const char* tail ) // static
0032 {
0033     SNameOrder<T> name_order(reverse, tail); 
0034     std::sort( a.begin(), a.end(), name_order );  
0035 }
0036 
0037 template <typename T>
0038 inline std::string SNameOrder<T>::Desc(const std::vector<T*>& a, const char* tail, int w ) // static
0039 {
0040     std::stringstream ss ; 
0041     ss << "SNameOrder::Desc" 
0042        << " tail " << ( tail ? tail : "-" )
0043        << std::endl 
0044        ; 
0045     for(unsigned i=0 ; i < a.size() ; i++)
0046     {
0047         T* obj = a[i] ;
0048         const std::string name = obj->GetName();
0049         const std::string sname = sstr::StripTail(name, tail ) ;
0050         ss
0051             << std::setw(4) << i
0052             << " : "
0053             << std::setw(w) << name
0054             << " : "
0055             << std::setw(w) << sname
0056             << std::endl ;
0057     }
0058     std::string str = ss.str(); 
0059     return str ; 
0060 }
0061 
0062 template <typename T>
0063 inline SNameOrder<T>::SNameOrder(bool reverse_, const char* tail_) 
0064     :   
0065     reverse(reverse_),
0066     tail(tail_ ? strdup(tail_) : nullptr)
0067 {
0068 } 
0069 
0070 template <typename T>
0071 inline bool SNameOrder<T>::operator() (const T* a, const T* b) const
0072 {
0073     std::string an = a->GetName();
0074     std::string bn = b->GetName();
0075     if(tail)
0076     {
0077         an = sstr::StripTail(an, tail);
0078         bn = sstr::StripTail(bn, tail);
0079     }
0080     bool cmp = an < bn ;
0081     return reverse ? !cmp : cmp  ;
0082 }
0083