00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef API_LINKED_LIST_H
00023 #define API_LINKED_LIST_H
00024
00025 #include <QDebug>
00026
00027 #define apill_foreach(variable, upcasttype, apillist) \
00028 for(APILinkedListNode* apillnode = apillist.first(); apillnode!=0; apillnode = apillnode->next) \
00029 if (variable = (upcasttype*)apillnode) \
00030
00031
00032 class APILinkedListNode
00033 {
00034 public:
00035 APILinkedListNode () : next(0) {}
00036 virtual ~APILinkedListNode () {}
00037 APILinkedListNode* next;
00038 virtual bool is_smaller_then(APILinkedListNode* node) = 0;
00039 };
00040
00041 class APILinkedList
00042 {
00043 public:
00044 APILinkedList() : m_size(0), m_head(0), m_last(0) {}
00045 ~APILinkedList() {}
00046
00047 void append(APILinkedListNode* item);
00048 void add_and_sort(APILinkedListNode* node);
00049 void prepend(APILinkedListNode * item);
00050 int remove(APILinkedListNode* item);
00051
00052 APILinkedListNode* first() const {return m_head;}
00053 APILinkedListNode* last() const {return m_last;}
00054 int size() const {return m_size;}
00055 void clear() {m_head = 0; m_last = 0; m_size=0;}
00056 void sort(APILinkedListNode* node);
00057 bool isEmpty() {return m_size == 0 ? true : false;}
00058 int indexOf(APILinkedListNode* node);
00059 APILinkedListNode* at(int i);
00060
00061 private:
00062 int m_size;
00063 APILinkedListNode* m_head;
00064 APILinkedListNode* m_last;
00065 APILinkedListNode* slow_last() const;
00066
00067 void insert(APILinkedListNode* after, APILinkedListNode* item);
00068 };
00069
00070
00071 inline void APILinkedList::prepend(APILinkedListNode * item)
00072 {
00073 item->next = m_head;
00074 m_head = item;
00075 if (!m_size) {
00076 m_last = item;
00077 m_last->next = 0;
00078 }
00079 m_size++;
00080 Q_ASSERT(m_last == slow_last());
00081 }
00082
00083
00084 inline void APILinkedList::append(APILinkedListNode * item)
00085 {
00086 if(m_head) {
00087 m_last->next = item;
00088 m_last = item;
00089 } else {
00090 m_head = item;
00091 m_last = item;
00092 }
00093
00094 m_last->next = 0;
00095 m_size++;
00096
00097 Q_ASSERT(m_last == slow_last());
00098 }
00099
00100
00101 inline int APILinkedList::remove(APILinkedListNode * item)
00102 {
00103 Q_ASSERT(item);
00104
00105 if (!m_size) {
00106 return 0;
00107 }
00108
00109 if(m_head == item) {
00110 m_head = m_head->next;
00111 m_size--;
00112 if (m_size == 0) {
00113 m_last = m_head = 0;
00114 }
00115 Q_ASSERT(m_last == slow_last());
00116 return 1;
00117 }
00118
00119 APILinkedListNode *q,*r;
00120 r = m_head;
00121 q = m_head->next;
00122
00123 while( q!=0 ) {
00124 if( q == item ) {
00125 r->next = q->next;
00126 m_size--;
00127 if (!q->next) {
00128 m_last = r;
00129 m_last->next = 0;
00130 }
00131 Q_ASSERT(m_last == slow_last());
00132 return 1;
00133 }
00134
00135 r = q;
00136 q = q->next;
00137 }
00138
00139 return 0;
00140 }
00141
00142
00143 inline void APILinkedList::insert(APILinkedListNode* after, APILinkedListNode* item)
00144 {
00145 Q_ASSERT(after);
00146 Q_ASSERT(item);
00147
00148 APILinkedListNode* temp;
00149
00150 temp = item;
00151 temp->next = after->next;
00152 after->next = temp;
00153
00154 if (after == m_last) {
00155 m_last = item;
00156 m_last->next = 0;
00157 }
00158 m_size++;
00159
00160 Q_ASSERT(m_last == slow_last());
00161 }
00162
00163
00164 inline APILinkedListNode * APILinkedList::slow_last() const
00165 {
00166 if (!m_size) {
00167 return 0;
00168 }
00169
00170 APILinkedListNode* last = m_head;
00171
00172 while(last->next) {
00173 last = last->next;
00174 }
00175
00176 return last;
00177 }
00178
00179
00180 inline void APILinkedList::add_and_sort(APILinkedListNode * node)
00181 {
00182 if (!m_size) {
00183 append(node);
00184 } else {
00185 APILinkedListNode* q = m_head;
00186 if (node->is_smaller_then(q)) {
00187 prepend(node);
00188 } else {
00189 APILinkedListNode* afternode = q;
00190 while (q) {
00191 if (!node->is_smaller_then(q)) {
00192 afternode = q;
00193 } else {
00194 break;
00195 }
00196 q = q->next;
00197 }
00198 insert(afternode, node);
00199 }
00200 }
00201 }
00202
00203 inline int APILinkedList::indexOf(APILinkedListNode* node)
00204 {
00205 Q_ASSERT(node);
00206 int index = 0;
00207
00208 APILinkedListNode* q = m_head;
00209 while (q) {
00210 if (q == node) {
00211 return index;
00212 }
00213 ++index;
00214 q = q->next;
00215 }
00216 return -1;
00217 }
00218
00219 inline APILinkedListNode * APILinkedList::at(int i)
00220 {
00221 Q_ASSERT(i >= 0);
00222 Q_ASSERT(i < m_size);
00223
00224 int loopcounter = 0;
00225 APILinkedListNode* q = m_head;
00226 while (q) {
00227 if (loopcounter == i) {
00228 return q;
00229 }
00230 q = q->next;
00231 ++loopcounter;
00232 }
00233 return 0;
00234 }
00235
00236 inline void APILinkedList::sort(APILinkedListNode* node)
00237 {
00238 if (remove(node)) {
00239 add_and_sort(node);
00240 }
00241 }
00242
00243 #endif