[f66195] | 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;
|
---|
[266237] | 37 | walker->next = NULL;
|
---|
| 38 | walker->previous= NULL;
|
---|
[f66195] | 39 | };
|
---|
| 40 |
|
---|
| 41 | /** Adds new item before an item \a *end in a list.
|
---|
| 42 | * \param *pointer item to be added
|
---|
| 43 | * \param *end end of list
|
---|
| 44 | * \return true - addition succeeded, false - unable to add item to list
|
---|
| 45 | */
|
---|
| 46 | template <typename X> bool add(X *pointer, X *end)
|
---|
| 47 | {
|
---|
| 48 | if (end != NULL) {
|
---|
| 49 | link(pointer, end);
|
---|
| 50 | } else {
|
---|
| 51 | pointer->previous = NULL;
|
---|
| 52 | pointer->next = NULL;
|
---|
| 53 | }
|
---|
| 54 | return true;
|
---|
| 55 | };
|
---|
| 56 |
|
---|
| 57 | /** Finds item in list
|
---|
| 58 | * \param *suche search criteria
|
---|
| 59 | * \param *start begin of list
|
---|
| 60 | * \param *end end of list
|
---|
| 61 | * \return X - if found, NULL - if not found
|
---|
| 62 | */
|
---|
| 63 | template <typename X, typename Y> X * find(Y *suche, X *start, X *end)
|
---|
| 64 | {
|
---|
| 65 | X *walker = start;
|
---|
| 66 | while (walker->next != end) { // go through list
|
---|
| 67 | walker = walker->next; // step onward beforehand
|
---|
| 68 | if (*walker->sort == *suche) return (walker);
|
---|
| 69 | }
|
---|
| 70 | return NULL;
|
---|
| 71 | };
|
---|
| 72 |
|
---|
| 73 | /** Removes an item from the list without check.
|
---|
| 74 | * \param *walker item to be removed
|
---|
| 75 | * \return true - removing succeeded, false - given item not found in list
|
---|
| 76 | */
|
---|
| 77 | template <typename X> void removewithoutcheck(X *walker)
|
---|
| 78 | {
|
---|
| 79 | if (walker != NULL) {
|
---|
| 80 | unlink(walker);
|
---|
| 81 | delete(walker);
|
---|
| 82 | walker = NULL;
|
---|
| 83 | }
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | /** Removes an item from the list, checks if exists.
|
---|
| 87 | * Checks beforehand if atom is really within molecule list.
|
---|
| 88 | * \param *pointer item to be removed
|
---|
| 89 | * \param *start begin of list
|
---|
| 90 | * \param *end end of list
|
---|
| 91 | * \return true - removing succeeded, false - given item not found in list
|
---|
| 92 | */
|
---|
| 93 | template <typename X> bool remove(X *pointer, X *start, X *end)
|
---|
| 94 | {
|
---|
| 95 | X *walker = find (pointer->sort, start, end);
|
---|
| 96 | /* while (walker->next != pointer) { // search through list
|
---|
| 97 | walker = walker->next;
|
---|
| 98 | if (walker == end) return false; // item not found in list
|
---|
| 99 | }*/
|
---|
| 100 | // atom found, now unlink
|
---|
| 101 | if (walker != NULL)
|
---|
| 102 | removewithoutcheck(walker);
|
---|
| 103 | else
|
---|
| 104 | return false;
|
---|
| 105 | return true;
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 | /** Cleans the whole list.
|
---|
| 109 | * \param *start begin of list
|
---|
| 110 | * \param *end end of list
|
---|
| 111 | * \return true - list was cleaned successfully, false - error in list structure
|
---|
| 112 | */
|
---|
| 113 | template <typename X> bool cleanup(X *start, X *end)
|
---|
| 114 | {
|
---|
| 115 | X *pointer = start->next;
|
---|
[266237] | 116 | X *walker = NULL;
|
---|
[f66195] | 117 | while (pointer != end) { // go through list
|
---|
| 118 | walker = pointer; // mark current
|
---|
| 119 | pointer = pointer->next; // step onward beforehand
|
---|
| 120 | // remove walker
|
---|
[266237] | 121 | removewithoutcheck(walker);
|
---|
[f66195] | 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_ */
|
---|