diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /tiger-compiler/lib/misc/endomap.hxx | |
Diffstat (limited to 'tiger-compiler/lib/misc/endomap.hxx')
| -rw-r--r-- | tiger-compiler/lib/misc/endomap.hxx | 44 |
1 files changed, 44 insertions, 0 deletions
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 <misc/endomap.hh> + +namespace misc +{ + template <class T> + endomap<T>::endomap() + : map<T, T>() + , strictness_(strictness_type::nonstrict) + {} + + template <class T> endomap<T>* endomap<T>::clone() const + { + return new endomap<T>(*this); + } + + template <class T> T endomap<T>::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 <class T> T& endomap<T>::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 |
