Skip to content

Commit

Permalink
Initial implementation of SetDTO and it's converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucio Trincheri committed Dec 16, 2024
1 parent 43809ca commit 4c22029
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sbg/dto/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
DTO_SRC := \
$(DTO_ROOT)/interval_dto.cpp \
$(DTO_ROOT)/multidim_inter_dto.cpp \
$(DTO_ROOT)/pw_mdinter_dto.cpp \
$(CONVERTERS_ROOT)/interval_dto_converter.cpp \
$(CONVERTERS_ROOT)/multidim_inter_dto_converter.cpp
$(CONVERTERS_ROOT)/multidim_inter_dto_converter.cpp \
$(CONVERTERS_ROOT)/pw_mdinter_dto_converter.cpp

# DTO Objects
DTO_OBJ=$(addprefix $(BUILD_DIR)/, $(DTO_SRC:.cpp=.o))
46 changes: 46 additions & 0 deletions sbg/dto/converters/pw_mdinter_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/multidim_inter_dto_converter.hpp"
#include "sbg/dto/converters/pw_mdinter_dto_converter.hpp"

namespace SBG {

namespace API {
SBG::LIB::OrdSet PWMDInterDTOConverter::convertToOrdSet(const SetDTO& dto) {
SBG::LIB::OrdSet ord_set;
for (const SetPieceDTO& set_piece_dto : dto.pieces_) {
ord_set.emplace(SetPieceDTOConverter::convertToSetPiece(set_piece_dto));
}

return ord_set;
}

SBG::LIB::UnordSet PWMDInterDTOConverter::convertToUnordSet(const PWMDInterDTO& dto) {
SBG::LIB::UnordSet unord_set;
for (const SetPieceDTO& set_piece_dto : dto.pieces_) {
unord_set.emplace(SetPieceDTOConverter::convertToSetPiece(set_piece_dto));
}

return unord_set;
}

} // namespace API

} // namespace SBG
59 changes: 59 additions & 0 deletions sbg/dto/converters/pw_mdinter_dto_converter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/** @file pw_mdinter_dto_converter.hpp
@brief <b> PWMDInterDTOConverter implementation</b>
The PWMDInterDTOConverter is used to convert a PWMDInterDTO (SetDTO)
to the corresponding piecewise multi-dimensional interval representation.
Currently, it can convert to SBG::LIB::OrdSet and SBG::LIB::UnordSet,
but more conversions can be added in the future.
<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_PW_MDINTERVAL_CONVERTER_HPP
#define SBG_DTO_PW_MDINTERVAL_CONVERTER_HPP

#include "sbg/dto/pw_mdinter_dto.hpp"
#include "sbg/ord_pw_mdinter.hpp"
#include "sbg/unord_pw_mdinter.hpp"

namespace SBG {

namespace API {

class PWMDInterDTOConverter {
public:
/**
* @brief Converts a SetDTO object to an actual piecewise multi-dimensional
* interval implementation (e.g., SBG::LIB::OrdSet / SBG::LIB::UnordSet).
*
* @param dto: The SetDTO object to convert.
* @return SBG::LIB::OrdSet/UnordSet: The converted interval object.
*/
static SBG::LIB::OrdSet convertToOrdSet(const SetDTO& dto);
static SBG::LIB::UnordSet convertToUnordSet(const SetDTO& dto);
};

typedef PWMDInterDTOConverter SetDTOConverter;

} // namespace API

} // namespace SBG

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

namespace SBG {

namespace API {

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

std::ostream &operator<<(std::ostream &out, const SetPieceDTOVector &ii)
{
std::size_t sz = ii.size();

out << "{";
if (sz > 0) {
unsigned int j = 0;
for (const SetPieceDTO &mdi : ii) {
if (j < sz - 1)
out << mdi << ", ";
else
out << mdi;

++j;
}
}
out << "}";

return out;
}

// PWMDInterDTO ------------------------------------------------------------------

PWMDInterDTO::PWMDInterDTO() : pieces_() {}
PWMDInterDTO::PWMDInterDTO(MD_NAT x) : pieces_() {
pieces_.push_back(SetPieceDTO(x));
}
PWMDInterDTO::PWMDInterDTO(IntervalDTO i) : pieces_() {
if (!i.isEmpty())
pieces_.push_back(SetPieceDTO(i));
}
PWMDInterDTO::PWMDInterDTO(SetPieceDTO mdi) : pieces_() {
if (!mdi.isEmpty())
pieces_.push_back(mdi);
}
PWMDInterDTO::PWMDInterDTO(SetPieceDTOVector container) : pieces_() {
for (const SetPieceDTO &mdi : container) {
if (!mdi.isEmpty())
pieces_.push_back(mdi);
}
}

member_imp(PWMDInterDTO, SetPieceDTOVector, pieces);

std::size_t PWMDInterDTO::size() const { return pieces_.size(); }

void PWMDInterDTO::emplace(SetPieceDTO mdi)
{
if (!mdi.isEmpty())
pieces_.push_back(mdi);
}
void PWMDInterDTO::emplaceBack(SetPieceDTO mdi)
{
if (!mdi.isEmpty())
pieces_.push_back(mdi);
}

bool PWMDInterDTO::isEmpty() const { return pieces_.empty(); }

std::ostream &operator<<(std::ostream &out, const PWMDInterDTO &pwi)
{
out << pwi.pieces_; //TODO: check if this translates ok (it should use the operator<< for SetPieceDTOVector at the top of this file).

return out;
}

} // namespace API

} // namespace SBG
74 changes: 74 additions & 0 deletions sbg/dto/pw_mdinter_dto.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/** @file pw_mdinter_dto.hpp
@brief <b>Piecewise multi-dimensional interval dto implementation</b>
The PWMDInterDTO class is a Data Transfer Object (DTO) for handling
raw data, such as from JSON. It is designed for conversion into
concrete implementations like OrdSet/UnordSet via a dedicated converter.
<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_PW_MDINTERVAL_HPP
#define SBG_DTO_PW_MDINTERVAL_HPP

#include <vector>

#include "sbg/dto/multidim_inter_dto.hpp"

namespace SBG {

namespace API {

// Container -------------------------------------------------------------------

typedef std::vector<SetPieceDTO> SetPieceDTOVector;
std::ostream &operator<<(std::ostream &out, const SetPieceDTOVector &ii);

struct PWMDInterDTO {
member_class(SetPieceDTOVector, pieces);

/**
* @brief Constructors don't check if intervals are disjoint (performance).
*/
PWMDInterDTO();
PWMDInterDTO(Util::MD_NAT x);
PWMDInterDTO(IntervalDTO i);
PWMDInterDTO(SetPieceDTO mdi);
PWMDInterDTO(SetPieceDTOVector container);

std::size_t size() const;
void emplace(SetPieceDTO mdi);
void emplaceBack(SetPieceDTO mdi);

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

friend std::ostream &operator<<(std::ostream &out, const PWMDInterDTO &i);
};

typedef PWMDInterDTO SetDTO;

} // namespace API

} // namespace SBG

#endif

0 comments on commit 4c22029

Please sign in to comment.