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 ***************************************************************************/
35 ListIterator(ListNode
*head
) { iter
= head
; start
= head
; }
39 if (iter
== NULL
) return false; else return true;
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
;
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
;
71 char name
[IDENTIFIER_LENGTH
];
79 List() { head
= NULL
; totalElements
= 0;}
81 DbRetVal
append(void *elem
)
83 ListNode
*newNode
= new ListNode();
84 newNode
->element
= elem
;
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
;
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)
102 printError(ErrNotExists
, "There are no elements in the list. Empty list");
105 ListNode
*iter
= head
, *prev
= head
;
108 if (elem
== iter
->element
)
110 if (iter
== head
) { head
= iter
->next
; delete iter
; return OK
;}
111 prev
->next
= iter
->next
;
120 printError(ErrNotFound
, "There are no elements in the list");
124 //index start with one, such that 1->first element in list
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
;
142 if (elem
== iter
->element
)
151 ListIterator
getIterator()
153 ListIterator
iter(head
);
158 if (NULL
== head
) return;
159 ListNode
*iter
= head
, *prevIter
= head
;
160 while (iter
->next
!= NULL
)
174 return totalElements
;
183 UniqueID() { startID
= 1; mutex
.init(); }
188 startID
= id
;mutex
.init();
194 //TODO::change mutex to atomic increment instruction
195 if (mutex
.getLock(-1, false) != 0) return 0;
197 mutex
.releaseLock(-1, false);