From 4b547a491f5b48e73108d738aabe101399612844 Mon Sep 17 00:00:00 2001 From: Lucio Trincheri Date: Mon, 16 Dec 2024 14:49:49 -0300 Subject: [PATCH] iss-25: Add initial IntervalDTO, MultiDimInterDTO, and their associated converters (#37) * Fix missing include in util/defs.hpp header * Initial IntervalDTO implementation * Change namespace from DTO to API * Initial IntervalDTOConverter implementation * Re-add isEmpty method for future MultiDimInter implementation * Initial implementation of MultiDimInterDTO and it's converter. Separate makefiles * Remove include --------- Co-authored-by: Lucio Trincheri --- sbg/Makefile.include | 9 ++- sbg/dto/Makefile.include | 9 +++ sbg/dto/converters/interval_dto_converter.cpp | 32 ++++++++ sbg/dto/converters/interval_dto_converter.hpp | 53 +++++++++++++ .../multidim_inter_dto_converter.cpp | 45 +++++++++++ .../multidim_inter_dto_converter.hpp | 54 +++++++++++++ sbg/dto/interval_dto.cpp | 49 ++++++++++++ sbg/dto/interval_dto.hpp | 65 ++++++++++++++++ sbg/dto/multidim_inter_dto.cpp | 78 +++++++++++++++++++ sbg/dto/multidim_inter_dto.hpp | 62 +++++++++++++++ util/defs.hpp | 1 + 11 files changed, 456 insertions(+), 1 deletion(-) create mode 100644 sbg/dto/Makefile.include create mode 100644 sbg/dto/converters/interval_dto_converter.cpp create mode 100644 sbg/dto/converters/interval_dto_converter.hpp create mode 100644 sbg/dto/converters/multidim_inter_dto_converter.cpp create mode 100644 sbg/dto/converters/multidim_inter_dto_converter.hpp create mode 100644 sbg/dto/interval_dto.cpp create mode 100644 sbg/dto/interval_dto.hpp create mode 100644 sbg/dto/multidim_inter_dto.cpp create mode 100644 sbg/dto/multidim_inter_dto.hpp diff --git a/sbg/Makefile.include b/sbg/Makefile.include index fc19294..9812d12 100755 --- a/sbg/Makefile.include +++ b/sbg/Makefile.include @@ -1,6 +1,8 @@ # The Directories, Source, Includes, Objects, Binary UTIL_ROOT := util SBG_ROOT := sbg +DTO_ROOT := sbg/dto +CONVERTERS_ROOT := sbg/dto/converters # Sources SBG_SRC := \ @@ -17,9 +19,14 @@ SBG_ROOT := sbg $(SBG_ROOT)/sbg_algorithms.cpp include util/Makefile.include +include sbg/dto/Makefile.include # Objects SBG_OBJ=$(addprefix $(BUILD_DIR)/, $(SBG_SRC:.cpp=.o)) +# Combine SBG and DTO objects +SBG_OBJ += $(DTO_OBJ) create-folders:: - @mkdir -p $(BUILD_DIR)/$(SBG_ROOT) + @mkdir -p $(BUILD_DIR)/$(SBG_ROOT) \ + @mkdir -p $(BUILD_DIR)/$(DTO_ROOT) \ + @mkdir -p $(BUILD_DIR)/$(CONVERTERS_ROOT) diff --git a/sbg/dto/Makefile.include b/sbg/dto/Makefile.include new file mode 100644 index 0000000..f4630be --- /dev/null +++ b/sbg/dto/Makefile.include @@ -0,0 +1,9 @@ +# DTO Sources +DTO_SRC := \ + $(DTO_ROOT)/interval_dto.cpp \ + $(DTO_ROOT)/multidim_inter_dto.cpp \ + $(CONVERTERS_ROOT)/interval_dto_converter.cpp \ + $(CONVERTERS_ROOT)/multidim_inter_dto_converter.cpp + +# DTO Objects +DTO_OBJ=$(addprefix $(BUILD_DIR)/, $(DTO_SRC:.cpp=.o)) \ No newline at end of file diff --git a/sbg/dto/converters/interval_dto_converter.cpp b/sbg/dto/converters/interval_dto_converter.cpp new file mode 100644 index 0000000..b6178e0 --- /dev/null +++ b/sbg/dto/converters/interval_dto_converter.cpp @@ -0,0 +1,32 @@ +/******************************************************************************* + + 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/interval_dto_converter.hpp" + +namespace SBG { + +namespace API { + +SBG::LIB::Interval IntervalDTOConverter::convertToInterval(const IntervalDTO& dto) { + return SBG::LIB::Interval(dto.begin_, dto.step_, dto.end_); +} + +} // namespace API + +} // namespace SBG \ No newline at end of file diff --git a/sbg/dto/converters/interval_dto_converter.hpp b/sbg/dto/converters/interval_dto_converter.hpp new file mode 100644 index 0000000..e23efe0 --- /dev/null +++ b/sbg/dto/converters/interval_dto_converter.hpp @@ -0,0 +1,53 @@ +/** @file interval_dto_converter.hpp + + @brief IntervalDTOConverter implementation + + The IntervalDTOConverter provides the methods to convert an IntervalDTO to + the corresponding interval representation. Currently, it can convert to + LIB::Interval, but more conversions can/will 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_INTERVAL_CONVERTER_HPP +#define SBG_DTO_INTERVAL_CONVERTER_HPP + +#include "sbg/dto/interval_dto.hpp" +#include "sbg/interval.hpp" + +namespace SBG { + +namespace API { + +class IntervalDTOConverter { +public: + /** + * @brief Converts an IntervalDTO object to an actual interval implementation (e.g., LIB::Interval). + * + * @param dto The IntervalDTO object to convert. + * @return SBG::LIB::Interval The converted interval object. + */ + static SBG::LIB::Interval convertToInterval(const IntervalDTO& dto); +}; + +} // namespace API + +} // namespace SBG + +#endif // SBG_DTO_INTERVAL_CONVERTER_HPP diff --git a/sbg/dto/converters/multidim_inter_dto_converter.cpp b/sbg/dto/converters/multidim_inter_dto_converter.cpp new file mode 100644 index 0000000..e91e025 --- /dev/null +++ b/sbg/dto/converters/multidim_inter_dto_converter.cpp @@ -0,0 +1,45 @@ +/******************************************************************************* + + 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/interval_dto_converter.hpp" +#include "sbg/dto/converters/multidim_inter_dto_converter.hpp" +#include "sbg/dto/interval_dto.hpp" +#include "sbg/dto/multidim_inter_dto.hpp" +#include "sbg/multidim_inter.hpp" +#include "sbg/interval.hpp" + +namespace SBG { + +namespace API { + +SBG::LIB::MultiDimInter MultiDimInterDTOConverter::convertToMultiDimInter(const MultiDimInterDTO& dto) { + SBG::LIB::InterVector intervals; + intervals.reserve(dto.intervals_.size()); + + std::transform(dto.intervals_.begin(), dto.intervals_.end(), intervals.begin(), + [](const IntervalDTO& intervalDTO) { + return IntervalDTOConverter::convertToInterval(intervalDTO); + }); + + return SBG::LIB::MultiDimInter(intervals); +} + +} // namespace API + +} // namespace SBG \ No newline at end of file diff --git a/sbg/dto/converters/multidim_inter_dto_converter.hpp b/sbg/dto/converters/multidim_inter_dto_converter.hpp new file mode 100644 index 0000000..317cece --- /dev/null +++ b/sbg/dto/converters/multidim_inter_dto_converter.hpp @@ -0,0 +1,54 @@ +/** @file interval_dto_converter.hpp + + @brief MultiDimInterDTOConverter implementation + + The MultiDimInterDTOConverter provides the methods to convert an MultiDimInterDTO + to the corresponding multidimensional interval representation. Currently, it can + convert to LIB::MultiDimInter, but more conversions can/will 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_MULTIDIM_INTERVAL_CONVERTER_HPP +#define SBG_DTO_MULTIDIM_INTERVAL_CONVERTER_HPP + +#include "sbg/dto/multidim_inter_dto.hpp" +#include "sbg/multidim_inter.hpp" + +namespace SBG { + +namespace API { + +class MultiDimInterDTOConverter { +public: + /** + * @brief Converts a MultiDimInterDTO object to an actual multidimensional + * interval implementation (e.g., SBG::LIB::MultiDimInter). + * + * @param dto The MultiDimInterDTO object to convert. + * @return SBG::LIB::MultiDimInter The converted interval object. + */ + static SBG::LIB::MultiDimInter convertToMultiDimInter(const MultiDimInterDTO& dto); +}; + +} // namespace API + +} // namespace SBG + +#endif // SBG_DTO_MULTIDIM_INTERVAL_CONVERTER_HPP diff --git a/sbg/dto/interval_dto.cpp b/sbg/dto/interval_dto.cpp new file mode 100644 index 0000000..84baf31 --- /dev/null +++ b/sbg/dto/interval_dto.cpp @@ -0,0 +1,49 @@ +/******************************************************************************* + + 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/interval_dto.hpp" + +namespace SBG { + +namespace API { + +IntervalDTO::IntervalDTO() : begin_(1), step_(1), end_(0) {} +IntervalDTO::IntervalDTO(const NAT &x) : begin_(x), step_(1), end_(x) {} +IntervalDTO::IntervalDTO(const NAT &begin, const NAT &step, const NAT &end) + : begin_(begin), step_(step), end_(end) +{ +} + +// Set functions --------------------------------------------------------------- +bool IntervalDTO::isEmpty() const { return end_ < begin_; } + +// Operators ------------------------------------------------------------------- +std::ostream &operator<<(std::ostream &out, const IntervalDTO &i) +{ + out << "[" << Util::toStr(i.begin_); + if (i.step_ != 1) + out << ":" << Util::toStr(i.step_); + out << ":" << Util::toStr(i.end_) << "]"; + + return out; +} + +} // namespace API + +} // namespace SBG diff --git a/sbg/dto/interval_dto.hpp b/sbg/dto/interval_dto.hpp new file mode 100644 index 0000000..0d70ada --- /dev/null +++ b/sbg/dto/interval_dto.hpp @@ -0,0 +1,65 @@ +/** @file interval_dto.hpp + + @brief IntervalDTO implementation + + The IntervalDTO class is a Data Transfer Object (DTO) for parsing raw + interval data, such as from JSON. It is designed for conversion into + concrete implementations like Interval 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_INTERVAL_HPP +#define SBG_DTO_INTERVAL_HPP + +#include "util/defs.hpp" + +namespace SBG { + +namespace API { + +class IntervalDTOConverter; + +using NAT = Util::NAT; + +struct IntervalDTO { + + IntervalDTO(); + IntervalDTO(const NAT &x); + IntervalDTO(const NAT &begin, const NAT &step, const NAT &end); + + /** + * @brief Traditional set operations. + */ + bool isEmpty() const; + + private: + NAT begin_; + NAT step_; + NAT end_; + + friend std::ostream &operator<<(std::ostream &out, const IntervalDTO &i); + friend class IntervalDTOConverter; +}; + +} // namespace API + +} // namespace SBG + +#endif diff --git a/sbg/dto/multidim_inter_dto.cpp b/sbg/dto/multidim_inter_dto.cpp new file mode 100644 index 0000000..42416e0 --- /dev/null +++ b/sbg/dto/multidim_inter_dto.cpp @@ -0,0 +1,78 @@ +/******************************************************************************* + + 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_inter_dto.hpp" + +namespace SBG { + +namespace API { + +MultiDimInterDTO::MultiDimInterDTO() : intervals_() {} +MultiDimInterDTO::MultiDimInterDTO(const MD_NAT &x) : intervals_() { + for (NAT xi : x) + intervals_.emplace_back(IntervalDTO(xi, 1, xi)); +} +MultiDimInterDTO::MultiDimInterDTO(const IntervalDTO &i) : intervals_() +{ + intervals_.emplace_back(i); +} +MultiDimInterDTO::MultiDimInterDTO(const unsigned int &nmbr_copies + , const IntervalDTO &i) : intervals_() { + for (unsigned int j = 0; j < nmbr_copies; ++j) + intervals_.emplace_back(i); +} +MultiDimInterDTO::MultiDimInterDTO(const IntervalDTOVector &iv) : intervals_(iv) {} + +member_imp(MultiDimInterDTO, IntervalDTOVector, intervals); + +void MultiDimInterDTO::emplaceBack(IntervalDTO i) +{ + if (i.isEmpty()) + intervals_ = IntervalDTOVector(); + else + intervals_.emplace_back(i); + return; +} + +IntervalDTO &MultiDimInterDTO::operator[](std::size_t n) +{ + return intervals_[n]; +} + +const IntervalDTO &MultiDimInterDTO::operator[](std::size_t n) const +{ + return intervals_[n]; +} + +std::ostream &operator<<(std::ostream &out, const MultiDimInterDTO &mdi) +{ + std::size_t sz = mdi.intervals_.size(); + + if (sz > 0) { + for (std::size_t j = 0; j < sz - 1; ++j) + out << mdi[j] << "x"; + out << mdi[sz-1]; + } + + return out; +} + +} // namespace API + +} // namespace SBG diff --git a/sbg/dto/multidim_inter_dto.hpp b/sbg/dto/multidim_inter_dto.hpp new file mode 100644 index 0000000..a6ec678 --- /dev/null +++ b/sbg/dto/multidim_inter_dto.hpp @@ -0,0 +1,62 @@ +/** @file multidim_inter.hpp + + @brief Multi-dimensional interval 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_MULTIDIM_INTERVAL_HPP +#define SBG_DTO_MULTIDIM_INTERVAL_HPP + +#include "sbg/dto/interval_dto.hpp" + +namespace SBG { + +namespace API { + +using MD_NAT = Util::MD_NAT; +using NAT = Util::NAT; + +typedef std::vector IntervalDTOVector; + +struct MultiDimInterDTO { + + member_class(IntervalDTOVector, intervals); + + MultiDimInterDTO(); + MultiDimInterDTO(const MD_NAT &x); + MultiDimInterDTO(const IntervalDTO &i); + MultiDimInterDTO(const unsigned int &nmbr_copies, const IntervalDTO &i); + MultiDimInterDTO(const IntervalDTOVector &iv); + + void emplaceBack(IntervalDTO i); + IntervalDTO &operator[](std::size_t n); + const IntervalDTO &operator[](std::size_t n) const; + + friend std::ostream &operator<<(std::ostream &out, const MultiDimInterDTO &i); + friend class MultiDimInterDTOConverter; +}; + +typedef MultiDimInterDTO SetPieceDTO; + +} // namespace LIB + +} // namespace SBG + +#endif diff --git a/util/defs.hpp b/util/defs.hpp index 950859b..4314517 100755 --- a/util/defs.hpp +++ b/util/defs.hpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include