Created
October 10, 2019 21:54
-
-
Save loliGothicK/d97c8e47bfcc083fe13c170f6f2d5539 to your computer and use it in GitHub Desktop.
Entry for FlatMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MITAMA_CONTAINER_ENTRY_HPP | |
#define MITAMA_CONTAINER_ENTRY_HPP | |
#include <boost/container/flat_map.hpp> | |
#include <functional> | |
#include <type_traits> | |
#include <utility> | |
namespace mitama { | |
template <class K, class V> | |
class entry_t { | |
std::reference_wrapper<boost::container::flat_map<K, V>> ref; | |
K key; | |
public: | |
entry_t(boost::container::flat_map<K, V>& _map, K const& _key): ref(_map), key(_key) {} | |
entry_t(entry_t const&) = default; | |
entry_t(entry_t&&) = default; | |
entry_t& operator=(entry_t const&) = default; | |
entry_t& operator=(entry_t&&) = default; | |
template <class F, std::enable_if_t<std::is_invocable_v<F&&, V&>, bool> = false> | |
auto and_modify(F&& f) { | |
if (ref.get().contains(key)) | |
std::invoke(std::forward<F>(f), ref.get().at(key)); | |
return *this; | |
} | |
V& or_default() { | |
return ref.get().try_emplace(key).first->second; | |
} | |
template <class... Args, std::enable_if_t<std::is_constructible_v<V, Args&&...>, bool> = false> | |
V& or_emplace(Args&&... args) { | |
return ref.get().try_emplace(key, std::forward<Args>(args)...).first->second; | |
} | |
V& or_insert(V const& v) { | |
return ref.get().try_emplace(key, v).first->second; | |
} | |
template <class F, class... Args, | |
std::enable_if_t<std::conjunction_v< | |
std::is_invocable<F&&, Args&&...>, | |
std::is_constructible<V, std::invoke_result_t<F&&, Args&&...>>>, | |
bool> = false> | |
V& or_insert_with(F&& f, Args&&... args) { | |
return ref.get().try_emplace(key, std::invoke(std::forward<F>(f), std::forward<Args>(args)...)).first->second; | |
} | |
}; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment