| 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 | class atom;
|
|---|
| 12 |
|
|---|
| 13 | /******************************** Some templates for list management ***********************************/
|
|---|
| 14 |
|
|---|
| 15 | /** Adds linking of an item to a list.
|
|---|
| 16 | * \param *walker
|
|---|
| 17 | * \return true - adding succeeded, false - error in list
|
|---|
| 18 | */
|
|---|
| 19 | template <typename X> void link(X *walker, X *end)
|
|---|
| 20 | {
|
|---|
| 21 | X *vorher = end->previous;
|
|---|
| 22 | if (vorher != 0)
|
|---|
| 23 | vorher->next = walker;
|
|---|
| 24 | end->previous = walker;
|
|---|
| 25 | walker->previous = vorher;
|
|---|
| 26 | walker->next = end;
|
|---|
| 27 | };
|
|---|
| 28 |
|
|---|
| 29 | /** Removes linking of an item in a list.
|
|---|
| 30 | * \param *walker
|
|---|
| 31 | * \return true - removing succeeded, false - given item not found in list
|
|---|
| 32 | */
|
|---|
| 33 | template <typename X> void unlink(X *walker)
|
|---|
| 34 | {
|
|---|
| 35 | if (walker->next != 0)
|
|---|
| 36 | walker->next->previous = walker->previous;
|
|---|
| 37 | if (walker->previous != 0)
|
|---|
| 38 | walker->previous->next = walker->next;
|
|---|
| 39 | walker->next = 0;
|
|---|
| 40 | walker->previous= 0;
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | /** Adds new item before an item \a *end in a list.
|
|---|
| 44 | * \param *pointer item to be added
|
|---|
| 45 | * \param *end end of list
|
|---|
| 46 | * \return true - addition succeeded, false - unable to add item to list
|
|---|
| 47 | */
|
|---|
| 48 | template <typename X> bool add(X *pointer, X *end)
|
|---|
| 49 | {
|
|---|
| 50 | if (end != 0) {
|
|---|
| 51 | link(pointer, end);
|
|---|
| 52 | } else {
|
|---|
| 53 | pointer->previous = 0;
|
|---|
| 54 | pointer->next = 0;
|
|---|
| 55 | }
|
|---|
| 56 | return true;
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | /** Finds item in list
|
|---|
| 60 | * \param *suche search criteria
|
|---|
| 61 | * \param *start begin of list
|
|---|
| 62 | * \param *end end of list
|
|---|
| 63 | * \return X - if found, 0 - if not found
|
|---|
| 64 | */
|
|---|
| 65 | template <typename X, typename Y> X * find(Y *suche, X *start, X *end)
|
|---|
| 66 | {
|
|---|
| 67 | X *walker = start;
|
|---|
| 68 | while (walker->next != end) { // go through list
|
|---|
| 69 | walker = walker->next; // step onward beforehand
|
|---|
| 70 | if (*walker->sort == *suche) return (walker);
|
|---|
| 71 | }
|
|---|
| 72 | return 0;
|
|---|
| 73 | };
|
|---|
| 74 |
|
|---|
| 75 | /** Removes an item from the list without check.
|
|---|
| 76 | * \param *walker item to be removed
|
|---|
| 77 | * \return true - removing succeeded, false - given item not found in list
|
|---|
| 78 | */
|
|---|
| 79 | template <typename X> void removewithoutcheck(X *walker)
|
|---|
| 80 | {
|
|---|
| 81 | if (walker != 0) {
|
|---|
| 82 | unlink(walker);
|
|---|
| 83 | delete(walker);
|
|---|
| 84 | walker = 0;
|
|---|
| 85 | }
|
|---|
| 86 | };
|
|---|
| 87 |
|
|---|
| 88 | /** Removes an item from the list without check.
|
|---|
| 89 | * specialized for atoms, because these have to be removed from the world as well
|
|---|
| 90 | * the implementation for this declaration is in lists.cpp
|
|---|
| 91 | * \param *walker item to be removed
|
|---|
| 92 | * \return true - removing succeeded, false - given item not found in list
|
|---|
| 93 | */
|
|---|
| 94 | template <> void removewithoutcheck<atom>(atom *walker);
|
|---|
| 95 |
|
|---|
| 96 | /** Removes an item from the list, checks if exists.
|
|---|
| 97 | * Checks beforehand if atom is really within molecule list.
|
|---|
| 98 | * \param *pointer item to be removed
|
|---|
| 99 | * \param *start begin of list
|
|---|
| 100 | * \param *end end of list
|
|---|
| 101 | * \return true - removing succeeded, false - given item not found in list
|
|---|
| 102 | */
|
|---|
| 103 | template <typename X> bool remove(X *pointer, X *start, X *end)
|
|---|
| 104 | {
|
|---|
| 105 | X *walker = find (pointer->sort, start, end);
|
|---|
| 106 | /* while (walker->next != pointer) { // search through list
|
|---|
| 107 | walker = walker->next;
|
|---|
| 108 | if (walker == end) return false; // item not found in list
|
|---|
| 109 | }*/
|
|---|
| 110 | // atom found, now unlink
|
|---|
| 111 | if (walker != 0)
|
|---|
| 112 | removewithoutcheck(walker);
|
|---|
| 113 | else
|
|---|
| 114 | return false;
|
|---|
| 115 | return true;
|
|---|
| 116 | };
|
|---|
| 117 |
|
|---|
| 118 | /** Cleans the whole list.
|
|---|
| 119 | * \param *start begin of list
|
|---|
| 120 | * \param *end end of list
|
|---|
| 121 | * \return true - list was cleaned successfully, false - error in list structure
|
|---|
| 122 | */
|
|---|
| 123 | template <typename X> bool cleanup(X *start, X *end)
|
|---|
| 124 | {
|
|---|
| 125 | X *pointer = start->next;
|
|---|
| 126 | X *walker = 0;
|
|---|
| 127 | while (pointer != end) { // go through list
|
|---|
| 128 | walker = pointer; // mark current
|
|---|
| 129 | pointer = pointer->next; // step onward beforehand
|
|---|
| 130 | // remove walker
|
|---|
| 131 | removewithoutcheck(walker);
|
|---|
| 132 | }
|
|---|
| 133 | return true;
|
|---|
| 134 | };
|
|---|
| 135 |
|
|---|
| 136 | #endif /* LISTS_HPP_ */
|
|---|