remap.hpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //
  2. // remap.hpp
  3. // remap
  4. //
  5. // Created by Sam Jaffe on 9/16/18.
  6. //
  7. #pragma once
  8. #include <map>
  9. #include <unordered_map>
  10. /*
  11. * A remap referres to a "value re-mapping object". This is used to allow us to
  12. * rename
  13. */
  14. template <typename T, typename Impl> class remap_base {
  15. public:
  16. using value_type = typename Impl::value_type;
  17. using key_type = typename Impl::key_type;
  18. using mapped_type = typename Impl::mapped_type;
  19. private:
  20. Impl impl_;
  21. public:
  22. remap_base(std::initializer_list<value_type> mappings) : impl_(mappings) {}
  23. mapped_type const & operator[](T const & value) const {
  24. auto it = impl_.find(value);
  25. return (it != impl_.end()) ? it->second : value;
  26. }
  27. };
  28. template <typename T, typename Compare = std::less<T>,
  29. typename Alloc = std::allocator<std::pair<T const, T>>>
  30. using remap = remap_base<T, std::map<T, T, Compare, Alloc>>;
  31. template <typename T, typename Hash = std::hash<T>,
  32. typename Equal = std::equal_to<T>,
  33. typename Alloc = std::allocator<std::pair<T const, T>>>
  34. using unordered_remap =
  35. remap_base<T, std::unordered_map<T, T, Hash, Equal, Alloc>>;