summaryrefslogtreecommitdiff
path: root/graphs/cpp/bimap
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
commitc9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch)
tree3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/cpp/bimap
add: graphs et rushs
Diffstat (limited to 'graphs/cpp/bimap')
-rw-r--r--graphs/cpp/bimap/bimap.hh36
-rw-r--r--graphs/cpp/bimap/bimap.hxx74
-rw-r--r--graphs/cpp/bimap/bimap_example.cc26
3 files changed, 136 insertions, 0 deletions
diff --git a/graphs/cpp/bimap/bimap.hh b/graphs/cpp/bimap/bimap.hh
new file mode 100644
index 0000000..25f459d
--- /dev/null
+++ b/graphs/cpp/bimap/bimap.hh
@@ -0,0 +1,36 @@
+#pragma once
+
+#include <map>
+
+ template <typename Lhs, typename Rhs>
+ class Bimap
+{
+ static_assert(!std::is_same_v<Lhs, Rhs>,
+ "Lhs and Rhs must be different types");
+ using mapLhs = std::map<Lhs, Rhs>;
+ using mapRhs = std::map<Rhs, Lhs>;
+ using iteratorLhs = typename mapLhs::const_iterator;
+ using iteratorRhs = typename mapRhs::const_iterator;
+
+public:
+ bool insert(const Lhs& vl, const Rhs& vr);
+ bool insert(const Rhs& vr, const Lhs& vl);
+
+ std::size_t erase(const Lhs& vl);
+ std::size_t erase(const Rhs& vr);
+
+ iteratorLhs find(const Lhs& vl) const;
+ iteratorRhs find(const Rhs& vr) const;
+
+ std::size_t size() const;
+ void clear();
+
+ const mapLhs& get_lhs() const;
+ const mapRhs& get_rhs() const;
+
+private:
+ mapLhs lhs_;
+ mapRhs rhs_;
+};
+
+#include "bimap.hxx"
diff --git a/graphs/cpp/bimap/bimap.hxx b/graphs/cpp/bimap/bimap.hxx
new file mode 100644
index 0000000..9b1eb1f
--- /dev/null
+++ b/graphs/cpp/bimap/bimap.hxx
@@ -0,0 +1,74 @@
+#pragma once
+#include "bimap.hh"
+
+template <typename Lhs, typename Rhs>
+bool Bimap<Lhs, Rhs>::insert(const Lhs& vl, const Rhs& vr)
+{
+ if (lhs_.contains(vl) || rhs_.contains(vr))
+ return false;
+ if (lhs_.contains(vl) || !lhs_.insert({ vl, vr }).second)
+ return false;
+ if (rhs_.contains(vr) || !rhs_.insert({ vr, vl }).second)
+ return false;
+ return true;
+}
+template <typename Lhs, typename Rhs>
+bool Bimap<Lhs, Rhs>::insert(const Rhs& vr, const Lhs& vl)
+{
+ return insert(vl, vr);
+}
+template <typename Lhs, typename Rhs>
+std::size_t Bimap<Lhs, Rhs>::erase(const Lhs& vl)
+{
+ if (lhs_.size() <= 0)
+ return 0;
+ auto range = lhs_.find(vl);
+ if (range == lhs_.end())
+ return 0;
+ rhs_.erase(range->second);
+ size_t n = lhs_.erase(range->first);
+ return n;
+}
+template <typename Lhs, typename Rhs>
+std::size_t Bimap<Lhs, Rhs>::erase(const Rhs& vr)
+{
+ if (lhs_.size() <= 0)
+ return 0;
+ auto range = rhs_.find(vr);
+ if (range == rhs_.end())
+ return 0;
+ lhs_.erase(range->second);
+ size_t n = rhs_.erase(range->first);
+ return n;
+}
+template <typename Lhs, typename Rhs>
+typename Bimap<Lhs, Rhs>::iteratorLhs Bimap<Lhs, Rhs>::find(const Lhs& vl) const
+{
+ return lhs_.find(vl);
+}
+template <typename Lhs, typename Rhs>
+typename Bimap<Lhs, Rhs>::iteratorRhs Bimap<Lhs, Rhs>::find(const Rhs& vr) const
+{
+ return rhs_.find(vr);
+}
+template <typename Lhs, typename Rhs>
+std::size_t Bimap<Lhs, Rhs>::size() const
+{
+ return lhs_.size();
+}
+template <typename Lhs, typename Rhs>
+void Bimap<Lhs, Rhs>::clear()
+{
+ lhs_ = {};
+ rhs_ = {};
+}
+template <typename Lhs, typename Rhs>
+const typename Bimap<Lhs, Rhs>::mapLhs& Bimap<Lhs, Rhs>::get_lhs() const
+{
+ return lhs_;
+}
+template <typename Lhs, typename Rhs>
+const typename Bimap<Lhs, Rhs>::mapRhs& Bimap<Lhs, Rhs>::get_rhs() const
+{
+ return rhs_;
+} \ No newline at end of file
diff --git a/graphs/cpp/bimap/bimap_example.cc b/graphs/cpp/bimap/bimap_example.cc
new file mode 100644
index 0000000..771a2b0
--- /dev/null
+++ b/graphs/cpp/bimap/bimap_example.cc
@@ -0,0 +1,26 @@
+#include <iostream>
+#include <string>
+
+#include "bimap.hh"
+
+int main()
+{
+ Bimap<int, std::string> bm;
+
+ bm.insert(16, "Test.");
+ bm.insert("Prologin", 29);
+
+ std::cout << "The bimap contains: " << bm.size() << " elements\n";
+
+ auto it1 = bm.find("Test.");
+ auto it2 = bm.find(29);
+
+ std::cout << it1->first << ' ' << it1->second << '\n';
+ std::cout << it2->first << ' ' << it2->second << '\n';
+
+ bm.erase(16);
+ bm.erase("Prologin");
+
+ bm.clear();
+ return 0;
+}