Warning, /include/Geant4/tools/vmanip is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_vmanip
0005 #define tools_vmanip
0006
0007 #include "touplow"
0008
0009 #include <vector>
0010 #include <string>
0011
0012 namespace tools {
0013
0014 //////////////////////////////////////////////////////////
0015 /// T* : /////////////////////////////////////////////////
0016 //////////////////////////////////////////////////////////
0017
0018 template <class T>
0019 inline void safe_clear(std::vector<T*>& a_vec){
0020 // the below takes into account the case in
0021 // which "delete entry" could modify a_vec.
0022 typedef typename std::vector<T*>::iterator it_t;
0023 while(!a_vec.empty()) {
0024 it_t it = a_vec.begin();
0025 T* entry = *it;
0026 a_vec.erase(it);
0027 delete entry;
0028 }
0029 }
0030
0031 template <class T>
0032 inline void safe_reverse_clear(std::vector<T*>& a_vec){ //used in sg/group.
0033 // the below takes into account the case in
0034 // which "delete entry" could modify a_vec.
0035 typedef typename std::vector<T*>::iterator it_t;
0036 while(!a_vec.empty()) {
0037 it_t it = a_vec.end();
0038 it--;
0039 T* entry = *it;
0040 a_vec.erase(it);
0041 delete entry;
0042 }
0043 }
0044
0045 template <class T>
0046 inline void raw_clear(std::vector<T*>& a_vec){
0047 typedef typename std::vector<T*>::iterator it_t;
0048 for(it_t it = a_vec.begin();it!=a_vec.end();++it) delete *it;
0049 a_vec.clear();
0050 }
0051
0052 template <class T>
0053 inline void append(std::vector<T>& a_vec,const std::vector<T>& a_from) {
0054 typedef typename std::vector<T>::size_type sz_t;
0055 sz_t number = a_from.size();
0056 sz_t offset = a_vec.size();
0057 a_vec.resize(offset+number);
0058 for(sz_t index=0;index<number;index++,offset++) {
0059 a_vec[offset] = a_from[index];
0060 }
0061 }
0062
0063 template <class T>
0064 inline void append(std::vector<T>& a_vec,typename std::vector<T>::size_type a_num,const T* a_from) {
0065 typedef typename std::vector<T>::size_type sz_t;
0066 sz_t vsize = a_vec.size();
0067 a_vec.resize(vsize+a_num);
0068 sz_t offset = vsize;
0069 for(sz_t index=0;index<a_num;index++,offset++) {
0070 a_vec[offset] = a_from[index];
0071 }
0072 }
0073
0074 template <class T>
0075 inline void removep(std::vector<T*>& a_vec,const T* a_elem) {
0076 typedef typename std::vector<T*>::iterator it_t;
0077 for(it_t it=a_vec.begin();it!=a_vec.end();) {
0078 if(*it==a_elem) {
0079 it = a_vec.erase(it);
0080 } else {
0081 ++it;
0082 }
0083 }
0084 }
0085
0086 template <class T>
0087 inline bool is_inp(const std::vector<T*>& a_vec,const T* a_item){
0088 typedef typename std::vector<T*>::const_iterator it_t;
0089 for(it_t it=a_vec.begin();it!=a_vec.end();++it) {
0090 if(*it==a_item) return true;
0091 }
0092 return false;
0093 }
0094
0095 template <class T>
0096 inline T* find_named(const std::vector<T*>& a_vec,const std::string& a_name) {
0097 typedef typename std::vector<T*>::const_iterator it_t;
0098 it_t it;
0099 for(it=a_vec.begin();it!=a_vec.end();++it) {
0100 if((*it)->name()==a_name) return *it;
0101 }
0102 return 0;
0103 }
0104
0105 template <class T>
0106 inline T* find_named_case_insensitive(const std::vector<T*>& a_vec,const std::string& a_name) {
0107 std::string low_a_name = a_name;
0108 tolowercase(low_a_name);
0109 std::string low_name;
0110 typedef typename std::vector<T*>::const_iterator it_t;
0111 it_t it;
0112 for(it=a_vec.begin();it!=a_vec.end();++it) {
0113 low_name = (*it)->name();
0114 tolowercase(low_name);
0115 if(low_name==low_a_name) return *it;
0116 }
0117 return 0;
0118 }
0119
0120 ////////////////////////////////////////////////////////////////////////////
0121 /// T : ////////////////////////////////////////////////////////////////////
0122 ////////////////////////////////////////////////////////////////////////////
0123 template <class T>
0124 inline void vfilter(std::vector<T>& a_vals,bool(*a_filter_func)(const T& a_elem)){
0125 std::vector<T> vs;
0126 typedef typename std::vector<T>::iterator it_t;
0127 for(it_t it=a_vals.begin();it!=a_vals.end();++it) {
0128 if(a_filter_func(*it)) vs.push_back(*it);
0129 }
0130 a_vals = vs;
0131 }
0132
0133 template <class T>
0134 inline bool minimum(const std::vector<T>& a_vec,T& a_value) {
0135 if(a_vec.empty()) {a_value = T();return false;}
0136 a_value = a_vec[0];
0137 typedef typename std::vector<T>::const_iterator it_t;
0138 for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
0139 a_value = (a_value<(*it)?a_value:(*it));
0140 }
0141 return true;
0142 }
0143
0144 template <class T>
0145 inline bool maximum(const std::vector<T>& a_vec,T& a_value) {
0146 if(a_vec.empty()) {a_value = T();return false;}
0147 a_value = a_vec[0];
0148 typedef typename std::vector<T>::const_iterator it_t;
0149 for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
0150 a_value = (a_value>(*it)?a_value:(*it));
0151 }
0152 return true;
0153 }
0154
0155
0156 template <class FROM,class TO>
0157 inline void convert(const std::vector<FROM>& a_from,std::vector<TO>& a_to) {
0158 typedef typename std::vector<FROM>::const_iterator const_it_t;
0159 typedef typename std::vector<TO>::iterator it_t;
0160 a_to.resize(a_from.size());
0161 const_it_t ait = a_from.begin();
0162 it_t toit = a_to.begin();
0163 for(;ait!=a_from.end();++ait,++toit) {*toit = (TO)*ait;}
0164 }
0165
0166 }
0167
0168 #endif