Skip to content

Commit

Permalink
Add initial implementation of PWMapDTO and its converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucio Trincheri committed Dec 20, 2024
1 parent be506e3 commit 5ff2ec8
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sbg/dto/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ DTO_SRC := \
$(DTO_ROOT)/map_dto.cpp \
$(DTO_ROOT)/multidim_inter_dto.cpp \
$(DTO_ROOT)/multidim_lexp_dto.cpp \
$(DTO_ROOT)/pw_map_dto.cpp \
$(DTO_ROOT)/pw_mdinter_dto.cpp \
$(CONVERTERS_ROOT)/interval_dto_converter.cpp \
$(CONVERTERS_ROOT)/lexp_dto_converter.cpp \
$(CONVERTERS_ROOT)/map_dto_converter.cpp \
$(CONVERTERS_ROOT)/multidim_inter_dto_converter.cpp \
$(CONVERTERS_ROOT)/multidim_lexp_dto_converter.cpp \
$(CONVERTERS_ROOT)/pw_map_dto_converter.cpp \
$(CONVERTERS_ROOT)/pw_mdinter_dto_converter.cpp

# DTO Objects
Expand Down
46 changes: 46 additions & 0 deletions sbg/dto/converters/pw_map_dto_converter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*******************************************************************************
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/dto/converters/pw_map_dto_converter.hpp"
#include "sbg/dto/converters/map_dto_converter.hpp"

namespace SBG {

namespace API {
SBG::LIB::BasePWMap PWMapDTOConverter::convertToBasePWMap(const PWMapDTO& dto) {
SBG::LIB::BasePWMap base_pw_map;
for (const MapDTO& map_dto : dto.maps_) {
base_pw_map.emplaceBack(MapDTOConverter::convertToBaseMap(map_dto));
}

return base_pw_map;
}

SBG::LIB::CanonPWMap PWMapDTOConverter::convertToCanonPWMap(const PWMapDTO& dto) {
SBG::LIB::CanonPWMap canon_pw_map;
for (const MapDTO& map_dto : dto.maps_) {
canon_pw_map.emplaceBack(MapDTOConverter::convertToCanonMap(map_dto));
}

return canon_pw_map;
}

} // namespace API

} // namespace SBG
56 changes: 56 additions & 0 deletions sbg/dto/converters/pw_map_dto_converter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/** @file map_dto_converter.hpp
@brief <b> PWMapDTOConverter implementation</b>
The PWMapDTOConverter is used to convert a PWMapDTO
to the corresponding piecewise SBG map representation.
Currently, it can convert to SBG::LIB::PWMap<UnordSet> (BasePWMap) and
SBG::LIB::PWMap<OrdSet> (CanonPWMap), but more conversions can be added.
<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_DTO_PWMAP_CONVERTER_HPP
#define SBG_DTO_PWMAP_CONVERTER_HPP

#include "sbg/dto/pw_map_dto.hpp"
#include "sbg/pw_map.hpp"

namespace SBG {

namespace API {

class PWMapDTOConverter {
public:
/**
* @brief Converts a PWMapDTO object to an actual SBG piecewise map
* implementation (e.g., SBG::LIB::BasePWMap / SBG::LIB::CanonPWMap).
*
* @param dto: The PWMapDTO object to convert.
* @return SBG::LIB::BasePWMap/CanonPWMap: The converted SBG piecewise map object.
*/
static SBG::LIB::BasePWMap convertToBasePWMap(const PWMapDTO& dto);
static SBG::LIB::CanonPWMap convertToCanonPWMap(const PWMapDTO& dto);
};

} // namespace API

} // namespace SBG

#endif // SBG_DTO_PWMAP_CONVERTER_HPP
92 changes: 92 additions & 0 deletions sbg/dto/pw_map_dto.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*******************************************************************************
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/dto/pw_map_dto.hpp"

namespace SBG {

namespace API {

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

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

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

return out;
}

// PWMapDTO -----------------------------------------------------------------------

PWMapDTO::PWMapDTO() : maps_() {}
PWMapDTO::PWMapDTO(SetDTO s) : maps_() {
if (!s.isEmpty()) {
MapDTO map(s, ExpDTO(s.begin()->arity(), LExpDTO()));
maps_.emplace_back(map);
}
}
PWMapDTO::PWMapDTO(MapDTO map) : maps_() {
if (!map.dom().isEmpty())
maps_.emplace_back(map);
}
PWMapDTO::PWMapDTO(MapDTOVector maps) : maps_(maps) {}

member_imp(PWMapDTO, MapDTOVector, maps);

void PWMapDTO::emplace(MapDTO map) {
if (!map.dom().isEmpty())
maps_.emplace_back(map);
}

void PWMapDTO::emplaceBack(MapDTO map)
{
if (!map.dom().isEmpty())
maps_.emplace_back(map);
}

// PWMapDTO functions -------------------------------------------------------------

bool PWMapDTO::isEmpty() const { return maps_.empty(); }

// std::ostream operators ------------------------------------------------------

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

return out;
}

} // namespace API

} // namespace SBG
67 changes: 67 additions & 0 deletions sbg/dto/pw_map_dto.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/** @file pw_map.hpp
@brief <b>Piecewise map DTO 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_DTO_PWMAP_HPP
#define SBG_DTO_PWMAP_HPP

#include "sbg/dto/map_dto.hpp"

namespace SBG {

namespace API {

class PWMapDTOConverter;

/**
* @brief Unordered collection of map DTOs.
*/

using MapDTOVector = std::vector<MapDTO>; // ex MapSet.
std::ostream &operator<<(std::ostream &out, const MapDTOVector &ms);

struct PWMapDTO {
member_class(MapDTOVector, maps);

PWMapDTO();
PWMapDTO(SetDTO s); // Create id with s as domain
PWMapDTO(MapDTO m);
PWMapDTO(MapDTOVector maps);

std::size_t size() const;
void emplace(MapDTO m);
void emplaceBack(MapDTO m);

/**
* @brief Traditional map operations.
*/
bool isEmpty() const;

friend std::ostream &operator<<(std::ostream &out, const PWMapDTO &pw);
friend class PWMapDTOConverter;
};

} // namespace API

} // namespace SBG

#endif

0 comments on commit 5ff2ec8

Please sign in to comment.