X-Git-Url: https://jfr.im/git/irc/gameservirc.git/blobdiff_plain/0a1518faa49d2d19f189a0abfbf539c14a7c2f64..dd5841348a50279de4398d21394930fbd2479f80:/gameserv/list.h diff --git a/gameserv/list.h b/gameserv/list.h index 8a9287c..4053e4a 100644 --- a/gameserv/list.h +++ b/gameserv/list.h @@ -1,23 +1,32 @@ #ifndef LIST_H #define LIST_H -#include +#include #include #include "listnode.h" #include "aClient.h" +#include "extern.h" +#include "options.h" + +using std::cout; +using std::endl; +using std::flush; template class List { public: List(); //constructor ~List(); //deconstructor - void insertAtFront( const T & ); - void insertAtBack( T *&); + T *insertAtFront( const T & ); + T *insertAtBack( T *&); + ListNode *insertAtBack_RLN( T *&); bool removeFromFront( T & ); bool removeFromBack( T & ); - bool remove( T * ); + bool del( T *); + ListNode < T > *remove( T * ); bool isEmpty() const; void print() const; + ListNode < T > *Find( T * ); ListNode < T > *First() { return firstPtr; }; ListNode < T > *Last() { return lastPtr; }; private: @@ -35,7 +44,9 @@ List::~List() { if (!isEmpty()) { - cout << "Destroying Nodes" << endl; + #ifdef DEBUGMODE + log("Destroying Nodes"); + #endif ListNode *currentPtr = firstPtr, *tempPtr; @@ -43,19 +54,21 @@ List::~List() { tempPtr = currentPtr; currentPtr = currentPtr->Next(); -// if (!tempPtr->getData()->stats || tempPtr->getData()->stats->started == 0) - cout << "Deleting Memory address: " << tempPtr->getData() << endl << flush; + + #ifdef DEBUGMODE + log("Deleting Memory address: %s", tempPtr->getData()); + #endif delete tempPtr; -// else -// tempPtr->getData()->stats->started = 0; } - cout << "All Nodes destroyed" << endl; + #ifdef DEBUGMODE + log("All Nodes destroyed"); + #endif } } template -void List::insertAtFront( const T &value ) +T *List::insertAtFront( const T &value ) { ListNode *newPtr = getNewNode ( value ); @@ -68,10 +81,29 @@ void List::insertAtFront( const T &value ) firstPtr->prev = newPtr; firstPtr = newPtr; } + return firstPtr->getData(); +} + +template +T *List::insertAtBack(T *&value ) +{ + ListNode *newPtr = getNewNode(*value); + + if (isEmpty()) + { + firstPtr = lastPtr = newPtr; + } + else + { + newPtr->prev = lastPtr; + lastPtr->next = newPtr; + lastPtr = newPtr; + } + return lastPtr->getData(); } template -void List::insertAtBack(T *&value ) +ListNode *List::insertAtBack_RLN(T *&value ) { ListNode *newPtr = getNewNode(*value); @@ -85,6 +117,7 @@ void List::insertAtBack(T *&value ) lastPtr->next = newPtr; lastPtr = newPtr; } + return lastPtr; } @@ -142,7 +175,24 @@ ListNode *List::getNewNode( const T &value) return ptr; } +template +ListNode *List::Find( T *value ) +{ + if (isEmpty()) {return NULL;} + + ListNode *currentPtr; + currentPtr = firstPtr; + while (currentPtr) + { + cout << currentPtr->getData() << endl << value << endl; + if (currentPtr->getData() == value) + return currentPtr; + currentPtr = currentPtr->Next(); + } + return NULL; +} +#ifdef DEBUGMODE template void List::print() const { @@ -156,19 +206,31 @@ void List::print() const currentPtr = firstPtr; while (currentPtr) { - cout << "aClient: " << currentPtr->getData() << flush; + cout << "aClient: " << *currentPtr->getData() << flush; if (currentPtr->getData()->stats) - cout << " Player Name: " << ¤tPtr->getData()->stats->name - << " Password: " << ¤tPtr->getData()->stats->password << flush; + cout << " Player Name: " << currentPtr->getData()->stats->name + << " Password: " << currentPtr->getData()->stats->password << flush; cout << endl; currentPtr = currentPtr->next; } +} +#endif +template +bool List::del(T *remPtr) +{ + ListNode *removed = remove( remPtr ); + if (removed != NULL) + { + delete removed; + return true; + } + return false; } template -bool List::remove(T *remPtr) +ListNode *List::remove(T *remPtr) { ListNode *newPtr = firstPtr; T *testPtr; @@ -183,41 +245,42 @@ bool List::remove(T *remPtr) { if (firstPtr == lastPtr) { - cout << "One Element. Deleting it" << endl << flush; + #ifdef DEBUGMODE + log("One Element. Deleting it"); + #endif firstPtr = lastPtr = NULL; - delete newPtr; - return true; + return newPtr; } else if (newPtr != lastPtr && newPtr != firstPtr) { - cout << "Many elements, this one is in the middle. Deleting it" - << ", linking front to back, and back to front." << endl << flush; + #ifdef DEBUGMODE + log("Many elements, this one is in the middle. Deleting it, linking front to back, and back to front."); + #endif newPtr->prev->next = newPtr->next; newPtr->next->prev = newPtr->prev; - delete newPtr; - return true; + return newPtr; } else if (newPtr == lastPtr) { - cout << "This was the last element. Deleting it, and pointing the tail to " - << "its previous element." << endl << flush; + #ifdef DEBUGMODE + log("This was the last element. Deleting it, and pointing the tail to its previous element."); + #endif lastPtr = newPtr->prev; lastPtr->next = 0; - delete newPtr; - return true; + return newPtr; } else if (newPtr == firstPtr) { - cout << "This was the first element. Deleting it, and pointing the head to " - << "its next element." << endl << flush; + #ifdef DEBUGMODE + log("This was the first element. Deleting it, and pointing the head to its next element."); + #endif firstPtr = newPtr->next; firstPtr->prev = 0; - delete newPtr; - return true; + return newPtr; } } newPtr = newPtr->next; } - return false; + return NULL; } #endif