Skip to content

Commit

Permalink
Added piecewise maps to /sbg
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed Jul 25, 2023
1 parent cc8ebeb commit 4c26b56
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 7 deletions.
3 changes: 3 additions & 0 deletions sbg/interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Interval::Interval(NAT begin, NAT step, NAT end) : begin_(begin), step_(step), e
int rem = fmod(end - begin, step);
set_end(end - rem);
}

if (cardinal(*this) == 1) set_step(1);

}

member_imp(Interval, NAT, begin);
Expand Down
6 changes: 3 additions & 3 deletions sbg/pw_inter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace LIB {

// Type definitions ------------------------------------------------------------

bool LTInter::operator()(const SetPiece &x, const SetPiece &y) const { return x < y; }

std::ostream &operator<<(std::ostream &out, const InterSet &ii)
{
InterSet aux = ii;
Expand All @@ -47,9 +49,7 @@ std::ostream &operator<<(std::ostream &out, const InterSet &ii)
// PWInterval ------------------------------------------------------------------

PWInterval::PWInterval() : pieces_() {}
PWInterval::PWInterval(SetPiece i) : pieces_() {
pieces_ref().insert(i);
}
PWInterval::PWInterval(SetPiece i) : pieces_() { pieces_ref().insert(i); }
PWInterval::PWInterval(InterSet ii) : pieces_(ii) {}

member_imp(PWInterval, InterSet, pieces);
Expand Down
10 changes: 6 additions & 4 deletions sbg/pw_inter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
#ifndef SBG_PW_INTERVAL_HPP
#define SBG_PW_INTERVAL_HPP

#include <boost/container/set.hpp>
#include <set>

#include <boost/container/flat_set.hpp>

#include <sbg/interval.hpp>

Expand All @@ -47,14 +49,14 @@ namespace LIB {
* SetPieces, is not used).
*/
struct LTInter {
bool operator()(const SetPiece &x, const SetPiece &y) const { return x < y; }
bool operator()(const SetPiece &x, const SetPiece &y) const;
typedef SetPiece first_argument_type;
typedef SetPiece second_argument_type;
typedef bool result_type;
};

typedef boost::container::set<SetPiece, LTInter, boost::container::new_allocator<SetPiece>, void> InterSet;
typedef boost::container::set<SetPiece>::iterator InterSetIt;
typedef boost::container::flat_set<SetPiece, LTInter, boost::container::new_allocator<SetPiece>> InterSet;
typedef InterSet::iterator InterSetIt;
std::ostream &operator<<(std::ostream &out, const InterSet &ii);

struct PWInterval {
Expand Down
74 changes: 74 additions & 0 deletions sbg/pw_map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*******************************************************************************
This file is part of Set--Based Graph Library.
SBG Library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SBG Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SBG Library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include <sbg/pw_map.hpp>

namespace SBG {

namespace LIB {

// Type definitions ------------------------------------------------------------

bool LTMap::operator()(const SBGMap &x, const SBGMap &y) const { return LTInter(x.dom(), y.dom()); }

std::ostream &operator<<(std::ostream &out, const MapSet &ms)
{
MapSet aux = ms;
int sz = aux.size();

out << "<<";
if (sz > 0) {
auto it = aux.begin();
for (int i = 0; i < sz - 1; ++i) {
out << *it << ", ";
++it;
}
out << *it;
}
out << ">>";

return out;
}

// PWMap -----------------------------------------------------------------------

PWMap::PWMap() : maps_() {}
PWMap::PWMap(SBGMap sm) : maps_() { maps_ref().insert(sm); }
PWMap::PWMap(MapSet maps) : maps_(maps) {}

member_imp(PWMap, MapSet, maps);

// TODO
bool PWMap::operator==(const PWMap &other) const
{
return true;
}

std::ostream &operator(std::ostream &out, const PWMap &pw)
{
out << pw.maps();

return out;
}

// PWMap functions -------------------------------------------------------------

} // namespace LIB

} // namespace SBG
73 changes: 73 additions & 0 deletions sbg/pw_map.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/** @file pw_map.hpp
@brief <b>Piecewise map implementation</b>
<hr>
This file is part of Set--Based Graph Library.
SBG Library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SBG Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SBG Library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#ifndef SBG_PWMAP_HPP
#define SBG_PWMAP_HPP

#include <sbg/map.hpp>

namespace SBG {

namespace LIB {

/**
* @brief Ordered collection of maps.
*/

/* @struct LTMap
*
* @brief This function is defined to be used by the MapSet definition. The
* two inputs are disjoint (that's why the operator<, that compares any sort of
* Maps, is not used).
*/
struct LTMap {
bool operator()(const SBGMap &x, const SBGMap &y) const;
typedef SBGMap first_argument_type;
typedef SBGMap second_argument_type;
typedef bool result_type;
};

typedef boost::container::set<SBGMap, LTMap, boost::container::new_allocator<SBGMap>, void> MapSet;
typedef boost::container::set<SBGMap>::iterator MapSetIt;
std::ostream &operator<<(std::ostream &out, const MapSet &ii);

struct PWMap {
member_class(MapSet, maps);

PWMap();
PWMap(SBGMap m);
PWMap(MapSet maps);

eq_class(PWMap);
};
std::ostream &operator<<(std::ostream &out, const PWMap &pw);

/**
* @brief Traditional map operations.
*/

} // namespace LIB

} // namespace SBG

#endif
5 changes: 5 additions & 0 deletions test/set.test
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ maxElem({[500:3:1000], [1:1:100]})
-{[1:1:100], [101:2:300]}
-{[1:1:100], [101:3:300]}
-{[3:6:100]}
-{[3:10:100], [4:10:100], [5:10:100], [6:10:100], [7:10:100], [8:10:100]}
-{[3:10:100], [4:10:100], [5:10:100], [6:10:100], [7:10:100], [8:10:100], [10:2:12]}
-{[3:10:100], [4:10:100], [7:2:11], [28:10:100], [39:10:100]}
{[0:2], [4:10:100], ..., [12:10:100]}
{[0:3], [5:10:100], ..., [13:10:100]}

({[50:2:150], [300:1:500]} \ {[1:1:100], [101:2:300]})
({[1:3:100], [2:3:100]} \ {[3:6:100]})
Expand Down

0 comments on commit 4c26b56

Please sign in to comment.