From d56f47e7db657ad244476ee06a65da84bd130ea3 Mon Sep 17 00:00:00 2001 From: Boyang Han Date: Mon, 4 Oct 2021 20:55:11 +0800 Subject: [PATCH] Implement `PLA.cost` --- CMakeLists.txt | 2 +- src/Cost.cpp | 5 +++++ src/Cost.hpp | 21 +++++++++++++++++++++ src/Cube.cpp | 6 ++++++ src/Cube.hpp | 1 + src/Pla.cpp | 10 ++++++++++ src/Pla.hpp | 2 ++ src/main.cpp | 4 ++++ 8 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/Cost.cpp create mode 100644 src/Cost.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 24b0e79..d018e4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ add_library(smmincov STATIC add_executable( espresso src/main.cpp - src/Cover.cpp src/Cover.hpp src/Cube.cpp src/Cube.hpp src/Pla.cpp src/Pla.hpp src/SMMincov.cpp) + src/Cover.cpp src/Cover.hpp src/Cube.cpp src/Cube.hpp src/Pla.cpp src/Pla.hpp src/SMMincov.cpp src/Cost.cpp src/Cost.hpp) target_link_libraries(espresso smmincov) set_property(TARGET espresso PROPERTY CXX_STANDARD 20) diff --git a/src/Cost.cpp b/src/Cost.cpp new file mode 100644 index 0000000..6e57ca5 --- /dev/null +++ b/src/Cost.cpp @@ -0,0 +1,5 @@ +// +// Created by yqszxx on 10/4/21. +// + +#include "Cost.hpp" diff --git a/src/Cost.hpp b/src/Cost.hpp new file mode 100644 index 0000000..71fa757 --- /dev/null +++ b/src/Cost.hpp @@ -0,0 +1,21 @@ +// +// Created by yqszxx on 10/4/21. +// + +#ifndef ESPRESSO_COST_HPP +#define ESPRESSO_COST_HPP + +#include + +namespace espresso { + +class Cost { +public: + size_t cubes; + size_t in; + size_t out; +}; + +} // namespace espresso + +#endif //ESPRESSO_COST_HPP diff --git a/src/Cube.cpp b/src/Cube.cpp index c28e3d3..e8e67da 100644 --- a/src/Cube.cpp +++ b/src/Cube.cpp @@ -283,3 +283,9 @@ size_t espresso::Cube::count() const { void espresso::Cube::reset() { data->reset(); } + +espresso::Cube espresso::Cube::operator~() const { + Cube r; + (*r.data) = ~(*data); + return r; +} diff --git a/src/Cube.hpp b/src/Cube.hpp index b364471..d00c064 100644 --- a/src/Cube.hpp +++ b/src/Cube.hpp @@ -30,6 +30,7 @@ class Cube { Cube operator + (const Cube& x) const; Cube& operator += (const Cube& x); Cube operator & (const Cube& x) const; + Cube operator ~ () const; bool operator <= (const Cube& x) const; // implies bool operator <=> (const Cube& x) const; // disjoint bool operator == (const Cube& x) const; diff --git a/src/Pla.cpp b/src/Pla.cpp index e6b7afa..ad4d258 100644 --- a/src/Pla.cpp +++ b/src/Pla.cpp @@ -404,3 +404,13 @@ void PLA::expand() { } } } + +Cost PLA::cost() const { + Cost cost{}; + for (auto& c: F.cubes) { + cost.in += (~((*c) + Cube::getOutputMask())).count(); + cost.out += ((*c) & Cube::getOutputMask()).count(); + cost.cubes++; + } + return cost; +} diff --git a/src/Pla.hpp b/src/Pla.hpp index a58d9b5..6b156c8 100644 --- a/src/Pla.hpp +++ b/src/Pla.hpp @@ -6,6 +6,7 @@ #define ESPRESSO_PLA_HPP #include "Cover.hpp" +#include "Cost.hpp" namespace espresso { @@ -14,6 +15,7 @@ class PLA { explicit PLA(); void dump() const; void expand(); + Cost cost() const; private: Cover F, D, R; diff --git a/src/main.cpp b/src/main.cpp index 62f97a9..81e60d6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,11 @@ int main(int, char*[]) { espresso::PLA pla; + espresso::Cost cost = pla.cost(); + std::cerr << "I:c=" << cost.cubes << ",in=" << cost.in << ",out=" << cost.out << ",total=" << cost.in + cost.out << std::endl; pla.expand(); pla.dump(); + cost = pla.cost(); + std::cerr << "O:c=" << cost.cubes << ",in=" << cost.in << ",out=" << cost.out << ",total=" << cost.in + cost.out << std::endl; return EXIT_SUCCESS; }