Skip to content

Conversation

@MochaSummer2588
Copy link
Collaborator

@MochaSummer2588 MochaSummer2588 commented Dec 29, 2024

Sommario di Sourcery

Implementare la classe LinkedList e aggiungere classi di eccezioni per liste vuote ed errori di dispositivo non trovato. Aggiornare il tempo di spegnimento predefinito della TV a 60 minuti.

Nuove Funzionalità:

  • Implementata una classe LinkedList per la gestione dei dispositivi.

Test:

  • Aggiunto un file mainNode.cpp per testare l'implementazione di LinkedList.
Original summary in English

Summary by Sourcery

Implement the LinkedList class and add exception classes for empty list and device not found errors. Update the default shutdown time for the TV to 60 minutes.

New Features:

  • Implemented a LinkedList class for managing devices.

Tests:

  • Added a mainNode.cpp file for testing the LinkedList implementation.

@sourcery-ai
Copy link

sourcery-ai bot commented Dec 29, 2024

Here's the translation of the review guide to Italian:

Guida per il Revisore di Sourcery

Questa pull request completa l'implementazione della LinkedList e introduce classi di gestione degli errori. Include inoltre modifiche minori alle classi esistenti.

Diagramma di sequenza per il processo di rimozione del dispositivo

sequenceDiagram
    participant Client
    participant LinkedList
    participant Node
    participant Device

    Client->>LinkedList: removeDispositivo(nome)
    alt List is empty
        LinkedList-->>Client: throw ListaVuotaException
    else List not empty
        LinkedList->>LinkedList: searchDispositivo(nome)
        alt Device not found
            LinkedList-->>Client: throw DispositivoNonTrovatoException
        else Device found
            LinkedList->>Node: Update node connections
            LinkedList->>Node: Delete node
            LinkedList-->>Client: Success
        end
    end
Loading

Diagramma delle classi per LinkedList e classi di eccezioni

classDiagram
    class LinkedList {
      -Node* head
      -Node* tail
      +LinkedList()
      +LinkedList(Dispositivo& dispositivo)
      +void insert(Dispositivo& dispositivo)
      +void removeDispositivo(string nome)
      +vector~Dispositivo*~ removeAllDispositiviOff(int currentTime)
      +void removeTimer(string nome)
      +bool isEmpty()
      -Node* searchDispositivo(string nome)
      -void connectBefore(Node* p, Node* q)
    }

    class Node {
      +Dispositivo* disp
      +Node* prev
      +Node* next
      +Node(Dispositivo& data)
      +Node(const Node& data)
      +Node& operator=(const Node& data)
    }

    class DispositivoNonTrovatoException {
      +DispositivoNonTrovatoException()
    }

    class ListaVuotaException {
      +ListaVuotaException()
    }

    LinkedList *-- Node
    DispositivoNonTrovatoException --|> invalid_argument
    ListaVuotaException --|> out_of_range

    note for LinkedList "Implementazione di una lista doppiamente linkata per la gestione dei dispositivi"
    note for Node "Classe nodo interna per la lista linkata"
    note for DispositivoNonTrovatoException "Sollevata quando un dispositivo non viene trovato"
    note for ListaVuotaException "Sollevata quando la lista è vuota"
Loading

Modifiche a Livello di File

Modifica Dettagli File
Implementata la classe LinkedList e le sue funzionalità principali.
  • Aggiunti metodi per inserimento, eliminazione e ricerca di nodi.
  • Implementate funzionalità per rimuovere dispositivi in base a tempo e stato.
  • Inclusa gestione degli errori per scenari di lista vuota e dispositivo non trovato.
  • Aggiunto il file mainNode.cpp per testare l'implementazione di LinkedList.
src/LinkedList.cpp
include/LinkedList.h
src/mainNode.cpp
Introdotte classi di gestione degli errori per scenari specifici.
  • Creata DispositivoNonTrovatoException per gestire casi in cui un dispositivo non viene trovato.
  • Creata ListaVuotaException per gestire casi in cui viene eseguita un'operazione su una lista vuota.
include/DispositivoNonTrovatoException.h
include/ListaVuotaException.h
Modificata la classe Dispositivo per utilizzare una costante per i minuti massimi in un giorno.
  • Sostituito il numero magico MAX_MINUTI_GIORNATA con la costante MINUTI_GIORNATA per migliorare la leggibilità e la manutenibilità del codice.
  • Aggiornato il tempo di spegnimento predefinito per il dispositivo "Televisore" in ListaDispositivi.h da 0 a 60 minuti.
src/Dispositivo.cpp
include/ListaDispositivi.h

Suggerimenti e comandi

Interazione con Sourcery

  • Avviare una nuova revisione: Commenta @sourcery-ai review sulla pull request.
  • Continuare le discussioni: Rispondi direttamente ai commenti di revisione di Sourcery.
  • Generare un issue GitHub da un commento di revisione: Chiedi a Sourcery di creare un issue da un commento di revisione rispondendo al commento.
  • Generare un titolo per la pull request: Scrivi @sourcery-ai in qualsiasi punto del titolo della pull request per generare un titolo in qualsiasi momento.
  • Generare un riepilogo della pull request: Scrivi @sourcery-ai summary in qualsiasi punto del corpo della pull request per generare un riepilogo PR in qualsiasi momento. Puoi anche utilizzare questo comando per specificare dove inserire il riepilogo.

Personalizzazione della Tua Esperienza

Accedi alla tua dashboard per:

  • Abilitare o disabilitare funzionalità di revisione come il riepilogo della pull request generato da Sourcery, la guida per il revisore e altri.
  • Cambiare la lingua di revisione.
  • Aggiungere, rimuovere o modificare istruzioni di revisione personalizzate.
  • Regolare altre impostazioni di revisione.

Ottenere Aiuto

Original review guide in English

Reviewer's Guide by Sourcery

This pull request completes the LinkedList implementation and introduces error handling classes. It also includes minor modifications to existing classes.

Sequence diagram for device removal process

sequenceDiagram
    participant Client
    participant LinkedList
    participant Node
    participant Device

    Client->>LinkedList: removeDispositivo(nome)
    alt List is empty
        LinkedList-->>Client: throw ListaVuotaException
    else List not empty
        LinkedList->>LinkedList: searchDispositivo(nome)
        alt Device not found
            LinkedList-->>Client: throw DispositivoNonTrovatoException
        else Device found
            LinkedList->>Node: Update node connections
            LinkedList->>Node: Delete node
            LinkedList-->>Client: Success
        end
    end
Loading

Class diagram for LinkedList and Exception classes

classDiagram
    class LinkedList {
      -Node* head
      -Node* tail
      +LinkedList()
      +LinkedList(Dispositivo& dispositivo)
      +void insert(Dispositivo& dispositivo)
      +void removeDispositivo(string nome)
      +vector~Dispositivo*~ removeAllDispositiviOff(int currentTime)
      +void removeTimer(string nome)
      +bool isEmpty()
      -Node* searchDispositivo(string nome)
      -void connectBefore(Node* p, Node* q)
    }

    class Node {
      +Dispositivo* disp
      +Node* prev
      +Node* next
      +Node(Dispositivo& data)
      +Node(const Node& data)
      +Node& operator=(const Node& data)
    }

    class DispositivoNonTrovatoException {
      +DispositivoNonTrovatoException()
    }

    class ListaVuotaException {
      +ListaVuotaException()
    }

    LinkedList *-- Node
    DispositivoNonTrovatoException --|> invalid_argument
    ListaVuotaException --|> out_of_range

    note for LinkedList "Doubly linked list implementation for managing devices"
    note for Node "Internal node class for the linked list"
    note for DispositivoNonTrovatoException "Thrown when a device is not found"
    note for ListaVuotaException "Thrown when list is empty"
Loading

File-Level Changes

Change Details Files
Implemented the LinkedList class and its core functionalities.
  • Added methods for insertion, deletion, and search of nodes.
  • Implemented functionalities for removing devices based on time and status.
  • Included error handling for empty list and device not found scenarios.
  • Added a mainNode.cpp file to test the LinkedList implementation.
src/LinkedList.cpp
include/LinkedList.h
src/mainNode.cpp
Introduced error handling classes for specific scenarios.
  • Created DispositivoNonTrovatoException for handling cases where a device is not found.
  • Created ListaVuotaException for handling cases where an operation is performed on an empty list.
include/DispositivoNonTrovatoException.h
include/ListaVuotaException.h
Modified the Dispositivo class to use a constant for the maximum minutes in a day.
  • Replaced the MAX_MINUTI_GIORNATA magic number with the MINUTI_GIORNATA constant to improve code readability and maintainability.
  • Updated the default power-off time for the "Televisore" device in ListaDispositivi.h from 0 to 60 minutes.
src/Dispositivo.cpp
include/ListaDispositivi.h

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ciao @MochaSummer2588 - Ho esaminato le tue modifiche - ecco alcuni feedback:

Commenti generali:

  • Alla classe LinkedList mancano le implementazioni del costruttore di copia e dell'operatore di assegnazione, il che potrebbe causare problemi di memoria con le copie superficiali. Considera di implementarli o di eliminarli esplicitamente.
  • Ci sono diversi errori di battitura nei nomi dei dispositivi in mainNode.cpp (ad es. 'Frigrifero', 'Lavtrice', 'Televisre'). Per favore, correggili per garantire coerenza.
Ecco cosa ho esaminato durante la revisione
  • 🟡 Problemi generali: 3 problemi trovati
  • 🟢 Sicurezza: tutto a posto
  • 🟢 Test: tutto a posto
  • 🟡 Complessità: 1 problema trovato
  • 🟢 Documentazione: tutto a posto

La tua prova scade l'11 gennaio 2025. Per favore aggiorna per continuare a utilizzare Sourcery ✨

Aiutami a essere più utile! Per favore, fai clic su 👍 o 👎 su ogni commento e userò il feedback per migliorare le tue revisioni.
Original comment in English

Hey @MochaSummer2588 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The LinkedList class is missing copy constructor and assignment operator implementations, which could lead to memory issues with shallow copies. Consider implementing these or explicitly deleting them.
  • There are several typos in device names in mainNode.cpp (e.g., 'Frigrifero', 'Lavtrice', 'Televisre'). Please correct these for consistency.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Your trial expires on January 11, 2025. Please upgrade to continue using Sourcery ✨

Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

}
}

void LinkedList::removeDispositivo(const string nome)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

problema: Il metodo removeDispositivo deve gestire il caso in cui la lista ha esattamente un nodo (head == tail).

Attualmente, quando head == tail, tentare di accedere a head->next causerà un dereferenziamento di puntatore nullo. Aggiungi un caso separato per gestire questa situazione.

Original comment in English

issue: The removeDispositivo method needs to handle the case where the list has exactly one node (head == tail).

Currently, when head == tail, attempting to access head->next will cause a null pointer dereference. Add a separate case to handle this situation.

Comment on lines +89 to +91
vector<Dispositivo*> LinkedList::removeAllDispositiviOff(const int currentTime)
{
if(isEmpty())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggerimento (prestazioni): removeAllDispositiviOff ha una complessità O(n²) a causa di attraversamenti di liste nidificati.

Considera di ristrutturare per evitare di chiamare removeDispositivo all'interno del ciclo, che causa attraversamenti aggiuntivi della lista.

Suggested change
vector<Dispositivo*> LinkedList::removeAllDispositiviOff(const int currentTime)
{
if(isEmpty())
vector<Dispositivo*> LinkedList::removeAllDispositiviOff(const int currentTime)
{
vector<Dispositivo*> removedDevices;
if(isEmpty()) {
return removedDevices;
}
vector<Node*> nodesToRemove;
Node* current = head;
// Primo passaggio: raccogli tutti i nodi da rimuovere
while(current != nullptr) {
if(!current->disp->isOn(currentTime)) {
nodesToRemove.push_back(current);
removedDevices.push_back(current->disp);
}
current = current->next;
}
// Secondo passaggio: rimuovi i nodi raccolti
for(Node* nodeToRemove : nodesToRemove) {
if(nodeToRemove == head) {
head = nodeToRemove->next;
}
if(nodeToRemove == tail) {
tail = nodeToRemove->prev;
}
if(nodeToRemove->prev) {
nodeToRemove->prev->next = nodeToRemove->next;
}
if(nodeToRemove->next) {
nodeToRemove->next->prev = nodeToRemove->prev;
}
delete nodeToRemove;
}
return removedDevices;
Original comment in English

suggestion (performance): removeAllDispositiviOff has O(n²) complexity due to nested list traversals.

Consider restructuring to avoid calling removeDispositivo inside the loop, which causes additional list traversals.

Suggested change
vector<Dispositivo*> LinkedList::removeAllDispositiviOff(const int currentTime)
{
if(isEmpty())
vector<Dispositivo*> LinkedList::removeAllDispositiviOff(const int currentTime)
{
vector<Dispositivo*> removedDevices;
if(isEmpty()) {
return removedDevices;
}
vector<Node*> nodesToRemove;
Node* current = head;
// First pass: collect all nodes to remove
while(current != nullptr) {
if(!current->disp->isOn(currentTime)) {
nodesToRemove.push_back(current);
removedDevices.push_back(current->disp);
}
current = current->next;
}
// Second pass: remove collected nodes
for(Node* nodeToRemove : nodesToRemove) {
if(nodeToRemove == head) {
head = nodeToRemove->next;
}
if(nodeToRemove == tail) {
tail = nodeToRemove->prev;
}
if(nodeToRemove->prev) {
nodeToRemove->prev->next = nodeToRemove->next;
}
if(nodeToRemove->next) {
nodeToRemove->next->prev = nodeToRemove->prev;
}
delete nodeToRemove;
}
return removedDevices;

Comment on lines +9 to +10
Dispositivo Frigorifero = CreaDispositivo::creaDispositivo("Frigrifero", 76); //Manuale
Dispositivo Lavatrice = CreaDispositivo::creaDispositivo("Lavtrice", 8); //110 min
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

problema (errore di battitura): I nomi dei dispositivi contengono errori di battitura che potrebbero causare errori di ricerca.

"Frigrifero" dovrebbe essere "Frigorifero" e "Lavtrice" dovrebbe essere "Lavatrice". Questi errori di battitura causeranno errori di ricerca dei dispositivi.

Suggested change
Dispositivo Frigorifero = CreaDispositivo::creaDispositivo("Frigrifero", 76); //Manuale
Dispositivo Lavatrice = CreaDispositivo::creaDispositivo("Lavtrice", 8); //110 min
Dispositivo Frigorifero = CreaDispositivo::creaDispositivo("Frigorifero", 76); //Manuale
Dispositivo Lavatrice = CreaDispositivo::creaDispositivo("Lavatrice", 8); //110 min
Original comment in English

issue (typo): Device names contain typos that could cause lookup failures.

"Frigrifero" should be "Frigorifero" and "Lavtrice" should be "Lavatrice". These typos will cause device lookup failures.

Suggested change
Dispositivo Frigorifero = CreaDispositivo::creaDispositivo("Frigrifero", 76); //Manuale
Dispositivo Lavatrice = CreaDispositivo::creaDispositivo("Lavtrice", 8); //110 min
Dispositivo Frigorifero = CreaDispositivo::creaDispositivo("Frigorifero", 76); //Manuale
Dispositivo Lavatrice = CreaDispositivo::creaDispositivo("Lavatrice", 8); //110 min

@@ -0,0 +1,194 @@
#include "../include/LinkedList.h"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

problema (complessità): Considera di semplificare i metodi insert() e remove() unificando la logica di manipolazione dei puntatori in un unico metodo connectNodes(), riducendo la gestione dei casi speciali e migliorando la manutenibilità.

I metodi insert() e remove() possono essere semplificati unificando la logica di manipolazione dei puntatori. Ecco come ridurre la gestione dei casi speciali:

  1. Estendi connectBefore() per gestire tutti gli aggiornamenti dei puntatori:
void LinkedList::connectNodes(Node* prev, Node* curr, Node* next) {
    if (prev) prev->next = curr;
    else head = curr;

    if (next) next->prev = curr;
    else tail = curr;

    curr->prev = prev;
    curr->next = next;
}
  1. Semplifica insert() usando l'approccio unificato:
void LinkedList::insert(Dispositivo& dispositivo) {
    Node* newNode = new Node(dispositivo);
    if (isEmpty()) {
        connectNodes(nullptr, newNode, nullptr);
        return;
    }

    Node* current = head;
    while (current && current->disp->getOrarioAccensione() <= dispositivo.getOrarioAccensione()) {
        current = current->next;
    }

    connectNodes(current ? current->prev : tail, newNode, current);
}
  1. Semplifica allo stesso modo remove():
void LinkedList::removeDispositivo(const string nome) {
    if (isEmpty()) throw ListaVuotaException();

    Node* current = searchDispositivo(nome);
    connectNodes(current->prev, current->next, current->next);
    delete current;
}

Questo approccio:

  • Elimina la logica duplicata di manipolazione dei puntatori
  • Riduce la gestione dei casi speciali
  • Rende il codice più manutenibile
  • Preserva tutte le funzionalità
Original comment in English

issue (complexity): Consider simplifying the insert() and remove() methods by unifying the pointer manipulation logic into a single connectNodes() method, reducing special case handling and improving maintainability.

The insert() and remove() methods can be simplified by unifying the pointer manipulation logic. Here's how to reduce the special case handling:

  1. Extend connectBefore() to handle all pointer updates:
void LinkedList::connectNodes(Node* prev, Node* curr, Node* next) {
    if (prev) prev->next = curr;
    else head = curr;

    if (next) next->prev = curr;
    else tail = curr;

    curr->prev = prev;
    curr->next = next;
}
  1. Simplify insert() using the unified approach:
void LinkedList::insert(Dispositivo& dispositivo) {
    Node* newNode = new Node(dispositivo);
    if (isEmpty()) {
        connectNodes(nullptr, newNode, nullptr);
        return;
    }

    Node* current = head;
    while (current && current->disp->getOrarioAccensione() <= dispositivo.getOrarioAccensione()) {
        current = current->next;
    }

    connectNodes(current ? current->prev : tail, newNode, current);
}
  1. Similarly simplify remove():
void LinkedList::removeDispositivo(const string nome) {
    if (isEmpty()) throw ListaVuotaException();

    Node* current = searchDispositivo(nome);
    connectNodes(current->prev, current->next, current->next);
    delete current;
}

This approach:

  • Eliminates duplicate pointer manipulation logic
  • Reduces special case handling
  • Makes the code more maintainable
  • Preserves all functionality

@MochaSummer2588 MochaSummer2588 merged commit 0d82d99 into main Dec 29, 2024
2 checks passed
@MochaSummer2588 MochaSummer2588 deleted the Nodi-Array-Alberto branch December 29, 2024 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants