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