std::unordered_map initial range support (no extra template args yet)

master
Daniel Kolesa 2017-01-31 19:11:38 +01:00
parent 6ec72a6ecd
commit fbdaf77b62
2 changed files with 56 additions and 2 deletions

View File

@ -2,7 +2,7 @@
#include <ostd/algorithm.hh>
#include <ostd/vector.hh>
#include <ostd/map.hh>
#include <ostd/unordered_map.hh>
#include <ostd/range.hh>
#include <ostd/io.hh>
@ -52,7 +52,7 @@ int main() {
return range(v + 1);
}));
Map<std::string, int> m = {
std::unordered_map<std::string, int> m = {
{ "foo", 5 },
{ "bar", 10 },
{ "baz", 15 }

View File

@ -0,0 +1,54 @@
/* OctaSTD extensions for std::unordered_map.
*
* This file is part of OctaSTD. See COPYING.md for futher information.
*/
#ifndef OSTD_UNORDERED_MAP_HH
#define OSTD_UNORDERED_MAP_HH
#include <unordered_map>
#include "ostd/range.hh"
namespace ostd {
template<typename K, typename T>
struct ranged_traits<std::unordered_map<K, T>> {
using Range = IteratorRange<typename std::unordered_map<K, T>::iterator>;
static Range iter(std::unordered_map<K, T> &v) {
return Range{v.begin(), v.end()};
}
};
template<typename K, typename T>
struct ranged_traits<std::unordered_map<K, T> const> {
using Range = IteratorRange<typename std::unordered_map<K, T>::const_iterator>;
static Range iter(std::unordered_map<K, T> const &v) {
return Range{v.cbegin(), v.cend()};
}
};
template<typename K, typename T, typename R>
inline std::unordered_map<K, T> make_unordered_map(R range) {
/* TODO: specialize for contiguous ranges and matching value types */
std::unordered_map<K, T> ret;
for (; !range.empty(); range.pop_front()) {
ret.emplace(range.front());
}
return ret;
}
template<typename R>
inline std::unordered_map<
typename RangeValue<R>::first_type,
typename RangeValue<R>::second_type
> make_unordered_map(R range) {
return make_unordered_map<
typename RangeValue<R>::first_type,
typename RangeValue<R>::second_type
>(std::move(range));
}
} /* namespace ostd */
#endif