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.hh | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tiger-compiler/lib/misc/endomap.hh (limited to 'tiger-compiler/lib/misc/endomap.hh') diff --git a/tiger-compiler/lib/misc/endomap.hh b/tiger-compiler/lib/misc/endomap.hh new file mode 100644 index 0000000..4a69e87 --- /dev/null +++ b/tiger-compiler/lib/misc/endomap.hh @@ -0,0 +1,64 @@ +/** + ** \file misc/endomap.hh + ** \brief Declaration of misc::endomap. + */ + +#pragma once + +#include +#include + +#include + +namespace misc +{ + /** \brief A renaming mapping defaulting to identity: + Mapping a class with itself (endomorphism). + Unless a value is already mapped onto another, map it to itself + if nonstrict. This is a special case of std::map. */ + template class endomap : public map + { + public: + /// Super class type. + using super_type = map; + + // Import overloaded virtual functions. + using super_type::operator[]; + using super_type::operator(); + + enum class strictness_type + { + /// Querying about an unknown identifier returns itself. + nonstrict, + /// Querying about an unknown identifier is an error. + strict + }; + + /// \name Constructors and destructor. + /// \{ + endomap(); + /// \} + + /// Return a duplicate of \a this. + endomap* clone() const override; + + /// Return a reference to the value associated to \a t. + /// + /// If \a t was not inserted before, then, contrary to std::map, + /// we insert t -> t into the map, instead of `t -> T()'. This + /// spares the construction of a new T, which may have nasty + /// effects if this constructor has side effects (e.g., + /// incrementing a counter). + virtual T& operator[](const T& t); + + /// Lookup for \a t, returning it if unknown. + T operator()(const T& t) const override; + + protected: + /// If strict, queries for an unknown (unmapped) Identifier is an error. + strictness_type strictness_; + }; + +} // namespace misc + +#include -- cgit v1.2.3