23 #ifndef TURI_STL_UTIL_HPP 24 #define TURI_STL_UTIL_HPP 35 #include <core/logging/assertions.hpp> 36 #include <boost/exception/detail/is_output_streamable.hpp> 57 std::set<T>
set_union(
const std::set<T>& a,
const std::set<T>& b) {
61 std::inserter(output, output.begin()));
69 std::set<T>
set_union(
const std::set<T>& a,
const T& b) {
70 std::set<T> output = a;
81 std::set_intersection(a.begin(), a.end(),
83 std::inserter(output, output.begin()));
95 std::inserter(output, output.begin()));
103 template <
typename T>
105 std::set<T> output = a;
114 template <
typename T>
115 std::pair<std::set<T>,std::set<T> >
120 return std::make_pair(a, b);
126 template <
typename T>
128 return (intersection_size(a,b) == 0);
134 template <
typename T>
135 bool set_equal(
const std::set<T>& a,
const std::set<T>& b) {
136 if (a.size() != b.size())
return false;
143 template <
typename T>
144 bool includes(
const std::set<T>& a,
const std::set<T>& b) {
145 return std::includes(a.begin(), a.end(), b.begin(), b.end());
151 template <
typename T>
152 bool is_subset(
const std::set<T>& a,
const std::set<T>& b) {
159 template <
typename T>
168 template <
typename Container>
169 std::ostream& print_range(std::ostream& out,
const Container& c,
170 const std::string& left,
const std::string& sep,
const std::string& right) {
174 for(
auto it = c.begin();;) {
185 out << right << std::endl;
194 template <
typename T>
195 typename boost::enable_if_c<boost::is_output_streamable<T>::value,
197 operator<<(std::ostream& out, const std::set<T>& s) {
198 return print_range(out, s,
"{",
", ",
"}");
204 template <
typename T>
205 typename boost::enable_if_c<boost::is_output_streamable<T>::value,
207 operator<<(std::ostream& out, const std::vector<T>& v) {
208 return print_range(out, v,
"[",
", ",
"]");
218 template <
typename Key,
typename T>
221 typedef typename std::map<Key, T>::const_iterator iterator;
222 iterator iter = map.find(key);
232 template <
typename Key,
typename T>
234 const Key& key,
const T default_value) {
235 typedef typename std::map<Key, T>::const_iterator iterator;
236 iterator iter = map.find(key);
237 if (iter == map.end())
return default_value;
238 else return iter->second;
246 template <
typename OldKey,
typename NewKey,
typename T>
248 rekey(
const std::map<OldKey, T>& map,
249 const std::map<OldKey, NewKey>& key_map) {
250 std::map<NewKey, T> output;
251 typedef std::pair<OldKey, T> pair_type;
252 for(
const pair_type& pair: map) {
253 output[
safe_get(key_map, pair.first)] = pair.second;
263 template <
typename Key,
typename OldT,
typename NewT>
265 remap(
const std::map<Key, OldT>& map,
266 const std::map<OldT, NewT>& val_map) {
267 std::map<Key, NewT> output;
268 typedef std::pair<Key, OldT> pair_type;
269 for(
const pair_type& pair: map) {
270 output[pair.first] =
safe_get(val_map, pair.second);
278 template <
typename Key,
typename T>
280 const std::map<T, T>& val_map) {
281 typedef std::pair<Key, T> pair_type;
282 for(pair_type& pair: map) {
283 pair.second =
safe_get(val_map, pair.second);
290 template <
typename Key,
typename T>
293 const std::map<Key, T>& b) {
295 std::map<Key, T> output;
298 std::inserter(output, output.begin()),
299 output.value_comp());
306 template <
typename Key,
typename T>
309 const std::map<Key, T>& b) {
311 std::map<Key, T> output;
313 std::set_intersection(a.begin(), a.end(),
315 std::inserter(output, output.begin()),
316 output.value_comp());
323 template <
typename Key,
typename T>
326 const std::set<Key>&
keys) {
327 std::map<Key, T> output;
328 for(
const Key& key: keys) {
329 typename std::map<Key,T>::const_iterator it = m.find(key);
331 output[key] = it->second;
339 template <
typename Key,
typename T>
342 const std::map<Key, T>& b) {
344 std::map<Key, T> output;
348 std::inserter(output, output.begin()),
349 output.value_comp());
357 template <
typename Key,
typename T>
358 std::set<Key>
keys(
const std::map<Key, T>& map) {
359 std::set<Key> output;
360 typedef std::pair<Key, T> pair_type;
361 for(
const pair_type& pair: map) {
362 output.insert(pair.first);
370 template <
typename Key,
typename T>
372 std::vector<Key> output(map.size());
373 typedef std::pair<Key, T> pair_type;
375 for(
const pair_type& pair: map) {
376 output[i++] = pair.first;
385 template <
typename Key,
typename T>
386 std::set<T>
values(
const std::map<Key, T>& map) {
388 typedef std::pair<Key, T> pair_type;
389 for(
const pair_type& pair: map) {
390 output.insert(pair.second);
398 template <
typename Key,
typename T>
399 std::vector<T>
values(
const std::map<Key, T>& m,
400 const std::set<Key>&
keys) {
401 std::vector<T> output;
403 for(
const Key &i: keys) {
412 template <
typename Key,
typename T>
413 std::vector<T>
values(
const std::map<Key, T>& m,
414 const std::vector<Key>&
keys) {
415 std::vector<T> output;
416 for(
const Key &i: keys) {
424 template <
typename Key>
426 std::map<Key, Key> m;
427 for(
const Key& key: keys)
433 template <
typename Key,
typename T>
434 std::ostream& operator<<(std::ostream& out, const std::map<Key, T>& m) {
436 for (
typename std::map<Key, T>::const_iterator it = m.begin();
438 out << it->first <<
"-->" << it->second;
439 if (++it != m.end()) out <<
" ";
448 inline std::string
trim(
const std::string& str) {
449 std::string::size_type pos1 = str.find_first_not_of(
" \t");
450 std::string::size_type pos2 = str.find_last_not_of(
" \t");
451 return str.substr(pos1 == std::string::npos ? 0 : pos1,
452 pos2 == std::string::npos ? str.size()-1 : pos2-pos1+1);
460 std::stringstream strm;
470 std::stringstream strm(str);
483 const char pad_value =
'0') {
484 std::stringstream strm;
485 strm << std::setw((
int)npad) << std::setfill(pad_value)
505 inline std::vector<std::string>
strsplit(
const std::string& str,
506 const std::string& splitchars,
507 const bool auto_trim =
false) {
508 std::vector<std::string> tokens;
509 for(
size_t beg = 0, end = 0; end != std::string::npos; beg = end+1) {
510 end = str.find_first_of(splitchars, beg);
513 std::string tmp =
trim(str.substr(beg, end - beg));
514 if(!tmp.empty()) tokens.push_back(tmp);
516 }
else tokens.push_back(str.substr(beg, end - beg));
#define ASSERT_FALSE(cond)
bool is_superset(const std::set< T > &a, const std::set< T > &b)
std::set< T > set_difference(const std::set< T > &a, const T &b)
std::set< T > set_union(const std::set< T > &a, const std::set< T > &b)
std::set< Key > keys(const std::map< Key, T > &map)
std::vector< Key > keys_as_vector(const std::map< Key, T > &map)
bool set_equal(const std::set< T > &a, const std::set< T > &b)
std::map< Key, Key > make_identity_map(const std::set< Key > &keys)
const T & safe_get(const std::map< Key, T > &map, const Key &key)
T fromstr(const std::string &str)
std::map< Key, T > map_difference(const std::map< Key, T > &a, const std::map< Key, T > &b)
std::map< Key, T > map_intersect(const std::map< Key, T > &a, const std::map< Key, T > &b)
std::string trim(const std::string &str)
std::map< Key, T > map_union(const std::map< Key, T > &a, const std::map< Key, T > &b)
#define ASSERT_TRUE(cond)
bool includes(const std::set< T > &a, const std::set< T > &b)
std::map< Key, NewT > remap(const std::map< Key, OldT > &map, const std::map< OldT, NewT > &val_map)
std::set< T > set_intersect(const std::set< T > &a, const std::set< T > &b)
std::string tostr(const T &t)
std::set< T > values(const std::map< Key, T > &map)
std::pair< std::set< T >, std::set< T > > set_partition(const std::set< T > &s, const std::set< T > &partition)
std::set< T > set_difference(const std::set< T > &a, const std::set< T > &b)
std::map< NewKey, T > rekey(const std::map< OldKey, T > &map, const std::map< OldKey, NewKey > &key_map)
bool set_disjoint(const std::set< T > &a, const std::set< T > &b)
std::vector< std::string > strsplit(const std::string &str, const std::string &splitchars, const bool auto_trim=false)
std::string pad_number(const size_t number, const size_t npad, const char pad_value='0')
bool is_subset(const std::set< T > &a, const std::set< T > &b)
std::set< T > set_union(const std::set< T > &a, const T &b)