summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.75.0/src/ListManager.h
blob: f9e928c825358eefd0a4c57a257fd222a88f746a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
 * @file ListManager.h
 * Template class that manages items in a double-linked list.
 * If C++ could do it, this would just be a class that worked on an interface.
 *
 * @author  Ben Gardner
 * @license GPL v2+
 */

#ifndef LIST_MANAGER_H_INCLUDED
#define LIST_MANAGER_H_INCLUDED

/*
 * TODO: why do we provide this template class? can't we use
 * a double linked list std::deque from the standard library ?
 */
/**
 * A simple list manager for a double-linked list.
 * Class T must define 'next' and 'prev', which must be pointers to type T.
 */
template<class T>
class ListManager
{
protected:
   T *first; //! pointer to the head of list
   T *last;  //! pointer to tail of list

private:
   // Hide copy constructor
   ListManager(const ListManager &ref)
   {
      first = nullptr;
      last  = nullptr;
   }

public:
   ListManager()
   {
      first = nullptr;
      last  = nullptr;
   }


   /**
    * @brief return the first element of the linked list
    *
    * @return pointer to first element or nullptr if list is empty
    */
   T *GetHead() const
   {
      return(first);
   }


   /**
    * @brief return the last element of the linked list
    *
    * @return pointer to last element or nullptr if list is empty
    */
   T *GetTail() const
   {
      return(last);
   }


   /**
    *  @brief return the next element of the linked list
    *
    * @param[in] ref  pointer to current list element
    *
    * @return pointer to next element or nullptr if no next element exists
    */
   T *GetNext(const T *ref) const
   {
      return((ref != nullptr) ? ref->next : nullptr);
   }


   /**
    * @brief return the previous element of the linked list
    *
    * @param[in] ref  pointer to current list element
    *
    * @return pointer to previous element or nullptr if no previous element exists
    */
   T *GetPrev(const T *ref) const
   {
      return((ref != nullptr) ? ref->prev : nullptr);
   }


   /**
    * @brief remove an element from a linked list
    *
    * @param[in] obj  list element to remove
    */
   void Pop(T *obj)
   {
      if (obj != nullptr)
      {
         if (first == obj)
         {
            first = obj->next;
         }

         if (last == obj)
         {
            last = obj->prev;
         }

         if (obj->next != nullptr)
         {
            obj->next->prev = obj->prev;
         }

         if (obj->prev != nullptr)
         {
            obj->prev->next = obj->next;
         }
         obj->next = nullptr;
         obj->prev = nullptr;
      }
   }


   //! swap two elements of a list
   void Swap(T *obj1, T *obj2)
   {
      if (  obj1 != nullptr
         && obj2 != nullptr)
      {
         if (obj1->prev == obj2)
         {
            Pop(obj1);
            AddBefore(obj1, obj2);
         }
         else if (obj2->prev == obj1)
         {
            Pop(obj2);
            AddBefore(obj2, obj1);
         }
         else
         {
            T *prev1 = obj1->prev;
            Pop(obj1);

            T *prev2 = obj2->prev;
            Pop(obj2);

            AddAfter(obj1, prev2);
            AddAfter(obj2, prev1);
         }
      }
   }


   /**
    * @brief add a new element after a reference position in a list
    *
    * @param obj  new element to add to list
    * @param ref  chunk after which to insert new object
    */
   void AddAfter(T *obj, T *ref)
   {
      if (  obj != nullptr
         && ref != nullptr)
      {
         Pop(obj); // TODO: is this necessary?
         obj->next = ref->next;
         obj->prev = ref;

         if (ref->next != nullptr)
         {
            ref->next->prev = obj;
         }
         else
         {
            last = obj;
         }
         ref->next = obj;
      }
   }


   /**
    * @brief add a new element before a reference position in a list
    *
    * @param obj  new element to add to list
    * @param ref  chunk before to insert new object
    */
   void AddBefore(T *obj, T *ref)
   {
      if (  obj != nullptr
         && ref != nullptr)
      {
         Pop(obj);
         obj->next = ref;
         obj->prev = ref->prev;

         if (ref->prev != nullptr)
         {
            ref->prev->next = obj;
         }
         else
         {
            first = obj;
         }
         ref->prev = obj;
      }
   }


   /**
    * @brief add a new element to the tail of a lis
    *
    * @param obj  new element to add to the list
    */
   void AddTail(T *obj)
   {
      obj->next = nullptr;
      obj->prev = last;

      if (last == nullptr)
      {
         last  = obj;
         first = obj;
      }
      else
      {
         last->next = obj;
      }
      last = obj;
   }


   /**
    * @brief add a new element to the head of a list
    *
    * @param obj  new element to add to the list
    */
   void AddHead(T *obj)
   {
      obj->next = first;
      obj->prev = nullptr;

      if (first == nullptr)
      {
         last  = obj;
         first = obj;
      }
      else
      {
         first->prev = obj;
      }
      first = obj;
   }
};


#endif /* LIST_MANAGER_H_INCLUDED */