00001
00002
00003
00004
00005
00006
00007
00008 #ifndef UTIL_H_
00009 #define UTIL_H_
00010
00011 #include <istream>
00012 #include <sstream>
00013 #include <map>
00014
00015 namespace util
00016 {
00017
00023 template<typename T>
00024 T parse(std::string const& str)
00025 {
00026 T ret;
00027 std::istringstream ss(str);
00028 ss >> ret;
00029 if (!ss.eof())
00030 throw std::exception();
00031 return ret;
00032 }
00033
00034 template<>
00035 double parse<double>(std::string const& str);
00036
00037 template<>
00038 int64_t parse<int64_t>(std::string const& str);
00039
00046 template<typename Sep, typename... Args>
00047 std::string concatenate(Sep sep, Args... args)
00048 {
00049 std::ostringstream ss;
00050 concat_impl(ss, sep, args...);
00051 return ss.str();
00052 }
00053
00059 template<typename T>
00060 std::string to_str(T val)
00061 {
00062 return concatenate("", val);
00063 }
00064
00065 template<typename Sep, typename Tail>
00066 void concat_impl(std::ostringstream& ss, Sep, Tail t)
00067 {
00068 ss << t;
00069 }
00070
00071 template<typename Sep, typename Head, typename... Tail>
00072 void concat_impl(std::ostringstream& os, Sep sep, Head h, Tail... t)
00073 {
00074 os << h << sep;
00075 concat_impl(os, sep, t...);
00076 }
00077
00084 template < typename Eq, typename K, typename V, typename Compare = std::less<K>, typename Allocator = std::allocator<std::pair<const K, V> > >
00085 bool multimap_eq(std::multimap<K, V, Compare, Allocator> const& a, std::multimap<K, V, Compare, Allocator> const& b)
00086 {
00087 for (auto it = a.begin(); it != a.end(); ++it)
00088 {
00089 bool wrong = true;
00090 for (auto it2 = b.lower_bound(it->first); it2 != b.upper_bound(it->first); ++it2)
00091 {
00092 if (Eq()(it->second, it2->second))
00093 wrong = false;
00094 }
00095 if (wrong)
00096 return false;
00097 }
00098 return true;
00099 }
00100
00107 template < typename K, typename V, typename Compare = std::less<K>, typename Allocator = std::allocator<std::pair<const K, V> > >
00108 bool multimap_eq(std::multimap<K, V, Compare, Allocator> const& a, std::multimap<K, V, Compare, Allocator> const& b)
00109 {
00110 return multimap_eq<std::equal_to<K> >(a, b);
00111 }
00112
00120 template <typename Eq, typename It, typename T>
00121 It find(It first, It last, T const& value)
00122 {
00123 for ( ; first != last; first++) if ( Eq()(*first, value) ) break;
00124 return first;
00125 }
00126
00134 template <typename It, typename T>
00135 It find(It first, It last, T const& value)
00136 {
00137 return find<std::equal_to<T> >(first, last, value);
00138 }
00139
00144 class All
00145 {
00146 public:
00147 typedef bool result_type;
00154 template<typename It> bool operator()(It first, It last) const
00155 {
00156 bool ret = true;
00157 for (; first != last; ++first)
00158 {
00159 ret = ret && *first;
00160 }
00161 return ret;
00162 }
00163 };
00164
00171 std::string replace(std::string const& input, std::map<char, std::string> const& repl);
00172
00173 template<typename Less, typename Eq, typename A, typename B>
00174 bool greater_than_impl(A const& a, B const& b)
00175 {
00176 Less l;
00177 Eq e;
00178 return !e(a, b) && !l(a, b);
00179 }
00180
00181 template<typename Eq, typename A, typename B>
00182 bool not_eq_impl(A const& a, B const& b)
00183 {
00184 Eq e;
00185 return !e(a, b);
00186 }
00187
00188 template<typename Less, typename A, typename B>
00189 bool greater_eq_impl(A const& a, B const& b)
00190 {
00191 Less l;
00192 return !l(a, b);
00193 }
00194
00195 template<typename Less, typename Eq, typename A, typename B>
00196 bool less_eq_impl(A const& a, B const& b)
00197 {
00198 Less l;
00199 Eq e;
00200 return e(a, b) || l(a, b);
00201 }
00202
00203 template<typename A>
00204 bool greater_than_impl(A const& a, A const& b)
00205 {
00206 return greater_than_impl<std::less<A>, std::equal_to<A>, A, A>(a, b);
00207 }
00208
00209 template<typename A>
00210 bool not_eq_impl(A const& a, A const& b)
00211 {
00212 return not_eq_impl<std::equal_to<A>, A, A>(a, b);
00213 }
00214
00215 template<typename A>
00216 bool greater_eq_impl(A const& a, A const& b)
00217 {
00218 return greater_eq_impl<std::less<A>, A, A>(a, b);
00219 }
00220
00221 template<typename A>
00222 bool less_eq_impl(A const& a, A const& b)
00223 {
00224 return less_eq_impl<std::less<A>, std::equal_to<A>, A, A>(a, b);
00225 }
00226
00233 template<typename Eq, typename Col1, typename Col2>
00234 bool equal_collection(Col1 c1, Col2 c2)
00235 {
00236 return std::equal<decltype(c1.begin()), decltype(c2.begin()), Eq>(c1.begin(), c1.end(), c2.begin(), Eq());
00237 }
00238
00245 template<typename Col1, typename Col2>
00246 bool equal_collection(Col1 c1, Col2 c2)
00247 {
00248 typedef decltype(*c1.begin()) Elem;
00249 return equal_collection<Col1, Col2, std::equal_to<Elem> >(c1, c2);
00250 }
00251
00252 }
00253
00254 #endif