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/map.hh | 141 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 tiger-compiler/lib/misc/map.hh (limited to 'tiger-compiler/lib/misc/map.hh') diff --git a/tiger-compiler/lib/misc/map.hh b/tiger-compiler/lib/misc/map.hh new file mode 100644 index 0000000..952a1e2 --- /dev/null +++ b/tiger-compiler/lib/misc/map.hh @@ -0,0 +1,141 @@ +/** + ** \file misc/map.hh + ** \brief Declaration of misc::Map. + */ + +#pragma once + +#include +#include +#include +#include + +namespace misc +{ + /** \brief Wrapper around std::map providing syntactic sugar + for any mapping of types manipulation. */ + template class map + { + protected: + using map_type = std::map; + + public: + /// The map's key type. + using key_type = typename map_type::key_type; + /// The type of object associated with the keys. + using mapped_type = typename map_type::mapped_type; + /// The type of object, std::pair, + /// stored in the map. + using value_type = typename map_type::value_type; + /// Type of the function object that compares two keys for ordering. + using key_compare = typename map_type::key_compare; + /// Iterator used to iterate through a map. + using iterator = typename map_type::iterator; + /// Const iterator used to iterate through a map. + using const_iterator = typename map_type::const_iterator; + + public: + /// \name Constructors and destructor. + /// \{ + map() = default; + /// Return a duplicate of \a this. + virtual ~map() = default; + /// \} + + /// Return a duplicate of \a this. + virtual map* clone() const; + + /// Returns the key_compare object used by the map. + key_compare key_comp() const; + + /// Lookup for \a t. + /// \{ + virtual iterator find(const T& t); + virtual const_iterator find(const T& t) const; + /// \} + + /// Lookup for \a t and throw an exception if not available. + /// \{ + virtual iterator xfind(const T& t); + virtual const_iterator xfind(const T& t) const; + /// \} + + /// Lookup for the value associated to \a t. + virtual N operator[](const T& t) const; + + /// \brief Return a reference to the value associated to \a t. + /// + /// This method is not virtual, so that it is not automatically + /// instantiated at the instantiation of misc::map. This way, + /// there won't be errors from the compiler about \a N not being a + /// model of the concept Default Constructible. This is needed + /// for some instantiations of misc::map, notably the ones that + /// uses a symbol::Symbol as mapped type, which has no default + /// constructor (note that in this case everything will be fine as + /// long as this method is not called -- compile errors will occur + /// otherwise). + /// + /// We might want to provide two flavors of misc::map, one that + /// requires that its second parameter is a model of Default + /// Constructible and that provides an operator[] similar to + /// std::map's; and another one that doesn't. + N& operator[](const T& t); + + /// Lookup for \a t, throwing std::range_error if it is not found. + virtual N operator()(const T& t) const; + + /// Lookup for a list \a ts of T's, throwing std::range_error if + /// one or several elements are not found. + template + requires ConstIterableType + std::vector operator()(const Container& ts) const; + + /// \name Iterating. + /// \{ + virtual iterator begin(); + virtual iterator end(); + virtual const_iterator begin() const; + virtual const_iterator end() const; + /// \} + + /// Finds the first element whose key is not less than \a k. + /// \{ + virtual iterator lower_bound(const T& k); + virtual const_iterator lower_bound(const T& k) const; + /// \} + + /// Inserts \a x into the map. + virtual std::pair insert(const std::pair& x); + /// Copy the contents of \a other to \a this. + virtual void insert(const map& other); + + /// Insert a new element built in-place using \a args into the map. + template std::pair emplace(Args&&... args); + + /// Is the map empty ? + virtual bool empty() const; + + /// Number of known identifiers. + virtual size_t size() const; + + /// Remove and return mapping for \a t from the map. + virtual N take(const T& t); + + /// Erase all of the elements. + virtual void clear(); + + /// Description of known mappings. + virtual std::ostream& print(std::ostream& ostr) const; + + protected: + /// Mapping from T to N. + map_type map_; + }; + + /// Output \a m onto \a ostr. + template + std::ostream& operator<<(std::ostream& ostr, const map& m); + +} // namespace misc + +#include -- cgit v1.2.3