[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 |
|
---|
[d3347e] | 11 | class atom;
|
---|
| 12 |
|
---|
[f66195] | 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;
|
---|
[46d958] | 22 | if (vorher != 0)
|
---|
[f66195] | 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 | {
|
---|
[46d958] | 35 | if (walker->next != 0)
|
---|
[f66195] | 36 | walker->next->previous = walker->previous;
|
---|
[46d958] | 37 | if (walker->previous != 0)
|
---|
[f66195] | 38 | walker->previous->next = walker->next;
|
---|
[46d958] | 39 | walker->next = 0;
|
---|
| 40 | walker->previous= 0;
|
---|
[f66195] | 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 | {
|
---|
[46d958] | 50 | if (end != 0) {
|
---|
[f66195] | 51 | link(pointer, end);
|
---|
| 52 | } else {
|
---|
[46d958] | 53 | pointer->previous = 0;
|
---|
| 54 | pointer->next = 0;
|
---|
[f66195] | 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
|
---|
[46d958] | 63 | * \return X - if found, 0 - if not found
|
---|
[f66195] | 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 | }
|
---|
[46d958] | 72 | return 0;
|
---|
[f66195] | 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 | {
|
---|
[46d958] | 81 | if (walker != 0) {
|
---|
[f66195] | 82 | unlink(walker);
|
---|
| 83 | delete(walker);
|
---|
[46d958] | 84 | walker = 0;
|
---|
[f66195] | 85 | }
|
---|
| 86 | };
|
---|
| 87 |
|
---|
[46d958] | 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 |
|
---|
[f66195] | 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
|
---|
[46d958] | 111 | if (walker != 0)
|
---|
[f66195] | 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;
|
---|
[46d958] | 126 | X *walker = 0;
|
---|
[f66195] | 127 | while (pointer != end) { // go through list
|
---|
| 128 | walker = pointer; // mark current
|
---|
| 129 | pointer = pointer->next; // step onward beforehand
|
---|
| 130 | // remove walker
|
---|
[266237] | 131 | removewithoutcheck(walker);
|
---|
[f66195] | 132 | }
|
---|
| 133 | return true;
|
---|
| 134 | };
|
---|
| 135 |
|
---|
| 136 | #endif /* LISTS_HPP_ */
|
---|