-
Notifications
You must be signed in to change notification settings - Fork 1
Modifica test e main node #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,17 @@ | ||
| #include "../include/LinkedList.h" | ||
| using namespace std; | ||
|
|
||
| LinkedList::Node::Node(Dispositivo& data): prev{nullptr}, next{nullptr}, disp{&data} | ||
| { } | ||
|
|
||
| LinkedList::Node::Node(const Node& data): prev{data.prev}, next{data.next}, disp{data.disp} | ||
| LinkedList::LinkedList(): head{nullptr}, tail{nullptr} | ||
| { } | ||
|
|
||
| LinkedList::Node& LinkedList::Node::operator=(const Node& data) | ||
| { | ||
| prev = data.prev; | ||
| next = data.next; | ||
| disp = data.disp; | ||
| return *this; | ||
| } | ||
|
|
||
| LinkedList::Node::~Node() | ||
| { | ||
| delete disp; | ||
| prev = nullptr; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Il distruttore di Node non elimina più il puntatore disp - per favore chiarisci la semantica di proprietà Se LinkedList dovrebbe possedere gli oggetti Dispositivo, questo potrebbe causare memory leak. Se non è così, l'implementazione precedente era scorretta. Per favore documenta il modello di proprietà previsto. Original comment in Englishissue (bug_risk): The Node destructor no longer deletes the disp pointer - please clarify ownership semantics If LinkedList is supposed to own the Dispositivo objects, this could cause memory leaks. If not, the previous implementation was incorrect. Please document the intended ownership model. |
||
| next = nullptr; | ||
| } | ||
|
|
||
| LinkedList::LinkedList(): head{nullptr}, tail{nullptr} | ||
| { } | ||
|
|
||
| LinkedList::LinkedList(Dispositivo& dispositivo) | ||
| { | ||
| head = tail = new Node(dispositivo); | ||
|
|
@@ -62,71 +51,98 @@ void LinkedList::insert(Dispositivo& dispositivo) | |
| } | ||
| } | ||
|
|
||
| Dispositivo* LinkedList::removeDispositivo(const string nome) | ||
| Dispositivo* LinkedList::removeDispositivoName(const std::string nome) | ||
| { | ||
| if(isEmpty()) | ||
| { | ||
| throw std::out_of_range("Lista vuota!"); | ||
| } | ||
|
|
||
| Node* current = searchDispositivoName(nome); | ||
|
|
||
| if(current == head) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: Il caso di un singolo elemento non è gestito nelle funzioni di rimozione Quando head == tail (la lista ha un solo elemento), il codice corrente causerà una dereferenziazione di puntatore nullo. Aggiungi un caso speciale per le liste con un singolo elemento. Original comment in Englishissue: Single element case not handled in remove functions When head == tail (list has one element), the current code will cause null pointer dereference. Add a special case for single-element lists. |
||
| { | ||
| head = head->next; | ||
| head->prev = nullptr; | ||
| } | ||
| else if(current == tail) | ||
| { | ||
| tail = tail->prev; | ||
| tail->next = nullptr; | ||
| } | ||
| else | ||
| { | ||
| current->prev->next = current->next; | ||
| current->next->prev = current->prev; | ||
| } | ||
|
|
||
| Dispositivo* disp = current->disp; | ||
| delete current; | ||
| return disp; | ||
| } | ||
|
|
||
| Dispositivo* LinkedList::removeDispositivoId(const int id) | ||
| { | ||
| if(isEmpty()) | ||
| { | ||
| throw ListaVuotaException(); | ||
| throw std::out_of_range("Lista vuota!"); | ||
| } | ||
|
|
||
| Node* current = searchDispositivo(nome); | ||
| Node* current = searchDispositivoId(id); | ||
|
|
||
| if(current == head) | ||
| { | ||
| head = head->next; | ||
| head->prev = nullptr; | ||
| return current->disp; | ||
| } | ||
| else if(current == tail) | ||
| { | ||
| tail = tail->prev; | ||
| tail->next = nullptr; | ||
| return current->disp; | ||
| } | ||
| else | ||
| { | ||
| current->prev->next = current->next; | ||
| current->next->prev = current->prev; | ||
| return current->disp; | ||
| } | ||
|
|
||
| Dispositivo* disp = current->disp; | ||
| delete current; | ||
| return disp; | ||
| } | ||
|
|
||
| vector<Dispositivo*> LinkedList::removeAllDispositiviOff(const int currentTime) | ||
| std::vector<Dispositivo*> LinkedList::removeAllDispositiviOff(const int currentTime) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Modificare la lista durante l'iterazione può portare a un comportamento indefinito L'implementazione corrente modifica la struttura della lista durante l'iterazione. Considera di raccogliere prima i nodi da rimuovere, quindi rimuoverli in un passaggio separato. Original comment in Englishissue (bug_risk): Modifying list while iterating can lead to undefined behavior The current implementation modifies the list structure while iterating through it. Consider collecting nodes to remove first, then removing them in a separate pass. |
||
| { | ||
| if(isEmpty()) | ||
| { | ||
| throw ListaVuotaException(); | ||
| throw std::out_of_range("Lista vuota!"); | ||
| } | ||
|
|
||
| vector<Dispositivo*> dispositiviSpenti; | ||
| std::vector<Dispositivo*> dispositiviSpenti; | ||
| Node* current = head; | ||
| while(current) | ||
| { | ||
| if(current->disp->getOrarioSpegnimento() <= currentTime) | ||
| { | ||
| dispositiviSpenti.push_back(current->disp); | ||
| Node* temp = current; | ||
| current = current->next; | ||
| removeDispositivo(temp->disp->getNome()); | ||
| } | ||
| else | ||
| { | ||
| current = current->next; | ||
| removeDispositivoName(temp->disp->getNome()); | ||
| } | ||
| current = current->next; | ||
| } | ||
|
|
||
| return dispositiviSpenti; | ||
| } | ||
|
|
||
|
|
||
| void LinkedList::removeTimer(const string nome) | ||
| void LinkedList::removeTimer(const std::string nome) | ||
| { | ||
| if(isEmpty()) | ||
| { | ||
| throw ListaVuotaException(); | ||
| throw std::out_of_range("Lista vuota!");; | ||
| } | ||
|
|
||
| Node* current = searchDispositivo(nome); | ||
| Node* current = searchDispositivoName(nome); | ||
|
|
||
| current->disp->setTimerOff(); | ||
| } | ||
|
|
@@ -135,7 +151,7 @@ void LinkedList::removeAllTimers() | |
| { | ||
| if(isEmpty()) | ||
| { | ||
| throw ListaVuotaException(); | ||
| throw std::out_of_range("Lista vuota!");; | ||
| } | ||
|
|
||
| Node* current = head; | ||
|
|
@@ -151,7 +167,7 @@ bool LinkedList::isEmpty() const | |
| return (head == nullptr); | ||
| } | ||
|
|
||
| ostream& operator<<(ostream& os, const LinkedList& list) | ||
| std::ostream& operator<<(std::ostream& os, const LinkedList& list) | ||
| { | ||
| if(list.isEmpty()) | ||
| { | ||
|
|
@@ -180,11 +196,11 @@ void LinkedList::connectBefore(Node* p, Node* q) | |
| p->prev = q; | ||
| } | ||
|
|
||
| LinkedList::Node* LinkedList::searchDispositivo(const std::string nome) const | ||
| LinkedList::Node* LinkedList::searchDispositivoName(const std::string nome) const | ||
| { | ||
| if(isEmpty()) | ||
| { | ||
| throw ListaVuotaException(); | ||
| throw std::out_of_range("Lista vuota!");; | ||
| } | ||
|
|
||
| Node* current = head; | ||
|
|
@@ -195,7 +211,28 @@ LinkedList::Node* LinkedList::searchDispositivo(const std::string nome) const | |
|
|
||
| if(current == nullptr) | ||
| { | ||
| throw DispositivoNonTrovatoException(); | ||
| throw std::invalid_argument("Dispositivo non trovato!");; | ||
| } | ||
|
|
||
| return current; | ||
| } | ||
|
|
||
| LinkedList::Node* LinkedList::searchDispositivoId(const int id) const | ||
| { | ||
| if(isEmpty()) | ||
| { | ||
| throw std::out_of_range("Lista vuota!");; | ||
| } | ||
|
|
||
| Node* current = head; | ||
| while(current && current->disp->getId() != id) | ||
| { | ||
| current = current->next; | ||
| } | ||
|
|
||
| if(current == nullptr) | ||
| { | ||
| throw std::invalid_argument("Dispositivo non trovato!");; | ||
| } | ||
|
|
||
| return current; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,44 +6,48 @@ using namespace std; | |
|
|
||
| int main() | ||
| { | ||
| Dispositivo Frigorifero = CreaDispositivo::creaDispositivo("Frigrifero", 76); //Manuale | ||
| Dispositivo Lavatrice = CreaDispositivo::creaDispositivo("Lavtrice", 8); //110 min | ||
| Dispositivo ScaldaBagno = CreaDispositivo::creaDispositivo("Scalda bagno", 9); //Manuale | ||
| Dispositivo Televisore = CreaDispositivo::creaDispositivo("Televisre", 4); //60 min | ||
| Dispositivo Asciugatrice = CreaDispositivo::creaDispositivo("Asciugatrice", 2); //60 min | ||
| Dispositivo FornoMicroonde = CreaDispositivo::creaDispositivo("Forno a microonde", 1); //2 min | ||
|
|
||
| LinkedList list = LinkedList(); | ||
|
|
||
| list.insert(Frigorifero); | ||
| cout << list << endl; | ||
|
|
||
| list.insert(Lavatrice); | ||
| cout << list << endl; | ||
|
|
||
| list.insert(ScaldaBagno); | ||
| cout << list << endl; | ||
|
|
||
| list.insert(Televisore); | ||
| cout << list << endl; | ||
|
|
||
| list.insert(FornoMicroonde); | ||
| cout << list << endl; | ||
|
|
||
| list.insert(Asciugatrice); | ||
| cout << list << endl; | ||
|
|
||
| list.removeDispositivo("Scaldabagno"); | ||
| cout << list << endl; | ||
|
|
||
| cout << Televisore.getOrarioSpegnimento() << endl; | ||
|
|
||
| list.removeAllDispositiviOff(5); | ||
| cout << list << endl; | ||
|
|
||
| list.removeTimer("Asciugatrice"); | ||
| cout << list << endl; | ||
| cout << Asciugatrice.getOrarioSpegnimento() << endl; | ||
|
|
||
|
|
||
| try { | ||
| Dispositivo Frigorifero = CreaDispositivo::creaDispositivo("Frigrifero", 76); //Manuale | ||
| Dispositivo Lavatrice = CreaDispositivo::creaDispositivo("Lavtrice", 8); //110 min | ||
| Dispositivo ScaldaBagno = CreaDispositivo::creaDispositivo("Scalda bagno", 9); //Manuale | ||
| Dispositivo Televisore = CreaDispositivo::creaDispositivo("Televisre", 4); //60 min | ||
| Dispositivo Asciugatrice = CreaDispositivo::creaDispositivo("Asciugatrice", 2); //60 min | ||
| Dispositivo FornoMicroonde = CreaDispositivo::creaDispositivo("Forno a microonde", 1); //2 min | ||
|
|
||
| LinkedList list = LinkedList(); | ||
|
|
||
| list.insert(Frigorifero); | ||
| cout << list << endl; | ||
|
|
||
| list.insert(Lavatrice); | ||
| cout << list << endl; | ||
|
|
||
| list.insert(ScaldaBagno); | ||
| cout << list << endl; | ||
|
|
||
| list.insert(Televisore); | ||
| cout << list << endl; | ||
|
|
||
| list.insert(FornoMicroonde); | ||
| cout << list << endl; | ||
|
|
||
| list.insert(Asciugatrice); | ||
| cout << list << endl; | ||
|
|
||
| list.removeDispositivoName("Scaldabagno"); | ||
| cout << list << endl; | ||
|
|
||
| cout << Televisore.getOrarioSpegnimento() << endl; | ||
|
|
||
| list.removeAllDispositiviOff(5); | ||
| cout << list << endl; | ||
|
|
||
| list.removeTimer("Asciugatrice"); | ||
| cout << list << endl; | ||
| cout << Asciugatrice.getOrarioSpegnimento() << endl; | ||
| } | ||
| catch (exception& e) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Il gestore delle eccezioni dovrebbe registrare i dettagli dell'errore Considera di registrare i dettagli dell'eccezione utilizzando e.what() prima di restituire, per aiutare nel debug. Implementazione suggerita: catch (exception& e) {
cerr << "Errore: " << e.what() << endl;
return 1;
}
Se la base di codice utilizza un framework di registrazione specifico invece dell'output diretto su cerr, dovresti sostituire cerr con il meccanismo di registrazione appropriato per essere coerente con il resto della base di codice. Original comment in Englishsuggestion (bug_risk): Exception handler should log error details Consider logging the exception details using e.what() before returning, to help with debugging. Suggested implementation: catch (exception& e) {
cerr << "Error: " << e.what() << endl;
return 1;
}
If the codebase uses a specific logging framework instead of direct cerr output, you should replace cerr with the appropriate logging mechanism for consistency with the rest of the codebase. |
||
| return 1; | ||
| } | ||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: La documentazione per LinkedList sarebbe utile.
Sarebbe utile aggiungere documentazione che spieghi lo scopo e l'utilizzo della classe LinkedList. Questo aiuterà altri sviluppatori a capire come si inserisce nel progetto complessivo.
Implementazione suggerita:
La modifica precedente aggiunge la documentazione a livello di file. Dovresti inoltre aggiungere documentazione per:
La documentazione esatta dipenderà dai dettagli di implementazione effettivi della tua classe LinkedList, che non posso vedere nel frammento di codice fornito.
Esempio di documentazione a livello di classe da aggiungere:
Original comment in English
suggestion: Documentation for LinkedList would be helpful.
It would be beneficial to add documentation explaining the purpose and usage of the LinkedList class. This will help other developers understand how it fits into the overall project.
Suggested implementation:
The above change adds file-level documentation. You should also add documentation for:
The exact documentation will depend on the actual implementation details of your LinkedList class, which I cannot see in the provided code snippet.
Example of class-level documentation to add: