Testing NULL values in Update Statements
[csql.git] / include / Util.h
blobceacc4a35f13541e7488cf1dc8bf2850fa4aa378
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 public:
33 ListIterator(){}
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 int totalElements;
78 public:
79 List() { head = NULL; totalElements = 0;}
81 DbRetVal append(void *elem)
83 ListNode *newNode = new ListNode();
84 newNode->element = elem;
85 newNode->next = NULL;
86 totalElements++;
87 //If this is the first node, set it as head
88 if (NULL == head) { head = newNode; return OK; }
90 ListNode *iter = head;
91 while (NULL != iter->next) iter = iter->next;
92 iter->next = newNode;
93 return OK;
95 //Warning:Try to avoid using this method while using the iterator.The behavior
96 //is undefined. Instead set flag isRemove to yes and call nextElement of iterator.
97 DbRetVal remove(void *elem, bool err=true)
99 if (NULL == head)
101 if (err)
102 printError(ErrNotExists, "There are no elements in the list. Empty list");
103 return ErrNotExists;
105 ListNode *iter = head, *prev = head;
106 while (iter != NULL)
108 if (elem == iter->element)
110 if (iter == head) { head = iter->next; delete iter; return OK;}
111 prev->next = iter->next;
112 delete iter;
113 totalElements--;
114 return OK;
116 prev = iter;
117 iter = iter->next;
119 if (err)
120 printError(ErrNotFound, "There are no elements in the list");
121 return ErrNotFound;
124 //index start with one, such that 1->first element in list
125 void* get(int index)
127 ListNode *localIter = head;
128 if (localIter == NULL) return NULL;
129 for (int i=0; i <index -1; i++) {
130 localIter = localIter->next;
131 if (localIter == NULL) break;
133 return localIter->element;
137 bool exists(void *elem)
139 ListNode *iter = head;
140 while (iter != NULL)
142 if (elem == iter->element)
144 return true;
146 iter = iter->next;
148 return false;
151 ListIterator getIterator()
153 ListIterator iter(head);
154 return iter;
156 void reset()
158 if (NULL == head) return;
159 ListNode *iter = head, *prevIter = head;
160 while (iter->next != NULL)
162 prevIter = iter;
163 iter = iter->next;
164 delete prevIter;
167 delete iter;
168 head = NULL;
169 totalElements = 0;
170 return;
172 int size()
174 return totalElements;
178 class UniqueID
180 int startID;
181 Mutex mutex;
182 public:
183 UniqueID() { startID = 1; mutex.init(); }
186 void setID(int id)
188 startID = id;mutex.init();
192 int getID()
194 //TODO::change mutex to atomic increment instruction
195 if (mutex.getLock(-1, false) != 0) return 0;
196 startID++;
197 mutex.releaseLock(-1, false);
198 return startID;
202 #endif