librostlab
1.0.20
|
00001 /* 00002 Copyright (C) 2011 Laszlo Kajan, Technical University of Munich, Germany 00003 00004 This file is part of librostlab. 00005 00006 librostlab is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 #ifndef ROSTLAB_AUX_FUNTIONS 00020 #define ROSTLAB_AUX_FUNTIONS 1 00021 00022 #include <iostream> 00023 #include <map> 00024 #include <string> 00025 #include <vector> 00026 00027 namespace rostlab { 00028 00029 template<typename _Tp> 00030 inline std::basic_string<_Tp> 00031 join( const std::basic_string<_Tp>& __sep, typename std::vector< std::basic_string<_Tp> >::const_iterator __begin, typename std::vector< std::basic_string<_Tp> >::const_iterator __end ) 00032 { 00033 std::basic_string<_Tp> buf; 00034 for( typename std::vector< std::basic_string<_Tp> >::const_iterator v_i = __begin; v_i != __end; ++v_i ) 00035 { 00036 if( buf.size() ) buf += __sep; buf += *v_i; 00037 } 00038 return buf; 00039 } 00040 00041 00042 template<typename _Tp> 00043 inline std::basic_string<_Tp> 00044 join( const std::basic_string<_Tp>& __sep, const std::vector< std::basic_string<_Tp> >& __v ) 00045 { 00046 return join( __sep, __v.begin(), __v.end() ); 00047 } 00048 00049 00050 template<typename _Tp> 00051 inline std::basic_string<_Tp> 00052 join( const _Tp* __sep, const std::vector< std::basic_string<_Tp> >& __v ) 00053 { 00054 return join( std::string(__sep), __v ); 00055 } 00056 00057 00058 template<typename _Tp> 00059 inline std::basic_string<_Tp> 00060 join( const _Tp __sep, const std::vector< std::basic_string<_Tp> >& __v ) 00061 { 00062 return join( std::string( 1, __sep ), __v ); 00063 } 00064 00065 00066 inline std::vector<std::string> 00067 split( const std::string& __str, char __c ) 00068 { 00069 std::vector<std::string> ret; 00070 if( __str.empty() ) return ret; 00071 std::string::size_type pos = 0; 00072 do { std::string::size_type new_pos = __str.find( __c, pos ); ret.push_back( new_pos != std::string::npos ? __str.substr( pos, new_pos-pos ) : __str.substr( pos ) ); pos = new_pos; if( pos != std::string::npos ) ++pos; } while( pos != std::string::npos ); 00073 return ret; 00074 } 00075 00076 00077 template<typename _Tk, typename _Tv> 00078 inline std::vector<_Tk> 00079 map_keys( const std::map<_Tk, _Tv>& __map ) 00080 { 00081 std::vector<_Tk> ret; 00082 ret.reserve( __map.size() ); 00083 for( typename std::map<_Tk, _Tv>::const_iterator m_i = __map.begin(); m_i != __map.end(); ++m_i ) ret.push_back( m_i->first ); 00084 return ret; 00085 } 00086 00087 00088 inline void split_in_2( const std::string& __str, char __c, std::string& __left, std::string& __right ) 00089 { 00090 std::string::size_type pos = __str.find( __c ); 00091 if( pos == std::string::npos ) 00092 { 00093 __left = __str; 00094 } 00095 else 00096 { 00097 __left = __str.substr( 0, pos ); 00098 __right = __str.substr( pos+1 ); 00099 } 00100 } 00101 00102 00103 inline std::map<std::string, std::string> 00104 map_split_in_2( const std::vector<std::string>& __svec, char __c ) 00105 { 00106 std::map<std::string, std::string> ret; 00107 for( std::vector<std::string>::const_iterator s_i = __svec.begin(); s_i != __svec.end(); ++s_i ) 00108 { 00109 std::string left, right; 00110 split_in_2( *s_i, __c, left, right ); 00111 ret[left] = right; 00112 } 00113 return ret; 00114 } 00115 00116 00117 //class ios_base { 00118 // private: 00119 // static int _flagslot(){ static int slot = std::ios_base::xalloc(); return slot; } 00120 // public: 00121 // /// Stream formatting flags. 00122 // /** The flags are: 00123 // * - tabdelim 00124 // */ 00125 // typedef enum _Ios_Fmtflags { 00126 // /// Tab-delimited output format flag. 00127 // _S_tabdelim = 1L << 1 00128 // } fmtflags; 00129 // /// Print in tab-delimited format. 00130 // /** This makes sense for example when printing maps. */ 00131 // static const fmtflags tabdelim = _S_tabdelim; 00132 // 00133 // static fmtflags flags( std::ostream &os ) { return static_cast<fmtflags>( os.iword(_flagslot()) ); } 00134 // static fmtflags flags( std::ostream &os, const fmtflags f ) 00135 // { 00136 // fmtflags ret = static_cast<fmtflags>( os.iword(_flagslot()) ); 00137 // os.iword(_flagslot()) = f; 00138 // return ret; 00139 // } 00140 //} ; 00141 // 00142 // 00143 //template<typename K, typename V, class C, class A> 00144 //inline 00145 //std::ostream& operator<< (std::ostream &os, const rostlab::ios_base::fmtflags f ) 00146 //{ 00147 // rostlab::ios_base::flags(os, f); 00148 // return os; 00149 //} 00150 00151 00153 template<class _T1, class _T2> 00154 inline 00155 std::ostream & operator<< (std::ostream &os, const std::pair<_T1, _T2>& v) 00156 { 00157 os << v.first << " => " << v.second; 00158 return os; 00159 } 00160 00161 00163 template<typename K, typename V, class C, class A> 00164 inline 00165 std::ostream& operator<< (std::ostream &os, const std::map<K,V,C,A>& m) 00166 { 00167 os << "{ "; 00168 for( typename std::map<K,V,C,A>::const_iterator p = m.begin(), p_end = m.end(); p != p_end; ++p ) 00169 { 00170 if( p != m.begin() ) os << ", "; 00171 os << *p; 00172 } 00173 os << " }"; 00174 return os; 00175 } 00176 00177 00179 template<typename _Tp, typename _Alloc> 00180 inline 00181 std::ostream & operator<< (std::ostream &os, const std::vector<_Tp, _Alloc>& v) 00182 { 00183 typename std::vector<_Tp, _Alloc>::const_iterator v_i; 00184 os << "[ "; 00185 for( v_i = v.begin(); v_i != v.end(); ++v_i ) 00186 { if( v_i != v.begin() ) os << ", "; os << *v_i; } 00187 os << " ]"; 00188 return os; 00189 } 00190 00191 00192 }; // namespace rostlab 00193 00194 #endif // ROSTLAB_AUX_FUNTIONS 00195 // vim:et:ts=4:ai: