Skip to content

Commit e321f3a

Browse files
committed
New version of compact for UnordSet, OrdSet, UnordPWMap and OrdPWMap
1 parent 6f1855b commit e321f3a

6 files changed

Lines changed: 105 additions & 40 deletions

File tree

sbg/map_entry.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ bool MapEntry::operator<(const MapEntry& other) const
4848
{
4949
return _perimeter.min() < other._perimeter.min();
5050
}
51+
52+
MaybeMapEntry MapEntry::compact(const MapEntry& other) const
53+
{
54+
MaybeMap compacted = _map.compact(other._map);
55+
if (compacted) {
56+
return MaybeMapEntry{compacted.value()};
57+
}
58+
59+
return {};
60+
}
5161

5262
} // namespace detail
5363

sbg/map_entry.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@
3131
#include "sbg/set.hpp"
3232
#include "sbg/set_perimeter.hpp"
3333

34+
#include <optional>
35+
3436
namespace SBG {
3537

3638
namespace LIB {
3739

3840
namespace detail {
3941

42+
class MapEntry;
43+
44+
using MaybeMapEntry = std::optional<MapEntry>;
45+
4046
class MapEntry {
4147
public:
4248
MapEntry(const Set& s, const Expression& expr);
@@ -48,6 +54,8 @@ class MapEntry {
4854
bool operator==(const MapEntry& other) const;
4955
bool operator<(const MapEntry& other) const;
5056

57+
MaybeMapEntry compact(const MapEntry& other) const;
58+
5159
private:
5260
Map _map;
5361
SetPerimeter _perimeter;

sbg/ord_pwmap.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ namespace detail {
3737

3838
// Auxliary definitions --------------------------------------------------------
3939

40-
class MapLess {
40+
class MapEntryLess {
4141
public:
42-
bool operator()(const Map& a, const Map& b) const {
43-
return a.domain().minElem() < b.domain().minElem();
42+
bool operator()(const MapEntry& a, const MapEntry& b) const {
43+
return a.map().domain().minElem() < b.map().domain().minElem();
4444
}
4545
};
4646

@@ -735,9 +735,47 @@ Set OrdPWMap::lessImage(const OrdPWMap& other) const
735735
return traverse(other, LessImageCore{}).result();
736736
}
737737

738-
// TODO
739738
void OrdPWMap::compact()
740739
{
740+
using MapSet = std::set<MapEntry, MapEntryLess>;
741+
742+
OrdMapCollection result;
743+
744+
if (!isEmpty()) {
745+
MapSet set_result;
746+
MapSet to_erase;
747+
do {
748+
MapSet new_set_result;
749+
to_erase.clear();
750+
751+
MapSet::iterator ith = set_result.begin();
752+
MapSet::iterator last = set_result.end();
753+
for (; ith != last; ++ith) {
754+
MapEntry ith_compact = *ith;
755+
MapSet::iterator next = ith;
756+
++next;
757+
for (; next != last; ++next) {
758+
MaybeMapEntry new_compact = ith_compact.compact(*next);
759+
if (new_compact) {
760+
ith_compact = new_compact.value();
761+
to_erase.insert(*next);
762+
}
763+
}
764+
765+
if (to_erase.find(ith_compact) == to_erase.end()) {
766+
new_set_result.insert(ith_compact);
767+
}
768+
}
769+
770+
std::swap(set_result, new_set_result);
771+
} while (!to_erase.empty());
772+
773+
for (const MapEntry& map_entry : set_result) {
774+
result.push_back(map_entry);
775+
}
776+
}
777+
778+
_pieces = std::move(result);
741779
}
742780

743781
} // namespace detail

sbg/ord_set.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -635,21 +635,23 @@ SetPerimeter OrderedSet::perimeter() const
635635

636636
void OrderedSet::compact()
637637
{
638+
using MDISet = std::set<MultiDimInter>;
639+
638640
OrdMDICollection result;
639641

640642
if (!isEmpty()) {
641-
std::set<MultiDimInter> prev{_pieces.begin(), _pieces.end()};
642-
std::set<MultiDimInter> actual = prev;
643+
MDISet set_result{std::make_move_iterator(_pieces.begin())
644+
, std::make_move_iterator(_pieces.end())};
645+
MDISet to_erase;
643646
do {
644-
prev = actual;
645-
actual = std::set<MultiDimInter>{};
647+
MDISet new_set_result;
648+
to_erase.clear();
646649

647-
std::set<MultiDimInter>::iterator ith = prev.begin();
648-
std::set<MultiDimInter>::iterator last = prev.end();
649-
std::set<MultiDimInter> to_erase;
650+
MDISet::iterator ith = set_result.begin();
651+
MDISet::iterator last = set_result.end();
650652
for (; ith != last; ++ith) {
651653
MultiDimInter ith_compact = *ith;
652-
std::set<MultiDimInter>::iterator next = ith;
654+
MDISet::iterator next = ith;
653655
++next;
654656
for (; next != last; ++next) {
655657
MaybeMDI new_compact = ith_compact.compact(*next);
@@ -660,13 +662,15 @@ void OrderedSet::compact()
660662
}
661663

662664
if (to_erase.find(ith_compact) == to_erase.end()) {
663-
actual.insert(ith_compact);
665+
new_set_result.insert(ith_compact);
664666
}
665-
}
666-
} while (actual != prev);
667+
}
668+
669+
std::swap(set_result, new_set_result);
670+
} while (!to_erase.empty());
667671

668-
for (const MultiDimInter& mdi : actual) {
669-
result.push_back(mdi);
672+
for (const MultiDimInter& m : set_result) {
673+
result.push_back(m);
670674
}
671675
}
672676

sbg/unord_pwmap.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,15 @@ void UnordPWMap::compact()
487487
UnordMapCollection result;
488488

489489
if (!isEmpty()) {
490-
MapSet prev{std::make_move_iterator(_pieces.begin())
490+
MapSet set_result{std::make_move_iterator(_pieces.begin())
491491
, std::make_move_iterator(_pieces.end())};
492-
MapSet actual = prev;
492+
MapSet to_erase;
493493
do {
494-
prev = actual;
495-
actual = MapSet{};
494+
MapSet new_set_result;
495+
to_erase.clear();
496496

497-
MapSet::iterator ith = prev.begin();
498-
MapSet::iterator last = prev.end();
499-
MapSet to_erase;
497+
MapSet::iterator ith = set_result.begin();
498+
MapSet::iterator last = set_result.end();
500499
for (; ith != last; ++ith) {
501500
Map ith_compact = *ith;
502501
MapSet::iterator next = ith;
@@ -510,12 +509,14 @@ void UnordPWMap::compact()
510509
}
511510

512511
if (to_erase.find(ith_compact) == to_erase.end()) {
513-
actual.insert(ith_compact);
512+
new_set_result.insert(ith_compact);
514513
}
515514
}
516-
} while (actual != prev);
517515

518-
for (const Map& m : actual) {
516+
std::swap(set_result, new_set_result);
517+
} while (!to_erase.empty());
518+
519+
for (const Map& m : set_result) {
519520
result.push_back(m);
520521
}
521522
}

sbg/unord_set.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -437,21 +437,23 @@ SetPerimeter UnorderedSet::perimeter() const
437437

438438
void UnorderedSet::compact()
439439
{
440+
using MDISet = std::set<MultiDimInter>;
441+
440442
MDIUnordCollection result;
441443

442444
if (!isEmpty()) {
443-
std::set<MultiDimInter> prev{_pieces.begin(), _pieces.end()};
444-
std::set<MultiDimInter> actual = prev;
445+
MDISet set_result{std::make_move_iterator(_pieces.begin())
446+
, std::make_move_iterator(_pieces.end())};
447+
MDISet to_erase;
445448
do {
446-
prev = actual;
447-
actual = std::set<MultiDimInter>{};
449+
MDISet new_set_result;
450+
to_erase.clear();
448451

449-
std::set<MultiDimInter>::iterator ith = prev.begin();
450-
std::set<MultiDimInter>::iterator last = prev.end();
451-
std::set<MultiDimInter> to_erase;
452+
MDISet::iterator ith = set_result.begin();
453+
MDISet::iterator last = set_result.end();
452454
for (; ith != last; ++ith) {
453455
MultiDimInter ith_compact = *ith;
454-
std::set<MultiDimInter>::iterator next = ith;
456+
MDISet::iterator next = ith;
455457
++next;
456458
for (; next != last; ++next) {
457459
MaybeMDI new_compact = ith_compact.compact(*next);
@@ -462,13 +464,15 @@ void UnorderedSet::compact()
462464
}
463465

464466
if (to_erase.find(ith_compact) == to_erase.end()) {
465-
actual.insert(ith_compact);
467+
new_set_result.insert(ith_compact);
466468
}
467-
}
468-
} while (actual != prev);
469+
}
470+
471+
std::swap(set_result, new_set_result);
472+
} while (!to_erase.empty());
469473

470-
for (const MultiDimInter& mdi : actual) {
471-
result.push_back(mdi);
474+
for (const MultiDimInter& m : set_result) {
475+
result.push_back(m);
472476
}
473477
}
474478

0 commit comments

Comments
 (0)