Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iss-25: Add initial SetDTO and it's associated converter #39

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
18 changes: 6 additions & 12 deletions sbg/dto/converters/multidim_inter_dto_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,19 @@

#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());
SBG::LIB::SetPiece MultiDimInterDTOConverter::convertToSetPiece(const SetPieceDTO& dto) {
SBG::LIB::SetPiece set_piece;

std::transform(dto.intervals_.begin(), dto.intervals_.end(), intervals.begin(),
[](const IntervalDTO& intervalDTO) {
return IntervalDTOConverter::convertToInterval(intervalDTO);
});
for (const IntervalDTO& interval_dto : dto.intervals_) {
set_piece.emplaceBack(IntervalDTOConverter::convertToInterval(interval_dto));
}

return SBG::LIB::MultiDimInter(intervals);
return set_piece;
}

} // namespace API
Expand Down
14 changes: 8 additions & 6 deletions sbg/dto/converters/multidim_inter_dto_converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

@brief <b>MultiDimInterDTOConverter implementation</b>

The MultiDimInterDTOConverter provides the methods to convert an MultiDimInterDTO
The MultiDimInterDTOConverter is used to convert a MultiDimInterDTO (SetPieceDTO)
to the corresponding multidimensional interval representation. Currently, it can
convert to LIB::MultiDimInter, but more conversions can/will be added in the future.
convert to LIB::SetPiece, but more conversions can/will be added in the future.

<hr>

Expand Down Expand Up @@ -38,15 +38,17 @@ namespace API {
class MultiDimInterDTOConverter {
public:
/**
* @brief Converts a MultiDimInterDTO object to an actual multidimensional
* @brief Converts a SetPieceDTO 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.
* @param dto The SetPieceDTO object to convert.
* @return SBG::LIB::SetPiece The converted interval object.
*/
static SBG::LIB::MultiDimInter convertToMultiDimInter(const MultiDimInterDTO& dto);
static SBG::LIB::SetPiece convertToSetPiece(const SetPieceDTO& dto);
};

typedef MultiDimInterDTOConverter SetPieceDTOConverter;

} // namespace API

} // namespace SBG
Expand Down
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 SetDTO& 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
4 changes: 2 additions & 2 deletions sbg/dto/interval_dto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

@brief <b>IntervalDTO implementation</b>

The IntervalDTO class is a Data Transfer Object (DTO) for parsing raw
interval data, such as from JSON. It is designed for conversion into
The IntervalDTO class is a Data Transfer Object (DTO) for handling
raw data, such as from JSON. It is designed for conversion into
concrete implementations like Interval via a dedicated converter.

<hr>
Expand Down
2 changes: 2 additions & 0 deletions sbg/dto/multidim_inter_dto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const IntervalDTO &MultiDimInterDTO::operator[](std::size_t n) const
return intervals_[n];
}

bool MultiDimInterDTO::isEmpty() const { return intervals_.empty(); }

std::ostream &operator<<(std::ostream &out, const MultiDimInterDTO &mdi)
{
std::size_t sz = mdi.intervals_.size();
Expand Down
9 changes: 9 additions & 0 deletions sbg/dto/multidim_inter_dto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

@brief <b>Multi-dimensional interval implementation</b>

The MultiDimInterDTO class is a Data Transfer Object (DTO) for handling
raw data, such as from JSON. It is designed for conversion into
concrete implementations like SetPiece via a dedicated converter.

<hr>

This file is part of Set--Based Graph Library.
Expand Down Expand Up @@ -49,6 +53,11 @@ struct MultiDimInterDTO {
IntervalDTO &operator[](std::size_t n);
const IntervalDTO &operator[](std::size_t n) const;

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

friend std::ostream &operator<<(std::ostream &out, const MultiDimInterDTO &i);
friend class MultiDimInterDTOConverter;
};
Expand Down
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_;

return out;
}

} // namespace API

} // namespace SBG
Loading