From 5ff2ec87cb2b5ca94d78eeeb4e23a1bb8b5bc5d6 Mon Sep 17 00:00:00 2001 From: Lucio Trincheri Date: Fri, 20 Dec 2024 14:25:47 -0300 Subject: [PATCH] Add initial implementation of PWMapDTO and its converter. --- sbg/dto/Makefile.include | 2 + sbg/dto/converters/pw_map_dto_converter.cpp | 46 +++++++++++ sbg/dto/converters/pw_map_dto_converter.hpp | 56 +++++++++++++ sbg/dto/pw_map_dto.cpp | 92 +++++++++++++++++++++ sbg/dto/pw_map_dto.hpp | 67 +++++++++++++++ 5 files changed, 263 insertions(+) create mode 100644 sbg/dto/converters/pw_map_dto_converter.cpp create mode 100644 sbg/dto/converters/pw_map_dto_converter.hpp create mode 100644 sbg/dto/pw_map_dto.cpp create mode 100644 sbg/dto/pw_map_dto.hpp diff --git a/sbg/dto/Makefile.include b/sbg/dto/Makefile.include index 8034628..7aafd0c 100644 --- a/sbg/dto/Makefile.include +++ b/sbg/dto/Makefile.include @@ -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 diff --git a/sbg/dto/converters/pw_map_dto_converter.cpp b/sbg/dto/converters/pw_map_dto_converter.cpp new file mode 100644 index 0000000..de80733 --- /dev/null +++ b/sbg/dto/converters/pw_map_dto_converter.cpp @@ -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 . + + ******************************************************************************/ + +#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 \ No newline at end of file diff --git a/sbg/dto/converters/pw_map_dto_converter.hpp b/sbg/dto/converters/pw_map_dto_converter.hpp new file mode 100644 index 0000000..9a0b11a --- /dev/null +++ b/sbg/dto/converters/pw_map_dto_converter.hpp @@ -0,0 +1,56 @@ +/** @file map_dto_converter.hpp + + @brief PWMapDTOConverter implementation + + The PWMapDTOConverter is used to convert a PWMapDTO + to the corresponding piecewise SBG map representation. + Currently, it can convert to SBG::LIB::PWMap (BasePWMap) and + SBG::LIB::PWMap (CanonPWMap), but more conversions can be added. + +
+ + 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 . + + ******************************************************************************/ + +#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 diff --git a/sbg/dto/pw_map_dto.cpp b/sbg/dto/pw_map_dto.cpp new file mode 100644 index 0000000..f035330 --- /dev/null +++ b/sbg/dto/pw_map_dto.cpp @@ -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 . + + ******************************************************************************/ + +#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 diff --git a/sbg/dto/pw_map_dto.hpp b/sbg/dto/pw_map_dto.hpp new file mode 100644 index 0000000..47762af --- /dev/null +++ b/sbg/dto/pw_map_dto.hpp @@ -0,0 +1,67 @@ +/** @file pw_map.hpp + + @brief Piecewise map DTO implementation + +
+ + 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 . + + ******************************************************************************/ + +#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; // 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