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/algorithm.hxx | 84 +++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tiger-compiler/lib/misc/algorithm.hxx (limited to 'tiger-compiler/lib/misc/algorithm.hxx') diff --git a/tiger-compiler/lib/misc/algorithm.hxx b/tiger-compiler/lib/misc/algorithm.hxx new file mode 100644 index 0000000..00ca8ad --- /dev/null +++ b/tiger-compiler/lib/misc/algorithm.hxx @@ -0,0 +1,84 @@ +/** + ** \file misc/algorithm.hxx + ** \brief Some syntactic sugar to look for things in STL containers. + */ + +#pragma once + +#include +#include +#include + +#include + +namespace misc +{ + template + requires Iterable + void deep_clear(Container& c) + { + for (typename Container::value_type& x : c) + delete x; + c.clear(); + } + + // Find \a v in the whole \a c. + template + requires ConstIterable + inline typename Container::const_iterator + find(const Container& c, const typename Container::value_type& v) + { + return std::find(c.begin(), c.end(), v); + } + + // Find \a v in the whole \a c. + template + requires Iterable + inline typename Container::iterator + find(Container& c, const typename Container::value_type& v) + { + return std::find(c.begin(), c.end(), v); + } + + // Apply \a f to all the members of \a c, and return it. + template + requires ConstIterable + inline Functor& for_each(Container& c, Functor& f) + { + std::ranges::for_each(c, std::ref(f)); + return f; + } + + // Is \a v member of \a c? + template + requires ConstIterable + inline bool has(const Container& c, const typename Container::value_type& v) + { + // We specify the instance to solve a conflict between the + // two finds above, that compete against each other because + // the parameter Container can embed a "const". + return std::ranges::find(c, v) != c.end(); + } + + template + typename Map::iterator put(Map& map, + const typename Map::key_type& key, + const typename Map::mapped_type& value) + { + // See ``Efficient STL''. + auto i = map.lower_bound(key); + + if (i != map.end() && !(map.key_comp()(key, i->first))) + { + // Update. + i->second = value; + return i; + } + else + { + // First insertion. + return map.emplace(key, value).first; + } + } + +} // namespace misc -- cgit v1.2.3