/** -*- C++ -*- ** \file misc/endomap.hxx ** \brief Implementation of misc::endomap. */ #pragma once #include namespace misc { template endomap::endomap() : map() , strictness_(strictness_type::nonstrict) {} template endomap* endomap::clone() const { return new endomap(*this); } template T endomap::operator()(const T& t) const { if (const auto&& ires = this->find(t); ires != this->map_.end()) return ires->second; else if (this->strictness_ == strictness_type::nonstrict) return t; std::ostringstream err; err << "map: no mapping for " << t; throw std::range_error(err.str()); } template T& endomap::operator[](const T& t) { // Inspired by ``Efficient STL'' on efficient insert_or_update // for maps. See also misc::put. auto i = this->map_.lower_bound(t); if (i == this->map_.end() || this->map_.key_comp()(t, i->first)) i = this->map_.emplace(t, t).first; return i->second; } } // namespace misc