summaryrefslogtreecommitdiff
path: root/tiger-compiler/lib/misc/algorithm.hxx
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
commit967be9e750221ab2ab783f95df79bb26d290a45e (patch)
tree6802900a5e975f9f68b169f0f503f040056d6952 /tiger-compiler/lib/misc/algorithm.hxx
add: added projectsHEADmain
Diffstat (limited to 'tiger-compiler/lib/misc/algorithm.hxx')
-rw-r--r--tiger-compiler/lib/misc/algorithm.hxx84
1 files changed, 84 insertions, 0 deletions
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 <algorithm>
+#include <functional>
+#include <ostream>
+
+#include <misc/algorithm.hh>
+
+namespace misc
+{
+ template <typename Container>
+ requires Iterable<Container>
+ 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 <typename Container>
+ requires ConstIterable<Container>
+ 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 <typename Container>
+ requires Iterable<Container>
+ 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 <typename Container, typename Functor>
+ requires ConstIterable<Container>
+ 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 <typename Container>
+ requires ConstIterable<Container>
+ 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 <Map Map>
+ 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