1912116 remove getMaxTrans and getMaxThreads from Config
[csql.git] / include / Util.h
blob6df4e0802c55ac35482c322926f817c33cb4cc7c
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #ifndef UTIL_H
17 #define UTIL_H
18 #include<ErrorType.h>
19 #include<Debug.h>
20 class ListNode
22 public:
23 void *element;
24 ListNode *next;
28 class ListIterator
30 ListNode *iter;
31 ListNode *start;
32 ListIterator();
33 public:
35 ListIterator(ListNode *head) { iter = head; start = head; }
37 bool hasElement()
39 if (iter == NULL) return false; else return true;
42 void reset()
44 iter = start;
46 //isRemove ->the node needs to deleted after returning
47 void* nextElement(bool isRemove = false)
49 if (iter == NULL) return NULL;
50 ListNode *node = iter;
51 iter = iter ->next;
52 return node->element;
55 //index start with one, such that 1->first element in list
56 void* getElement(int index)
58 ListNode *localIter = start;
59 if (localIter == NULL) return NULL;
60 for (int i=0; i <index; i++) {
61 localIter = localIter->next;
62 if (localIter == NULL) break;
64 return localIter->element;
68 class Identifier
70 public:
71 char name[IDENTIFIER_LENGTH];
74 class List
76 ListNode *head;
77 public:
78 List() { head = NULL;}
80 DbRetVal append(void *elem)
82 ListNode *newNode = new ListNode();
83 newNode->element = elem;
84 newNode->next = NULL;
85 //If this is the first node, set it as head
86 if (NULL == head) { head = newNode; return OK; }
88 ListNode *iter = head;
89 while (NULL != iter->next) iter = iter->next;
90 iter->next = newNode;
91 return OK;
93 //Warning:Try to avoid using this method while using the iterator.The behavior
94 //is undefined. Instead set flag isRemove to yes and call nextElement of iterator.
95 DbRetVal remove(void *elem)
97 if (NULL == head)
99 printError(ErrNotExists, "There are no elements in the list. Empty list");
100 return ErrNotExists;
102 ListNode *iter = head, *prev = head;
103 while (iter != NULL)
105 if (elem == iter->element)
107 prev->next = iter->next;
108 delete iter;
109 if (iter == head) { head = NULL; return OK;}
110 return OK;
112 prev = iter;
113 iter = iter->next;
115 printError(ErrNotFound, "There are no elements in the list");
116 return ErrNotFound;
119 //index start with one, such that 1->first element in list
120 void* get(int index)
122 ListNode *localIter = head;
123 if (localIter == NULL) return NULL;
124 for (int i=0; i <index -1; i++) {
125 localIter = localIter->next;
126 if (localIter == NULL) break;
128 return localIter->element;
132 bool exists(void *elem)
134 ListNode *iter = head;
135 while (iter != NULL)
137 if (elem == iter->element)
139 return true;
141 iter = iter->next;
143 return false;
146 ListIterator getIterator()
148 ListIterator iter(head);
149 return iter;
151 void reset()
153 if (NULL == head) return;
154 ListNode *iter = head, *prevIter = head;
155 while (iter->next != NULL)
157 prevIter = iter;
158 iter = iter->next;
159 delete prevIter;
162 delete iter;
163 head = NULL;
164 return;
166 int size()
168 int count =1;
169 if (NULL == head) return 0;
170 ListNode *iter = head;
171 while (iter->next != NULL)
173 count++;
174 iter = iter->next;
176 return count;
180 class UniqueID
182 int startID;
183 Mutex mutex;
184 public:
185 UniqueID() { startID = 1; mutex.init(); }
186 int getID()
188 if (mutex.getLock(-1, false) != 0) return 0;
189 startID++;
190 mutex.releaseLock(-1, false);
191 return startID;
195 #endif