4 #ifndef EXCLUDE_RUDIMENTS_TEMPLATE_IMPLEMENTATIONS
6 #ifdef RUDIMENTS_HAVE_STDLIB_H
11 #include <rudiments/private/rudimentsinlines.h>
13 #ifdef RUDIMENTS_NAMESPACE
17 #define LINKEDLIST_TEMPLATE template <class datatype, class linkedlistnodetype>
19 #define LINKEDLIST_CLASS linkedlist<datatype,linkedlistnodetype>
22 RUDIMENTS_TEMPLATE_INLINE
23 LINKEDLIST_CLASS::linkedlist() {
29 RUDIMENTS_TEMPLATE_INLINE
30 LINKEDLIST_CLASS::~linkedlist() {
35 RUDIMENTS_TEMPLATE_INLINE
36 void LINKEDLIST_CLASS::append(datatype data) {
37 linkedlistnodetype *node=
new linkedlistnodetype();
43 RUDIMENTS_TEMPLATE_INLINE
44 void LINKEDLIST_CLASS::append(linkedlistnodetype *node) {
47 node->setPrevious(last);
56 RUDIMENTS_TEMPLATE_INLINE
57 bool LINKEDLIST_CLASS::insert(uint64_t index, datatype data) {
58 linkedlistnodetype *node=
new linkedlistnodetype();
60 return insert(index,node);
64 RUDIMENTS_TEMPLATE_INLINE
65 bool LINKEDLIST_CLASS::insert(uint64_t index, linkedlistnodetype *node) {
70 first->setPrevious(node);
71 first=(linkedlistnodetype *)node;
76 linkedlistnodetype *current=getNodeByIndex(index-1);
80 node->setPrevious(current);
81 node->setNext(current->getNext());
82 current->getNext()->setPrevious(node);
83 current->setNext(node);
88 RUDIMENTS_TEMPLATE_INLINE
89 bool LINKEDLIST_CLASS::setDataByIndex(uint64_t index, datatype data) {
90 linkedlistnodetype *current=getNodeByIndex(index);
92 current->setData(data);
99 RUDIMENTS_TEMPLATE_INLINE
100 bool LINKEDLIST_CLASS::removeByIndex(uint64_t index) {
101 return removeNode(getNodeByIndex(index));
105 RUDIMENTS_TEMPLATE_INLINE
106 bool LINKEDLIST_CLASS::removeByData(datatype data) {
107 for (linkedlistnodetype *current=first; current;
108 current=(linkedlistnodetype *)current->getNext()) {
109 if (!current->compare(data)) {
110 return removeNode(current);
117 RUDIMENTS_TEMPLATE_INLINE
118 bool LINKEDLIST_CLASS::removeAllByData(datatype data) {
120 linkedlistnodetype *current=first;
121 linkedlistnodetype *next;
123 if (!current->compare(data)) {
124 next=(linkedlistnodetype *)current->getNext();
125 if (!removeNode(current)) {
130 current=(linkedlistnodetype *)current->getNext();
137 RUDIMENTS_TEMPLATE_INLINE
138 bool LINKEDLIST_CLASS::removeNode(linkedlistnodetype *node) {
142 if (node->getNext()) {
143 node->getNext()->setPrevious(node->getPrevious());
145 if (node->getPrevious()) {
146 node->getPrevious()->setNext(node->getNext());
149 first=(linkedlistnodetype *)node->getNext();
152 last=(linkedlistnodetype *)node->getPrevious();
159 RUDIMENTS_TEMPLATE_INLINE
160 bool LINKEDLIST_CLASS::getDataByIndex(uint64_t index, datatype *data) {
161 linkedlistnodetype *current=getNodeByIndex(index);
163 *data=current->getData();
170 RUDIMENTS_TEMPLATE_INLINE
171 uint64_t LINKEDLIST_CLASS::getLength()
const {
173 for (linkedlistnodetype *current=first; current;
174 current=(linkedlistnodetype *)current->getNext()) {
181 RUDIMENTS_TEMPLATE_INLINE
182 linkedlistnodetype *LINKEDLIST_CLASS::getFirstNode() {
183 return (linkedlistnodetype *)first;
187 RUDIMENTS_TEMPLATE_INLINE
188 linkedlistnodetype *LINKEDLIST_CLASS::getLastNode() {
189 return (linkedlistnodetype *)last;
193 RUDIMENTS_TEMPLATE_INLINE
194 linkedlistnodetype *LINKEDLIST_CLASS::getNodeByIndex(uint64_t index) {
195 linkedlistnodetype *current=(linkedlistnodetype *)first;
196 for (uint64_t i=0; current && i<index; i++) {
197 current=(linkedlistnodetype *)current->getNext();
203 RUDIMENTS_TEMPLATE_INLINE
204 linkedlistnodetype *LINKEDLIST_CLASS::getNodeByData(datatype data) {
205 return getNodeByData((linkedlistnodetype *)first,data);
209 RUDIMENTS_TEMPLATE_INLINE
210 linkedlistnodetype *LINKEDLIST_CLASS::getNodeByData(
211 linkedlistnodetype *startnode,
213 for (linkedlistnodetype *current=startnode; current;
214 current=(linkedlistnodetype *)current->getNext()) {
215 if (!current->compare(data)) {
223 RUDIMENTS_TEMPLATE_INLINE
224 void LINKEDLIST_CLASS::clear() {
225 linkedlistnodetype *next;
226 linkedlistnodetype *current=first;
228 next=(linkedlistnodetype *)current->getNext();
237 RUDIMENTS_TEMPLATE_INLINE
238 void LINKEDLIST_CLASS::print()
const {
240 for (linkedlistnodetype *current=first; current;
241 current=(linkedlistnodetype *)current->getNext()) {
242 printf(
"index %lld: ",i);
249 #ifdef RUDIMENTS_NAMESPACE