summaryrefslogtreecommitdiff
path: root/graphs/cpp/bimap/bimap.hxx
blob: 9b1eb1f2fb50d1380d1a334c9ddbabf4bc7b1f32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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_;
}