Skip to the content.

:heavy_check_mark: adaptor/virtual_map.hpp

Depends on

Required by

Verified with

Code

#pragma once

#include <utility>
#include <functional>

#include "internal/dev_env.hpp"


namespace uni {


template<class> struct virtual_combined_map {};

template<class Mapped, class... Keys>
struct virtual_combined_map<Mapped(Keys...)> {
    using key_type = std::tuple<Keys...>;
    using mapped_type = Mapped;
    using value_type = std::pair<key_type,mapped_type>;

  protected:
    std::function<Mapped(Keys...)> _f;

  public:
    virtual_combined_map() = default;

    template<class F>
    explicit virtual_combined_map(F&& f) noexcept(NO_EXCEPT) : _f(f) {};

    inline const auto* get_functor() const noexcept(NO_EXCEPT) { return this->_f; }
    inline auto* get_functor() noexcept(NO_EXCEPT) { return this->_f; }

    template<class F>
    inline auto& set_functor(F&& f) const noexcept(NO_EXCEPT) { return this->_f = f; }

    inline mapped_type operator()(const Keys&... key) const noexcept(NO_EXCEPT) {
        return this->_f(key...);
    }

    inline mapped_type operator[](const key_type& key) const noexcept(NO_EXCEPT) {
        return std::apply(this->_f, key);
    }
};

template<class Key, class Mapped>
struct virtual_map : virtual_combined_map<Mapped(Key)> {
    using key_type = Key;
    using mapped_type = Mapped;
    using value_type = std::pair<key_type,mapped_type>;

    using virtual_combined_map<mapped_type(key_type)>::virtual_combined_map;

    inline mapped_type operator[](const key_type& key) const noexcept(NO_EXCEPT) {
        return this->_f(key);
    }
};

}; // namesapce uni
#line 2 "adaptor/virtual_map.hpp"

#include <utility>
#include <functional>

#line 2 "internal/dev_env.hpp"


#ifdef LOCAL_JUDGE
    inline constexpr bool DEV_ENV = true;
    inline constexpr bool NO_EXCEPT = false;
#else
    inline constexpr bool DEV_ENV = false;
    inline constexpr bool NO_EXCEPT = true;
#endif // LOCAL_JUDGE


#if __cplusplus >= 202100L
    #define CPP20 true
    #define CPP23 true
#elif __cplusplus >= 202002L
    #define CPP20 true
    #define CPP23 false
#else
    #define CPP20 false
    #define CPP23 false
#endif
#line 7 "adaptor/virtual_map.hpp"


namespace uni {


template<class> struct virtual_combined_map {};

template<class Mapped, class... Keys>
struct virtual_combined_map<Mapped(Keys...)> {
    using key_type = std::tuple<Keys...>;
    using mapped_type = Mapped;
    using value_type = std::pair<key_type,mapped_type>;

  protected:
    std::function<Mapped(Keys...)> _f;

  public:
    virtual_combined_map() = default;

    template<class F>
    explicit virtual_combined_map(F&& f) noexcept(NO_EXCEPT) : _f(f) {};

    inline const auto* get_functor() const noexcept(NO_EXCEPT) { return this->_f; }
    inline auto* get_functor() noexcept(NO_EXCEPT) { return this->_f; }

    template<class F>
    inline auto& set_functor(F&& f) const noexcept(NO_EXCEPT) { return this->_f = f; }

    inline mapped_type operator()(const Keys&... key) const noexcept(NO_EXCEPT) {
        return this->_f(key...);
    }

    inline mapped_type operator[](const key_type& key) const noexcept(NO_EXCEPT) {
        return std::apply(this->_f, key);
    }
};

template<class Key, class Mapped>
struct virtual_map : virtual_combined_map<Mapped(Key)> {
    using key_type = Key;
    using mapped_type = Mapped;
    using value_type = std::pair<key_type,mapped_type>;

    using virtual_combined_map<mapped_type(key_type)>::virtual_combined_map;

    inline mapped_type operator[](const key_type& key) const noexcept(NO_EXCEPT) {
        return this->_f(key);
    }
};

}; // namesapce uni
Back to top page