diff --git a/examples/format.cc b/examples/format.cc index dbd359e..a9b5c15 100644 --- a/examples/format.cc +++ b/examples/format.cc @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -52,7 +52,7 @@ int main() { return range(v + 1); })); - Map m = { + std::unordered_map m = { { "foo", 5 }, { "bar", 10 }, { "baz", 15 } diff --git a/ostd/unordered_map.hh b/ostd/unordered_map.hh new file mode 100644 index 0000000..5ce1a55 --- /dev/null +++ b/ostd/unordered_map.hh @@ -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 + +#include "ostd/range.hh" + +namespace ostd { + +template +struct ranged_traits> { + using Range = IteratorRange::iterator>; + static Range iter(std::unordered_map &v) { + return Range{v.begin(), v.end()}; + } +}; + +template +struct ranged_traits const> { + using Range = IteratorRange::const_iterator>; + static Range iter(std::unordered_map const &v) { + return Range{v.cbegin(), v.cend()}; + } +}; + +template +inline std::unordered_map make_unordered_map(R range) { + /* TODO: specialize for contiguous ranges and matching value types */ + std::unordered_map ret; + for (; !range.empty(); range.pop_front()) { + ret.emplace(range.front()); + } + return ret; +} + +template +inline std::unordered_map< + typename RangeValue::first_type, + typename RangeValue::second_type +> make_unordered_map(R range) { + return make_unordered_map< + typename RangeValue::first_type, + typename RangeValue::second_type + >(std::move(range)); +} + +} /* namespace ostd */ + +#endif