Derecho  0.9
Distributed systems toolkit for RDMA
container_template_functions.hpp
Go to the documentation of this file.
1 
8 #pragma once
9 #include <algorithm>
10 #include <list>
11 #include <map>
12 #include <set>
13 #include <vector>
14 
15 namespace derecho {
16 
24 template <typename T>
25 std::set<T> functional_insert(std::set<T>& a, const std::set<T>& b) {
26  a.insert(b.begin(), b.end());
27  return a;
28 }
29 
33 template <typename T>
34 std::vector<T> functional_append(const std::vector<T>& original, const T& item) {
35  std::vector<T> appended_vec(original);
36  appended_vec.emplace_back(item);
37  return appended_vec;
38 }
39 
51 template <typename T, typename... RestArgs>
52 std::vector<T> functional_append(const std::vector<T>& original, const T& first_item, RestArgs... rest_items) {
53  std::vector<T> appended_vec = functional_append(original, first_item);
54  return functional_append(appended_vec, rest_items...);
55 }
56 
64 template <typename K1, typename K2, typename V>
65 std::size_t multimap_size(const std::map<K1, std::map<K2, V>>& multimap) {
66  std::size_t count = 0;
67  for(const auto& map_pair : multimap) {
68  count += map_pair.second.size();
69  }
70  return count;
71 }
72 
79 template <typename K, typename V>
80 std::list<K> keys_as_list(const std::map<K, V>& map) {
81  std::list<K> keys;
82  for(const auto& pair : map) {
83  keys.emplace_back(pair.first);
84  }
85  return keys;
86 }
87 
98 template <typename Container>
99 std::size_t index_of(const Container& container, const typename Container::value_type& elem) {
100  return std::distance(std::begin(container),
101  std::find(std::begin(container),
102  std::end(container), elem));
103 }
104 
105 } // namespace derecho
std::list< K > keys_as_list(const std::map< K, V > &map)
Constructs a std::list of the keys in a std::map, in the same order as they appear in the std::map...
std::size_t multimap_size(const std::map< K1, std::map< K2, V >> &multimap)
Returns the size of a std::map of std::maps, by counting up the sizes of all the inner maps...
std::set< T > functional_insert(std::set< T > &a, const std::set< T > &b)
Inserts set b into set a and returns the modified a.
std::size_t index_of(const Container &container, const typename Container::value_type &elem)
Finds a value in a STL container, and returns the index of that value in the container.
std::vector< T > functional_append(const std::vector< T > &original, const T &item)
Base case for functional_append, with one argument.