From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/cpp/bimap/bimap.hh | 36 +++++++++++++++++++ graphs/cpp/bimap/bimap.hxx | 74 +++++++++++++++++++++++++++++++++++++++ graphs/cpp/bimap/bimap_example.cc | 26 ++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 graphs/cpp/bimap/bimap.hh create mode 100644 graphs/cpp/bimap/bimap.hxx create mode 100644 graphs/cpp/bimap/bimap_example.cc (limited to 'graphs/cpp/bimap') 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 + + template + class Bimap +{ + static_assert(!std::is_same_v, + "Lhs and Rhs must be different types"); + using mapLhs = std::map; + using mapRhs = std::map; + 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 +bool Bimap::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 +bool Bimap::insert(const Rhs& vr, const Lhs& vl) +{ + return insert(vl, vr); +} +template +std::size_t Bimap::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 +std::size_t Bimap::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 Bimap::iteratorLhs Bimap::find(const Lhs& vl) const +{ + return lhs_.find(vl); +} +template +typename Bimap::iteratorRhs Bimap::find(const Rhs& vr) const +{ + return rhs_.find(vr); +} +template +std::size_t Bimap::size() const +{ + return lhs_.size(); +} +template +void Bimap::clear() +{ + lhs_ = {}; + rhs_ = {}; +} +template +const typename Bimap::mapLhs& Bimap::get_lhs() const +{ + return lhs_; +} +template +const typename Bimap::mapRhs& Bimap::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 +#include + +#include "bimap.hh" + +int main() +{ + Bimap 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; +} -- cgit v1.2.3