From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- tiger-compiler/lib/misc/endomap.hxx | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tiger-compiler/lib/misc/endomap.hxx (limited to 'tiger-compiler/lib/misc/endomap.hxx') diff --git a/tiger-compiler/lib/misc/endomap.hxx b/tiger-compiler/lib/misc/endomap.hxx new file mode 100644 index 0000000..58c1912 --- /dev/null +++ b/tiger-compiler/lib/misc/endomap.hxx @@ -0,0 +1,44 @@ +/** -*- 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 -- cgit v1.2.3