include/Util.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Prabakaran Thirumalai   *
00003  *   praba_tuty@yahoo.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #ifndef UTIL_H
00017 #define UTIL_H
00018 #include<ErrorType.h>
00019 #include<Debug.h>
00020 class ListNode
00021 {
00022     public:
00023     void *element;
00024     ListNode *next;
00025     
00026 };
00027 
00028 class ListIterator
00029 {
00030     ListNode *iter;
00031     ListNode *start;
00032     ListIterator();
00033     public:
00034 
00035     ListIterator(ListNode *head) { iter = head; start = head; }
00036 
00037     bool hasElement()
00038     {
00039         if (iter == NULL) return false; else return true;
00040     }
00041 
00042     void reset()
00043     {
00044         iter = start;
00045     }
00046     //isRemove ->the node needs to deleted after returning
00047     void* nextElement(bool isRemove = false) 
00048     {
00049         if (iter == NULL) return NULL;
00050         ListNode *node = iter;
00051         iter = iter ->next;
00052         return node->element;
00053     }
00054 
00055     //index start with one, such that 1->first element in list
00056     void* getElement(int index)
00057     {
00058         ListNode *localIter = start;
00059         if (localIter == NULL) return NULL;
00060         for (int i=0; i <index; i++) {
00061            localIter = localIter->next;
00062            if (localIter == NULL) break;
00063         }
00064         return localIter->element;
00065     }
00066 };
00067 
00068 class Identifier
00069 {
00070     public:
00071     char name[IDENTIFIER_LENGTH];
00072 };
00073 
00074 class List
00075 {
00076     ListNode *head;
00077     int totalElements;
00078     public:
00079     List() { head = NULL; totalElements = 0;}
00080 
00081     DbRetVal append(void *elem)
00082     {
00083         ListNode *newNode = new ListNode();
00084         newNode->element = elem;
00085         newNode->next = NULL;
00086         totalElements++;
00087         //If this is the first node, set it as head
00088         if (NULL == head) { head = newNode; return OK; }
00089 
00090         ListNode *iter = head;
00091         while (NULL != iter->next) iter = iter->next;
00092         iter->next = newNode;
00093         return OK;
00094     }
00095     //Warning:Try to avoid using this method while using the iterator.The behavior 
00096     //is undefined. Instead set flag isRemove to yes and call nextElement of iterator.
00097     DbRetVal remove(void *elem) 
00098     {
00099         if (NULL == head)
00100         {
00101             printError(ErrNotExists, "There are no elements in the list. Empty list");
00102             return ErrNotExists;
00103         }
00104         ListNode *iter = head, *prev = head;
00105         while (iter != NULL)
00106         {
00107             if (elem == iter->element)
00108             {
00109                 prev->next = iter->next;
00110                 delete iter;
00111                 totalElements--;
00112                 if (iter == head) { head = NULL; return OK;}
00113                 return OK;
00114             }
00115             prev = iter;
00116             iter = iter->next;
00117         }
00118         printError(ErrNotFound, "There are no elements in the list");
00119         return ErrNotFound;
00120     }
00121 
00122     //index start with one, such that 1->first element in list
00123     void* get(int index)
00124     {
00125         ListNode *localIter = head;
00126         if (localIter == NULL) return NULL;
00127         for (int i=0; i <index -1; i++) {
00128            localIter = localIter->next;
00129            if (localIter == NULL) break;
00130         }
00131         return localIter->element;
00132     }
00133 
00134 
00135     bool exists(void *elem) 
00136     {
00137         ListNode *iter = head;
00138         while (iter != NULL)
00139         {
00140             if (elem == iter->element)
00141             {
00142                 return true;
00143             }
00144             iter = iter->next;
00145         }
00146         return false;
00147     }
00148 
00149     ListIterator getIterator()
00150     {
00151         ListIterator iter(head);
00152         return iter;
00153     }
00154     void reset()
00155     {
00156         if (NULL == head) return;
00157         ListNode *iter = head, *prevIter = head;
00158         while (iter->next != NULL)
00159         {
00160             prevIter = iter;
00161             iter = iter->next;
00162             delete prevIter;
00163 
00164         }
00165         delete iter;
00166         head = NULL;
00167         totalElements = 0;
00168         return;
00169     }
00170     int size()
00171     {
00172         return totalElements;
00173     }
00174 
00175 };
00176 class UniqueID
00177 {
00178    int startID;
00179    Mutex mutex;
00180    public:
00181    UniqueID() { startID = 1; mutex.init(); }
00182    int getID()
00183    {
00184       //TODO::change mutex to atomic increment instruction
00185       if (mutex.getLock(-1, false) != 0) return 0;
00186       startID++;
00187       mutex.releaseLock(-1, false); 
00188       return startID;
00189    }
00190 };
00191 
00192 #endif

Generated on Mon Jun 9 22:37:14 2008 for csql by  doxygen 1.4.7