1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
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. *
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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
44 ListIterator(ListNode
*head
) { iter
= head
; start
= head
; }
48 if (iter
== NULL
) return false; else return true;
55 //isRemove ->the node needs to deleted after returning
56 void* nextElement(bool isRemove
= false)
58 if (iter
== NULL
) return NULL
;
59 ListNode
*node
= iter
;
63 //index start with one, such that 1->first element in list
64 void* getElement(int index
)
66 ListNode
*localIter
= start
;
67 if (localIter
== NULL
) return NULL
;
68 for (int i
=0; i
<index
; i
++) {
69 localIter
= localIter
->next
;
70 if (localIter
== NULL
) break;
72 return localIter
->element
;
79 char name
[IDENTIFIER_LENGTH
];
87 List() { head
= NULL
; totalElements
= 0;}
89 DbRetVal
append(void *elem
)
91 ListNode
*newNode
= new ListNode();
92 newNode
->element
= elem
;
95 //If this is the first node, set it as head
96 if (NULL
== head
) { head
= newNode
; return OK
; }
98 ListNode
*iter
= head
;
99 while (NULL
!= iter
->next
) iter
= iter
->next
;
100 iter
->next
= newNode
;
103 //Warning:Try to avoid using this method while using the iterator.The behavior
104 //is undefined. Instead set flag isRemove to yes and call nextElement of iterator.
105 DbRetVal
remove(void *elem
, bool err
=true)
110 printError(ErrNotExists
, "There are no elements in the list. Empty list");
113 ListNode
*iter
= head
, *prev
= head
;
116 if (elem
== iter
->element
)
118 if (iter
== head
) { head
= iter
->next
; delete iter
; return OK
;}
119 prev
->next
= iter
->next
;
128 printError(ErrNotFound
, "There are no elements in the list");
132 //index start with one, such that 1->first element in list
135 ListNode
*localIter
= head
;
136 if (localIter
== NULL
) return NULL
;
137 for (int i
=0; i
<index
-1; i
++) {
138 localIter
= localIter
->next
;
139 if (localIter
== NULL
) break;
141 return localIter
->element
;
145 bool exists(void *elem
)
147 ListNode
*iter
= head
;
150 if (elem
== iter
->element
)
159 ListIterator
getIterator()
161 ListIterator
iter(head
);
166 if (NULL
== head
) return;
167 ListNode
*iter
= head
, *prevIter
= head
;
168 while (iter
->next
!= NULL
)
182 return totalElements
;
186 #define MAX_UNIQUE_ID 10
191 GlobalUniqueID() { ptr
= NULL
; }
194 DbRetVal
close() { os::shm_detach(ptr
); return OK
; }
196 int getID(UniqueIDType type
);
204 UniqueID() { startID
= 1; mutex
.init(); }
209 startID
= id
;mutex
.init();
215 //TODO::change mutex to atomic increment instruction
216 if (mutex
.getLock(-1, false) != 0) return 0;
218 mutex
.releaseLock(-1, false);