librostlab 1.0.20
Loading...
Searching...
No Matches
aux_functions.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2011 Laszlo Kajan, Technical University of Munich, Germany
3
4 This file is part of librostlab.
5
6 librostlab is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef ROSTLAB_AUX_FUNTIONS
20#define ROSTLAB_AUX_FUNTIONS 1
21
22#include <iostream>
23#include <map>
24#include <string>
25#include <vector>
26
27namespace rostlab {
28
29template<typename _Tp>
30inline std::basic_string<_Tp>
31 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 )
32{
33 std::basic_string<_Tp> buf;
34 for( typename std::vector< std::basic_string<_Tp> >::const_iterator v_i = __begin; v_i != __end; ++v_i )
35 {
36 if( buf.size() ) buf += __sep; buf += *v_i;
37 }
38 return buf;
39}
40
41
42template<typename _Tp>
43inline std::basic_string<_Tp>
44 join( const std::basic_string<_Tp>& __sep, const std::vector< std::basic_string<_Tp> >& __v )
45{
46 return join( __sep, __v.begin(), __v.end() );
47}
48
49
50template<typename _Tp>
51inline std::basic_string<_Tp>
52 join( const _Tp* __sep, const std::vector< std::basic_string<_Tp> >& __v )
53{
54 return join( std::string(__sep), __v );
55}
56
57
58template<typename _Tp>
59inline std::basic_string<_Tp>
60 join( const _Tp __sep, const std::vector< std::basic_string<_Tp> >& __v )
61{
62 return join( std::string( 1, __sep ), __v );
63}
64
65
66inline std::vector<std::string>
67 split( const std::string& __str, char __c )
68{
69 std::vector<std::string> ret;
70 if( __str.empty() ) return ret;
71 std::string::size_type pos = 0;
72 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 );
73 return ret;
74}
75
76
77template<typename _Tk, typename _Tv>
78inline std::vector<_Tk>
79 map_keys( const std::map<_Tk, _Tv>& __map )
80{
81 std::vector<_Tk> ret;
82 ret.reserve( __map.size() );
83 for( typename std::map<_Tk, _Tv>::const_iterator m_i = __map.begin(); m_i != __map.end(); ++m_i ) ret.push_back( m_i->first );
84 return ret;
85}
86
87
88inline void split_in_2( const std::string& __str, char __c, std::string& __left, std::string& __right )
89{
90 std::string::size_type pos = __str.find( __c );
91 if( pos == std::string::npos )
92 {
93 __left = __str;
94 }
95 else
96 {
97 __left = __str.substr( 0, pos );
98 __right = __str.substr( pos+1 );
99 }
100}
101
102
103inline std::map<std::string, std::string>
104 map_split_in_2( const std::vector<std::string>& __svec, char __c )
105{
106 std::map<std::string, std::string> ret;
107 for( std::vector<std::string>::const_iterator s_i = __svec.begin(); s_i != __svec.end(); ++s_i )
108 {
109 std::string left, right;
110 split_in_2( *s_i, __c, left, right );
111 ret[left] = right;
112 }
113 return ret;
114}
115
116
117//class ios_base {
118// private:
119// static int _flagslot(){ static int slot = std::ios_base::xalloc(); return slot; }
120// public:
121// /// Stream formatting flags.
122// /** The flags are:
123// * - tabdelim
124// */
125// typedef enum _Ios_Fmtflags {
126// /// Tab-delimited output format flag.
127// _S_tabdelim = 1L << 1
128// } fmtflags;
129// /// Print in tab-delimited format.
130// /** This makes sense for example when printing maps. */
131// static const fmtflags tabdelim = _S_tabdelim;
132//
133// static fmtflags flags( std::ostream &os ) { return static_cast<fmtflags>( os.iword(_flagslot()) ); }
134// static fmtflags flags( std::ostream &os, const fmtflags f )
135// {
136// fmtflags ret = static_cast<fmtflags>( os.iword(_flagslot()) );
137// os.iword(_flagslot()) = f;
138// return ret;
139// }
140//} ;
141//
142//
143//template<typename K, typename V, class C, class A>
144//inline
145//std::ostream& operator<< (std::ostream &os, const rostlab::ios_base::fmtflags f )
146//{
147// rostlab::ios_base::flags(os, f);
148// return os;
149//}
150
151
153template<class _T1, class _T2>
154inline
155std::ostream & operator<< (std::ostream &os, const std::pair<_T1, _T2>& v)
156{
157 os << v.first << " => " << v.second;
158 return os;
159}
160
161
163template<typename K, typename V, class C, class A>
164inline
165std::ostream& operator<< (std::ostream &os, const std::map<K,V,C,A>& m)
166{
167 os << "{ ";
168 for( typename std::map<K,V,C,A>::const_iterator p = m.begin(), p_end = m.end(); p != p_end; ++p )
169 {
170 if( p != m.begin() ) os << ", ";
171 os << *p;
172 }
173 os << " }";
174 return os;
175}
176
177
179template<typename _Tp, typename _Alloc>
180inline
181std::ostream & operator<< (std::ostream &os, const std::vector<_Tp, _Alloc>& v)
182{
183 typename std::vector<_Tp, _Alloc>::const_iterator v_i;
184 os << "[ ";
185 for( v_i = v.begin(); v_i != v.end(); ++v_i )
186 { if( v_i != v.begin() ) os << ", "; os << *v_i; }
187 os << " ]";
188 return os;
189}
190
191
192}; // namespace rostlab
193
194#endif // ROSTLAB_AUX_FUNTIONS
195// vim:et:ts=4:ai:
std::map< std::string, std::string > map_split_in_2(const std::vector< std::string > &__svec, char __c)
std::basic_string< _Tp > 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)
std::ostream & operator<<(std::ostream &os, const std::pair< _T1, _T2 > &v)
std::pair output operator.
std::vector< std::string > split(const std::string &__str, char __c)
std::vector< _Tk > map_keys(const std::map< _Tk, _Tv > &__map)
void split_in_2(const std::string &__str, char __c, std::string &__left, std::string &__right)