From efb155ca3ff50630821dd5d45927d7c8a58ae57b Mon Sep 17 00:00:00 2001 From: Lucio Trincheri Date: Tue, 17 Dec 2024 16:31:40 -0300 Subject: [PATCH 1/6] Fix typo --- sbg/multidim_lexp.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sbg/multidim_lexp.hpp b/sbg/multidim_lexp.hpp index 96caae9..0cd97f2 100755 --- a/sbg/multidim_lexp.hpp +++ b/sbg/multidim_lexp.hpp @@ -1,6 +1,6 @@ /** @file multidim_lexp.hpp - @brief Multi-dimensional inear expressions implementation + @brief Multi-dimensional linear expressions implementation
From eac64ec3d5ac9d029d93dbdbb3a9e0c6c7b0db6e Mon Sep 17 00:00:00 2001 From: Lucio Trincheri Date: Tue, 17 Dec 2024 16:32:05 -0300 Subject: [PATCH 2/6] Make converters friend class of class being converted --- sbg/dto/multidim_inter_dto.hpp | 2 ++ sbg/dto/pw_mdinter_dto.hpp | 3 +++ 2 files changed, 5 insertions(+) diff --git a/sbg/dto/multidim_inter_dto.hpp b/sbg/dto/multidim_inter_dto.hpp index 510bc78..5c2cb2c 100644 --- a/sbg/dto/multidim_inter_dto.hpp +++ b/sbg/dto/multidim_inter_dto.hpp @@ -34,6 +34,8 @@ namespace SBG { namespace API { +class MultiDimInterDTOConverter; + using MD_NAT = Util::MD_NAT; using NAT = Util::NAT; diff --git a/sbg/dto/pw_mdinter_dto.hpp b/sbg/dto/pw_mdinter_dto.hpp index 61e5c9b..e0a718a 100644 --- a/sbg/dto/pw_mdinter_dto.hpp +++ b/sbg/dto/pw_mdinter_dto.hpp @@ -36,6 +36,8 @@ namespace SBG { namespace API { +class PWMDInterDTOConverter; + // Container ------------------------------------------------------------------- typedef std::vector SetPieceDTOVector; @@ -63,6 +65,7 @@ struct PWMDInterDTO { bool isEmpty() const; friend std::ostream &operator<<(std::ostream &out, const PWMDInterDTO &i); + friend class PWMDInterDTOConverter; }; typedef PWMDInterDTO SetDTO; From 66926842b38ca271af224513054e5ed9f4aca848 Mon Sep 17 00:00:00 2001 From: Lucio Trincheri Date: Tue, 17 Dec 2024 16:34:14 -0300 Subject: [PATCH 3/6] Add initial implementation of LExpDTO, MDLExpDTO, and MapDTO with their converters. --- sbg/dto/Makefile.include | 6 ++ sbg/dto/converters/lexp_dto_converter.cpp | 31 +++++++++ sbg/dto/converters/lexp_dto_converter.hpp | 55 +++++++++++++++ sbg/dto/converters/map_dto_converter.cpp | 37 ++++++++++ sbg/dto/converters/map_dto_converter.hpp | 58 ++++++++++++++++ .../multidim_lexp_dto_converter.cpp | 37 ++++++++++ .../multidim_lexp_dto_converter.hpp | 57 ++++++++++++++++ sbg/dto/lexp_dto.cpp | 63 +++++++++++++++++ sbg/dto/lexp_dto.hpp | 56 +++++++++++++++ sbg/dto/map_dto.cpp | 61 +++++++++++++++++ sbg/dto/map_dto.hpp | 68 +++++++++++++++++++ sbg/dto/multidim_lexp_dto.cpp | 62 +++++++++++++++++ sbg/dto/multidim_lexp_dto.hpp | 64 +++++++++++++++++ 13 files changed, 655 insertions(+) create mode 100644 sbg/dto/converters/lexp_dto_converter.cpp create mode 100644 sbg/dto/converters/lexp_dto_converter.hpp create mode 100644 sbg/dto/converters/map_dto_converter.cpp create mode 100644 sbg/dto/converters/map_dto_converter.hpp create mode 100644 sbg/dto/converters/multidim_lexp_dto_converter.cpp create mode 100644 sbg/dto/converters/multidim_lexp_dto_converter.hpp create mode 100644 sbg/dto/lexp_dto.cpp create mode 100644 sbg/dto/lexp_dto.hpp create mode 100644 sbg/dto/map_dto.cpp create mode 100644 sbg/dto/map_dto.hpp create mode 100644 sbg/dto/multidim_lexp_dto.cpp create mode 100644 sbg/dto/multidim_lexp_dto.hpp diff --git a/sbg/dto/Makefile.include b/sbg/dto/Makefile.include index 70a76db..8034628 100644 --- a/sbg/dto/Makefile.include +++ b/sbg/dto/Makefile.include @@ -1,10 +1,16 @@ # DTO Sources DTO_SRC := \ $(DTO_ROOT)/interval_dto.cpp \ + $(DTO_ROOT)/lexp_dto.cpp \ + $(DTO_ROOT)/map_dto.cpp \ $(DTO_ROOT)/multidim_inter_dto.cpp \ + $(DTO_ROOT)/multidim_lexp_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_mdinter_dto_converter.cpp # DTO Objects diff --git a/sbg/dto/converters/lexp_dto_converter.cpp b/sbg/dto/converters/lexp_dto_converter.cpp new file mode 100644 index 0000000..72157bb --- /dev/null +++ b/sbg/dto/converters/lexp_dto_converter.cpp @@ -0,0 +1,31 @@ +/******************************************************************************* + + 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/lexp_dto_converter.hpp" + +namespace SBG { + +namespace API { +SBG::LIB::LExp LExpDTOConverter::convertToLExp(const LExpDTO& dto) { + return SBG::LIB::LExp(dto.slope_, dto.offset_); +} + +} // namespace API + +} // namespace SBG \ No newline at end of file diff --git a/sbg/dto/converters/lexp_dto_converter.hpp b/sbg/dto/converters/lexp_dto_converter.hpp new file mode 100644 index 0000000..80f8e89 --- /dev/null +++ b/sbg/dto/converters/lexp_dto_converter.hpp @@ -0,0 +1,55 @@ +/** @file lexp_dto_converter.hpp + + @brief LExpDTOConverter implementation + + The LExpDTOConverter is used to convert a LExpDTO to + the corresponding linear expression representation. + Currently, it can convert to SBG::LIB::LExp, + but more conversions can be added in the future. + +
+ + 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_LEXP_CONVERTER_HPP +#define SBG_DTO_LEXP_CONVERTER_HPP + +#include "sbg/dto/lexp_dto.hpp" +#include "sbg/lexp.hpp" + +namespace SBG { + +namespace API { + +class LExpDTOConverter { +public: + /** + * @brief Converts a LExpDTO object to an actual linear + * expression implementation (e.g., SBG::LIB::LExp). + * + * @param dto: The LExpDTO object to convert. + * @return SBG::LIB::LExp: The converted linear expression object. + */ + static SBG::LIB::LExp convertToLExp(const LExpDTO& dto); +}; + +} // namespace API + +} // namespace SBG + +#endif // SBG_DTO_LEXP_CONVERTER_HPP diff --git a/sbg/dto/converters/map_dto_converter.cpp b/sbg/dto/converters/map_dto_converter.cpp new file mode 100644 index 0000000..2fd161b --- /dev/null +++ b/sbg/dto/converters/map_dto_converter.cpp @@ -0,0 +1,37 @@ +/******************************************************************************* + + 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/map_dto_converter.hpp" +#include "sbg/dto/converters/pw_mdinter_dto_converter.hpp" +#include "sbg/dto/converters/multidim_lexp_dto_converter.hpp" + +namespace SBG { + +namespace API { +SBG::LIB::BaseMap SBGMapDTOConverter::convertToBaseMap(const MapDTO& dto) { + return SBG::LIB::BaseMap(SetDTOConverter::convertToUnordSet(dto.dom_), ExpDTOConverter::convertToExp(dto.exp_)); +} + +SBG::LIB::CanonMap SBGMapDTOConverter::convertToCanonMap(const MapDTO& dto) { + return SBG::LIB::CanonMap(SetDTOConverter::convertToOrdSet(dto.dom_), ExpDTOConverter::convertToExp(dto.exp_)); +} + +} // namespace API + +} // namespace SBG \ No newline at end of file diff --git a/sbg/dto/converters/map_dto_converter.hpp b/sbg/dto/converters/map_dto_converter.hpp new file mode 100644 index 0000000..77080fb --- /dev/null +++ b/sbg/dto/converters/map_dto_converter.hpp @@ -0,0 +1,58 @@ +/** @file map_dto_converter.hpp + + @brief SBGMapDTOConverter implementation + + The SBGMapDTOConverter is used to convert a SBGMapDTO (MapDTO) + to the corresponding SBG map representation. + Currently, it can convert to SBG::LIB::SBGMap (BaseMap) and + SBG::LIB::SBGMap (CanonMap), 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_MAP_CONVERTER_HPP +#define SBG_DTO_MAP_CONVERTER_HPP + +#include "sbg/dto/map_dto.hpp" +#include "sbg/map.hpp" + +namespace SBG { + +namespace API { + +class SBGMapDTOConverter { +public: + /** + * @brief Converts a MapDTO object to an actual SBG map implementation + * (e.g., SBG::LIB::BaseMap / SBG::LIB::CanonMap). + * + * @param dto: The MapDTO object to convert. + * @return SBG::LIB::BaseMap/CanonMap: The converted SBG map object. + */ + static SBG::LIB::BaseMap convertToBaseMap(const MapDTO& dto); + static SBG::LIB::CanonMap convertToCanonMap(const MapDTO& dto); +}; + +typedef SBGMapDTOConverter MapDTOConverter; + +} // namespace API + +} // namespace SBG + +#endif // SBG_DTO_MAP_CONVERTER_HPP diff --git a/sbg/dto/converters/multidim_lexp_dto_converter.cpp b/sbg/dto/converters/multidim_lexp_dto_converter.cpp new file mode 100644 index 0000000..d3e4cf0 --- /dev/null +++ b/sbg/dto/converters/multidim_lexp_dto_converter.cpp @@ -0,0 +1,37 @@ +/******************************************************************************* + + 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/multidim_lexp_dto_converter.hpp" +#include "sbg/dto/converters/lexp_dto_converter.hpp" + +namespace SBG { + +namespace API { +SBG::LIB::Exp MDLExpDTOConverter::convertToExp(const ExpDTO& dto) { + SBG::LIB::Exp exp; + for (const LExpDTO& lexp_dto : dto.exps_) { + exp.emplaceBack(LExpDTOConverter::convertToLExp(lexp_dto)); + } + + return exp; +} + +} // namespace API + +} // namespace SBG \ No newline at end of file diff --git a/sbg/dto/converters/multidim_lexp_dto_converter.hpp b/sbg/dto/converters/multidim_lexp_dto_converter.hpp new file mode 100644 index 0000000..277144b --- /dev/null +++ b/sbg/dto/converters/multidim_lexp_dto_converter.hpp @@ -0,0 +1,57 @@ +/** @file multidim_lexp_dto_converter.hpp + + @brief MDLExpDTOConverter implementation + + The MDLExpDTOConverter is used to convert a MDLExpDTO (ExpDTO) to the + corresponding multi-dimensional linear expression representation. + Currently, it can convert to SBG::LIB::MDLExp (Exp), + but more conversions can be added in the future. + +
+ + 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_MDLEXP_CONVERTER_HPP +#define SBG_DTO_MDLEXP_CONVERTER_HPP + +#include "sbg/dto/multidim_lexp_dto.hpp" +#include "sbg/multidim_lexp.hpp" + +namespace SBG { + +namespace API { + +class MDLExpDTOConverter { +public: + /** + * @brief Converts a ExpDTO object to an actual multi-dimensional + * linear expression implementation (e.g., SBG::LIB::Exp). + * + * @param dto: The ExpDTO object to convert. + * @return SBG::LIB::Exp: The converted multi-dimensional linear expression object. + */ + static SBG::LIB::Exp convertToExp(const ExpDTO& dto); +}; + +typedef MDLExpDTOConverter ExpDTOConverter; + +} // namespace API + +} // namespace SBG + +#endif // SBG_DTO_MDLEXP_CONVERTER_HPP diff --git a/sbg/dto/lexp_dto.cpp b/sbg/dto/lexp_dto.cpp new file mode 100644 index 0000000..fbe4ea2 --- /dev/null +++ b/sbg/dto/lexp_dto.cpp @@ -0,0 +1,63 @@ +/******************************************************************************* + + 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/lexp_dto.hpp" + +namespace SBG { + +namespace API { + +LExpDTO::LExpDTO() : slope_(1), offset_(0) {} +LExpDTO::LExpDTO(RAT slope, RAT offset) : slope_(slope), offset_(offset) {} + +member_imp(LExpDTO, RAT, slope); +member_imp(LExpDTO, RAT, offset); + +std::ostream &operator<<(std::ostream &out, const LExpDTO &le) +{ + RAT slo = le.slope_, off = le.offset_; + + if (slo == 0) { + out << off; + return out; + } + + if (slo == 1) { + out << "x"; + } else { + if (slo.numerator() != 1) + out << slo.numerator(); + + if (slo.denominator() != 1) + out << "x/" << slo.denominator(); + + else + out << "x"; + } + + if (off != 0) { + out << "+" << off; + } + + return out; +} + +} // namespace API + +} // namespace SBG diff --git a/sbg/dto/lexp_dto.hpp b/sbg/dto/lexp_dto.hpp new file mode 100644 index 0000000..1a372f9 --- /dev/null +++ b/sbg/dto/lexp_dto.hpp @@ -0,0 +1,56 @@ +/** @file lexp_dto.hpp + + @brief Linear expressions DTO implementation + + The LExpDTO class is a Data Transfer Object (DTO) for handling + raw data, such as from JSON. It is designed for conversion into + concrete implementations like LExp via a dedicated converter. + +
+ + 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_LEXP_HPP +#define SBG_DTO_LEXP_HPP + +#include "util/defs.hpp" + +namespace SBG { + +namespace API { + +class LExpDTOConverter; + +using RAT = Util::RATIONAL; + +struct LExpDTO { + member_class(RAT, slope); + member_class(RAT, offset); + + LExpDTO(); + LExpDTO(RAT slope, RAT offset); + + friend std::ostream &operator<<(std::ostream &out, const LExpDTO &le); + friend class LExpDTOConverter; +}; + +} // namespace API + +} // namespace SBG + +#endif diff --git a/sbg/dto/map_dto.cpp b/sbg/dto/map_dto.cpp new file mode 100644 index 0000000..b7848e8 --- /dev/null +++ b/sbg/dto/map_dto.cpp @@ -0,0 +1,61 @@ +/******************************************************************************* + + 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/map_dto.hpp" + +namespace SBG { + +namespace API { + +SBGMapDTO::SBGMapDTO() : dom_(SetDTO()), exp_(ExpDTO()) {} +SBGMapDTO::SBGMapDTO(Util::MD_NAT x, ExpDTO exp) : dom_(), exp_() { + SetPieceDTO mdi(x); + dom_ = SetDTO(mdi); + exp_ = exp; +} +SBGMapDTO::SBGMapDTO(IntervalDTO i, LExpDTO le) : dom_(), exp_() { + dom_ = SetDTO(SetPieceDTO(i)); + exp_ = ExpDTO(le); +} +SBGMapDTO::SBGMapDTO(SetPieceDTO mdi, ExpDTO exp) : dom_(), exp_() { + dom_ = SetDTO(mdi); + exp_ = exp; +} +SBGMapDTO::SBGMapDTO(SetDTO dom, ExpDTO exp) : dom_(), exp_() { + if (!dom.isEmpty()) { + dom_ = dom; + exp_ = exp; + } +} + +member_imp(SBGMapDTO, SetDTO, dom); +member_imp(SBGMapDTO, ExpDTO, exp); + +bool SBGMapDTO::isEmpty() const { return dom_.isEmpty(); } + +std::ostream &operator<<(std::ostream &out, const SBGMapDTO &sbgmap) +{ + out << sbgmap.dom_ << " -> " << sbgmap.exp_; + + return out; +} + +} // namespace LIB + +} // namespace SBG diff --git a/sbg/dto/map_dto.hpp b/sbg/dto/map_dto.hpp new file mode 100644 index 0000000..b49c65a --- /dev/null +++ b/sbg/dto/map_dto.hpp @@ -0,0 +1,68 @@ +/** @file map_dto.hpp + + @brief SBG map DTO implementation + + The SBGMapDTO class is a Data Transfer Object (DTO) for handling + raw data, such as from JSON. It is designed for conversion into + concrete implementations like SBGMap via a dedicated converter. + +
+ + 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_MAP_HPP +#define SBG_DTO_MAP_HPP + +#include "sbg/dto/multidim_lexp_dto.hpp" +#include "sbg/dto/pw_mdinter_dto.hpp" +#include "util/debug.hpp" + +namespace SBG { + +namespace API { + +class SBGMapDTOConverter; + +//TODO: no need for template given that the map converter will convert the SetDTO to an actual UnordSet/OrdSet (BaseMap/CanonMap) +//template +struct SBGMapDTO { + member_class(SetDTO, dom); + member_class(ExpDTO, exp); + + SBGMapDTO(); + SBGMapDTO(Util::MD_NAT x, ExpDTO exp); + SBGMapDTO(IntervalDTO i, LExpDTO le); + SBGMapDTO(SetPieceDTO mdi, ExpDTO exp); + SBGMapDTO(SetDTO dom, ExpDTO exp); + + /** + * @brief Traditional map operations. + */ + bool isEmpty() const; + + friend std::ostream &operator<<(std::ostream &out, const SBGMapDTO &sbgmap); + friend class SBGMapDTOConverter; +}; + +typedef SBGMapDTO MapDTO; + +} // namespace API + +} // namespace SBG + +#endif diff --git a/sbg/dto/multidim_lexp_dto.cpp b/sbg/dto/multidim_lexp_dto.cpp new file mode 100644 index 0000000..7b6580c --- /dev/null +++ b/sbg/dto/multidim_lexp_dto.cpp @@ -0,0 +1,62 @@ +/******************************************************************************* + + 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/multidim_lexp_dto.hpp" + +namespace SBG { + +namespace API { + +MDLExpDTO::MDLExpDTO() : exps_() {} +MDLExpDTO::MDLExpDTO(Util::MD_NAT x) +{ + for (Util::NAT xi : x) + exps_.emplace_back(LExpDTO(0, Util::RATIONAL(xi))); +} +MDLExpDTO::MDLExpDTO(LExpDTO le) : exps_() { exps_.emplace_back(le); } +MDLExpDTO::MDLExpDTO(unsigned int nmbr_copies, LExpDTO le) : exps_() +{ + for (unsigned int j = 0; j < nmbr_copies; ++j) + exps_.emplace_back(le); +} +MDLExpDTO::MDLExpDTO(LExpDTOVector v) : exps_(v) {} + +member_imp(MDLExpDTO, LExpDTOVector, exps); + +void MDLExpDTO::emplaceBack(LExpDTO le) { exps_.emplace_back(le); } + +LExpDTO &MDLExpDTO::operator[](std::size_t n) { return exps_[n]; } +const LExpDTO &MDLExpDTO::operator[](std::size_t n) const { return exps_[n]; } + +std::ostream &operator<<(std::ostream &out, const MDLExpDTO &mdle) +{ + unsigned int sz = mdle.exps_.size(); + + if (sz > 0) { + for (unsigned int j = 0; j < sz-1; ++j) + out << mdle[j] << "|"; + out << mdle[sz-1]; + } + + return out; +} + +} // namespace API + +} // namespace SBG diff --git a/sbg/dto/multidim_lexp_dto.hpp b/sbg/dto/multidim_lexp_dto.hpp new file mode 100644 index 0000000..7c54dd3 --- /dev/null +++ b/sbg/dto/multidim_lexp_dto.hpp @@ -0,0 +1,64 @@ +/** @file multidim_lexp_dto.hpp + + @brief Multi-dimensional linear expressions DTO implementation + + The MDLExpDTO class is a Data Transfer Object (DTO) for handling + raw data, such as from JSON. It is designed for conversion into + concrete implementations like MDLExp via a dedicated converter. + +
+ + 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_MULTIDIM_LEXP_HPP +#define SBG_DTO_MULTIDIM_LEXP_HPP + +#include "sbg/dto/lexp_dto.hpp" + +namespace SBG { + +namespace API { + +class MDLExpDTOConverter; + +typedef std::vector LExpDTOVector; + +struct MDLExpDTO { + member_class(LExpDTOVector, exps); + + MDLExpDTO(); + MDLExpDTO(Util::MD_NAT x); // Expression mapping to x + MDLExpDTO(LExpDTO le); + MDLExpDTO(unsigned int nmbr_copies, LExpDTO le); + MDLExpDTO(LExpDTOVector v); + + void emplaceBack(LExpDTO le); + LExpDTO &operator[](std::size_t n); + const LExpDTO &operator[](std::size_t n) const; + + friend std::ostream &operator<<(std::ostream &out, const MDLExpDTO &mdle); + friend class MDLExpDTOConverter; +}; + +typedef MDLExpDTO ExpDTO; + +} // namespace API + +} // namespace SBG + +#endif From e890a821453459438a7f3f4b0448e51cbb63ba39 Mon Sep 17 00:00:00 2001 From: Lucio Trincheri Date: Wed, 18 Dec 2024 12:20:58 -0300 Subject: [PATCH 4/6] Remove TODO --- sbg/dto/map_dto.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/sbg/dto/map_dto.hpp b/sbg/dto/map_dto.hpp index b49c65a..1586dcd 100644 --- a/sbg/dto/map_dto.hpp +++ b/sbg/dto/map_dto.hpp @@ -38,8 +38,6 @@ namespace API { class SBGMapDTOConverter; -//TODO: no need for template given that the map converter will convert the SetDTO to an actual UnordSet/OrdSet (BaseMap/CanonMap) -//template struct SBGMapDTO { member_class(SetDTO, dom); member_class(ExpDTO, exp); From be506e396f2a9c32df24b1a6882ef185b983cd42 Mon Sep 17 00:00:00 2001 From: Lucio Trincheri Date: Fri, 20 Dec 2024 14:24:42 -0300 Subject: [PATCH 5/6] Add functions for future support of PWMapDTO --- sbg/dto/multidim_inter_dto.cpp | 2 ++ sbg/dto/multidim_inter_dto.hpp | 1 + sbg/dto/pw_mdinter_dto.cpp | 2 ++ sbg/dto/pw_mdinter_dto.hpp | 1 + 4 files changed, 6 insertions(+) diff --git a/sbg/dto/multidim_inter_dto.cpp b/sbg/dto/multidim_inter_dto.cpp index 4272127..7a45d56 100644 --- a/sbg/dto/multidim_inter_dto.cpp +++ b/sbg/dto/multidim_inter_dto.cpp @@ -60,6 +60,8 @@ const IntervalDTO &MultiDimInterDTO::operator[](std::size_t n) const return intervals_[n]; } +std::size_t MultiDimInterDTO::arity() const { return intervals_.size(); } + bool MultiDimInterDTO::isEmpty() const { return intervals_.empty(); } std::ostream &operator<<(std::ostream &out, const MultiDimInterDTO &mdi) diff --git a/sbg/dto/multidim_inter_dto.hpp b/sbg/dto/multidim_inter_dto.hpp index 5c2cb2c..0c28c05 100644 --- a/sbg/dto/multidim_inter_dto.hpp +++ b/sbg/dto/multidim_inter_dto.hpp @@ -58,6 +58,7 @@ struct MultiDimInterDTO { /** * @brief Traditional set operations. */ + std::size_t arity() const; bool isEmpty() const; friend std::ostream &operator<<(std::ostream &out, const MultiDimInterDTO &i); diff --git a/sbg/dto/pw_mdinter_dto.cpp b/sbg/dto/pw_mdinter_dto.cpp index 24c2837..8b8499b 100644 --- a/sbg/dto/pw_mdinter_dto.cpp +++ b/sbg/dto/pw_mdinter_dto.cpp @@ -69,6 +69,8 @@ PWMDInterDTO::PWMDInterDTO(SetPieceDTOVector container) : pieces_() { member_imp(PWMDInterDTO, SetPieceDTOVector, pieces); +SetPieceDTOVector::iterator PWMDInterDTO::begin() { return pieces_.begin(); } + std::size_t PWMDInterDTO::size() const { return pieces_.size(); } void PWMDInterDTO::emplace(SetPieceDTO mdi) diff --git a/sbg/dto/pw_mdinter_dto.hpp b/sbg/dto/pw_mdinter_dto.hpp index e0a718a..582bf61 100644 --- a/sbg/dto/pw_mdinter_dto.hpp +++ b/sbg/dto/pw_mdinter_dto.hpp @@ -55,6 +55,7 @@ struct PWMDInterDTO { PWMDInterDTO(SetPieceDTO mdi); PWMDInterDTO(SetPieceDTOVector container); + SetPieceDTOVector::iterator begin(); std::size_t size() const; void emplace(SetPieceDTO mdi); void emplaceBack(SetPieceDTO mdi); From 5ff2ec87cb2b5ca94d78eeeb4e23a1bb8b5bc5d6 Mon Sep 17 00:00:00 2001 From: Lucio Trincheri Date: Fri, 20 Dec 2024 14:25:47 -0300 Subject: [PATCH 6/6] 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