| 1 | /*
 | 
|---|
| 2 |  * lists.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Oct 9, 2009
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef LISTS_HPP_
 | 
|---|
| 9 | #define LISTS_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | /******************************** Some templates for list management ***********************************/
 | 
|---|
| 12 | 
 | 
|---|
| 13 | /** Adds linking of an item to a list.
 | 
|---|
| 14 |  * \param *walker
 | 
|---|
| 15 |  * \return true - adding succeeded, false - error in list
 | 
|---|
| 16 |  */
 | 
|---|
| 17 | template <typename X> void link(X *walker, X *end)
 | 
|---|
| 18 | {
 | 
|---|
| 19 |   X *vorher = end->previous;
 | 
|---|
| 20 |   if (vorher != NULL)
 | 
|---|
| 21 |     vorher->next = walker;
 | 
|---|
| 22 |   end->previous = walker;
 | 
|---|
| 23 |   walker->previous = vorher;
 | 
|---|
| 24 |   walker->next = end;
 | 
|---|
| 25 | };
 | 
|---|
| 26 | 
 | 
|---|
| 27 | /** Removes linking of an item in a list.
 | 
|---|
| 28 |  * \param *walker
 | 
|---|
| 29 |  * \return true - removing succeeded, false - given item not found in list
 | 
|---|
| 30 |  */
 | 
|---|
| 31 | template <typename X> void unlink(X *walker)
 | 
|---|
| 32 | {
 | 
|---|
| 33 |   if (walker->next != NULL)
 | 
|---|
| 34 |     walker->next->previous = walker->previous;
 | 
|---|
| 35 |   if (walker->previous != NULL)
 | 
|---|
| 36 |     walker->previous->next = walker->next;
 | 
|---|
| 37 | };
 | 
|---|
| 38 | 
 | 
|---|
| 39 | /** Adds new item before an item \a *end in a list.
 | 
|---|
| 40 |  * \param *pointer   item to be added
 | 
|---|
| 41 |  * \param *end  end of list
 | 
|---|
| 42 |  * \return true - addition succeeded, false - unable to add item to list
 | 
|---|
| 43 |  */
 | 
|---|
| 44 | template <typename X>  bool add(X *pointer, X *end)
 | 
|---|
| 45 | {
 | 
|---|
| 46 |   if (end != NULL) {
 | 
|---|
| 47 |     link(pointer, end);
 | 
|---|
| 48 |   } else {
 | 
|---|
| 49 |     pointer->previous = NULL;
 | 
|---|
| 50 |     pointer->next = NULL;
 | 
|---|
| 51 |   }
 | 
|---|
| 52 |   return true;
 | 
|---|
| 53 | };
 | 
|---|
| 54 | 
 | 
|---|
| 55 | /** Finds item in list
 | 
|---|
| 56 |  * \param *suche  search criteria
 | 
|---|
| 57 |  * \param *start  begin of list
 | 
|---|
| 58 |  * \param *end  end of list
 | 
|---|
| 59 |  * \return X - if found, NULL - if not found
 | 
|---|
| 60 |  */
 | 
|---|
| 61 | template <typename X, typename Y> X * find(Y *suche, X *start, X *end)
 | 
|---|
| 62 | {
 | 
|---|
| 63 |   X *walker = start;
 | 
|---|
| 64 |   while (walker->next != end) { // go through list
 | 
|---|
| 65 |     walker = walker->next; // step onward beforehand
 | 
|---|
| 66 |     if (*walker->sort == *suche) return (walker);
 | 
|---|
| 67 |   }
 | 
|---|
| 68 |   return NULL;
 | 
|---|
| 69 | };
 | 
|---|
| 70 | 
 | 
|---|
| 71 | /** Removes an item from the list without check.
 | 
|---|
| 72 |  * \param *walker item to be removed
 | 
|---|
| 73 |  * \return true - removing succeeded, false - given item not found in list
 | 
|---|
| 74 |  */
 | 
|---|
| 75 | template <typename X> void removewithoutcheck(X *walker)
 | 
|---|
| 76 | {
 | 
|---|
| 77 |   if (walker != NULL) {
 | 
|---|
| 78 |     unlink(walker);
 | 
|---|
| 79 |     delete(walker);
 | 
|---|
| 80 |     walker = NULL;
 | 
|---|
| 81 |   }
 | 
|---|
| 82 | };
 | 
|---|
| 83 | 
 | 
|---|
| 84 | /** Removes an item from the list, checks if exists.
 | 
|---|
| 85 |  * Checks beforehand if atom is really within molecule list.
 | 
|---|
| 86 |  * \param *pointer   item to be removed
 | 
|---|
| 87 |  * \param *start  begin of list
 | 
|---|
| 88 |  * \param *end  end of list
 | 
|---|
| 89 |  * \return true - removing succeeded, false - given item not found in list
 | 
|---|
| 90 |  */
 | 
|---|
| 91 | template <typename X> bool remove(X *pointer, X *start, X *end)
 | 
|---|
| 92 | {
 | 
|---|
| 93 |   X *walker = find (pointer->sort, start, end);
 | 
|---|
| 94 | /*  while (walker->next != pointer) { // search through list
 | 
|---|
| 95 |     walker = walker->next;
 | 
|---|
| 96 |     if (walker == end) return false;  // item not found in list
 | 
|---|
| 97 |   }*/
 | 
|---|
| 98 |   // atom found, now unlink
 | 
|---|
| 99 |   if (walker != NULL)
 | 
|---|
| 100 |     removewithoutcheck(walker);
 | 
|---|
| 101 |   else
 | 
|---|
| 102 |     return false;
 | 
|---|
| 103 |   return true;
 | 
|---|
| 104 | };
 | 
|---|
| 105 | 
 | 
|---|
| 106 | /** Cleans the whole list.
 | 
|---|
| 107 |  * \param *start begin of list
 | 
|---|
| 108 |  * \param *end end of list
 | 
|---|
| 109 |  * \return true - list was cleaned successfully, false - error in list structure
 | 
|---|
| 110 |  */
 | 
|---|
| 111 | template <typename X> bool cleanup(X *start, X *end)
 | 
|---|
| 112 | {
 | 
|---|
| 113 |   X *pointer = start->next;
 | 
|---|
| 114 |   X *walker;
 | 
|---|
| 115 |   while (pointer != end) { // go through list
 | 
|---|
| 116 |     walker = pointer; // mark current
 | 
|---|
| 117 |     pointer = pointer->next; // step onward beforehand
 | 
|---|
| 118 |     // remove walker
 | 
|---|
| 119 |     unlink(walker);
 | 
|---|
| 120 |     delete(walker);
 | 
|---|
| 121 |     walker = NULL;
 | 
|---|
| 122 |   }
 | 
|---|
| 123 |   return true;
 | 
|---|
| 124 | };
 | 
|---|
| 125 | 
 | 
|---|
| 126 | /** Returns the first marker in a chain list.
 | 
|---|
| 127 |  * \param *me one arbitrary item in chain list
 | 
|---|
| 128 |  * \return poiner to first marker
 | 
|---|
| 129 |  */
 | 
|---|
| 130 | template <typename X> X *GetFirst(X *me)
 | 
|---|
| 131 | {
 | 
|---|
| 132 |   X *Binder = me;
 | 
|---|
| 133 |   while(Binder->previous != NULL)
 | 
|---|
| 134 |     Binder = Binder->previous;
 | 
|---|
| 135 |   return Binder;
 | 
|---|
| 136 | };
 | 
|---|
| 137 | 
 | 
|---|
| 138 | /** Returns the last marker in a chain list.
 | 
|---|
| 139 |  * \param *me one arbitrary item in chain list
 | 
|---|
| 140 |  * \return poiner to last marker
 | 
|---|
| 141 |  */
 | 
|---|
| 142 | template <typename X> X *GetLast(X *me)
 | 
|---|
| 143 | {
 | 
|---|
| 144 |   X *Binder = me;
 | 
|---|
| 145 |   while(Binder->next != NULL)
 | 
|---|
| 146 |     Binder = Binder->next;
 | 
|---|
| 147 |   return Binder;
 | 
|---|
| 148 | };
 | 
|---|
| 149 | 
 | 
|---|
| 150 | #endif /* LISTS_HPP_ */
 | 
|---|