Skip to content

Commit

Permalink
Add initial implementation of LExpDTO, MDLExpDTO, and MapDTO with the…
Browse files Browse the repository at this point in the history
…ir converters.
  • Loading branch information
Lucio Trincheri committed Dec 17, 2024
1 parent eac64ec commit 6692684
Show file tree
Hide file tree
Showing 13 changed files with 655 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sbg/dto/Makefile.include
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 31 additions & 0 deletions sbg/dto/converters/lexp_dto_converter.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/

#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
55 changes: 55 additions & 0 deletions sbg/dto/converters/lexp_dto_converter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/** @file lexp_dto_converter.hpp
@brief <b> LExpDTOConverter implementation</b>
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.
<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_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
37 changes: 37 additions & 0 deletions sbg/dto/converters/map_dto_converter.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/

#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
58 changes: 58 additions & 0 deletions sbg/dto/converters/map_dto_converter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/** @file map_dto_converter.hpp
@brief <b> SBGMapDTOConverter implementation</b>
The SBGMapDTOConverter is used to convert a SBGMapDTO (MapDTO)
to the corresponding SBG map representation.
Currently, it can convert to SBG::LIB::SBGMap<UnordSet> (BaseMap) and
SBG::LIB::SBGMap<OrdSet> (CanonMap), but more conversions can be added.
<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_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
37 changes: 37 additions & 0 deletions sbg/dto/converters/multidim_lexp_dto_converter.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/

#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
57 changes: 57 additions & 0 deletions sbg/dto/converters/multidim_lexp_dto_converter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/** @file multidim_lexp_dto_converter.hpp
@brief <b> MDLExpDTOConverter implementation</b>
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.
<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_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
63 changes: 63 additions & 0 deletions sbg/dto/lexp_dto.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/

#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
Loading

0 comments on commit 6692684

Please sign in to comment.