summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.78.1/src/ListManager.h
blob: 64bc5fd83c350e1a369f0fcb963e229f906e14f5 (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
/**
 * @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

#include "chunk.h"

/**
 * A simple list manager for a double-linked list of Chunk items.
 */
class ChunkListManager
{
protected:
   Chunk *m_head;  // pointer to the head of list
   Chunk *m_tail;  // pointer to tail of list

public:
   ChunkListManager()
   {
      m_head = Chunk::NullChunkPtr;
      m_tail = Chunk::NullChunkPtr;
   }


   /**
    * @return pointer to first element of the linked list
    */
   Chunk *GetHead() const
   {
      return(m_head);
   }


   /**
    * @return pointer to last element of the linked list
    */
   Chunk *GetTail() const
   {
      return(m_tail);
   }


   /**
    * @brief remove an element from a linked list
    * @param[in] obj chunk to remove from the list
    */
   void Remove(Chunk *obj)
   {
      if (obj != Chunk::NullChunkPtr)
      {
         if (m_head == obj)
         {
            m_head = obj->m_next;
         }

         if (m_tail == obj)
         {
            m_tail = obj->m_prev;
         }

         if (obj->m_next != Chunk::NullChunkPtr)
         {
            obj->m_next->m_prev = obj->m_prev;
         }

         if (obj->m_prev != Chunk::NullChunkPtr)
         {
            obj->m_prev->m_next = obj->m_next;
         }
         obj->m_next = Chunk::NullChunkPtr;
         obj->m_prev = Chunk::NullChunkPtr;
      }
   }


   //! swap two elements of a list
   void Swap(Chunk *obj1, Chunk *obj2)
   {
      if (  obj1 != Chunk::NullChunkPtr
         && obj2 != Chunk::NullChunkPtr)
      {
         if (obj1->m_prev == obj2)
         {
            Remove(obj1);
            AddBefore(obj1, obj2);
         }
         else if (obj2->m_prev == obj1)
         {
            Remove(obj2);
            AddBefore(obj2, obj1);
         }
         else
         {
            Chunk *m_prev1 = obj1->m_prev;
            Remove(obj1);

            Chunk *m_prev2 = obj2->m_prev;
            Remove(obj2);

            AddAfter(obj1, m_prev2);
            AddAfter(obj2, m_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(Chunk *obj, Chunk *ref)
   {
      if (  obj != Chunk::NullChunkPtr
         && ref != Chunk::NullChunkPtr)
      {
         obj->m_next = ref->m_next;
         obj->m_prev = ref;

         if (ref->m_next != Chunk::NullChunkPtr)
         {
            ref->m_next->m_prev = obj;
         }
         else
         {
            m_tail = obj;
         }
         ref->m_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(Chunk *obj, Chunk *ref)
   {
      if (  obj != Chunk::NullChunkPtr
         && ref != Chunk::NullChunkPtr)
      {
         Remove(obj);
         obj->m_next = ref;
         obj->m_prev = ref->m_prev;

         if (ref->m_prev != Chunk::NullChunkPtr)
         {
            ref->m_prev->m_next = obj;
         }
         else
         {
            m_head = obj;
         }
         ref->m_prev = obj;
      }
   }


   /**
    * @brief add a new element to the tail of a lis
    *
    * @param obj  new element to add to the list
    */
   void AddTail(Chunk *obj)
   {
      obj->m_next = Chunk::NullChunkPtr;
      obj->m_prev = m_tail;

      if (m_tail == Chunk::NullChunkPtr)
      {
         m_tail = obj;
         m_head = obj;
      }
      else
      {
         m_tail->m_next = obj;
      }
      m_tail = obj;
   }


   /**
    * @brief add a new element to the head of a list
    *
    * @param obj  new element to add to the list
    */
   void AddHead(Chunk *obj)
   {
      obj->m_next = m_head;
      obj->m_prev = Chunk::NullChunkPtr;

      if (m_head == Chunk::NullChunkPtr)
      {
         m_tail = obj;
         m_head = obj;
      }
      else
      {
         m_head->m_prev = obj;
      }
      m_head = obj;
   }
};


#endif /* LIST_MANAGER_H_INCLUDED */