41 #ifndef GRAPHLAB_STL_UTIL_HPP
42 #define GRAPHLAB_STL_UTIL_HPP
53 #include <graphlab/logger/assertions.hpp>
59 #include <graphlab/macros_def.hpp>
70 std::set<T>
set_union(
const std::set<T>& a,
const std::set<T>& b) {
74 std::inserter(output, output.begin()));
79 std::set<T>
set_union(
const std::set<T>& a,
const T& b) {
80 std::set<T> output = a;
86 std::set<T> set_intersect(
const std::set<T>& a,
const std::set<T>& b) {
88 std::set_intersection(a.begin(), a.end(),
90 std::inserter(output, output.begin()));
95 std::set<T> set_difference(
const std::set<T>& a,
const std::set<T>& b) {
97 std::set_difference(a.begin(), a.end(),
99 std::inserter(output, output.begin()));
104 template <
typename T>
105 std::set<T> set_difference(
const std::set<T>& a,
const T& b) {
106 std::set<T> output = a;
112 template <
typename T>
113 std::pair<std::set<T>,std::set<T> >
116 a = set_intersect(s, partition);
117 b = set_difference(s, partition);
118 return std::make_pair(a, b);
121 template <
typename T>
122 bool set_disjoint(
const std::set<T>& a,
const std::set<T>& b) {
123 return (intersection_size(a,b) == 0);
126 template <
typename T>
127 bool set_equal(
const std::set<T>& a,
const std::set<T>& b) {
128 if (a.size() != b.size())
return false;
132 template <
typename T>
133 bool includes(
const std::set<T>& a,
const std::set<T>& b) {
134 return std::includes(a.begin(), a.end(), b.begin(), b.end());
140 template <
typename T>
141 bool is_subset(
const std::set<T>& a,
const std::set<T>& b) {
142 return includes(b, a);
145 template <
typename T>
146 bool is_superset(
const std::set<T>& a,
const std::set<T>& b) {
147 return includes(a, b);
151 template <
typename T>
152 std::ostream& operator<<(std::ostream& out, const std::set<T>& s) {
153 return print_range(out, s,
'{',
' ',
'}');
162 template <
typename Key,
typename T>
165 typedef typename std::map<Key, T>::const_iterator iterator;
166 iterator iter = map.find(key);
167 ASSERT_TRUE(iter != map.end());
176 template <
typename Key,
typename T>
178 const Key& key,
const T default_value) {
179 typedef typename std::map<Key, T>::const_iterator iterator;
180 iterator iter = map.find(key);
181 if (iter == map.end())
return default_value;
182 else return iter->second;
190 template <
typename OldKey,
typename NewKey,
typename T>
192 rekey(
const std::map<OldKey, T>& map,
193 const std::map<OldKey, NewKey>& key_map) {
194 std::map<NewKey, T> output;
195 typedef std::pair<OldKey, T> pair_type;
196 foreach(
const pair_type& pair, map) {
197 output[
safe_get(key_map, pair.first)] = pair.second;
207 template <
typename Key,
typename OldT,
typename NewT>
209 remap(
const std::map<Key, OldT>& map,
210 const std::map<OldT, NewT>& val_map) {
211 std::map<Key, NewT> output;
212 typedef std::pair<Key, OldT> pair_type;
213 foreach(
const pair_type& pair, map) {
214 output[pair.first] =
safe_get(val_map, pair.second);
222 template <
typename Key,
typename T>
224 const std::map<T, T>& val_map) {
225 typedef std::pair<Key, T> pair_type;
226 foreach(pair_type& pair, map) {
227 pair.second =
safe_get(val_map, pair.second);
234 template <
typename Key,
typename T>
237 const std::map<Key, T>& b) {
239 std::map<Key, T> output;
242 std::inserter(output, output.begin()),
243 output.value_comp());
250 template <
typename Key,
typename T>
253 const std::map<Key, T>& b) {
255 std::map<Key, T> output;
257 std::set_intersection(a.begin(), a.end(),
259 std::inserter(output, output.begin()),
260 output.value_comp());
267 template <
typename Key,
typename T>
270 const std::set<Key>&
keys) {
271 std::map<Key, T> output;
272 foreach(
const Key& key, keys) {
273 typename std::map<Key,T>::const_iterator it = m.find(key);
275 output[key] = it->second;
283 template <
typename Key,
typename T>
286 const std::map<Key, T>& b) {
288 std::map<Key, T> output;
290 std::set_difference(a.begin(), a.end(),
292 std::inserter(output, output.begin()),
293 output.value_comp());
301 template <
typename Key,
typename T>
302 std::set<Key>
keys(
const std::map<Key, T>& map) {
303 std::set<Key> output;
304 typedef std::pair<Key, T> pair_type;
305 foreach(
const pair_type& pair, map) {
306 output.insert(pair.first);
314 template <
typename Key,
typename T>
316 std::vector<Key> output(map.size());
317 typedef std::pair<Key, T> pair_type;
319 foreach(
const pair_type& pair, map) {
320 output[i++] = pair.first;
329 template <
typename Key,
typename T>
330 std::set<T>
values(
const std::map<Key, T>& map) {
332 typedef std::pair<Key, T> pair_type;
333 foreach(
const pair_type& pair, map) {
334 output.insert(pair.second);
339 template <
typename Key,
typename T>
340 std::vector<T>
values(
const std::map<Key, T>& m,
341 const std::set<Key>&
keys) {
342 std::vector<T> output;
344 foreach(
const Key &i, keys) {
350 template <
typename Key,
typename T>
351 std::vector<T>
values(
const std::map<Key, T>& m,
352 const std::vector<Key>& keys) {
353 std::vector<T> output;
354 foreach(
const Key &i, keys) {
361 template <
typename Key>
363 std::map<Key, Key> m;
364 foreach(
const Key& key, keys)
370 template <
typename Key,
typename T>
371 std::ostream& operator<<(std::ostream& out, const std::map<Key, T>& m) {
373 for (
typename std::map<Key, T>::const_iterator it = m.begin();
375 out << it->first <<
"-->" << it->second;
376 if (++it != m.end()) out <<
" ";
385 inline std::string
trim(
const std::string& str) {
386 std::string::size_type pos1 = str.find_first_not_of(
" \t");
387 std::string::size_type pos2 = str.find_last_not_of(
" \t");
388 return str.substr(pos1 == std::string::npos ? 0 : pos1,
389 pos2 == std::string::npos ? str.size()-1 : pos2-pos1+1);
397 std::stringstream strm;
407 std::stringstream strm(str);
410 ASSERT_FALSE(strm.fail());
420 const char pad_value =
'0') {
421 std::stringstream strm;
422 strm << std::setw((
int)npad) << std::setfill(pad_value)
442 inline std::vector<std::string>
strsplit(
const std::string& str,
443 const std::string& splitchars,
444 const bool auto_trim =
false) {
445 std::vector<std::string> tokens;
446 for(
size_t beg = 0, end = 0; end != std::string::npos; beg = end+1) {
447 end = str.find_first_of(splitchars, beg);
450 std::string tmp =
trim(str.substr(beg, end - beg));
451 if(!tmp.empty()) tokens.push_back(tmp);
453 }
else tokens.push_back(str.substr(beg, end - beg));
471 #include <graphlab/macros_undef.hpp>