]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/list.h
I've begun to add daily news and split up gameserv.cpp a bit
[irc/gameservirc.git] / gameserv / list.h
index 384e9ada364929a5a3dbed97e67ac8ed553d0e2a..d49c0d98d1610a67cd22576cd302e4d052cd4741 100644 (file)
@@ -1,24 +1,34 @@
 #ifndef LIST_H
 #define LIST_H
 
-#include <iostream.h>
+#include <iostream>
 #include <cassert>
 #include "listnode.h"
 #include "aClient.h"
+#include "extern.h"
+#include "options.h"
 
+using std::cout;
+using std::endl;
+using std::flush;
 
 template <class T>
 class List {
     public:
        List();         //constructor
        ~List();        //deconstructor
-       void insertAtFront( const T & );
-       void insertAtBack( T *&);
+       T *insertAtFront( const T & );
+       T *insertAtBack( T *&);
+       ListNode<T> *insertAtBack_RLN( T *&);
        bool removeFromFront( T & );
        bool removeFromBack( T & );
-       bool remove( T * );
+       bool del( T *);
+       ListNode < T > *remove( T * );
        bool isEmpty() const;
+       #ifdef DEBUGMODE
        void print() const;
+       #endif
+
        ListNode < T > *Find( T * );
         ListNode < T > *First() { return firstPtr; };
        ListNode < T > *Last() { return lastPtr; };
@@ -37,7 +47,9 @@ List<T>::~List()
 {
     if (!isEmpty())
     {
-       cout << "Destroying Nodes" << endl;
+       #ifdef DEBUGMODE
+           log("Destroying Nodes");
+       #endif
 
        ListNode<T> *currentPtr = firstPtr, *tempPtr;
 
@@ -45,19 +57,21 @@ List<T>::~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<class T>
-void List<T>::insertAtFront( const T &value )
+T *List<T>::insertAtFront( const T &value )
 {
     ListNode<T> *newPtr = getNewNode ( value );
 
@@ -70,10 +84,29 @@ void List<T>::insertAtFront( const T &value )
        firstPtr->prev = newPtr;
        firstPtr = newPtr;
     }
+    return firstPtr->getData();
+}
+
+template<class T>
+T *List<T>::insertAtBack(T *&value )
+{
+    ListNode<T> *newPtr = getNewNode(*value);
+
+    if (isEmpty())
+    {
+       firstPtr = lastPtr = newPtr;
+    }
+    else
+    {
+       newPtr->prev = lastPtr;
+       lastPtr->next = newPtr;
+       lastPtr = newPtr;
+    }
+    return lastPtr->getData();
 }
 
 template<class T>
-void List<T>::insertAtBack(T *&value )
+ListNode<T> *List<T>::insertAtBack_RLN(T *&value )
 {
     ListNode<T> *newPtr = getNewNode(*value);
 
@@ -87,6 +120,7 @@ void List<T>::insertAtBack(T *&value )
        lastPtr->next = newPtr;
        lastPtr = newPtr;
     }
+    return lastPtr;
 }
 
 
@@ -153,6 +187,7 @@ ListNode<T> *List<T>::Find( T *value )
     currentPtr = firstPtr;
     while (currentPtr)
     {
+       cout << currentPtr->getData() << endl << value << endl;
        if (currentPtr->getData() == value)
            return currentPtr;
        currentPtr = currentPtr->Next();
@@ -160,6 +195,7 @@ ListNode<T> *List<T>::Find( T *value )
     return NULL;
 }
 
+#ifdef DEBUGMODE
 template <class T>
 void List<T>::print() const
 {
@@ -173,19 +209,34 @@ void List<T>::print() const
     currentPtr = firstPtr;
     while (currentPtr)
     {
+       /*
        cout << "aClient: " << *currentPtr->getData() << flush;
 
         if (currentPtr->getData()->stats)
            cout << "  Player Name: " << currentPtr->getData()->stats->name 
                 << "   Password: " << currentPtr->getData()->stats->password << flush;
        cout << endl;
+       */
+       cout << currentPtr->getData()->getString() << endl;
        currentPtr = currentPtr->next;
     }
+}
+#endif
 
+template <class T>
+bool List<T>::del(T *remPtr)
+{
+    ListNode<T> *removed = remove( remPtr );
+    if (removed != NULL)
+    {
+       delete removed;
+       return true;
+    }
+    return false;
 }
 
 template <class T>
-bool List<T>::remove(T *remPtr)
+ListNode<T> *List<T>::remove(T *remPtr)
 {
     ListNode<T> *newPtr = firstPtr;
     T *testPtr;
@@ -200,41 +251,42 @@ bool List<T>::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