source: molecuilder/src/lists.hpp@ 5bf941

Last change on this file since 5bf941 was 7bfc19, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Made the world solely responsible for creating and destroying atoms.

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[17b3a5c]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
[7bfc19]11class atom;
12
[17b3a5c]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 */
19template <typename X> void link(X *walker, X *end)
20{
21 X *vorher = end->previous;
[7bfc19]22 if (vorher != 0)
[17b3a5c]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 */
33template <typename X> void unlink(X *walker)
34{
[7bfc19]35 if (walker->next != 0)
[17b3a5c]36 walker->next->previous = walker->previous;
[7bfc19]37 if (walker->previous != 0)
[17b3a5c]38 walker->previous->next = walker->next;
[7bfc19]39 walker->next = 0;
40 walker->previous= 0;
[17b3a5c]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 */
48template <typename X> bool add(X *pointer, X *end)
49{
[7bfc19]50 if (end != 0) {
[17b3a5c]51 link(pointer, end);
52 } else {
[7bfc19]53 pointer->previous = 0;
54 pointer->next = 0;
[17b3a5c]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
[7bfc19]63 * \return X - if found, 0 - if not found
[17b3a5c]64 */
65template <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 }
[7bfc19]72 return 0;
[17b3a5c]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 */
79template <typename X> void removewithoutcheck(X *walker)
80{
[7bfc19]81 if (walker != 0) {
[17b3a5c]82 unlink(walker);
83 delete(walker);
[7bfc19]84 walker = 0;
[17b3a5c]85 }
86};
87
[7bfc19]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 */
94template <> void removewithoutcheck<atom>(atom *walker);
95
[17b3a5c]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 */
103template <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
[7bfc19]111 if (walker != 0)
[17b3a5c]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 */
123template <typename X> bool cleanup(X *start, X *end)
124{
125 X *pointer = start->next;
[7bfc19]126 X *walker = 0;
[17b3a5c]127 while (pointer != end) { // go through list
128 walker = pointer; // mark current
129 pointer = pointer->next; // step onward beforehand
130 // remove walker
[872b51]131 removewithoutcheck(walker);
[17b3a5c]132 }
133 return true;
134};
135
136/** Returns the first marker in a chain list.
137 * \param *me one arbitrary item in chain list
138 * \return poiner to first marker
139 */
140template <typename X> X *GetFirst(X *me)
141{
142 X *Binder = me;
[7bfc19]143 while(Binder->previous != 0)
[17b3a5c]144 Binder = Binder->previous;
145 return Binder;
146};
147
148/** Returns the last marker in a chain list.
149 * \param *me one arbitrary item in chain list
150 * \return poiner to last marker
151 */
152template <typename X> X *GetLast(X *me)
153{
154 X *Binder = me;
[7bfc19]155 while(Binder->next != 0)
[17b3a5c]156 Binder = Binder->next;
157 return Binder;
158};
159
160#endif /* LISTS_HPP_ */
Note: See TracBrowser for help on using the repository browser.