Skip to content

Commit d57cd8a

Browse files
committed
Added PWMap::ConstIt
1 parent de7bbd7 commit d57cd8a

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

sbg/pw_map.cpp

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,49 @@
1818
******************************************************************************/
1919

2020
#include "sbg/pw_map.hpp"
21+
#include "sbg/set_fact.hpp"
2122
#include "util/debug.hpp"
23+
#include "util/defs.hpp"
2224

2325
#include <iostream>
2426

2527
namespace SBG {
2628

2729
namespace LIB {
2830

31+
////////////////////////////////////////////////////////////////////////////////
32+
// PWMap Iterator --------------------------------------------------------------
33+
////////////////////////////////////////////////////////////////////////////////
34+
35+
PWMap::ConstIt::ConstIt(detail::UnordPWMap::ConstIt it) : _it(it) {}
36+
37+
PWMap::ConstIt::ConstIt(detail::OrdPWMap::ConstIt it) : _it(it) {}
38+
39+
const Map& PWMap::ConstIt::operator*()
40+
{
41+
auto it_visitor = SBG::Util::Overload {
42+
[](const detail::UnordPWMap::ConstIt& i) -> const Map& { return *i; },
43+
[](const detail::OrdPWMap::ConstIt& i) -> const Map& { return i->map(); }
44+
};
45+
return std::visit(it_visitor, _it);
46+
}
47+
48+
PWMap::ConstIt PWMap::ConstIt::operator++()
49+
{
50+
std::visit([](auto& i) { ++i; }, _it);
51+
return *this;
52+
}
53+
54+
bool PWMap::ConstIt::operator==(const ConstIt& other)
55+
{
56+
return _it == other._it;
57+
}
58+
59+
bool PWMap::ConstIt::operator!=(const ConstIt& other)
60+
{
61+
return _it != other._it;
62+
}
63+
2964
////////////////////////////////////////////////////////////////////////////////
3065
// PWMap implementations -------------------------------------------------------
3166
////////////////////////////////////////////////////////////////////////////////
@@ -72,6 +107,7 @@ PWMap::PWMap(const PWMapKind kind) : _impl()
72107
}
73108

74109
case PWMapKind::kOrdered: {
110+
_impl = detail::OrdPWMap{};
75111
break;
76112
}
77113

@@ -95,6 +131,7 @@ PWMap::PWMap(const PWMapKind kind, Set s) : _impl()
95131
}
96132

97133
case PWMapKind::kOrdered: {
134+
_impl = detail::UnordPWMap{s};
98135
break;
99136
}
100137

@@ -118,6 +155,7 @@ PWMap::PWMap(const PWMapKind kind, Map m) : _impl()
118155
}
119156

120157
case PWMapKind::kOrdered: {
158+
_impl = detail::UnordPWMap{m};
121159
break;
122160
}
123161

@@ -140,12 +178,14 @@ PWMap::PWMap(detail::PWMapImpl&& impl) : _impl(std::move(impl)) {}
140178

141179
PWMap::ConstIt PWMap::begin()
142180
{
143-
return std::visit([](const auto& a) { return a.begin(); }, _impl);
181+
return std::visit([](const auto& a) { return PWMap::ConstIt(a.begin()); }
182+
, _impl);
144183
}
145184

146185
PWMap::ConstIt PWMap::end()
147186
{
148-
return std::visit([](const auto& a) { return a.end(); }, _impl);
187+
return std::visit([](const auto& a) { return PWMap::ConstIt(a.end()); }
188+
, _impl);
149189
}
150190

151191
// Setters ---------------------------------------------------------------------
@@ -394,7 +434,7 @@ Set PWMap::equalImage(const PWMap& other) const
394434
return a.equalImage(b);
395435
} else {
396436
Util::ERROR("PWMap::equalImage: mismatched implementations\n");
397-
return PWMap{PWMapKind::kUnordered};
437+
return SET_FACT.createSet();
398438
}
399439
}
400440
, _impl, other._impl);
@@ -410,7 +450,7 @@ Set PWMap::lessImage(const PWMap& other) const
410450
return a.lessImage(b);
411451
} else {
412452
Util::ERROR("PWMap::lessImage: mismatched implementations\n");
413-
return PWMap{PWMapKind::kUnordered};
453+
return SET_FACT.createSet();
414454
}
415455
}
416456
, _impl, other._impl);

sbg/pw_map.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "sbg/map.hpp"
3838
#include "sbg/set.hpp"
39+
#include "sbg/ord_pwmap.hpp"
3940
#include "sbg/unord_pwmap.hpp"
4041

4142
namespace SBG {
@@ -48,7 +49,7 @@ namespace detail {
4849
// PWMaps implementations ------------------------------------------------------
4950
////////////////////////////////////////////////////////////////////////////////
5051

51-
using PWMapImpl = std::variant<UnordPWMap>;
52+
using PWMapImpl = std::variant<UnordPWMap, OrdPWMap>;
5253

5354
} // namespace detail
5455

@@ -62,7 +63,21 @@ std::ostream& operator<<(std::ostream& out, PWMapKind kind);
6263

6364
class PWMap {
6465
public:
65-
using ConstIt = std::vector<Map>::const_iterator;
66+
class ConstIt {
67+
public:
68+
const Map& operator*();
69+
ConstIt operator++();
70+
bool operator==(const ConstIt& other);
71+
bool operator!=(const ConstIt& other);
72+
73+
private:
74+
ConstIt(detail::UnordPWMap::ConstIt it);
75+
ConstIt(detail::OrdPWMap::ConstIt it);
76+
77+
std::variant<detail::UnordPWMap::ConstIt, detail::OrdPWMap::ConstIt> _it;
78+
79+
friend class PWMap;
80+
};
6681

6782
/**
6883
* @brief Constructs an empty domain pw.

0 commit comments

Comments
 (0)