From 868fb6549844fe16d22299cd3d132543d0f48f4b Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sat, 24 Feb 2024 16:26:52 +0000 Subject: [PATCH] style: format code with ClangFormat This commit fixes the style issues introduced in b737699 according to the output from ClangFormat. Details: None --- Qt/barchartwidget.cpp | 297 +++++++--------- Qt/barchartwidget.h | 66 ++-- Qt/main.cpp | 11 +- Qt/mainwindow.cpp | 173 +++++---- Qt/mainwindow.h | 116 +++--- Qt/piechartwidget.cpp | 184 +++++----- Qt/piechartwidget.h | 68 ++-- Qt/popup.cpp | 154 ++++---- Qt/popup.h | 71 ++-- Qt/qtabella.cpp | 185 +++++----- Qt/qtabella.h | 79 ++-- Qt/set.hpp | 749 +++++++++++++++++--------------------- Qt/ui_mainwindow.h | 265 +++++++------- main.cpp | 811 +++++++++++++++++++++--------------------- set.hpp | 738 +++++++++++++++++--------------------- 15 files changed, 1876 insertions(+), 2091 deletions(-) diff --git a/Qt/barchartwidget.cpp b/Qt/barchartwidget.cpp index c685639..9d7714e 100644 --- a/Qt/barchartwidget.cpp +++ b/Qt/barchartwidget.cpp @@ -1,186 +1,163 @@ -#include #include "barchartwidget.h" -#include "set.hpp" #include "qtabella.h" +#include "set.hpp" +#include - -struct Pred -{ +struct Pred { public: - explicit Pred(std::string dataa) : data(dataa) {} + explicit Pred(std::string dataa) : data(dataa) {} - inline bool operator()(const dipinto &e) const { return (e.data.substr(0, 2) == this->data.substr(0, 2)); } + inline bool operator()(const dipinto &e) const { + return (e.data.substr(0, 2) == this->data.substr(0, 2)); + } private: - std::string data; + std::string data; }; - -BarChartWidget::BarChartWidget(const DipintoSet *dipinti, QWidget *parent) : QWidget(parent) -{ - this->chart = new QChart(); - this->barSeries = nullptr; - this->axisX = nullptr; - this->axisY = nullptr; - this->chartView = new QChartView(chart, this); - this->layout = new QVBoxLayout(this); - - chart->setTitle("Numero di dipinti in base alla Data/Secolo"); - chart->legend()->setAlignment(Qt::AlignLeft); - chart->createDefaultAxes(); - - chartView->setRenderHint(QPainter::Antialiasing); - chartView->setRubberBand(QChartView::HorizontalRubberBand); - - layout->addWidget(chartView); - setLayout(layout); - updateChart(dipinti); +BarChartWidget::BarChartWidget(const DipintoSet *dipinti, QWidget *parent) + : QWidget(parent) { + this->chart = new QChart(); + this->barSeries = nullptr; + this->axisX = nullptr; + this->axisY = nullptr; + this->chartView = new QChartView(chart, this); + this->layout = new QVBoxLayout(this); + + chart->setTitle("Numero di dipinti in base alla Data/Secolo"); + chart->legend()->setAlignment(Qt::AlignLeft); + chart->createDefaultAxes(); + + chartView->setRenderHint(QPainter::Antialiasing); + chartView->setRubberBand(QChartView::HorizontalRubberBand); + + layout->addWidget(chartView); + setLayout(layout); + updateChart(dipinti); } -void BarChartWidget::updateChart(const DipintoSet *dipinti) -{ - this->clean(); - - this->barSeries = new QBarSeries(this); - QStringList categories; - Serie serie; - // Ragruppa in base al secolo del dipinto, dopo cristo e prepara i dati - for (const auto &dipinto : *dipinti) - { - std::string data = dipinto.data; - std::string firstTwoDigits; - - firstTwoDigits = ""; - for (size_t j = 0; j < data.length(); ++j) - { - if (std::isdigit(data[j])) - { - size_t numberEnd = data.find_first_not_of("0123456789", j); - std::string number = data.substr(j, numberEnd - j); - - if (number.length() == 4) - { - firstTwoDigits = number.substr(0, 2); - break; - } - } - } - - if (firstTwoDigits.empty()) - { - for (size_t j = 0; j < data.length(); ++j) - { - if (std::isdigit(data[j])) - { - size_t numberEnd = data.find_first_not_of("0123456789", j); - std::string number = data.substr(j, numberEnd - j); - - if (number.length() == 3) - { - firstTwoDigits = number.substr(0, 1); - ; // Utilizza l'intero numero a 3 cifre - break; - } - } - } +void BarChartWidget::updateChart(const DipintoSet *dipinti) { + this->clean(); + + this->barSeries = new QBarSeries(this); + QStringList categories; + Serie serie; + // Ragruppa in base al secolo del dipinto, dopo cristo e prepara i dati + for (const auto &dipinto : *dipinti) { + std::string data = dipinto.data; + std::string firstTwoDigits; + + firstTwoDigits = ""; + for (size_t j = 0; j < data.length(); ++j) { + if (std::isdigit(data[j])) { + size_t numberEnd = data.find_first_not_of("0123456789", j); + std::string number = data.substr(j, numberEnd - j); + + if (number.length() == 4) { + firstTwoDigits = number.substr(0, 2); + break; } - if (!firstTwoDigits.empty()) - serie.add(firstTwoDigits + "00"); - } - - auto sortedDipinti = std::vector(serie.begin(), serie.end()); - std::sort(sortedDipinti.begin(), sortedDipinti.end()); - // Colori dausare nel barchart - QVector barColors = { - QColor(Qt::red), - QColor(Qt::green), - QColor(Qt::blue), - QColor(Qt::yellow), - QColor(22, 33, 99), - QColor(90, 33, 9), - QColor(122, 3, 109), - QColor(22, 200, 9), - QColor(50, 53, 59) - - }; - - - for (const auto &s : sortedDipinti) - { - Pred pred(s); - DipintoSet d = filter_out(*dipinti, pred); - - QBarSet *barSet = new QBarSet(QString::fromStdString(s), this); - *barSet << d.size(); - barSet->setLabel(QString::fromStdString(s) + " (" + QString::number(d.size()) + ")"); - barSet->setBrush(barColors.at(barSeries->barSets().size() % barColors.size())); - barSeries->append(barSet); - categories << QString::fromStdString(s); - } - - chart->addSeries(barSeries); - - // Prepara gli assi - if (!axisX) - { - axisX = new QBarCategoryAxis(this); - chart->addAxis(axisX, Qt::AlignBottom); - barSeries->attachAxis(axisX); + } } - if (!axisY) - { - axisY = new QValueAxis(this); - chart->addAxis(axisY, Qt::AlignLeft); - barSeries->attachAxis(axisY); + if (firstTwoDigits.empty()) { + for (size_t j = 0; j < data.length(); ++j) { + if (std::isdigit(data[j])) { + size_t numberEnd = data.find_first_not_of("0123456789", j); + std::string number = data.substr(j, numberEnd - j); + + if (number.length() == 3) { + firstTwoDigits = number.substr(0, 1); + ; // Utilizza l'intero numero a 3 cifre + break; + } + } + } } - - chartView->update(); - - // Ridimensione del font in base alla dimensione della finestra e al numero di secoli mostrati - int numLegendFields = chart->legend()->markers().count(); - if(numLegendFields >0){ + if (!firstTwoDigits.empty()) + serie.add(firstTwoDigits + "00"); + } + + auto sortedDipinti = std::vector(serie.begin(), serie.end()); + std::sort(sortedDipinti.begin(), sortedDipinti.end()); + // Colori dausare nel barchart + QVector barColors = {QColor(Qt::red), QColor(Qt::green), + QColor(Qt::blue), QColor(Qt::yellow), + QColor(22, 33, 99), QColor(90, 33, 9), + QColor(122, 3, 109), QColor(22, 200, 9), + QColor(50, 53, 59) + + }; + + for (const auto &s : sortedDipinti) { + Pred pred(s); + DipintoSet d = filter_out(*dipinti, pred); + + QBarSet *barSet = new QBarSet(QString::fromStdString(s), this); + *barSet << d.size(); + barSet->setLabel(QString::fromStdString(s) + " (" + + QString::number(d.size()) + ")"); + barSet->setBrush( + barColors.at(barSeries->barSets().size() % barColors.size())); + barSeries->append(barSet); + categories << QString::fromStdString(s); + } + + chart->addSeries(barSeries); + + // Prepara gli assi + if (!axisX) { + axisX = new QBarCategoryAxis(this); + chart->addAxis(axisX, Qt::AlignBottom); + barSeries->attachAxis(axisX); + } + + if (!axisY) { + axisY = new QValueAxis(this); + chart->addAxis(axisY, Qt::AlignLeft); + barSeries->attachAxis(axisY); + } + + chartView->update(); + + // Ridimensione del font in base alla dimensione della finestra e al numero di + // secoli mostrati + int numLegendFields = chart->legend()->markers().count(); + if (numLegendFields > 0) { int area = parentWidget()->width() * parentWidget()->height(); - int fontSize = static_cast(qSqrt(area) / (15 * qMax(5, numLegendFields))); + int fontSize = + static_cast(qSqrt(area) / (15 * qMax(5, numLegendFields))); QFont font("Arial", fontSize); chart->setTitleFont(font); chart->legend()->setFont(font); axisX->setTitleText("Secolo"); - }else{ - QFont font("Arial", 0); - chart->setTitleFont(font); - chart->legend()->setFont(font); - axisX->setTitleText(""); - } + } else { + QFont font("Arial", 0); + chart->setTitleFont(font); + chart->legend()->setFont(font); + axisX->setTitleText(""); + } } -void BarChartWidget::clean() -{ - if (barSeries) - { - chart->removeSeries(barSeries); - delete barSeries; - barSeries = nullptr; - } - - if (axisX) - { - chart->removeAxis(axisX); - delete axisX; - axisX = nullptr; - } - if (axisY) - { - chart->removeAxis(axisY); - delete axisY; - axisY = nullptr; - } +void BarChartWidget::clean() { + if (barSeries) { + chart->removeSeries(barSeries); + delete barSeries; + barSeries = nullptr; + } + + if (axisX) { + chart->removeAxis(axisX); + delete axisX; + axisX = nullptr; + } + if (axisY) { + chart->removeAxis(axisY); + delete axisY; + axisY = nullptr; + } } - -QSize BarChartWidget::sizeHint() const -{ - return QSize(500, 500); -} +QSize BarChartWidget::sizeHint() const { return QSize(500, 500); } diff --git a/Qt/barchartwidget.h b/Qt/barchartwidget.h index 00db1a0..21c75ae 100644 --- a/Qt/barchartwidget.h +++ b/Qt/barchartwidget.h @@ -1,45 +1,47 @@ #ifndef BARCHARTWIDGET_H #define BARCHARTWIDGET_H +#include "qtabella.h" +#include #include #include -#include -#include "qtabella.h" /** - * @brief La classe BarChartWidget crea un widget per visualizzare grafici a barre utilizzando la libreria QtCharts. + * @brief La classe BarChartWidget crea un widget per visualizzare grafici a + * barre utilizzando la libreria QtCharts. */ -class BarChartWidget : public QWidget -{ - Q_OBJECT +class BarChartWidget : public QWidget { + Q_OBJECT public: - QChart *chart; /**< Puntatore all'oggetto del grafico. */ - QBarSeries *barSeries; /**< Puntatore all'oggetto della serie a barre. */ - QBarCategoryAxis *axisX; /**< Puntatore all'asse X (asse delle categorie) per il grafico a barre. */ - QValueAxis *axisY; /**< Puntatore all'asse Y (asse dei valori) per il grafico a barre. */ - QChartView *chartView; /**< Puntatore all'oggetto della vista del grafico. */ - QVBoxLayout *layout; /**< Puntatore al layout verticale per il widget. */ + QChart *chart; /**< Puntatore all'oggetto del grafico. */ + QBarSeries *barSeries; /**< Puntatore all'oggetto della serie a barre. */ + QBarCategoryAxis *axisX; /**< Puntatore all'asse X (asse delle categorie) per + il grafico a barre. */ + QValueAxis *axisY; /**< Puntatore all'asse Y (asse dei valori) per il grafico + a barre. */ + QChartView *chartView; /**< Puntatore all'oggetto della vista del grafico. */ + QVBoxLayout *layout; /**< Puntatore al layout verticale per il widget. */ - /** - * @brief Costruisce un BarChartWidget con il set dato di dipinti. - * @param dipinti Set di dipinti da visualizzare sul grafico. - * @param parent Widget genitore (default è nullptr). - */ - explicit BarChartWidget(const DipintoSet *dipinti, QWidget *parent = nullptr); - /** - * @brief Aggiorna il grafico con un nuovo set di dipinti. - * @param dipinti Nuovo set di dipinti per aggiornare il grafico. - */ - void updateChart(const DipintoSet *dipinti); - /** - * @brief Pulisce e resetta il grafico. - */ - void clean(); - /** - * @brief Restituisce la dimensione consigliata per il widget. - * @return La dimensione consigliata per il widget. - */ - QSize sizeHint() const override; + /** + * @brief Costruisce un BarChartWidget con il set dato di dipinti. + * @param dipinti Set di dipinti da visualizzare sul grafico. + * @param parent Widget genitore (default è nullptr). + */ + explicit BarChartWidget(const DipintoSet *dipinti, QWidget *parent = nullptr); + /** + * @brief Aggiorna il grafico con un nuovo set di dipinti. + * @param dipinti Nuovo set di dipinti per aggiornare il grafico. + */ + void updateChart(const DipintoSet *dipinti); + /** + * @brief Pulisce e resetta il grafico. + */ + void clean(); + /** + * @brief Restituisce la dimensione consigliata per il widget. + * @return La dimensione consigliata per il widget. + */ + QSize sizeHint() const override; }; #endif // BARCHARTWIDGET_H diff --git a/Qt/main.cpp b/Qt/main.cpp index aff1dfa..66ba6d9 100644 --- a/Qt/main.cpp +++ b/Qt/main.cpp @@ -2,11 +2,10 @@ #include -int main(int argc, char *argv[]) -{ - QApplication a(argc, argv); - MainWindow w; +int main(int argc, char *argv[]) { + QApplication a(argc, argv); + MainWindow w; - w.show(); - return a.exec(); + w.show(); + return a.exec(); } diff --git a/Qt/mainwindow.cpp b/Qt/mainwindow.cpp index 909cd9c..7fcd9a5 100644 --- a/Qt/mainwindow.cpp +++ b/Qt/mainwindow.cpp @@ -1,119 +1,106 @@ #include "mainwindow.h" -#include "ui_mainwindow.h" -#include "piechartwidget.h" #include "barchartwidget.h" -#include "qtabella.h" +#include "piechartwidget.h" #include "popup.h" +#include "qtabella.h" #include "set.hpp" +#include "ui_mainwindow.h" -struct SogTit -{ +struct SogTit { public: - explicit SogTit(std::string soggetto_o_titolo) : soggetto_o_titolo(soggetto_o_titolo) {} + explicit SogTit(std::string soggetto_o_titolo) + : soggetto_o_titolo(soggetto_o_titolo) {} - inline bool operator()(const dipinto &e) const { return e.soggetto_o_titolo.find(soggetto_o_titolo) != std::string::npos; } + inline bool operator()(const dipinto &e) const { + return e.soggetto_o_titolo.find(soggetto_o_titolo) != std::string::npos; + } private: - std::string soggetto_o_titolo; + std::string soggetto_o_titolo; }; MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), ui(new Ui::MainWindow) -{ - this->dipinti = new DipintoSet(readDipintiFromFile("dipinti_uffizi.csv")); - this->dipinti_show = new DipintoSet(*dipinti); - ui->setupUi(this); - ui->tableWidget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); - populateTableFromSet(*ui->tableWidget, *this->dipinti_show); + : QMainWindow(parent), ui(new Ui::MainWindow) { + this->dipinti = new DipintoSet(readDipintiFromFile("dipinti_uffizi.csv")); + this->dipinti_show = new DipintoSet(*dipinti); + ui->setupUi(this); + ui->tableWidget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); + populateTableFromSet(*ui->tableWidget, *this->dipinti_show); - this->pieChartWidget = new PieChartWidget(this->dipinti_show, this); - this->barChartWidget = new BarChartWidget(this->dipinti_show, this); - ui->chartArea->addWidget(pieChartWidget); - this->popup = new PopUp(this->dipinti, this); - connect(ui->Add, SIGNAL(clicked()), this, SLOT(openPopup())); - connect(ui->lineEdit, SIGNAL(editingFinished()), this, SLOT(search())); - connect(ui->Remove, SIGNAL(clicked()), this, SLOT(remove())); - connect(this, SIGNAL(resized()), this, SLOT(update())); + this->pieChartWidget = new PieChartWidget(this->dipinti_show, this); + this->barChartWidget = new BarChartWidget(this->dipinti_show, this); + ui->chartArea->addWidget(pieChartWidget); + this->popup = new PopUp(this->dipinti, this); + connect(ui->Add, SIGNAL(clicked()), this, SLOT(openPopup())); + connect(ui->lineEdit, SIGNAL(editingFinished()), this, SLOT(search())); + connect(ui->Remove, SIGNAL(clicked()), this, SLOT(remove())); + connect(this, SIGNAL(resized()), this, SLOT(update())); - ui->chartArea->addWidget(barChartWidget); -} -void MainWindow::resizeEvent(QResizeEvent *event) -{ - QMainWindow::resizeEvent(event); - emit resized(); + ui->chartArea->addWidget(barChartWidget); } -void MainWindow::openPopup() -{ - this->popup->exec(); +void MainWindow::resizeEvent(QResizeEvent *event) { + QMainWindow::resizeEvent(event); + emit resized(); } -void MainWindow::remove() -{ - unsigned int index = this->ui->indexSelector->value(); - if (index > 0) - index--; - else - index = 0; - try - { - this->dipinti->remove((*dipinti_show)[index]); - this->dipinti_show->remove((*dipinti_show)[index]); - search(); - } - catch (...) - { - QMessageBox *errorDialog = new QMessageBox(QMessageBox::Critical, "Error", "Indice non trovato", QMessageBox::Ok, this); - errorDialog->setAttribute(Qt::WA_DeleteOnClose); - QTimer *timer = new QTimer(errorDialog); - QObject::connect(timer, &QTimer::timeout, errorDialog, &QMessageBox::close); - timer->start(2000); - errorDialog->exec(); - } +void MainWindow::openPopup() { this->popup->exec(); } +void MainWindow::remove() { + unsigned int index = this->ui->indexSelector->value(); + if (index > 0) + index--; + else + index = 0; + try { + this->dipinti->remove((*dipinti_show)[index]); + this->dipinti_show->remove((*dipinti_show)[index]); + search(); + } catch (...) { + QMessageBox *errorDialog = + new QMessageBox(QMessageBox::Critical, "Error", "Indice non trovato", + QMessageBox::Ok, this); + errorDialog->setAttribute(Qt::WA_DeleteOnClose); + QTimer *timer = new QTimer(errorDialog); + QObject::connect(timer, &QTimer::timeout, errorDialog, &QMessageBox::close); + timer->start(2000); + errorDialog->exec(); + } } -void MainWindow::search() -{ +void MainWindow::search() { - QString line = ui->lineEdit->text(); - std::string content = line.toStdString(); + QString line = ui->lineEdit->text(); + std::string content = line.toStdString(); - if (!content.empty()) - { - SogTit sog(content); - DipintoSet d = filter_out(*dipinti, sog); - DipintoSet *a = new DipintoSet(d); - clear(dipinti_show); - dipinti_show = a; - } - else - { - DipintoSet *a = new DipintoSet(*dipinti); - clear(dipinti_show); - dipinti_show = a; - } + if (!content.empty()) { + SogTit sog(content); + DipintoSet d = filter_out(*dipinti, sog); + DipintoSet *a = new DipintoSet(d); + clear(dipinti_show); + dipinti_show = a; + } else { + DipintoSet *a = new DipintoSet(*dipinti); + clear(dipinti_show); + dipinti_show = a; + } - cleanTable(ui->tableWidget); - populateTableFromSet(*ui->tableWidget, *dipinti_show); - pieChartWidget->updateChart(dipinti_show); - barChartWidget->updateChart(dipinti_show); + cleanTable(ui->tableWidget); + populateTableFromSet(*ui->tableWidget, *dipinti_show); + pieChartWidget->updateChart(dipinti_show); + barChartWidget->updateChart(dipinti_show); } -void MainWindow::clear(DipintoSet *d) -{ - if (d != nullptr) - { - delete d; - d = nullptr; - } +void MainWindow::clear(DipintoSet *d) { + if (d != nullptr) { + delete d; + d = nullptr; + } } -MainWindow::~MainWindow() -{ - cleanTable(ui->tableWidget); - delete ui; - clear(dipinti); - clear(dipinti_show); +MainWindow::~MainWindow() { + cleanTable(ui->tableWidget); + delete ui; + clear(dipinti); + clear(dipinti_show); } -void MainWindow::update() -{ - pieChartWidget->updateChart(dipinti_show); - barChartWidget->updateChart(dipinti_show); +void MainWindow::update() { + pieChartWidget->updateChart(dipinti_show); + barChartWidget->updateChart(dipinti_show); } diff --git a/Qt/mainwindow.h b/Qt/mainwindow.h index e01c305..6f7da87 100644 --- a/Qt/mainwindow.h +++ b/Qt/mainwindow.h @@ -1,80 +1,82 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include -#include "qtabella.h" -#include "popup.h" -#include "piechartwidget.h" #include "barchartwidget.h" +#include "piechartwidget.h" +#include "popup.h" +#include "qtabella.h" #include +#include QT_BEGIN_NAMESPACE -namespace Ui { class MainWindow; } +namespace Ui { +class MainWindow; +} QT_END_NAMESPACE /** - * @brief La classe MainWindow rappresenta la finestra principale dell'applicazione. + * @brief La classe MainWindow rappresenta la finestra principale + * dell'applicazione. */ -class MainWindow : public QMainWindow -{ - Q_OBJECT +class MainWindow : public QMainWindow { + Q_OBJECT public: - /** - * @brief Costruttore della classe MainWindow. - * @param parent Puntatore al widget genitore (default è nullptr). - */ - MainWindow(QWidget *parent = nullptr); - /** - * @brief Distruttore della classe MainWindow. - */ - ~MainWindow(); - /** - * @brief dealloca il set di dipinti specificato. - * @param d Puntatore al set di dipinti da pulire. - */ - void clear(DipintoSet *d); - /** - * @brief Sovrascrive l'evento di ridimensionamento della finestra. - * @param event Puntatore all'oggetto QResizeEvent. - */ - void resizeEvent(QResizeEvent *event) override; -private: - Ui::MainWindow *ui; /**< Puntatore all'oggetto dell'interfaccia utente. */ - DipintoSet *dipinti; /**< Puntatore al set di tutti i dipinti. */ - DipintoSet *dipinti_show; /**< Puntatore al set di dipinti visualizzati. */ - PopUp *popup; /**< Puntatore all'oggetto del popup. */ - PieChartWidget *pieChartWidget; /**< Puntatore all'oggetto del widget del grafico a torta. */ - BarChartWidget *barChartWidget; /**< Puntatore all'oggetto del widget del grafico a barre. */ + /** + * @brief Costruttore della classe MainWindow. + * @param parent Puntatore al widget genitore (default è nullptr). + */ + MainWindow(QWidget *parent = nullptr); + /** + * @brief Distruttore della classe MainWindow. + */ + ~MainWindow(); + /** + * @brief dealloca il set di dipinti specificato. + * @param d Puntatore al set di dipinti da pulire. + */ + void clear(DipintoSet *d); + /** + * @brief Sovrascrive l'evento di ridimensionamento della finestra. + * @param event Puntatore all'oggetto QResizeEvent. + */ + void resizeEvent(QResizeEvent *event) override; +private: + Ui::MainWindow *ui; /**< Puntatore all'oggetto dell'interfaccia utente. */ + DipintoSet *dipinti; /**< Puntatore al set di tutti i dipinti. */ + DipintoSet *dipinti_show; /**< Puntatore al set di dipinti visualizzati. */ + PopUp *popup; /**< Puntatore all'oggetto del popup. */ + PieChartWidget *pieChartWidget; /**< Puntatore all'oggetto del widget del + grafico a torta. */ + BarChartWidget *barChartWidget; /**< Puntatore all'oggetto del widget del + grafico a barre. */ public slots: - /** - * @brief Slot per aprire il popup. - */ - void openPopup(); + /** + * @brief Slot per aprire il popup. + */ + void openPopup(); - /** - * @brief Slot per eseguire una ricerca. - */ - void search(); + /** + * @brief Slot per eseguire una ricerca. + */ + void search(); - /** - * @brief Slot per rimuovere un elemento. - */ - void remove(); + /** + * @brief Slot per rimuovere un elemento. + */ + void remove(); - /** - * @brief Slot per aggiornare l'interfaccia utente. - */ - void update(); + /** + * @brief Slot per aggiornare l'interfaccia utente. + */ + void update(); signals: - /** - * @brief Segnale emesso quando la finestra viene ridimensionata. - */ - void resized(); - - + /** + * @brief Segnale emesso quando la finestra viene ridimensionata. + */ + void resized(); }; #endif // MAINWINDOW_H diff --git a/Qt/piechartwidget.cpp b/Qt/piechartwidget.cpp index 81adcbb..0724d62 100644 --- a/Qt/piechartwidget.cpp +++ b/Qt/piechartwidget.cpp @@ -1,115 +1,107 @@ -#include #include "piechartwidget.h" -#include "set.hpp" #include "qtabella.h" +#include "set.hpp" +#include #include QT_CHARTS_USE_NAMESPACE -struct P -{ +struct P { public: - explicit P(std::string scuola) : scuola(scuola) {} + explicit P(std::string scuola) : scuola(scuola) {} - inline bool operator()(const dipinto &e) const { return e.scuola == scuola; } + inline bool operator()(const dipinto &e) const { return e.scuola == scuola; } private: - std::string scuola; + std::string scuola; }; -PieChartWidget::PieChartWidget(const DipintoSet *dipinti, QWidget *parent) : QWidget(parent) -{ - - this->series = nullptr; - this->chart = new QChart(); - this->chartView = new QChartView(chart, this); - chartView->setRenderHint(QPainter::Antialiasing); - this->layout = new QVBoxLayout(this); - this->setMinimumWidth(500); - this->setMaximumHeight(500); - layout->addWidget(chartView); - setLayout(layout); - chart->setTitle("Percetuale per scuola"); - chart->legend()->setAlignment(Qt::AlignLeft); - - updateChart(dipinti); +PieChartWidget::PieChartWidget(const DipintoSet *dipinti, QWidget *parent) + : QWidget(parent) { + + this->series = nullptr; + this->chart = new QChart(); + this->chartView = new QChartView(chart, this); + chartView->setRenderHint(QPainter::Antialiasing); + this->layout = new QVBoxLayout(this); + this->setMinimumWidth(500); + this->setMaximumHeight(500); + layout->addWidget(chartView); + setLayout(layout); + chart->setTitle("Percetuale per scuola"); + chart->legend()->setAlignment(Qt::AlignLeft); + + updateChart(dipinti); } -void PieChartWidget::updateChart(const DipintoSet *dipinti) -{ - clean(); //rest del grafico - - // Preparazione delle informazioni - this->series = new QPieSeries(this); - Serie serie; - - for (const auto &dipinto : *dipinti) - serie.add(dipinto.scuola); - - std::vector> data; - - for (const auto &s : serie) - { - P p(s); - DipintoSet d = filter_out(*dipinti, p); - data.emplace_back(s, d.size()); - } - - std::sort(data.begin(), data.end(), [](const std::pair &a, const std::pair &b) - { return a.second > b.second; }); - - // Prende solo i primi 10 elementi, rimuovi gli altri dal vettore e somma i loro valori - int sumOfRemaining = 0; - auto endIter = data.begin() + std::min(static_cast(10), data.size()); - for (auto iter = data.begin(); iter != endIter; ++iter) - { - series->append(QString::fromStdString(iter->first), iter->second); - } - - for (auto iter = endIter; iter != data.end(); ++iter) - { - sumOfRemaining += iter->second; - } - - // Accorpa valori dopo il decimo - if (sumOfRemaining > 0) - { - series->append("Altro", sumOfRemaining); +void PieChartWidget::updateChart(const DipintoSet *dipinti) { + clean(); // rest del grafico + + // Preparazione delle informazioni + this->series = new QPieSeries(this); + Serie serie; + + for (const auto &dipinto : *dipinti) + serie.add(dipinto.scuola); + + std::vector> data; + + for (const auto &s : serie) { + P p(s); + DipintoSet d = filter_out(*dipinti, p); + data.emplace_back(s, d.size()); + } + + std::sort( + data.begin(), data.end(), + [](const std::pair &a, + const std::pair &b) { return a.second > b.second; }); + + // Prende solo i primi 10 elementi, rimuovi gli altri dal vettore e somma i + // loro valori + int sumOfRemaining = 0; + auto endIter = data.begin() + std::min(static_cast(10), data.size()); + for (auto iter = data.begin(); iter != endIter; ++iter) { + series->append(QString::fromStdString(iter->first), iter->second); + } + + for (auto iter = endIter; iter != data.end(); ++iter) { + sumOfRemaining += iter->second; + } + + // Accorpa valori dopo il decimo + if (sumOfRemaining > 0) { + series->append("Altro", sumOfRemaining); + } + + chart->addSeries(series); + + // Mostra percentuali + const auto slices = series->slices(); + for (int i = 0; i < slices.size(); ++i) { + const QString label = slices[i]->label(); + QPieLegendMarker *marker = + qobject_cast(chart->legend()->markers(series)[i]); + if (marker) { + marker->setLabel( + QString("%1 (%2%)").arg(label).arg(slices[i]->percentage())); } - - chart->addSeries(series); - - // Mostra percentuali - const auto slices = series->slices(); - for (int i = 0; i < slices.size(); ++i) - { - const QString label = slices[i]->label(); - QPieLegendMarker *marker = qobject_cast(chart->legend()->markers(series)[i]); - if (marker) - { - marker->setLabel(QString("%1 (%2%)").arg(label).arg(slices[i]->percentage())); - } - } - - // Riadatta dimensione font in base alla dimensione della finestra - int area = parentWidget()->width() * parentWidget()->height(); - int fontSize = static_cast(qSqrt(area) / 150); - QFont font("Arial", fontSize); - chart->setTitleFont(font); - chart->legend()->setFont(font); + } + + // Riadatta dimensione font in base alla dimensione della finestra + int area = parentWidget()->width() * parentWidget()->height(); + int fontSize = static_cast(qSqrt(area) / 150); + QFont font("Arial", fontSize); + chart->setTitleFont(font); + chart->legend()->setFont(font); } -void PieChartWidget::clean() -{ - if (series != nullptr) - { +void PieChartWidget::clean() { + if (series != nullptr) { - chart->removeSeries(series); - delete series; - series = nullptr; - } + chart->removeSeries(series); + delete series; + series = nullptr; + } } -QSize PieChartWidget::sizeHint() const -{ - return QSize(500, 500); -} +QSize PieChartWidget::sizeHint() const { return QSize(500, 500); } diff --git a/Qt/piechartwidget.h b/Qt/piechartwidget.h index d2bbfb7..d1c0838 100644 --- a/Qt/piechartwidget.h +++ b/Qt/piechartwidget.h @@ -1,47 +1,47 @@ #ifndef PIECHARTWIDGET_H #define PIECHARTWIDGET_H +#include "qtabella.h" +#include #include #include -#include -#include "qtabella.h" /** - * @brief La classe PieChartWidget rappresenta un widget per visualizzare grafici a torta utilizzando la libreria QtCharts. + * @brief La classe PieChartWidget rappresenta un widget per visualizzare + * grafici a torta utilizzando la libreria QtCharts. */ -class PieChartWidget : public QWidget -{ - Q_OBJECT +class PieChartWidget : public QWidget { + Q_OBJECT public: - QPieSeries *series; /**< Puntatore all'oggetto della serie a torta. */ - QChart *chart; /**< Puntatore all'oggetto del grafico. */ - QChartView *chartView; /**< Puntatore all'oggetto della vista del grafico. */ - QVBoxLayout *layout; /**< Puntatore al layout verticale per il widget. */ - - /** - * @brief Costruttore della classe PieChartWidget. - * @param dipinti Set di dipinti da utilizzare per inizializzare il grafico. - * @param parent Puntatore al widget genitore (default è nullptr). - */ - explicit PieChartWidget(const DipintoSet *dipinti, QWidget *parent = nullptr); - - /** - * @brief Aggiorna il grafico a torta con un nuovo set di dipinti. - * @param dipinti Nuovo set di dipinti per aggiornare il grafico. - */ - void updateChart(const DipintoSet *dipinti); - - /** - * @brief Pulisce e resetta il grafico a torta. - */ - void clean(); - - /** - * @brief Restituisce la dimensione consigliata per il widget. - * @return La dimensione consigliata per il widget. - */ - QSize sizeHint() const override; + QPieSeries *series; /**< Puntatore all'oggetto della serie a torta. */ + QChart *chart; /**< Puntatore all'oggetto del grafico. */ + QChartView *chartView; /**< Puntatore all'oggetto della vista del grafico. */ + QVBoxLayout *layout; /**< Puntatore al layout verticale per il widget. */ + + /** + * @brief Costruttore della classe PieChartWidget. + * @param dipinti Set di dipinti da utilizzare per inizializzare il grafico. + * @param parent Puntatore al widget genitore (default è nullptr). + */ + explicit PieChartWidget(const DipintoSet *dipinti, QWidget *parent = nullptr); + + /** + * @brief Aggiorna il grafico a torta con un nuovo set di dipinti. + * @param dipinti Nuovo set di dipinti per aggiornare il grafico. + */ + void updateChart(const DipintoSet *dipinti); + + /** + * @brief Pulisce e resetta il grafico a torta. + */ + void clean(); + + /** + * @brief Restituisce la dimensione consigliata per il widget. + * @return La dimensione consigliata per il widget. + */ + QSize sizeHint() const override; }; #endif // PIECHARTWIDGET_H diff --git a/Qt/popup.cpp b/Qt/popup.cpp index d17c3fa..83b4a13 100644 --- a/Qt/popup.cpp +++ b/Qt/popup.cpp @@ -1,88 +1,86 @@ // popup.cpp #include "popup.h" -#include -#include #include "mainwindow.h" +#include +#include -PopUp::PopUp(DipintoSet *dipint_ptr, QWidget *parent) : QDialog(parent) -{ - this->dipint_ptr = dipint_ptr; - this->setWindowTitle("Add Item"); - this->setFixedSize(500, 400); - - initializeComponents(); -} +PopUp::PopUp(DipintoSet *dipint_ptr, QWidget *parent) : QDialog(parent) { + this->dipint_ptr = dipint_ptr; + this->setWindowTitle("Add Item"); + this->setFixedSize(500, 400); -PopUp::~PopUp() -{ - dipint_ptr = nullptr; + initializeComponents(); } -void PopUp::initializeComponents() -{ - scuolaLineEdit = new QLineEdit(this); - autoreLineEdit = new QLineEdit(this); - soggettoTitoloLineEdit = new QLineEdit(this); - dataLineEdit = new QLineEdit(this); - salaLineEdit = new QLineEdit(this); - aggiungiDipintoButton = new QPushButton("Aggiungi Dipinto", this); - - QVBoxLayout *layout = new QVBoxLayout(this); - layout->addWidget(new QLabel("Scuola:", this)); - layout->addWidget(scuolaLineEdit); - layout->addWidget(new QLabel("Autore:", this)); - layout->addWidget(autoreLineEdit); - layout->addWidget(new QLabel("Soggetto/Titolo:", this)); - layout->addWidget(soggettoTitoloLineEdit); - layout->addWidget(new QLabel("Data:", this)); - layout->addWidget(dataLineEdit); - layout->addWidget(new QLabel("Sala:", this)); - layout->addWidget(salaLineEdit); - layout->addWidget(aggiungiDipintoButton); - - connect(aggiungiDipintoButton, &QPushButton::clicked, this, &PopUp::onAggiungiDipintoClicked); +PopUp::~PopUp() { dipint_ptr = nullptr; } + +void PopUp::initializeComponents() { + scuolaLineEdit = new QLineEdit(this); + autoreLineEdit = new QLineEdit(this); + soggettoTitoloLineEdit = new QLineEdit(this); + dataLineEdit = new QLineEdit(this); + salaLineEdit = new QLineEdit(this); + aggiungiDipintoButton = new QPushButton("Aggiungi Dipinto", this); + + QVBoxLayout *layout = new QVBoxLayout(this); + layout->addWidget(new QLabel("Scuola:", this)); + layout->addWidget(scuolaLineEdit); + layout->addWidget(new QLabel("Autore:", this)); + layout->addWidget(autoreLineEdit); + layout->addWidget(new QLabel("Soggetto/Titolo:", this)); + layout->addWidget(soggettoTitoloLineEdit); + layout->addWidget(new QLabel("Data:", this)); + layout->addWidget(dataLineEdit); + layout->addWidget(new QLabel("Sala:", this)); + layout->addWidget(salaLineEdit); + layout->addWidget(aggiungiDipintoButton); + + connect(aggiungiDipintoButton, &QPushButton::clicked, this, + &PopUp::onAggiungiDipintoClicked); } -void PopUp::onAggiungiDipintoClicked() -{ - QString scuola = scuolaLineEdit->text().trimmed(); - QString autore = autoreLineEdit->text().trimmed(); - QString soggettoTitolo = soggettoTitoloLineEdit->text().trimmed(); - QString data = dataLineEdit->text().trimmed(); - QString sala = salaLineEdit->text().trimmed(); - - if (scuola.isEmpty() || autore.isEmpty() || soggettoTitolo.isEmpty() || data.isEmpty() || sala.isEmpty()) - { - QMessageBox::warning(this, "Errore", "Compila tutti i campi per aggiungere un dipinto."); - return; - } - - scuolaLineEdit->clear(); - autoreLineEdit->clear(); - soggettoTitoloLineEdit->clear(); - dataLineEdit->clear(); - salaLineEdit->clear(); - - dipinto d = dipinto(scuola.toStdString(), autore.toStdString(), soggettoTitolo.toStdString(), - data.toStdString(), sala.toStdString()); - - bool added = dipint_ptr->add(d); - MainWindow *mainWindow = dynamic_cast(parent()); - - mainWindow->search(); - - accept(); - - // Se elemento non aggiunto mostra errore - if (!added) - { - QMessageBox *errorDialog = new QMessageBox(QMessageBox::Critical, "Message", "Item Già Presente", QMessageBox::Ok, this); - errorDialog->setAttribute(Qt::WA_DeleteOnClose); - - QTimer *timer = new QTimer(errorDialog); - QObject::connect(timer, &QTimer::timeout, errorDialog, &QMessageBox::close); - timer->start(2000); - - errorDialog->exec(); - } +void PopUp::onAggiungiDipintoClicked() { + QString scuola = scuolaLineEdit->text().trimmed(); + QString autore = autoreLineEdit->text().trimmed(); + QString soggettoTitolo = soggettoTitoloLineEdit->text().trimmed(); + QString data = dataLineEdit->text().trimmed(); + QString sala = salaLineEdit->text().trimmed(); + + if (scuola.isEmpty() || autore.isEmpty() || soggettoTitolo.isEmpty() || + data.isEmpty() || sala.isEmpty()) { + QMessageBox::warning(this, "Errore", + "Compila tutti i campi per aggiungere un dipinto."); + return; + } + + scuolaLineEdit->clear(); + autoreLineEdit->clear(); + soggettoTitoloLineEdit->clear(); + dataLineEdit->clear(); + salaLineEdit->clear(); + + dipinto d = dipinto(scuola.toStdString(), autore.toStdString(), + soggettoTitolo.toStdString(), data.toStdString(), + sala.toStdString()); + + bool added = dipint_ptr->add(d); + MainWindow *mainWindow = dynamic_cast(parent()); + + mainWindow->search(); + + accept(); + + // Se elemento non aggiunto mostra errore + if (!added) { + QMessageBox *errorDialog = + new QMessageBox(QMessageBox::Critical, "Message", "Item Già Presente", + QMessageBox::Ok, this); + errorDialog->setAttribute(Qt::WA_DeleteOnClose); + + QTimer *timer = new QTimer(errorDialog); + QObject::connect(timer, &QTimer::timeout, errorDialog, &QMessageBox::close); + timer->start(2000); + + errorDialog->exec(); + } } diff --git a/Qt/popup.h b/Qt/popup.h index bb74a70..0b9fe4f 100644 --- a/Qt/popup.h +++ b/Qt/popup.h @@ -3,51 +3,56 @@ #include "qtabella.h" #include +#include #include -#include #include -#include +#include #include /** - * @brief La classe PopUp rappresenta una finestra di dialogo per l'aggiunta di nuovi dipinti. + * @brief La classe PopUp rappresenta una finestra di dialogo per l'aggiunta di + * nuovi dipinti. */ -class PopUp : public QDialog -{ - Q_OBJECT +class PopUp : public QDialog { + Q_OBJECT public: - /** - * @brief Costruttore della classe PopUp. - * @param dipint_ptr Puntatore al set di dipinti su cui verranno apportate le modifiche. - * @param parent Puntatore al widget genitore (default è nullptr). - */ - PopUp(DipintoSet *dipint_ptr, QWidget *parent = nullptr); - - /** - * @brief Distruttore della classe PopUp. - */ - ~PopUp(); + /** + * @brief Costruttore della classe PopUp. + * @param dipint_ptr Puntatore al set di dipinti su cui verranno apportate le + * modifiche. + * @param parent Puntatore al widget genitore (default è nullptr). + */ + PopUp(DipintoSet *dipint_ptr, QWidget *parent = nullptr); + + /** + * @brief Distruttore della classe PopUp. + */ + ~PopUp(); private slots: - /** - * @brief Slot chiamato quando viene premuto il pulsante "Add". - */ - void onAggiungiDipintoClicked(); + /** + * @brief Slot chiamato quando viene premuto il pulsante "Add". + */ + void onAggiungiDipintoClicked(); private: - QLineEdit *scuolaLineEdit; /**< LineEdit per inserire la scuola del dipinto. */ - QLineEdit *autoreLineEdit; /**< LineEdit per inserire l'autore del dipinto. */ - QLineEdit *soggettoTitoloLineEdit; /**< LineEdit per inserire il soggetto o il titolo del dipinto. */ - QLineEdit *dataLineEdit; /**< LineEdit per inserire la data del dipinto. */ - QLineEdit *salaLineEdit; /**< LineEdit per inserire la sala del dipinto. */ - QPushButton *aggiungiDipintoButton; /**< Pulsante per aggiungere il dipinto. */ - DipintoSet *dipint_ptr; /**< Puntatore al set di dipinti su cui verranno apportate le modifiche. */ - - /** - * @brief Funzione per inizializzare i componenti grafici. - */ - void initializeComponents(); + QLineEdit + *scuolaLineEdit; /**< LineEdit per inserire la scuola del dipinto. */ + QLineEdit *autoreLineEdit; /**< LineEdit per inserire l'autore del dipinto. */ + QLineEdit *soggettoTitoloLineEdit; /**< LineEdit per inserire il soggetto o il + titolo del dipinto. */ + QLineEdit *dataLineEdit; /**< LineEdit per inserire la data del dipinto. */ + QLineEdit *salaLineEdit; /**< LineEdit per inserire la sala del dipinto. */ + QPushButton + *aggiungiDipintoButton; /**< Pulsante per aggiungere il dipinto. */ + DipintoSet *dipint_ptr; /**< Puntatore al set di dipinti su cui verranno + apportate le modifiche. */ + + /** + * @brief Funzione per inizializzare i componenti grafici. + */ + void initializeComponents(); }; #endif // POPUP_H diff --git a/Qt/qtabella.cpp b/Qt/qtabella.cpp index 9e7d927..73eac54 100644 --- a/Qt/qtabella.cpp +++ b/Qt/qtabella.cpp @@ -1,116 +1,113 @@ #include "qtabella.h" -#include "set.hpp" -#include "iostream" #include "fstream" +#include "iostream" +#include "set.hpp" #include "sstream" -#include -#include +#include #include +#include #include -#include #include -#include +#include +#include -typedef Set, std::allocator> Serie; +typedef Set, + std::allocator> + Serie; -DipintoSet readDipintiFromFile(const std::string &filePath) -{ - DipintoSet dipintoSet; +DipintoSet readDipintiFromFile(const std::string &filePath) { + DipintoSet dipintoSet; - std::ifstream inputFile(filePath); + std::ifstream inputFile(filePath); - if (!inputFile.is_open()) - { - std::cerr << "Error opening file: " << filePath << std::endl; - return dipintoSet; - } + if (!inputFile.is_open()) { + std::cerr << "Error opening file: " << filePath << std::endl; + return dipintoSet; + } + + std::string line; + int lineCount = 0; + // Parsing + while (std::getline(inputFile, line)) { - std::string line; - int lineCount = 0; - // Parsing - while (std::getline(inputFile, line)) - { - - ++lineCount; - if (lineCount == 1) - { - continue; - } - - std::stringstream ss(line); - std::string scuola, autore, soggettoTitolo, data, sala; - - std::getline(ss, scuola, ','); - - std::getline(ss, autore, ','); - if (autore.front() == '\"' && autore.back() != '\"') - { - std::string temp; - std::getline(ss, temp, '\"'); - autore.erase(autore.begin()); - autore = autore + temp; - std::getline(ss, temp, ','); - } - - std::getline(ss, soggettoTitolo, ','); - if (soggettoTitolo.front() == '\"' && soggettoTitolo.back() != '\"') - { - std::string temp; - std::getline(ss, temp, '\"'); - soggettoTitolo.erase(soggettoTitolo.begin()); - soggettoTitolo = soggettoTitolo + temp; - std::getline(ss, temp, ','); - } - - std::getline(ss, data, ','); - std::getline(ss, sala, ','); - - dipinto currentDipinto(scuola, autore, soggettoTitolo, data, sala); - dipintoSet.add(currentDipinto); + ++lineCount; + if (lineCount == 1) { + continue; } - inputFile.close(); - return dipintoSet; -} + std::stringstream ss(line); + std::string scuola, autore, soggettoTitolo, data, sala; + + std::getline(ss, scuola, ','); -void populateTableFromSet(QTableWidget &table, const DipintoSet &dipintiSet) -{ - // Popolo tabella passata con dipinti - table.setRowCount(dipintiSet.size()); - table.setColumnCount(5); - table.setHorizontalHeaderLabels({"Scuola", "Autore", "Soggetto/Titolo", "Data", "Sala"}); - - int row = 0; - for (const auto &dipinto : dipintiSet) - { - table.setItem(row, 0, new QTableWidgetItem(QString::fromStdString(dipinto.scuola))); - table.setItem(row, 1, new QTableWidgetItem(QString::fromStdString(dipinto.autore))); - table.setItem(row, 2, new QTableWidgetItem(QString::fromStdString(dipinto.soggetto_o_titolo))); - table.setItem(row, 3, new QTableWidgetItem(QString::fromStdString(dipinto.data))); - table.setItem(row, 4, new QTableWidgetItem(QString::fromStdString(dipinto.sala))); - - ++row; + std::getline(ss, autore, ','); + if (autore.front() == '\"' && autore.back() != '\"') { + std::string temp; + std::getline(ss, temp, '\"'); + autore.erase(autore.begin()); + autore = autore + temp; + std::getline(ss, temp, ','); } - table.setEditTriggers(QAbstractItemView::NoEditTriggers); + std::getline(ss, soggettoTitolo, ','); + if (soggettoTitolo.front() == '\"' && soggettoTitolo.back() != '\"') { + std::string temp; + std::getline(ss, temp, '\"'); + soggettoTitolo.erase(soggettoTitolo.begin()); + soggettoTitolo = soggettoTitolo + temp; + std::getline(ss, temp, ','); + } + + std::getline(ss, data, ','); + std::getline(ss, sala, ','); + + dipinto currentDipinto(scuola, autore, soggettoTitolo, data, sala); + dipintoSet.add(currentDipinto); + } + + inputFile.close(); + return dipintoSet; +} + +void populateTableFromSet(QTableWidget &table, const DipintoSet &dipintiSet) { + // Popolo tabella passata con dipinti + table.setRowCount(dipintiSet.size()); + table.setColumnCount(5); + table.setHorizontalHeaderLabels( + {"Scuola", "Autore", "Soggetto/Titolo", "Data", "Sala"}); + + int row = 0; + for (const auto &dipinto : dipintiSet) { + table.setItem(row, 0, + new QTableWidgetItem(QString::fromStdString(dipinto.scuola))); + table.setItem(row, 1, + new QTableWidgetItem(QString::fromStdString(dipinto.autore))); + table.setItem(row, 2, + new QTableWidgetItem( + QString::fromStdString(dipinto.soggetto_o_titolo))); + table.setItem(row, 3, + new QTableWidgetItem(QString::fromStdString(dipinto.data))); + table.setItem(row, 4, + new QTableWidgetItem(QString::fromStdString(dipinto.sala))); + + ++row; + } + + table.setEditTriggers(QAbstractItemView::NoEditTriggers); } -void cleanTable(QTableWidget *table) -{ - for (int row = 0; row < table->rowCount(); ++row) - { - for (int col = 0; col < table->columnCount(); ++col) - { - QTableWidgetItem *item = table->item(row, col); - if (item) - { - delete item; - table->setItem(row, col, nullptr); - } - } +void cleanTable(QTableWidget *table) { + for (int row = 0; row < table->rowCount(); ++row) { + for (int col = 0; col < table->columnCount(); ++col) { + QTableWidgetItem *item = table->item(row, col); + if (item) { + delete item; + table->setItem(row, col, nullptr); + } } + } - table->setRowCount(0); - table->setColumnCount(0); + table->setRowCount(0); + table->setColumnCount(0); } diff --git a/Qt/qtabella.h b/Qt/qtabella.h index 3385ac6..4d1a7bb 100644 --- a/Qt/qtabella.h +++ b/Qt/qtabella.h @@ -1,58 +1,61 @@ #ifndef QTABELLA_H #define QTABELLA_H -#include -#include "set.hpp" #include "iostream" -#include +#include "set.hpp" #include +#include +#include /** - * @brief La classe Serie rappresenta un set di stringhe utilizzando la classe template Set. + * @brief La classe Serie rappresenta un set di stringhe utilizzando la classe + * template Set. */ -typedef Set, std::allocator> Serie; +typedef Set, + std::allocator> + Serie; /** - * @brief La struttura dipinto rappresenta un'entità dipinto con attributi specifici. + * @brief La struttura dipinto rappresenta un'entità dipinto con attributi + * specifici. */ -struct dipinto -{ - std::string scuola; /**< Nome della scuola associata al dipinto. */ - std::string autore; /**< Nome dell'autore del dipinto. */ - std::string soggetto_o_titolo; /**< Soggetto o titolo del dipinto. */ - std::string data; /**< Data di creazione del dipinto. */ - std::string sala; /**< Sala in cui è esposto il dipinto. */ +struct dipinto { + std::string scuola; /**< Nome della scuola associata al dipinto. */ + std::string autore; /**< Nome dell'autore del dipinto. */ + std::string soggetto_o_titolo; /**< Soggetto o titolo del dipinto. */ + std::string data; /**< Data di creazione del dipinto. */ + std::string sala; /**< Sala in cui è esposto il dipinto. */ - /** - * @brief Costruttore della struttura dipinto. - * @param s Nome della scuola. - * @param a Nome dell'autore. - * @param st Soggetto o titolo. - * @param d Data di creazione. - * @param sl Sala di esposizione. - */ - dipinto(const std::string &s, const std::string &a, const std::string &st, const std::string &d, const std::string &sl) - : scuola(s), autore(a), soggetto_o_titolo(st), data(d), sala(sl) {} + /** + * @brief Costruttore della struttura dipinto. + * @param s Nome della scuola. + * @param a Nome dell'autore. + * @param st Soggetto o titolo. + * @param d Data di creazione. + * @param sl Sala di esposizione. + */ + dipinto(const std::string &s, const std::string &a, const std::string &st, + const std::string &d, const std::string &sl) + : scuola(s), autore(a), soggetto_o_titolo(st), data(d), sala(sl) {} - /** - * @brief Operatore di confronto per la struttura dipinto. - * @param other Altro oggetto dipinto da confrontare. - * @return True se i dipinti sono uguali, altrimenti False. - */ - bool operator==(const dipinto &other) const - { - return scuola == other.scuola && - autore == other.autore && - soggetto_o_titolo == other.soggetto_o_titolo && - data == other.data && - sala == other.sala; - } + /** + * @brief Operatore di confronto per la struttura dipinto. + * @param other Altro oggetto dipinto da confrontare. + * @return True se i dipinti sono uguali, altrimenti False. + */ + bool operator==(const dipinto &other) const { + return scuola == other.scuola && autore == other.autore && + soggetto_o_titolo == other.soggetto_o_titolo && data == other.data && + sala == other.sala; + } }; /** - * @brief La classe DipintoSet rappresenta un set di dipinti utilizzando la classe template Set. + * @brief La classe DipintoSet rappresenta un set di dipinti utilizzando la + * classe template Set. */ -typedef Set, std::allocator> DipintoSet; +typedef Set, std::allocator> + DipintoSet; /** * @brief Legge i dipinti da un file specificato. diff --git a/Qt/set.hpp b/Qt/set.hpp index aa9c04d..378e946 100644 --- a/Qt/set.hpp +++ b/Qt/set.hpp @@ -9,10 +9,10 @@ #define SET_H #include // std::swap -#include // std::ostream #include // std::ptrdiff_t -#include // std::forward_iterator_tag #include // std::ofstream +#include // std::forward_iterator_tag +#include // std::ostream /** * @brief classe set @@ -21,405 +21,333 @@ * @tparam Equal funtore di uguaglianza * @tparam Allocator tipo di allocazione */ -template -class Set -{ +template class Set { public: - typedef unsigned int size_type; ///< Tipo di dato per la dimensione dell'array - typedef T value_type; + typedef unsigned int size_type; ///< Tipo di dato per la dimensione dell'array + typedef T value_type; private: - value_type *_elements; ///< Array of elements - size_type _size; ///< Number of elements - Equal _eq; ///< funtore - Allocator _allocator; ///< allocazione + value_type *_elements; ///< Array of elements + size_type _size; ///< Number of elements + Equal _eq; ///< funtore + Allocator _allocator; ///< allocazione public: - // Costruttore di default - Set() : _elements(nullptr), _size(0) {} - // Distruttore - ~Set() - { - clear(); + // Costruttore di default + Set() : _elements(nullptr), _size(0) {} + // Distruttore + ~Set() { clear(); } + // Costruttore Copia + Set(const Set &other) : _elements(nullptr), _size(0) { + if (other._size > 0) { + _elements = _allocator.allocate(other._size); + _size = other._size; + + for (size_type i = 0; i < _size; i++) { + _allocator.construct(_elements + i, other._elements[i]); + } } - // Costruttore Copia - Set(const Set &other) : _elements(nullptr), _size(0) - { - if (other._size > 0) - { - _elements = _allocator.allocate(other._size); - _size = other._size; - - for (size_type i = 0; i < _size; i++) - { - _allocator.construct(_elements + i, other._elements[i]); - } - } + } + + /** + * @brief Costruttore che crea un set riempito con dati presi + * da una sequenza identificata da un iteratore di inizio e + * uno di fine. + * + * + * @tparam Iter tipo di iteratore + * @param begin iteratore di inizio sequenza + * @param end iteratore di fine sequenza + * @throw std::bad_alloc possibile eccezione di allocazione + */ + + template + Set(Iter begin, Iter end) : _elements(nullptr), _size(0) { + Iter curr = begin; + try { + for (; curr != end; ++curr) { + add(static_cast(*curr)); + } + } catch (...) { + clear(); + throw; } - - /** - * @brief Costruttore che crea un set riempito con dati presi - * da una sequenza identificata da un iteratore di inizio e - * uno di fine. - * - * - * @tparam Iter tipo di iteratore - * @param begin iteratore di inizio sequenza - * @param end iteratore di fine sequenza - * @throw std::bad_alloc possibile eccezione di allocazione - */ - - template - Set(Iter begin, Iter end) : _elements(nullptr), - _size(0) - { - Iter curr = begin; - try - { - for (; curr != end; ++curr) - { - add(static_cast(*curr)); - } - } - catch (...) - { - clear(); - throw; - } + } + /** + * @brief Aggiungere un elemento alla lista + * + * + * @param value valore da inserire nella lista + * @return true se l'elemento è stato inserito, false altrimenti + * @throw std::bad_alloc possibile eccezione di allocazione + * + */ + bool add(const value_type &value) { + if (contains(value)) { + return false; } - /** - * @brief Aggiungere un elemento alla lista - * - * - * @param value valore da inserire nella lista - * @return true se l'elemento è stato inserito, false altrimenti - * @throw std::bad_alloc possibile eccezione di allocazione - * - */ - bool add(const value_type &value) - { - if (contains(value)) - { - return false; - } - if (_size + 1 == 0) - { - throw std::bad_alloc(); - } - value_type *tmp = _allocator.allocate(_size + 1); - - for (size_type i = 0; i < _size; i++) - { - try - { - _allocator.construct(tmp + i, _elements[i]); - } - catch (...) - { - for (size_type j = 0; j < i; j++) - { - _allocator.destroy(tmp + j); - } - _allocator.deallocate(tmp, _size); - throw; - } - } - - _allocator.construct(tmp + _size, value); - - for (size_type i = 0; i < _size; i++) - { - _allocator.destroy(_elements + i); + if (_size + 1 == 0) { + throw std::bad_alloc(); + } + value_type *tmp = _allocator.allocate(_size + 1); + + for (size_type i = 0; i < _size; i++) { + try { + _allocator.construct(tmp + i, _elements[i]); + } catch (...) { + for (size_type j = 0; j < i; j++) { + _allocator.destroy(tmp + j); } - _allocator.deallocate(_elements, _size); + _allocator.deallocate(tmp, _size); + throw; + } + } - _elements = tmp; - _size++; + _allocator.construct(tmp + _size, value); - return true; + for (size_type i = 0; i < _size; i++) { + _allocator.destroy(_elements + i); } + _allocator.deallocate(_elements, _size); - /** - * @brief Rimuovere un elemento dal set - * - * @param value valore da rimuovere dal set - * @return true se l'elemento è stato rimosso, false altrimenti - * @throw std::bad_alloc possibile eccezione di allocazione - */ - - bool remove(const value_type &value) - { - if (!contains(value)) - { - return false; - } + _elements = tmp; + _size++; - if (_size == 1) - { - clear(); - return true; - } - - value_type *tmp = _allocator.allocate(_size - 1); - - size_type j = 0; - for (size_type i = 0; i < _size; i++) - { - if (!_eq(_elements[i], value)) - { - try - { - _allocator.construct(tmp + j, _elements[i]); - } - catch (...) - { - for (size_type k = 0; k < j; k++) - { - _allocator.destroy(tmp + k); - } - _allocator.deallocate(tmp, _size - 1); - throw; - } - j++; - } - } + return true; + } - for (size_type i = 0; i < _size; i++) - { - _allocator.destroy(_elements + i); - } + /** + * @brief Rimuovere un elemento dal set + * + * @param value valore da rimuovere dal set + * @return true se l'elemento è stato rimosso, false altrimenti + * @throw std::bad_alloc possibile eccezione di allocazione + */ - _allocator.deallocate(_elements, _size); - - _elements = tmp; - _size--; + bool remove(const value_type &value) { + if (!contains(value)) { + return false; + } - return true; + if (_size == 1) { + clear(); + return true; } - /** - * @brief Controlla se un elemento è presente nel set - * - * @param value valore da cercare nel set - * @return true se l'elemento è presente nel set, false altrimenti - */ - bool contains(const value_type &value) const - { - for (size_type i = 0; i < _size; i++) - { - if (_eq(_elements[i], value)) - { - return true; - } + value_type *tmp = _allocator.allocate(_size - 1); + + size_type j = 0; + for (size_type i = 0; i < _size; i++) { + if (!_eq(_elements[i], value)) { + try { + _allocator.construct(tmp + j, _elements[i]); + } catch (...) { + for (size_type k = 0; k < j; k++) { + _allocator.destroy(tmp + k); + } + _allocator.deallocate(tmp, _size - 1); + throw; } - return false; + j++; + } } - /** - * @brief Scambia il contenuto di due set - * - * @param other set con cui scambiare il contenuto - */ - void swap(Set &other) - { - std::swap(_elements, other._elements); - std::swap(_size, other._size); - std::swap(_eq, other._eq); + for (size_type i = 0; i < _size; i++) { + _allocator.destroy(_elements + i); } - /** - * @brief operatore di assegnamento - * - * @param other set da copiare - * @return reference a this - */ - Set &operator=(const Set &other) - { - if (&other != this) - { - Set tmp(other); - this->swap(tmp); - } - - return *this; - } + _allocator.deallocate(_elements, _size); - /** - @brief operatore di uguaglianza - @param other set da confrontare - @return true se i due set sono uguali, tutti gli elementi coincidono, false altrimenti - */ - bool operator==(const Set &other) const - { - if (_size != other._size) - { - return false; - } + _elements = tmp; + _size--; - for (size_type i = 0; i < _size; i++) - { - if (!contains(other._elements[i])) - return false; - } + return true; + } + /** + * @brief Controlla se un elemento è presente nel set + * + * @param value valore da cercare nel set + * @return true se l'elemento è presente nel set, false altrimenti + */ + bool contains(const value_type &value) const { + for (size_type i = 0; i < _size; i++) { + if (_eq(_elements[i], value)) { return true; + } } - /** - @brief Accesso in sola lettura ad un elemento tramite operatore [] - @param i indice dell'elemento da leggere - @return reference all'elemento di indice i - @throw std::out_of_range se i >= _size - */ - const value_type &operator[](size_type i) const - { - - if (i < _size) - { - return _elements[i]; - } - else - { - throw std::out_of_range("Index out of range"); - } + return false; + } + + /** + * @brief Scambia il contenuto di due set + * + * @param other set con cui scambiare il contenuto + */ + void swap(Set &other) { + std::swap(_elements, other._elements); + std::swap(_size, other._size); + std::swap(_eq, other._eq); + } + + /** + * @brief operatore di assegnamento + * + * @param other set da copiare + * @return reference a this + */ + Set &operator=(const Set &other) { + if (&other != this) { + Set tmp(other); + this->swap(tmp); } - /** - * @brief Accesso in sola lettura ad un elemento tramite operatore [] - * - * @param i indice dell'elemento da leggere - * @return reference all'elemento di indice i - * @throw std::out_of_range se i >= _size - */ - size_type size(void) const - { - return _size; + return *this; + } + + /** + @brief operatore di uguaglianza + @param other set da confrontare + @return true se i due set sono uguali, tutti gli elementi coincidono, + false altrimenti + */ + bool operator==(const Set &other) const { + if (_size != other._size) { + return false; } - /** - * @brief Controlla se il set è vuoto - * - * @return true se il set è vuoto, false altrimenti - */ - bool empty(void) const - { - return (_size == 0); + for (size_type i = 0; i < _size; i++) { + if (!contains(other._elements[i])) + return false; } - /** - * @brief Svuota il set - * - */ - void clear(void) - { - if (_size > 0) - { - for (size_type i = 0; i < _size; i++) - { - _allocator.destroy(_elements + i); - } - - _allocator.deallocate(_elements, _size); - } - - _elements = nullptr; - _size = 0; + return true; + } + /** + @brief Accesso in sola lettura ad un elemento tramite operatore [] + @param i indice dell'elemento da leggere + @return reference all'elemento di indice i + @throw std::out_of_range se i >= _size + */ + const value_type &operator[](size_type i) const { + + if (i < _size) { + return _elements[i]; + } else { + throw std::out_of_range("Index out of range"); + } + } + + /** + * @brief Accesso in sola lettura ad un elemento tramite operatore [] + * + * @param i indice dell'elemento da leggere + * @return reference all'elemento di indice i + * @throw std::out_of_range se i >= _size + */ + size_type size(void) const { return _size; } + + /** + * @brief Controlla se il set è vuoto + * + * @return true se il set è vuoto, false altrimenti + */ + bool empty(void) const { return (_size == 0); } + + /** + * @brief Svuota il set + * + */ + void clear(void) { + if (_size > 0) { + for (size_type i = 0; i < _size; i++) { + _allocator.destroy(_elements + i); + } + + _allocator.deallocate(_elements, _size); } - /** - * @brief Iteratore costante - * - */ - class const_iterator - { - public: - typedef std::forward_iterator_tag iterator_category; - typedef T value_type; - typedef std::ptrdiff_t difference_type; - typedef const T *pointer; - typedef const T &reference; - - // Costruttore di default - const_iterator() : ptr(nullptr) {} - - // Costruttore Copia - const_iterator(const const_iterator &other) : ptr(other.ptr) {} - - // Distruttore - ~const_iterator() {} - - // Operatore di assegnamento - const_iterator &operator=(const const_iterator &other) - { - ptr = other.ptr; - return *this; - } - // Operatore di iterazione post-incremento - const_iterator &operator++() - { - ++ptr; - return *this; - } + _elements = nullptr; + _size = 0; + } + /** + * @brief Iteratore costante + * + */ + class const_iterator { + public: + typedef std::forward_iterator_tag iterator_category; + typedef T value_type; + typedef std::ptrdiff_t difference_type; + typedef const T *pointer; + typedef const T &reference; - // Operatore di iterazione pre-incremento - const_iterator operator++(int) - { - const_iterator temp(*this); - ++ptr; - return temp; - } + // Costruttore di default + const_iterator() : ptr(nullptr) {} - // Operatore di dereferenziazione - reference operator*() const - { - return *ptr; - } + // Costruttore Copia + const_iterator(const const_iterator &other) : ptr(other.ptr) {} - // Operatore di accesso a membro - pointer operator->() const - { - return ptr; - } + // Distruttore + ~const_iterator() {} - // Operatore di uguaglianza - bool operator==(const const_iterator &other) const - { - return ptr == other.ptr; - } + // Operatore di assegnamento + const_iterator &operator=(const const_iterator &other) { + ptr = other.ptr; + return *this; + } - // Operatore di disuguaglianza - bool operator!=(const const_iterator &other) const - { - return !(*this == other); - } + // Operatore di iterazione post-incremento + const_iterator &operator++() { + ++ptr; + return *this; + } - private: - const T *ptr; + // Operatore di iterazione pre-incremento + const_iterator operator++(int) { + const_iterator temp(*this); + ++ptr; + return temp; + } - friend class Set; + // Operatore di dereferenziazione + reference operator*() const { return *ptr; } - // Costruttore privato di inizializzazione usato dalla classe container - const_iterator(const T *p) : ptr(p) {} - }; + // Operatore di accesso a membro + pointer operator->() const { return ptr; } - /** - * @brief Iteratore costante di inizio sequenza - * - * @return const_iterator di inizio sequenza - */ - const_iterator begin() const - { - return const_iterator(_elements); + // Operatore di uguaglianza + bool operator==(const const_iterator &other) const { + return ptr == other.ptr; } - /** - * @brief Iteratore costante di fine sequenza - * - * @return const_iterator di fine sequenza - */ - const_iterator end() const - { - return const_iterator(_elements + _size); + // Operatore di disuguaglianza + bool operator!=(const const_iterator &other) const { + return !(*this == other); } + + private: + const T *ptr; + + friend class Set; + + // Costruttore privato di inizializzazione usato dalla classe container + const_iterator(const T *p) : ptr(p) {} + }; + + /** + * @brief Iteratore costante di inizio sequenza + * + * @return const_iterator di inizio sequenza + */ + const_iterator begin() const { return const_iterator(_elements); } + + /** + * @brief Iteratore costante di fine sequenza + * + * @return const_iterator di fine sequenza + */ + const_iterator end() const { return const_iterator(_elements + _size); } }; /** @@ -433,16 +361,15 @@ class Set * @return reference allo stream di output */ template -std::ostream &operator<<(std::ostream &os, const Set &s) -{ +std::ostream &operator<<(std::ostream &os, const Set &s) { - os << s.size(); - for (typename Set::const_iterator i = s.begin(); i != s.end(); ++i) - { - os << " (" << *i << ")"; - } + os << s.size(); + for (typename Set::const_iterator i = s.begin(); + i != s.end(); ++i) { + os << " (" << *i << ")"; + } - return os; + return os; } /** @@ -458,20 +385,18 @@ std::ostream &operator<<(std::ostream &os, const Set &s) * @return nuovo set filtrato */ template -Set filter_out(const Set &s, P predicate) -{ - typename Set::const_iterator i, ie; - Set result; - - for (i = s.begin(), ie = s.end(); i != ie; ++i) - { - if (predicate(*i)) - { - result.add(*i); - } +Set filter_out(const Set &s, + P predicate) { + typename Set::const_iterator i, ie; + Set result; + + for (i = s.begin(), ie = s.end(); i != ie; ++i) { + if (predicate(*i)) { + result.add(*i); } + } - return result; + return result; } /** @@ -485,23 +410,21 @@ Set filter_out(const Set &s, P predica * @return nuovo set unione */ template -Set operator+(const Set &s1, const Set &s2) -{ +Set operator+(const Set &s1, + const Set &s2) { - typename Set::const_iterator i, ie; - Set result; + typename Set::const_iterator i, ie; + Set result; - for (i = s1.begin(), ie = s1.end(); i != ie; ++i) - { - result.add(*i); - } + for (i = s1.begin(), ie = s1.end(); i != ie; ++i) { + result.add(*i); + } - for (i = s2.begin(), ie = s2.end(); i != ie; ++i) - { - result.add(*i); - } + for (i = s2.begin(), ie = s2.end(); i != ie; ++i) { + result.add(*i); + } - return result; + return result; } /** @@ -515,21 +438,19 @@ Set operator+(const Set &s1, const Set * @return nuovo set intersezione */ template -Set operator-(const Set &s1, const Set &s2) -{ +Set operator-(const Set &s1, + const Set &s2) { - typename Set::const_iterator i, ie; - Set result; + typename Set::const_iterator i, ie; + Set result; - for (i = s1.begin(), ie = s1.end(); i != ie; ++i) - { - if (s2.contains(*i)) - { - result.add(*i); - } + for (i = s1.begin(), ie = s1.end(); i != ie; ++i) { + if (s2.contains(*i)) { + result.add(*i); } + } - return result; + return result; } /** @@ -541,30 +462,28 @@ Set operator-(const Set &s1, const Set * @param filename nome del file su cui salvare il set */ template -void save(const Set &s, const std::string &filename) -{ - std::ostream *os = nullptr; - std::ofstream ofs; - - ofs.open(filename.c_str()); - if (ofs.fail()) - { - throw std::runtime_error("Cannot open file"); - } +void save(const Set &s, + const std::string &filename) { + std::ostream *os = nullptr; + std::ofstream ofs; - os = &ofs; + ofs.open(filename.c_str()); + if (ofs.fail()) { + throw std::runtime_error("Cannot open file"); + } - *os << s; + os = &ofs; - ofs.close(); + *os << s; - if (ofs.fail()) - { - throw std::runtime_error("Cannot close file"); - } + ofs.close(); + + if (ofs.fail()) { + throw std::runtime_error("Cannot close file"); + } - os = nullptr; + os = nullptr; - return; + return; } #endif // SET_H diff --git a/Qt/ui_mainwindow.h b/Qt/ui_mainwindow.h index 6aa76ca..e48d719 100644 --- a/Qt/ui_mainwindow.h +++ b/Qt/ui_mainwindow.h @@ -27,149 +27,142 @@ QT_BEGIN_NAMESPACE -class Ui_MainWindow -{ +class Ui_MainWindow { public: - QWidget *centralwidget; - QVBoxLayout *verticalLayout_2; - QHBoxLayout *horizontalLayout_3; - QVBoxLayout *Table; - QHBoxLayout *horizontalLayout; - QPushButton *Add; - QLabel *label; - QLineEdit *lineEdit; - QVBoxLayout *verticalLayout_3; - QTableWidget *tableWidget; - QHBoxLayout *horizontalLayout_2; - QPushButton *Remove; - QSpinBox *indexSelector; - QSpacerItem *horizontalSpacer; - QPushButton *Exit; - QVBoxLayout *chartArea; - QSpacerItem *horizontalSpacer_2; - QMenuBar *menubar; - QStatusBar *statusbar; + QWidget *centralwidget; + QVBoxLayout *verticalLayout_2; + QHBoxLayout *horizontalLayout_3; + QVBoxLayout *Table; + QHBoxLayout *horizontalLayout; + QPushButton *Add; + QLabel *label; + QLineEdit *lineEdit; + QVBoxLayout *verticalLayout_3; + QTableWidget *tableWidget; + QHBoxLayout *horizontalLayout_2; + QPushButton *Remove; + QSpinBox *indexSelector; + QSpacerItem *horizontalSpacer; + QPushButton *Exit; + QVBoxLayout *chartArea; + QSpacerItem *horizontalSpacer_2; + QMenuBar *menubar; + QStatusBar *statusbar; + + void setupUi(QMainWindow *MainWindow) { + if (MainWindow->objectName().isEmpty()) + MainWindow->setObjectName(QString::fromUtf8("MainWindow")); + MainWindow->resize(800, 661); + centralwidget = new QWidget(MainWindow); + centralwidget->setObjectName(QString::fromUtf8("centralwidget")); + centralwidget->setEnabled(true); + centralwidget->setMinimumSize(QSize(800, 611)); + verticalLayout_2 = new QVBoxLayout(centralwidget); + verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2")); + horizontalLayout_3 = new QHBoxLayout(); + horizontalLayout_3->setObjectName(QString::fromUtf8("horizontalLayout_3")); + Table = new QVBoxLayout(); + Table->setObjectName(QString::fromUtf8("Table")); + Table->setSizeConstraint(QLayout::SetMaximumSize); + horizontalLayout = new QHBoxLayout(); + horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout")); + Add = new QPushButton(centralwidget); + Add->setObjectName(QString::fromUtf8("Add")); + + horizontalLayout->addWidget(Add); + + label = new QLabel(centralwidget); + label->setObjectName(QString::fromUtf8("label")); + + horizontalLayout->addWidget(label); + + lineEdit = new QLineEdit(centralwidget); + lineEdit->setObjectName(QString::fromUtf8("lineEdit")); + + horizontalLayout->addWidget(lineEdit); + + Table->addLayout(horizontalLayout); + + verticalLayout_3 = new QVBoxLayout(); + verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3")); + tableWidget = new QTableWidget(centralwidget); + tableWidget->setObjectName(QString::fromUtf8("tableWidget")); + QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(tableWidget->sizePolicy().hasHeightForWidth()); + tableWidget->setSizePolicy(sizePolicy); + + verticalLayout_3->addWidget(tableWidget); + + Table->addLayout(verticalLayout_3); + + horizontalLayout_2 = new QHBoxLayout(); + horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2")); + Remove = new QPushButton(centralwidget); + Remove->setObjectName(QString::fromUtf8("Remove")); + + horizontalLayout_2->addWidget(Remove); + + indexSelector = new QSpinBox(centralwidget); + indexSelector->setObjectName(QString::fromUtf8("indexSelector")); + indexSelector->setMinimum(1); + indexSelector->setMaximum(99999999); + + horizontalLayout_2->addWidget(indexSelector); + + horizontalSpacer = + new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); + + horizontalLayout_2->addItem(horizontalSpacer); + + Exit = new QPushButton(centralwidget); + Exit->setObjectName(QString::fromUtf8("Exit")); + + horizontalLayout_2->addWidget(Exit); + + Table->addLayout(horizontalLayout_2); + + horizontalLayout_3->addLayout(Table); + + chartArea = new QVBoxLayout(); + chartArea->setObjectName(QString::fromUtf8("chartArea")); + horizontalSpacer_2 = + new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); + + chartArea->addItem(horizontalSpacer_2); + + horizontalLayout_3->addLayout(chartArea); + + verticalLayout_2->addLayout(horizontalLayout_3); + + MainWindow->setCentralWidget(centralwidget); + menubar = new QMenuBar(MainWindow); + menubar->setObjectName(QString::fromUtf8("menubar")); + menubar->setGeometry(QRect(0, 0, 800, 25)); + MainWindow->setMenuBar(menubar); + statusbar = new QStatusBar(MainWindow); + statusbar->setObjectName(QString::fromUtf8("statusbar")); + MainWindow->setStatusBar(statusbar); - void setupUi(QMainWindow *MainWindow) - { - if (MainWindow->objectName().isEmpty()) - MainWindow->setObjectName(QString::fromUtf8("MainWindow")); - MainWindow->resize(800, 661); - centralwidget = new QWidget(MainWindow); - centralwidget->setObjectName(QString::fromUtf8("centralwidget")); - centralwidget->setEnabled(true); - centralwidget->setMinimumSize(QSize(800, 611)); - verticalLayout_2 = new QVBoxLayout(centralwidget); - verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2")); - horizontalLayout_3 = new QHBoxLayout(); - horizontalLayout_3->setObjectName(QString::fromUtf8("horizontalLayout_3")); - Table = new QVBoxLayout(); - Table->setObjectName(QString::fromUtf8("Table")); - Table->setSizeConstraint(QLayout::SetMaximumSize); - horizontalLayout = new QHBoxLayout(); - horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout")); - Add = new QPushButton(centralwidget); - Add->setObjectName(QString::fromUtf8("Add")); + retranslateUi(MainWindow); + QObject::connect(Exit, SIGNAL(clicked()), MainWindow, SLOT(close())); - horizontalLayout->addWidget(Add); - - label = new QLabel(centralwidget); - label->setObjectName(QString::fromUtf8("label")); - - horizontalLayout->addWidget(label); - - lineEdit = new QLineEdit(centralwidget); - lineEdit->setObjectName(QString::fromUtf8("lineEdit")); - - horizontalLayout->addWidget(lineEdit); - - - Table->addLayout(horizontalLayout); - - verticalLayout_3 = new QVBoxLayout(); - verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3")); - tableWidget = new QTableWidget(centralwidget); - tableWidget->setObjectName(QString::fromUtf8("tableWidget")); - QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); - sizePolicy.setHorizontalStretch(0); - sizePolicy.setVerticalStretch(0); - sizePolicy.setHeightForWidth(tableWidget->sizePolicy().hasHeightForWidth()); - tableWidget->setSizePolicy(sizePolicy); - - verticalLayout_3->addWidget(tableWidget); - - - Table->addLayout(verticalLayout_3); - - horizontalLayout_2 = new QHBoxLayout(); - horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2")); - Remove = new QPushButton(centralwidget); - Remove->setObjectName(QString::fromUtf8("Remove")); - - horizontalLayout_2->addWidget(Remove); - - indexSelector = new QSpinBox(centralwidget); - indexSelector->setObjectName(QString::fromUtf8("indexSelector")); - indexSelector->setMinimum(1); - indexSelector->setMaximum(99999999); - - horizontalLayout_2->addWidget(indexSelector); - - horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); - - horizontalLayout_2->addItem(horizontalSpacer); - - Exit = new QPushButton(centralwidget); - Exit->setObjectName(QString::fromUtf8("Exit")); - - horizontalLayout_2->addWidget(Exit); - - - Table->addLayout(horizontalLayout_2); - - - horizontalLayout_3->addLayout(Table); - - chartArea = new QVBoxLayout(); - chartArea->setObjectName(QString::fromUtf8("chartArea")); - horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); - - chartArea->addItem(horizontalSpacer_2); - - - horizontalLayout_3->addLayout(chartArea); - - - verticalLayout_2->addLayout(horizontalLayout_3); - - MainWindow->setCentralWidget(centralwidget); - menubar = new QMenuBar(MainWindow); - menubar->setObjectName(QString::fromUtf8("menubar")); - menubar->setGeometry(QRect(0, 0, 800, 25)); - MainWindow->setMenuBar(menubar); - statusbar = new QStatusBar(MainWindow); - statusbar->setObjectName(QString::fromUtf8("statusbar")); - MainWindow->setStatusBar(statusbar); - - retranslateUi(MainWindow); - QObject::connect(Exit, SIGNAL(clicked()), MainWindow, SLOT(close())); - - QMetaObject::connectSlotsByName(MainWindow); - } // setupUi - - void retranslateUi(QMainWindow *MainWindow) - { - MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", nullptr)); - Add->setText(QApplication::translate("MainWindow", "Add", nullptr)); - label->setText(QApplication::translate("MainWindow", "Serach", nullptr)); - Remove->setText(QApplication::translate("MainWindow", "Remove", nullptr)); - Exit->setText(QApplication::translate("MainWindow", "Exit", nullptr)); - } // retranslateUi + QMetaObject::connectSlotsByName(MainWindow); + } // setupUi + void retranslateUi(QMainWindow *MainWindow) { + MainWindow->setWindowTitle( + QApplication::translate("MainWindow", "MainWindow", nullptr)); + Add->setText(QApplication::translate("MainWindow", "Add", nullptr)); + label->setText(QApplication::translate("MainWindow", "Serach", nullptr)); + Remove->setText(QApplication::translate("MainWindow", "Remove", nullptr)); + Exit->setText(QApplication::translate("MainWindow", "Exit", nullptr)); + } // retranslateUi }; namespace Ui { - class MainWindow: public Ui_MainWindow {}; +class MainWindow : public Ui_MainWindow {}; } // namespace Ui QT_END_NAMESPACE diff --git a/main.cpp b/main.cpp index 6153000..ac8c3f5 100644 --- a/main.cpp +++ b/main.cpp @@ -2,48 +2,42 @@ * @file main.cpp * @brief Test d'uso della classe set templata */ -#include +#include "set.hpp" +#include #include -#include #include +#include #include -#include -#include "set.hpp" +#include /** * @brief Definizione di un funtore per il confronto di interi */ -struct equal_int -{ - bool operator()(int a, int b) const - { - return a == b; - } +struct equal_int { + bool operator()(int a, int b) const { return a == b; } }; /** - * @brief Definizione di un typedef per un set di interi utilizzando la tua classe Set + * @brief Definizione di un typedef per un set di interi utilizzando la tua + * classe Set */ typedef Set> setint; /** * @brief Definizione della struttura dipinto */ -struct dipinto -{ - std::string scuola; +struct dipinto { + std::string scuola; - dipinto(const std::string &s) - : scuola(s) {} + dipinto(const std::string &s) : scuola(s) {} - bool operator==(const dipinto &other) const - { - return scuola == other.scuola; - } + bool operator==(const dipinto &other) const { return scuola == other.scuola; } }; /** - * @brief Definizione di un typedef per un set di dipinti utilizzando la tua classe Set + * @brief Definizione di un typedef per un set di dipinti utilizzando la tua + * classe Set */ -typedef Set, std::allocator> DipintoSet; +typedef Set, std::allocator> + DipintoSet; /** * @brief Definizione di un predicato pre il confronto tra dipinti @@ -52,553 +46,542 @@ typedef Set, std::allocator> DipintoSet /** * @brief Definizione di un predicato per il filtraggio di un set di interi */ -struct pred -{ - bool operator()(int a) const - { - return a == 2; - } +struct pred { + bool operator()(int a) const { return a == 2; } }; /** * @brief Definizione della struttura point */ -struct point -{ - int x; ///< coordinata x del punto - int y; ///< coordinata y del punto +struct point { + int x; ///< coordinata x del punto + int y; ///< coordinata y del punto - point(int xx, int yy) : x(xx), y(yy) {} + point(int xx, int yy) : x(xx), y(yy) {} }; /** * @brief Definizione dell'operatore << per la struttura point */ -std::ostream &operator<<(std::ostream &os, const point &p) -{ - std::cout << "(" << p.x << "," << p.y << ")"; - return os; +std::ostream &operator<<(std::ostream &os, const point &p) { + std::cout << "(" << p.x << "," << p.y << ")"; + return os; } /** * @brief Definizione del funtore per il confronto di due punti */ -struct equal_point -{ - bool operator()(const point &p1, const point &p2) const - { - return (p1.x == p2.x) && (p1.y == p2.y); - } +struct equal_point { + bool operator()(const point &p1, const point &p2) const { + return (p1.x == p2.x) && (p1.y == p2.y); + } }; /** * @brief Funzione di test per i metodi fondamentali */ -void test_metodi_fondamentali() -{ - std::cout << "******** Test metodi fondamental della struttura dati Set ********" << std::endl; +void test_metodi_fondamentali() { + std::cout + << "******** Test metodi fondamental della struttura dati Set ********" + << std::endl; - setint s; // ctor default + setint s; // ctor default - s.add(1); // add - s.add(2); - s.add(50); + s.add(1); // add + s.add(2); + s.add(50); - std::cout << "Stampa di s dopo inserimenti:" << std::endl; - std::cout << s << std::endl; + std::cout << "Stampa di s dopo inserimenti:" << std::endl; + std::cout << s << std::endl; - setint s2(s); // cctor + setint s2(s); // cctor - std::cout << "Stampa di s2 dopo copy constructor:" << std::endl; - std::cout << s2 << std::endl; + std::cout << "Stampa di s2 dopo copy constructor:" << std::endl; + std::cout << s2 << std::endl; - setint s3; + setint s3; - s3 = s; // operator= + s3 = s; // operator= - std::cout << "Stampa di s3 dopo assegnamento:" << std::endl; - std::cout << s3 << std::endl; + std::cout << "Stampa di s3 dopo assegnamento:" << std::endl; + std::cout << s3 << std::endl; } /** * @brief Funzione di test per i metodi add, contains, remove e clear */ -void test_metodi_set() -{ - std::cout << "******** Test metodi add, contin, delete and clear della struttura dati Set ********" << std::endl; +void test_metodi_set() { + std::cout << "******** Test metodi add, contin, delete and clear della " + "struttura dati Set ********" + << std::endl; - setint s; // ctor default + setint s; // ctor default - s.add(1); - assert(s.contains(1)); + s.add(1); + assert(s.contains(1)); - s.add(2); - assert(s.contains(2)); + s.add(2); + assert(s.contains(2)); - s.add(3); - assert(s.contains(3)); + s.add(3); + assert(s.contains(3)); - assert(s.add(50) == true); - assert(s.contains(50)); - assert(s.add(1) == false); + assert(s.add(50) == true); + assert(s.contains(50)); + assert(s.add(1) == false); - std::cout << "Stampa di s dopo contains:" << std::endl; - std::cout << s << std::endl; + std::cout << "Stampa di s dopo contains:" << std::endl; + std::cout << s << std::endl; - s.remove(1); - assert(!s.contains(1)); + s.remove(1); + assert(!s.contains(1)); - s.remove(2); - assert(!s.contains(2)); + s.remove(2); + assert(!s.contains(2)); - s.remove(3); - assert(!s.contains(3)); + s.remove(3); + assert(!s.contains(3)); - assert(s.remove(50) == true); - assert(!s.contains(50)); - assert(s.remove(50) == false); + assert(s.remove(50) == true); + assert(!s.contains(50)); + assert(s.remove(50) == false); - std::cout << "Stampa di s dopo remove:" << std::endl; - std::cout << s << std::endl; - std::cout << "is empty ? : " << s.empty() << std::endl; + std::cout << "Stampa di s dopo remove:" << std::endl; + std::cout << s << std::endl; + std::cout << "is empty ? : " << s.empty() << std::endl; } /** * @brief Funzione di test per i metodi di confronto */ -void test_confornto() -{ - std::cout << "******** Test metodi confronto della struttura dati Set ********" << std::endl; +void test_confornto() { + std::cout + << "******** Test metodi confronto della struttura dati Set ********" + << std::endl; - setint s; // ctor default + setint s; // ctor default - s.add(1); - s.add(2); - s.add(3); - s.add(50); + s.add(1); + s.add(2); + s.add(3); + s.add(50); - std::cout << "Stampa di s dopo inserimenti:" << std::endl; - std::cout << s << std::endl; + std::cout << "Stampa di s dopo inserimenti:" << std::endl; + std::cout << s << std::endl; - setint s2(s); // cctor + setint s2(s); // cctor - std::cout << "Stampa di s2 dopo copy constructor:" << std::endl; - std::cout << s2 << std::endl; + std::cout << "Stampa di s2 dopo copy constructor:" << std::endl; + std::cout << s2 << std::endl; - setint s3; + setint s3; - s3 = s; // operator= + s3 = s; // operator= - std::cout << "Stampa di s3 dopo assegnamento:" << std::endl; - std::cout << s3 << std::endl; + std::cout << "Stampa di s3 dopo assegnamento:" << std::endl; + std::cout << s3 << std::endl; - setint s4; - s4.add(1); - s4.add(2); - s4.add(3); - std::cout << "Stampa di s4 dopo inserimenti:" << std::endl; + setint s4; + s4.add(1); + s4.add(2); + s4.add(3); + std::cout << "Stampa di s4 dopo inserimenti:" << std::endl; - std::cout << "s == s2 ? : " << (s == s2) << std::endl; - assert((s == s2) == true); - std::cout << "s == s3 ? : " << (s == s3) << std::endl; - assert((s == s3) == true); - std::cout << "s2 == s3 ? : " << (s2 == s3) << std::endl; - assert((s2 == s3) == true); + std::cout << "s == s2 ? : " << (s == s2) << std::endl; + assert((s == s2) == true); + std::cout << "s == s3 ? : " << (s == s3) << std::endl; + assert((s == s3) == true); + std::cout << "s2 == s3 ? : " << (s2 == s3) << std::endl; + assert((s2 == s3) == true); - std::cout << "s == s4 ? : " << (s == s4) << std::endl; - assert((s == s4) == false); + std::cout << "s == s4 ? : " << (s == s4) << std::endl; + assert((s == s4) == false); - s4.add(51); - std::cout << "s == s4 ? : " << (s == s4) << std::endl; - assert((s == s4) == false); + s4.add(51); + std::cout << "s == s4 ? : " << (s == s4) << std::endl; + assert((s == s4) == false); - s4.remove(51); - s4.add(50); - std::cout << "s == s4 ? : " << (s == s4) << std::endl; - assert((s == s4) == true); + s4.remove(51); + s4.add(50); + std::cout << "s == s4 ? : " << (s == s4) << std::endl; + assert((s == s4) == true); - setint s5; - s5.add(1); - s5.add(50); - s5.add(3); - s5.add(2); + setint s5; + s5.add(1); + s5.add(50); + s5.add(3); + s5.add(2); - std::cout << "s == s5 ? : " << (s == s5) << std::endl; - assert((s == s5) == true); + std::cout << "s == s5 ? : " << (s == s5) << std::endl; + assert((s == s5) == true); } /** * @brief Funzione di test per l'operatore di accesso [] */ -void test_quadre_op() -{ - std::cout << "******** Test Operatore solo lettura [] ********" << std::endl; +void test_quadre_op() { + std::cout << "******** Test Operatore solo lettura [] ********" << std::endl; - setint s; // ctor default + setint s; // ctor default - s.add(1); - assert(s[0] == 1); + s.add(1); + assert(s[0] == 1); - s.add(2); - assert(s[1] == 2); + s.add(2); + assert(s[1] == 2); - s.add(3); - assert(s[2] == 3); + s.add(3); + assert(s[2] == 3); - s.add(50); - assert(s[3] == 50); + s.add(50); + assert(s[3] == 50); - std::cout << "Stampa di s dopo inserimenti:" << std::endl; - std::cout << s << std::endl; + std::cout << "Stampa di s dopo inserimenti:" << std::endl; + std::cout << s << std::endl; - // scrittura usando [] - // s[0] = 10; Read only operator. non è possibile quindi scrive usando [] + // scrittura usando [] + // s[0] = 10; Read only operator. non è possibile quindi scrive usando [] - // throw std::out_of_range("Index out of range"); + // throw std::out_of_range("Index out of range"); - try - { - s[10]; - } - catch (std::out_of_range &e) - { - std::cout << e.what() << std::endl; - std::cout << "Lanciata eccezione out_of_range" << std::endl; - } + try { + s[10]; + } catch (std::out_of_range &e) { + std::cout << e.what() << std::endl; + std::cout << "Lanciata eccezione out_of_range" << std::endl; + } } /** * @brief Funzione di test per gli iteratori */ -void test_iterator() -{ - std::cout << "******** Test Const Iterator ********" << std::endl; - - setint s; - s.add(1); - s.add(2); - s.add(3); - s.add(50); - - std::cout << "Set dopo inserimento:" << std::endl; - std::cout << s << std::endl; - - std::cout << "Set usando const iterators:" << std::endl; - setint::const_iterator it = s.begin(); - setint::const_iterator ite = s.end(); - - for (; it != ite; ++it) - { - std::cout << *it << std::endl; - } - - std::cout << "test operatore uguaglianza :" << std::endl; - std::cout << "it == ite ? : " << (it == ite) << std::endl; - assert((it == ite) == true); - - std::cout << "Test operatore incremento prefix e postfix :" << std::endl; - it = s.begin(); - std::cout << "it++ : " << *(it++) << std::endl; - assert(*it == 2); - std::cout << "dopo post fisso deferenzio: " << *it << std::endl; - std::cout << "++it : " << *(++it) << std::endl; - assert(*it == 3); - std::cout << "Dopo prefisso deferenzio: " << *it << std::endl; - - std::cout << "Test operatore deferenziazione:" << std::endl; - it = s.begin(); - std::cout << "*it : " << *it << std::endl; - assert(*it == 1); - - std::cout << "Test assegnamento:" << std::endl; - setint::const_iterator it2 = it; - std::cout << "it2 : " << *it2 << std::endl; - assert(*it2 == 1); +void test_iterator() { + std::cout << "******** Test Const Iterator ********" << std::endl; + + setint s; + s.add(1); + s.add(2); + s.add(3); + s.add(50); + + std::cout << "Set dopo inserimento:" << std::endl; + std::cout << s << std::endl; + + std::cout << "Set usando const iterators:" << std::endl; + setint::const_iterator it = s.begin(); + setint::const_iterator ite = s.end(); + + for (; it != ite; ++it) { + std::cout << *it << std::endl; + } + + std::cout << "test operatore uguaglianza :" << std::endl; + std::cout << "it == ite ? : " << (it == ite) << std::endl; + assert((it == ite) == true); + + std::cout << "Test operatore incremento prefix e postfix :" << std::endl; + it = s.begin(); + std::cout << "it++ : " << *(it++) << std::endl; + assert(*it == 2); + std::cout << "dopo post fisso deferenzio: " << *it << std::endl; + std::cout << "++it : " << *(++it) << std::endl; + assert(*it == 3); + std::cout << "Dopo prefisso deferenzio: " << *it << std::endl; + + std::cout << "Test operatore deferenziazione:" << std::endl; + it = s.begin(); + std::cout << "*it : " << *it << std::endl; + assert(*it == 1); + + std::cout << "Test assegnamento:" << std::endl; + setint::const_iterator it2 = it; + std::cout << "it2 : " << *it2 << std::endl; + assert(*it2 == 1); } /** * @brief Definizione funtore ugualgianza tra stringhe */ -struct equal_string -{ - bool operator()(const std::string &a, const std::string &b) const - { - return (a == b); - } +struct equal_string { + bool operator()(const std::string &a, const std::string &b) const { + return (a == b); + } }; /** * @brief Funzione di test per la lista di stringhe */ -void test_lista_di_stringhe(void) -{ - std::cout << "******** Test sulla lista di stringhe ********" << std::endl; +void test_lista_di_stringhe(void) { + std::cout << "******** Test sulla lista di stringhe ********" << std::endl; - Set> ss; + Set> ss; - std::cout << "Insertimento dei valori 'A', 'B', 'C', 'D'" << std::endl; - ss.add("A"); - ss.add("B"); - ss.add("C"); - ss.add("D"); + std::cout << "Insertimento dei valori 'A', 'B', 'C', 'D'" << std::endl; + ss.add("A"); + ss.add("B"); + ss.add("C"); + ss.add("D"); - std::cout << "Stampa con operator<<" << std::endl; - std::cout << ss << std::endl; + std::cout << "Stampa con operator<<" << std::endl; + std::cout << ss << std::endl; - std::cout << "Dimensione set: " << ss.size() << std::endl; + std::cout << "Dimensione set: " << ss.size() << std::endl; - std::cout << "Set Contiene A: " << ss.contains("A") << std::endl; - assert(ss.contains("A") == true); - std::cout << "Set Contiene Z: " << ss.contains("Z") << std::endl; - assert(ss.contains("Z") == false); + std::cout << "Set Contiene A: " << ss.contains("A") << std::endl; + assert(ss.contains("A") == true); + std::cout << "Set Contiene Z: " << ss.contains("Z") << std::endl; + assert(ss.contains("Z") == false); - std::cout << "Stampa con iteratori" << std::endl; + std::cout << "Stampa con iteratori" << std::endl; - Set>::const_iterator i, ie; + Set>::const_iterator i, + ie; - for (i = ss.begin(), ie = ss.end(); i != ie; ++i) - std::cout << *i << std::endl; + for (i = ss.begin(), ie = ss.end(); i != ie; ++i) + std::cout << *i << std::endl; - ss.remove("A"); + ss.remove("A"); - assert(ss.contains("A") == false); + assert(ss.contains("A") == false); - ss.clear(); - std::cout << "Chiamo clear()" << std::endl; - std::cout << "Dimensione set: " << ss.size() << std::endl; - assert(ss.empty() == true); + ss.clear(); + std::cout << "Chiamo clear()" << std::endl; + std::cout << "Dimensione set: " << ss.size() << std::endl; + assert(ss.empty() == true); } /** * @brief Funzione di test contruttore a partire da iteratore */ -void test_iter() -{ - std::cout << "******** Test costruttore da iteratore ********" << std::endl; - // Found C-style array declaration in place of std::array or std::vector - std::vector a = std::vector(5); - a[0] = 1; - a[1] = 2; - a[2] = 3; - a[3] = 4; - a[4] = 5; - Set> s(a.begin(), a.end()); - - std::cout << "Stampa con operator<<" << std::endl; - std::cout << s << std::endl; - assert(s.size() == 5); - - Set> s2(s.begin(), s.end()); - - std::cout << "Stampa con operator<<" << std::endl; - std::cout << s2 << std::endl; - assert(s2.size() == 5); - - std::vector v; - v.push_back(1.1); - v.push_back(2.2); - v.push_back(3.3); - v.push_back(4.4); - v.push_back(5.5); - - Set> s3(v.begin(), v.end()); - - std::cout << "Stampa con operator<<" << std::endl; - std::cout << s3 << std::endl; - assert(s3.size() == 5); +void test_iter() { + std::cout << "******** Test costruttore da iteratore ********" << std::endl; + // Found C-style array declaration in place of std::array or std::vector + std::vector a = std::vector(5); + a[0] = 1; + a[1] = 2; + a[2] = 3; + a[3] = 4; + a[4] = 5; + Set> s(a.begin(), a.end()); + + std::cout << "Stampa con operator<<" << std::endl; + std::cout << s << std::endl; + assert(s.size() == 5); + + Set> s2(s.begin(), s.end()); + + std::cout << "Stampa con operator<<" << std::endl; + std::cout << s2 << std::endl; + assert(s2.size() == 5); + + std::vector v; + v.push_back(1.1); + v.push_back(2.2); + v.push_back(3.3); + v.push_back(4.4); + v.push_back(5.5); + + Set> s3(v.begin(), v.end()); + + std::cout << "Stampa con operator<<" << std::endl; + std::cout << s3 << std::endl; + assert(s3.size() == 5); } /** * @brief Funzione di test su oggetti custom (point) */ -void test_lista_di_point(void) -{ - Set> sp; +void test_lista_di_point(void) { + Set> sp; - std::cout << "******** Test sull set di point ********" << std::endl; + std::cout << "******** Test sull set di point ********" << std::endl; - std::cout << "Insertimento dei valori (1,1), (1,2), (2,7), (0,0), (5,4)" << std::endl; - sp.add(point(1, 1)); - sp.add(point(1, 2)); - sp.add(point(2, 7)); - sp.add(point(0, 0)); - sp.add(point(5, 4)); + std::cout << "Insertimento dei valori (1,1), (1,2), (2,7), (0,0), (5,4)" + << std::endl; + sp.add(point(1, 1)); + sp.add(point(1, 2)); + sp.add(point(2, 7)); + sp.add(point(0, 0)); + sp.add(point(5, 4)); - std::cout << "Stampa con operator<<" << std::endl; - std::cout << sp << std::endl; + std::cout << "Stampa con operator<<" << std::endl; + std::cout << sp << std::endl; - std::cout << "Dimensione della lista: " << sp.size() << std::endl; - assert(sp.size() == 5); + std::cout << "Dimensione della lista: " << sp.size() << std::endl; + assert(sp.size() == 5); - Set>::const_iterator i, ie; - std::cout << "Stampa con iteratori" << std::endl; + Set>::const_iterator i, ie; + std::cout << "Stampa con iteratori" << std::endl; - for (i = sp.begin(), ie = sp.end(); i != ie; ++i) - std::cout << *i << std::endl; + for (i = sp.begin(), ie = sp.end(); i != ie; ++i) + std::cout << *i << std::endl; - std::cout << "Set Contiene (1,1): " << sp.contains(point(1, 1)) << std::endl; - assert(sp.contains(point(1, 1)) == true); + std::cout << "Set Contiene (1,1): " << sp.contains(point(1, 1)) << std::endl; + assert(sp.contains(point(1, 1)) == true); - std::cout << "Set Contiene (1,3): " << sp.contains(point(1, 3)) << std::endl; - assert(sp.contains(point(1, 3)) == false); + std::cout << "Set Contiene (1,3): " << sp.contains(point(1, 3)) << std::endl; + assert(sp.contains(point(1, 3)) == false); - std::cout << "Set Contiene (0,0): " << sp.contains(point(0, 0)) << std::endl; - assert(sp.contains(point(0, 0)) == true); + std::cout << "Set Contiene (0,0): " << sp.contains(point(0, 0)) << std::endl; + assert(sp.contains(point(0, 0)) == true); - std::cout << "Set Contiene (5,4): " << sp.contains(point(5, 4)) << std::endl; - assert(sp.contains(point(5, 4)) == true); + std::cout << "Set Contiene (5,4): " << sp.contains(point(5, 4)) << std::endl; + assert(sp.contains(point(5, 4)) == true); - std::cout << "Set Contiene (5,5): " << sp.contains(point(5, 5)) << std::endl; + std::cout << "Set Contiene (5,5): " << sp.contains(point(5, 5)) << std::endl; - std::cout << "Rimozione di (1,1)" << std::endl; - sp.remove(point(1, 1)); - std::cout << "Dimensione della lista: " << sp.size() << std::endl; - assert(sp.size() == 4); + std::cout << "Rimozione di (1,1)" << std::endl; + sp.remove(point(1, 1)); + std::cout << "Dimensione della lista: " << sp.size() << std::endl; + assert(sp.size() == 4); - std::cout << "Rimozione di (1,3)" << std::endl; - sp.remove(point(1, 3)); - std::cout << "Dimensione della lista: " << sp.size() << std::endl; - assert(sp.size() == 4); + std::cout << "Rimozione di (1,3)" << std::endl; + sp.remove(point(1, 3)); + std::cout << "Dimensione della lista: " << sp.size() << std::endl; + assert(sp.size() == 4); - std::cout << "Rimozione di (0,0)" << std::endl; - sp.remove(point(0, 0)); - std::cout << "Dimensione della lista: " << sp.size() << std::endl; - assert(sp.size() == 3); + std::cout << "Rimozione di (0,0)" << std::endl; + sp.remove(point(0, 0)); + std::cout << "Dimensione della lista: " << sp.size() << std::endl; + assert(sp.size() == 3); - std::cout << "Rimozione di (5,4)" << std::endl; - sp.remove(point(5, 4)); - std::cout << "Dimensione della lista: " << sp.size() << std::endl; - assert(sp.size() == 2); + std::cout << "Rimozione di (5,4)" << std::endl; + sp.remove(point(5, 4)); + std::cout << "Dimensione della lista: " << sp.size() << std::endl; + assert(sp.size() == 2); - std::cout << "Rimozione di (2,7)" << std::endl; - sp.remove(point(2, 7)); - std::cout << "Dimensione della lista: " << sp.size() << std::endl; - assert(sp.size() == 1); + std::cout << "Rimozione di (2,7)" << std::endl; + sp.remove(point(2, 7)); + std::cout << "Dimensione della lista: " << sp.size() << std::endl; + assert(sp.size() == 1); - std::cout << "Rimozione di (1,2)" << std::endl; - sp.remove(point(1, 2)); - std::cout << "Dimensione della lista: " << sp.size() << std::endl; - assert(sp.size() == 0); + std::cout << "Rimozione di (1,2)" << std::endl; + sp.remove(point(1, 2)); + std::cout << "Dimensione della lista: " << sp.size() << std::endl; + assert(sp.size() == 0); - std::cout << "Is empty? : " << sp.empty() << std::endl; - std::cout << sp.empty() << std::endl; - assert(sp.empty() == true); + std::cout << "Is empty? : " << sp.empty() << std::endl; + std::cout << sp.empty() << std::endl; + assert(sp.empty() == true); } /** * @brief Funzione di test per le funzioni globali */ -void test_global_method(void) -{ - std::cout << "******** Test della funzione globale filter_out ********" << std::endl; +void test_global_method(void) { + std::cout << "******** Test della funzione globale filter_out ********" + << std::endl; - setint s; // ctor default + setint s; // ctor default - s.add(1); - s.add(2); - s.add(3); + s.add(1); + s.add(2); + s.add(3); - std::cout << "Stampa di s dopo inserimenti:" << std::endl; - std::cout << s << std::endl; + std::cout << "Stampa di s dopo inserimenti:" << std::endl; + std::cout << s << std::endl; - setint s2 = filter_out(s, pred()); - std::cout << "Stampa di s dopo filter_out:" << std::endl; - std::cout << s << std::endl; - std::cout << "Stampa di s2 dopo filter_out:" << std::endl; - std::cout << s2 << std::endl; - assert(s2.size() == 1); + setint s2 = filter_out(s, pred()); + std::cout << "Stampa di s dopo filter_out:" << std::endl; + std::cout << s << std::endl; + std::cout << "Stampa di s2 dopo filter_out:" << std::endl; + std::cout << s2 << std::endl; + assert(s2.size() == 1); - std::cout << "******** Test della funzione globale operator+ ********" << std::endl; + std::cout << "******** Test della funzione globale operator+ ********" + << std::endl; - setint s3 = s + s2; - std::cout << "Stampa di s3 = s + s2 dopo operator+:" << std::endl; - std::cout << s3 << std::endl; + setint s3 = s + s2; + std::cout << "Stampa di s3 = s + s2 dopo operator+:" << std::endl; + std::cout << s3 << std::endl; - setint s4; - s4.add(10); - s4.add(20); - s4.add(30); - std::cout << "Stampa di s4 dopo filter_out:" << std::endl; - std::cout << s4 << std::endl; + setint s4; + s4.add(10); + s4.add(20); + s4.add(30); + std::cout << "Stampa di s4 dopo filter_out:" << std::endl; + std::cout << s4 << std::endl; - s4 = s3 + s4; + s4 = s3 + s4; - std::cout << "Stampa di s4 = s3 + s4 dopo operator+:" << std::endl; - std::cout << s4 << std::endl; + std::cout << "Stampa di s4 = s3 + s4 dopo operator+:" << std::endl; + std::cout << s4 << std::endl; - std::cout << "******** Test della funzione globale operator- ********" << std::endl; + std::cout << "******** Test della funzione globale operator- ********" + << std::endl; - setint s5 = s4 - s3; - std::cout << "Stampa di s5 = s4 - s3 dopo operator-:" << std::endl; - std::cout << s5 << std::endl; + setint s5 = s4 - s3; + std::cout << "Stampa di s5 = s4 - s3 dopo operator-:" << std::endl; + std::cout << s5 << std::endl; - setint s6; - s6.add(10); - s6.add(20); - s6.add(30); + setint s6; + s6.add(10); + s6.add(20); + s6.add(30); - setint s7 = s6 - s4; - std::cout << "Stampa di s7 = s6 - s4 dopo operator-:" << std::endl; - std::cout << s7 << std::endl; + setint s7 = s6 - s4; + std::cout << "Stampa di s7 = s6 - s4 dopo operator-:" << std::endl; + std::cout << s7 << std::endl; - std::cout << "******** Test della funzione globale save ********" << std::endl; + std::cout << "******** Test della funzione globale save ********" + << std::endl; - // save(s7, "test.txt"); Compiele error acceta solo Set + // save(s7, "test.txt"); Compiele error acceta solo Set - Set> ss; + Set> ss; - std::cout << "Insertimento dei valori 'A', 'B', 'C', 'D'" << std::endl; - ss.add("A"); - ss.add("B"); - ss.add("C"); - ss.add("D"); + std::cout << "Insertimento dei valori 'A', 'B', 'C', 'D'" << std::endl; + ss.add("A"); + ss.add("B"); + ss.add("C"); + ss.add("D"); - std::cout << "Stampa con operator<<" << std::endl; - std::cout << ss << std::endl; + std::cout << "Stampa con operator<<" << std::endl; + std::cout << ss << std::endl; - save(ss, "t.txt"); + save(ss, "t.txt"); - // check file - std::ifstream file("t.txt"); - std::string str; - std::getline(file, str); - std::cout << "Stampa del file test.txt:" << std::endl; - std::cout << str << std::endl; - assert(str == "4 (A) (B) (C) (D)"); + // check file + std::ifstream file("t.txt"); + std::string str; + std::getline(file, str); + std::cout << "Stampa del file test.txt:" << std::endl; + std::cout << str << std::endl; + assert(str == "4 (A) (B) (C) (D)"); } /** * @brief Funzione di test su oggetto dipinto set usando new */ -void test_con_new() -{ - struct SogTit - { - public: - explicit SogTit(std::string soggetto_o_titolo) : soggetto_o_titolo(soggetto_o_titolo) {} - - inline bool operator()(const dipinto &e) const { return e.scuola.find(soggetto_o_titolo) != std::string::npos; } - - private: - std::string soggetto_o_titolo; - }; - DipintoSet *prova = new DipintoSet(); - - prova->add(dipinto("C")); - prova->add(dipinto("B")); - SogTit sog("A"); - std::cout << "Chiamo filter out"; - assert(prova->size() == 2); - DipintoSet d = filter_out(*prova, sog); - assert(d.size() == 0); - std::cout << "Inizializzo copio e distruggo oggetto"; - DipintoSet *a = new DipintoSet(d); - delete a; - delete prova; +void test_con_new() { + struct SogTit { + public: + explicit SogTit(std::string soggetto_o_titolo) + : soggetto_o_titolo(soggetto_o_titolo) {} + + inline bool operator()(const dipinto &e) const { + return e.scuola.find(soggetto_o_titolo) != std::string::npos; + } + + private: + std::string soggetto_o_titolo; + }; + DipintoSet *prova = new DipintoSet(); + + prova->add(dipinto("C")); + prova->add(dipinto("B")); + SogTit sog("A"); + std::cout << "Chiamo filter out"; + assert(prova->size() == 2); + DipintoSet d = filter_out(*prova, sog); + assert(d.size() == 0); + std::cout << "Inizializzo copio e distruggo oggetto"; + DipintoSet *a = new DipintoSet(d); + delete a; + delete prova; } /** * @brief Funzione main */ -int main() -{ - test_metodi_fondamentali(); - test_metodi_set(); - test_quadre_op(); - test_confornto(); - test_iterator(); - test_lista_di_stringhe(); - test_lista_di_point(); - test_global_method(); - test_iter(); - test_con_new(); +int main() { + test_metodi_fondamentali(); + test_metodi_set(); + test_quadre_op(); + test_confornto(); + test_iterator(); + test_lista_di_stringhe(); + test_lista_di_point(); + test_global_method(); + test_iter(); + test_con_new(); } diff --git a/set.hpp b/set.hpp index 286e7dd..3984df7 100644 --- a/set.hpp +++ b/set.hpp @@ -9,10 +9,10 @@ #define SET_H #include // std::swap -#include // std::ostream #include // std::ptrdiff_t -#include // std::forward_iterator_tag #include // std::ofstream +#include // std::forward_iterator_tag +#include // std::ostream /** * @brief classe set @@ -21,377 +21,314 @@ * @tparam Equal funtore di uguaglianza * @tparam Allocator tipo di allocazione */ -template -class Set -{ +template class Set { public: - typedef unsigned int size_type; ///< Tipo di dato per la dimensione dell'array - typedef T value_type; + typedef unsigned int size_type; ///< Tipo di dato per la dimensione dell'array + typedef T value_type; private: - value_type *_elements; ///< Array of elements - size_type _size; ///< Number of elements - Equal _eq; ///< funtore - Allocator _allocator; ///< allocazione + value_type *_elements; ///< Array of elements + size_type _size; ///< Number of elements + Equal _eq; ///< funtore + Allocator _allocator; ///< allocazione public: - // Costruttore di default - Set() : _elements(nullptr), _size(0) {} - // Distruttore - ~Set() - { - clear(); + // Costruttore di default + Set() : _elements(nullptr), _size(0) {} + // Distruttore + ~Set() { clear(); } + // Costruttore Copia + Set(const Set &other) : _elements(nullptr), _size(0) { + if (other._size > 0) { + _elements = _allocator.allocate(other._size); + _size = other._size; + + for (size_type i = 0; i < _size; i++) { + _allocator.construct(_elements + i, other._elements[i]); + } } - // Costruttore Copia - Set(const Set &other) : _elements(nullptr), _size(0) - { - if (other._size > 0) - { - _elements = _allocator.allocate(other._size); - _size = other._size; - - for (size_type i = 0; i < _size; i++) - { - _allocator.construct(_elements + i, other._elements[i]); - } - } + } + + /** + * @brief Costruttore che crea un set riempito con dati presi + * da una sequenza identificata da un iteratore di inizio e + * uno di fine. + * + * + * @tparam Iter tipo di iteratore + * @param begin iteratore di inizio sequenza + * @param end iteratore di fine sequenza + * @throw std::bad_alloc possibile eccezione di allocazione + */ + + template + Set(Iter begin, Iter end) : _elements(nullptr), _size(0) { + Iter curr = begin; + try { + for (; curr != end; ++curr) { + add(static_cast(*curr)); + } + } catch (...) { + clear(); + throw; } - - /** - * @brief Costruttore che crea un set riempito con dati presi - * da una sequenza identificata da un iteratore di inizio e - * uno di fine. - * - * - * @tparam Iter tipo di iteratore - * @param begin iteratore di inizio sequenza - * @param end iteratore di fine sequenza - * @throw std::bad_alloc possibile eccezione di allocazione - */ - - template - Set(Iter begin, Iter end) : _elements(nullptr), - _size(0) - { - Iter curr = begin; - try - { - for (; curr != end; ++curr) - { - add(static_cast(*curr)); - } - } - catch (...) - { - clear(); - throw; - } + } + /** + * @brief Aggiungere un elemento alla lista + * + * + * @param value valore da inserire nella lista + * @return true se l'elemento è stato inserito, false altrimenti + * @throw std::bad_alloc possibile eccezione di allocazione + * + */ + bool add(const value_type &value) { + if (contains(value)) { + return false; } - /** - * @brief Aggiungere un elemento alla lista - * - * - * @param value valore da inserire nella lista - * @return true se l'elemento è stato inserito, false altrimenti - * @throw std::bad_alloc possibile eccezione di allocazione - * - */ - bool add(const value_type &value) - { - if (contains(value)) - { - return false; - } - - value_type *tmp = _allocator.allocate(_size + 1); - - for (size_type i = 0; i < _size; i++) - { - _allocator.construct(tmp + i, _elements[i]); - } - - _allocator.construct(tmp + _size, value); - - for (size_type i = 0; i < _size; i++) - { - _allocator.destroy(_elements + i); - } - _allocator.deallocate(_elements, _size); - - _elements = tmp; - _size++; - return true; + value_type *tmp = _allocator.allocate(_size + 1); + + for (size_type i = 0; i < _size; i++) { + _allocator.construct(tmp + i, _elements[i]); } - /** - * @brief Rimuovere un elemento dal set - * - * @param value valore da rimuovere dal set - * @return true se l'elemento è stato rimosso, false altrimenti - * @throw std::bad_alloc possibile eccezione di allocazione - */ - - bool remove(const value_type &value) - { - if (!contains(value)) - { - return false; - } - - if (_size == 1) - { - clear(); - return true; - } - value_type *tmp = _allocator.allocate(_size - 1); - - size_type j = 0; - for (size_type i = 0; i < _size; i++) - { - if (!_eq(_elements[i], value)) - { - _allocator.construct(tmp + j, _elements[i]); - j++; - } - } - - for (size_type i = 0; i < _size; i++) - { - _allocator.destroy(_elements + i); - } - - _allocator.deallocate(_elements, _size); - - _elements = tmp; - _size--; + _allocator.construct(tmp + _size, value); - return true; + for (size_type i = 0; i < _size; i++) { + _allocator.destroy(_elements + i); } + _allocator.deallocate(_elements, _size); - /** - * @brief Controlla se un elemento è presente nel set - * - * @param value valore da cercare nel set - * @return true se l'elemento è presente nel set, false altrimenti - */ - bool contains(const value_type &value) const - { - for (size_type i = 0; i < _size; i++) - { - if (_eq(_elements[i], value)) - { - return true; - } - } - return false; + _elements = tmp; + _size++; + + return true; + } + + /** + * @brief Rimuovere un elemento dal set + * + * @param value valore da rimuovere dal set + * @return true se l'elemento è stato rimosso, false altrimenti + * @throw std::bad_alloc possibile eccezione di allocazione + */ + + bool remove(const value_type &value) { + if (!contains(value)) { + return false; } - /** - * @brief Scambia il contenuto di due set - * - * @param other set con cui scambiare il contenuto - */ - void swap(Set &other) - { - std::swap(_elements, other._elements); - std::swap(_size, other._size); - std::swap(_eq, other._eq); + if (_size == 1) { + clear(); + return true; + } + value_type *tmp = _allocator.allocate(_size - 1); + + size_type j = 0; + for (size_type i = 0; i < _size; i++) { + if (!_eq(_elements[i], value)) { + _allocator.construct(tmp + j, _elements[i]); + j++; + } } - /** - * @brief operatore di assegnamento - * - * @param other set da copiare - * @return reference a this - */ - Set &operator=(const Set &other) - { - if (&other != this) - { - Set tmp(other); - this->swap(tmp); - } - - return *this; + for (size_type i = 0; i < _size; i++) { + _allocator.destroy(_elements + i); } - /** - @brief operatore di uguaglianza - @param other set da confrontare - @return true se i due set sono uguali, tutti gli elementi coincidono, false altrimenti - */ - bool operator==(const Set &other) const - { - if (_size != other._size) - { - return false; - } - - for (size_type i = 0; i < _size; i++) - { - if (!contains(other._elements[i])) - return false; - } + _allocator.deallocate(_elements, _size); + + _elements = tmp; + _size--; + + return true; + } + /** + * @brief Controlla se un elemento è presente nel set + * + * @param value valore da cercare nel set + * @return true se l'elemento è presente nel set, false altrimenti + */ + bool contains(const value_type &value) const { + for (size_type i = 0; i < _size; i++) { + if (_eq(_elements[i], value)) { return true; + } + } + return false; + } + + /** + * @brief Scambia il contenuto di due set + * + * @param other set con cui scambiare il contenuto + */ + void swap(Set &other) { + std::swap(_elements, other._elements); + std::swap(_size, other._size); + std::swap(_eq, other._eq); + } + + /** + * @brief operatore di assegnamento + * + * @param other set da copiare + * @return reference a this + */ + Set &operator=(const Set &other) { + if (&other != this) { + Set tmp(other); + this->swap(tmp); + } + + return *this; + } + + /** + @brief operatore di uguaglianza + @param other set da confrontare + @return true se i due set sono uguali, tutti gli elementi coincidono, + false altrimenti + */ + bool operator==(const Set &other) const { + if (_size != other._size) { + return false; + } + + for (size_type i = 0; i < _size; i++) { + if (!contains(other._elements[i])) + return false; } - /** - @brief Accesso in sola lettura ad un elemento tramite operatore [] - @param i indice dell'elemento da leggere - @return reference all'elemento di indice i - @throw std::out_of_range se i >= _size - */ - const value_type &operator[](size_type i) const - { - - if (i < _size) - { - return _elements[i]; - } - else - { - throw std::out_of_range("Index out of range"); - } + + return true; + } + /** + @brief Accesso in sola lettura ad un elemento tramite operatore [] + @param i indice dell'elemento da leggere + @return reference all'elemento di indice i + @throw std::out_of_range se i >= _size + */ + const value_type &operator[](size_type i) const { + + if (i < _size) { + return _elements[i]; + } else { + throw std::out_of_range("Index out of range"); + } + } + + /** + * @brief Dimensione del set + * + * + * @return Il numero di elementi nel set + * + */ + size_type size(void) const { return _size; } + + /** + * @brief Controlla se il set è vuoto + * + * @return true se il set è vuoto, false altrimenti + */ + bool empty(void) const { return (_size == 0); } + + /** + * @brief Svuota il set + * + */ + void clear(void) { + if (_size > 0) { + for (size_type i = 0; i < _size; i++) { + _allocator.destroy(_elements + i); + } + + _allocator.deallocate(_elements, _size); } - /** - * @brief Dimensione del set - * - * - * @return Il numero di elementi nel set - * - */ - size_type size(void) const - { - return _size; + _elements = nullptr; + _size = 0; + } + /** + * @brief Iteratore costante + * + */ + class const_iterator { + public: + typedef std::forward_iterator_tag iterator_category; + typedef T value_type; + typedef std::ptrdiff_t difference_type; + typedef const T *pointer; + typedef const T &reference; + + // Costruttore di default + const_iterator() : ptr(nullptr) {} + + // Costruttore Copia + const_iterator(const const_iterator &other) : ptr(other.ptr) {} + + // Distruttore + ~const_iterator() {} + + // Operatore di assegnamento + const_iterator &operator=(const const_iterator &other) { + ptr = other.ptr; + return *this; } - /** - * @brief Controlla se il set è vuoto - * - * @return true se il set è vuoto, false altrimenti - */ - bool empty(void) const - { - return (_size == 0); + // Operatore di iterazione post-incremento + const_iterator &operator++() { + ++ptr; + return *this; } - /** - * @brief Svuota il set - * - */ - void clear(void) - { - if (_size > 0) - { - for (size_type i = 0; i < _size; i++) - { - _allocator.destroy(_elements + i); - } - - _allocator.deallocate(_elements, _size); - } - - _elements = nullptr; - _size = 0; + // Operatore di iterazione pre-incremento + const_iterator operator++(int) { + const_iterator temp(*this); + ++ptr; + return temp; } - /** - * @brief Iteratore costante - * - */ - class const_iterator - { - public: - typedef std::forward_iterator_tag iterator_category; - typedef T value_type; - typedef std::ptrdiff_t difference_type; - typedef const T *pointer; - typedef const T &reference; - - // Costruttore di default - const_iterator() : ptr(nullptr) {} - - // Costruttore Copia - const_iterator(const const_iterator &other) : ptr(other.ptr) {} - - // Distruttore - ~const_iterator() {} - - // Operatore di assegnamento - const_iterator &operator=(const const_iterator &other) - { - ptr = other.ptr; - return *this; - } - - // Operatore di iterazione post-incremento - const_iterator &operator++() - { - ++ptr; - return *this; - } - - // Operatore di iterazione pre-incremento - const_iterator operator++(int) - { - const_iterator temp(*this); - ++ptr; - return temp; - } - - // Operatore di dereferenziazione - reference operator*() const - { - return *ptr; - } - - // Operatore di accesso a membro - pointer operator->() const - { - return ptr; - } - - // Operatore di uguaglianza - bool operator==(const const_iterator &other) const - { - return ptr == other.ptr; - } - - // Operatore di disuguaglianza - bool operator!=(const const_iterator &other) const - { - return !(*this == other); - } - - private: - const T *ptr; - - friend class Set; - - // Costruttore privato di inizializzazione usato dalla classe container - const_iterator(const T *p) : ptr(p) {} - }; - - /** - * @brief Iteratore costante di inizio sequenza - * - * @return const_iterator di inizio sequenza - */ - const_iterator begin() const - { - return const_iterator(_elements); + + // Operatore di dereferenziazione + reference operator*() const { return *ptr; } + + // Operatore di accesso a membro + pointer operator->() const { return ptr; } + + // Operatore di uguaglianza + bool operator==(const const_iterator &other) const { + return ptr == other.ptr; } - /** - * @brief Iteratore costante di fine sequenza - * - * @return const_iterator di fine sequenza - */ - const_iterator end() const - { - return const_iterator(_elements + _size); + // Operatore di disuguaglianza + bool operator!=(const const_iterator &other) const { + return !(*this == other); } + + private: + const T *ptr; + + friend class Set; + + // Costruttore privato di inizializzazione usato dalla classe container + const_iterator(const T *p) : ptr(p) {} + }; + + /** + * @brief Iteratore costante di inizio sequenza + * + * @return const_iterator di inizio sequenza + */ + const_iterator begin() const { return const_iterator(_elements); } + + /** + * @brief Iteratore costante di fine sequenza + * + * @return const_iterator di fine sequenza + */ + const_iterator end() const { return const_iterator(_elements + _size); } }; /** @@ -405,16 +342,15 @@ class Set * @return reference allo stream di output */ template -std::ostream &operator<<(std::ostream &os, const Set &s) -{ +std::ostream &operator<<(std::ostream &os, const Set &s) { - os << s.size(); - for (typename Set::const_iterator i = s.begin(); i != s.end(); ++i) - { - os << " (" << *i << ")"; - } + os << s.size(); + for (typename Set::const_iterator i = s.begin(); + i != s.end(); ++i) { + os << " (" << *i << ")"; + } - return os; + return os; } /** @@ -430,20 +366,18 @@ std::ostream &operator<<(std::ostream &os, const Set &s) * @return nuovo set filtrato */ template -Set filter_out(const Set &s, P predicate) -{ - typename Set::const_iterator i, ie; - Set result; - - for (i = s.begin(), ie = s.end(); i != ie; ++i) - { - if (predicate(*i)) - { - result.add(*i); - } +Set filter_out(const Set &s, + P predicate) { + typename Set::const_iterator i, ie; + Set result; + + for (i = s.begin(), ie = s.end(); i != ie; ++i) { + if (predicate(*i)) { + result.add(*i); } + } - return result; + return result; } /** @@ -457,23 +391,21 @@ Set filter_out(const Set &s, P predica * @return nuovo set unione */ template -Set operator+(const Set &s1, const Set &s2) -{ +Set operator+(const Set &s1, + const Set &s2) { - typename Set::const_iterator i, ie; - Set result; + typename Set::const_iterator i, ie; + Set result; - for (i = s1.begin(), ie = s1.end(); i != ie; ++i) - { - result.add(*i); - } + for (i = s1.begin(), ie = s1.end(); i != ie; ++i) { + result.add(*i); + } - for (i = s2.begin(), ie = s2.end(); i != ie; ++i) - { - result.add(*i); - } + for (i = s2.begin(), ie = s2.end(); i != ie; ++i) { + result.add(*i); + } - return result; + return result; } /** @@ -487,21 +419,19 @@ Set operator+(const Set &s1, const Set * @return nuovo set intersezione */ template -Set operator-(const Set &s1, const Set &s2) -{ - - typename Set::const_iterator i, ie; - Set result; - - for (i = s1.begin(), ie = s1.end(); i != ie; ++i) - { - if (s2.contains(*i)) - { - result.add(*i); - } +Set operator-(const Set &s1, + const Set &s2) { + + typename Set::const_iterator i, ie; + Set result; + + for (i = s1.begin(), ie = s1.end(); i != ie; ++i) { + if (s2.contains(*i)) { + result.add(*i); } + } - return result; + return result; } /** @@ -513,30 +443,28 @@ Set operator-(const Set &s1, const Set * @param filename nome del file su cui salvare il set */ template -void save(const Set &s, const std::string &filename) -{ - std::ostream *os = nullptr; - std::ofstream ofs; - - ofs.open(filename.c_str()); - if (ofs.fail()) - { - throw std::runtime_error("Cannot open file"); - } +void save(const Set &s, + const std::string &filename) { + std::ostream *os = nullptr; + std::ofstream ofs; - os = &ofs; + ofs.open(filename.c_str()); + if (ofs.fail()) { + throw std::runtime_error("Cannot open file"); + } - *os << s; + os = &ofs; - ofs.close(); + *os << s; - if (ofs.fail()) - { - throw std::runtime_error("Cannot close file"); - } + ofs.close(); + + if (ofs.fail()) { + throw std::runtime_error("Cannot close file"); + } - os = nullptr; + os = nullptr; - return; + return; } #endif // SET_H