15template<
typename Left,
typename Right>
18template<
typename Left,
typename Right>
21template<
typename Left,
typename Right,
typename Predicate>
24template<
typename Left,
typename Right,
typename Predicate>
27template<
typename Left,
typename Right,
typename Predicate>
33namespace _QxBimapPrivate
36template<
typename Left,
typename Right,
typename Predicate>
44 ConstIterator it = bm.
cbegin();
45 const ConstIterator end = bm.
cend();
60 if (pred(std::move(*it)))
83template<
typename Left,
typename Right>
91 friend class Bimap<Left, Right>;
94 using PIterator =
typename QHash<Left, const Right*>::const_iterator;
109 const Left&
left()
const {
return mPItr.key(); }
110 const Right&
right()
const {
return *mPItr.value(); }
114 bool operator==(
const const_iterator& other)
const =
default;
115 std::pair<const Left&, const Right&>
operator*()
const {
return std::pair<const Left&, const Right&>(
left(),
right()); }
133 using size_type =
typename QHash<Left, const Right*>::size_type;
137 QHash<Left, const Right*> mL2R;
138 QHash<Right, const Left*> mR2L;
144 Bimap(std::initializer_list<std::pair<Left, Right>> list)
147 for(
auto it = list.begin(); it != list.end(); ++it)
148 insert(it->first, it->second);
153 template<
typename AMap,
typename BMap,
typename V>
154 void removeCrossReference(AMap& am, BMap& bm,
const V& v)
156 if constexpr(std::same_as<AMap, BMap>)
157 Q_ASSERT(&am != &bm);
163 template<
typename AMap,
typename BMap,
typename V>
164 bool remove(AMap& am, BMap& bm,
const V& v)
176 const_iterator existingRelation(
const Left& l,
const Right& r)
const
178 auto itr = mL2R.constFind(l);
182 void removeCrossReferences(
const Left& l,
const Right& r)
185 removeCrossReference(mL2R, mR2L, l);
186 removeCrossReference(mR2L, mL2R, r);
191 auto lItr = mL2R.insert(l,
nullptr);
192 auto rItr = mR2L.insert(r,
nullptr);
193 lItr.value() = &rItr.key();
194 rItr.value() = &lItr.key();
213 auto rItr = mR2L.constFind(r);
214 return const_iterator(rItr != mR2L.cend() ? mL2R.constFind(*(*rItr)) : mL2R.cend());
228 auto lItr = pos.mPItr;
229 auto rItr = mR2L.constFind(pos.
right());
239 for(
auto it = other.
begin(); it != other.
end(); it++)
240 insert(it.left(), it.right());
245 if(
auto itr = existingRelation(l, r); itr !=
cend())
248 removeCrossReferences(l, r);
249 return addOrUpdateRelation(l, r);
257 return mL2R.contains(l) ? *mL2R[l] : Right();
260 Right
fromLeft(
const Left& l,
const Right& defaultValue)
const
262 return mL2R.contains(l) ? *mL2R[l] : defaultValue;
267 return mR2L.contains(l) ? *mR2L[l] : Left();
270 Left
fromRight(
const Right& l,
const Left& defaultValue)
const
272 return mR2L.contains(l) ? *mR2L[l] : defaultValue;
281 Left
toLeft(
const Right& r,
const Left& defaultValue)
const {
return fromRight(r, defaultValue); }
283 Right
toRight(
const Left& l,
const Right& defaultValue)
const {
return fromLeft(l, defaultValue); }
287 bool removeLeft(
const Left& l) {
return remove(mL2R, mR2L, l); }
288 bool removeRight(
const Right& r) {
return remove(mR2L, mL2R, r); }
290 template<
typename Predicate>
292 qsizetype
removeIf(Predicate pred) {
return _QxBimapPrivate::_erase_if(*
this, pred); }
313 mL2R.swap(other.mL2R);
314 mR2L.swap(other.mR2L);
317 qsizetype
size()
const {
return mL2R.size(); }
323 qsizetype
capacity()
const {
return mL2R.capacity(); }
324 void clear() { mL2R.clear(); mR2L.clear(); }
326 void squeeze() { mL2R.squeeze(); mR2L.squeeze(); }
328 QList<Left>
lefts()
const {
return mL2R.keys(); }
329 QList<Right>
rights()
const {
return mR2L.keys(); }
342 QList<std::pair<Left, Right>> rel;
343 for(
auto [k, v] : mL2R.asKeyValueRange())
344 rel.append(std::make_pair(k, *v));
360 if(!mL2R.contains(l))
361 throw std::invalid_argument(
"Access into bimap with a value it does not contain!");
367 if(!mR2L.contains(r))
368 throw std::invalid_argument(
"Access into bimap with a value it does not contain!");
374 const auto& oL2R = other.mL2R;
375 for (
auto [l, rp] : mL2R.asKeyValueRange())
376 if(!oL2R.contains(l) || *rp != *oL2R[l])
394template<
typename Left,
typename Right,
typename Predicate>
The Bimap::const_iterator class provides an STL-style const iterator for Bimap.
Definition qx-bimap.h:90
const_iterator operator++(int)
Definition qx-bimap.h:118
const_iterator()
Definition qx-bimap.h:105
bool operator==(const const_iterator &other) const =default
const Left & left() const
Definition qx-bimap.h:109
const_iterator & operator++()
Definition qx-bimap.h:116
const Right & right() const
Definition qx-bimap.h:110
std::pair< const Left &, const Right & > operator*() const
Definition qx-bimap.h:115
The Bimap template class offers a rudimentary bi-directional associative map.
Definition qx-bimap.h:85
Bimap()
Definition qx-bimap.h:142
Left from(const Right &r, const Left &defaultValue) const
Definition qx-bimap.h:278
Left from(const Right &r) const
Definition qx-bimap.h:277
bool operator!=(const Bimap &other) const =default
bool operator==(const Bimap &other) const
Definition qx-bimap.h:372
void insert(const Bimap &other)
Definition qx-bimap.h:234
const_iterator constBegin() const
Definition qx-bimap.h:202
QList< Left > lefts() const
Definition qx-bimap.h:328
QList< std::pair< Left, Right > > relationships() const
Definition qx-bimap.h:340
Right fromLeft(const Left &l, const Right &defaultValue) const
Definition qx-bimap.h:260
qsizetype capacity() const
Definition qx-bimap.h:323
Left take(const Right &r)
Definition qx-bimap.h:309
const_iterator insert(const Left &l, const Right &r)
Definition qx-bimap.h:243
Left takeLeft(const Right &r)
Definition qx-bimap.h:301
Right fromLeft(const Left &l) const
Definition qx-bimap.h:255
iterator findRight(const Right &r)
Definition qx-bimap.h:223
const_iterator end() const
Definition qx-bimap.h:204
bool removeRight(const Right &r)
Definition qx-bimap.h:288
const_iterator findLeft(const Left &l) const
Definition qx-bimap.h:222
const_iterator begin() const
Definition qx-bimap.h:200
Left toLeft(const Right &r) const
Definition qx-bimap.h:280
bool removeLeft(const Left &l)
Definition qx-bimap.h:287
typename QHash< Left, const Right * >::difference_type difference_type
Definition qx-bimap.h:132
Bimap(std::initializer_list< std::pair< Left, Right > > list)
Definition qx-bimap.h:144
const_iterator constFind(const Right &r) const
Definition qx-bimap.h:208
const_iterator find(const Left &l) const
Definition qx-bimap.h:218
const_iterator constFindRight(const Right &r) const
Definition qx-bimap.h:211
qsizetype size() const
Definition qx-bimap.h:317
Right right_type
Definition qx-bimap.h:130
const_iterator ConstIterator
Definition qx-bimap.h:131
bool containsLeft(const Left &l) const
Definition qx-bimap.h:252
const_iterator cend() const
Definition qx-bimap.h:205
Right from(const Left &l) const
Definition qx-bimap.h:275
bool remove(const Left &l)
Definition qx-bimap.h:285
Right operator[](const Left &l) const
Definition qx-bimap.h:354
qsizetype removeIf(Predicate pred)
Definition qx-bimap.h:292
const_iterator findRight(const Right &r) const
Definition qx-bimap.h:224
Right toRight(const Left &l) const
Definition qx-bimap.h:282
Right from(const Left &l, const Right &defaultValue) const
Definition qx-bimap.h:276
const_iterator constEnd() const
Definition qx-bimap.h:206
void squeeze()
Definition qx-bimap.h:326
iterator find(const Right &r)
Definition qx-bimap.h:219
bool isEmpty() const
Definition qx-bimap.h:319
iterator find(const Left &l)
Definition qx-bimap.h:217
Left fromRight(const Right &l) const
Definition qx-bimap.h:265
const_iterator constFindLeft(const Left &l) const
Definition qx-bimap.h:209
iterator begin()
Definition qx-bimap.h:199
bool containsRight(const Right &r) const
Definition qx-bimap.h:253
void swap(Bimap< Left, Right > &other)
Definition qx-bimap.h:311
iterator end()
Definition qx-bimap.h:203
QList< Right > rights() const
Definition qx-bimap.h:329
float load_factor() const
Definition qx-bimap.h:321
Right toRight(const Left &l, const Right &defaultValue) const
Definition qx-bimap.h:283
bool remove(const Right &r)
Definition qx-bimap.h:286
const_iterator cbegin() const
Definition qx-bimap.h:201
Left left_type
Definition qx-bimap.h:129
bool empty() const
Definition qx-bimap.h:320
void clear()
Definition qx-bimap.h:324
const_iterator constFind(const Left &l) const
Definition qx-bimap.h:207
iterator findLeft(const Left &l)
Definition qx-bimap.h:221
void reserve(qsizetype size)
Definition qx-bimap.h:325
qsizetype count() const
Definition qx-bimap.h:318
Left operator[](const Right &r) const
Definition qx-bimap.h:365
Right takeRight(const Left &l)
Definition qx-bimap.h:294
typename QHash< Left, const Right * >::size_type size_type
Definition qx-bimap.h:133
Right take(const Left &l)
Definition qx-bimap.h:308
Left toLeft(const Right &r, const Left &defaultValue) const
Definition qx-bimap.h:281
const_iterator iterator
Definition qx-bimap.h:128
Left fromRight(const Right &l, const Left &defaultValue) const
Definition qx-bimap.h:270
const_iterator erase(const_iterator pos)
Definition qx-bimap.h:226
const_iterator find(const Right &r) const
Definition qx-bimap.h:220
Specifies that a bimap has different Left and Right types.
Definition qx-bimap.h:19
Specifies that a predicate is a valid, iterator based predicate for a bimap.
Definition qx-bimap.h:22
Specifies that a predicate is a valid, pair based predicate for a bimap.
Definition qx-bimap.h:25
Specifies that a predicate is a valid predicate for a bimap.
Definition qx-bimap.h:28
Specifies that a type defines a call operator for the specified argument types (with strict return).
Definition qx-concepts.h:247
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-abstracterror.cpp:13
qsizetype erase_if(Bimap< Left, Right > &bimap, Predicate pred)
Definition qx-bimap.h:395
The qx-concepts header file provides a library of general purpose concepts as an extension of the sta...