GCC Code Coverage Report


Directory: ./
File: include/DLList_impl.hpp
Date: 2026-12-03 12:28:13
Warnings: 1 unchecked decisions!
Exec Total Coverage
Lines: 42 52 80.8%
Functions: 6 15 40.0%
Branches: 30 132 22.7%
Decisions: 7 14 50.0%

Line Branch Decision Exec Source
1 #ifndef INCLUDE_DLLIST_IMPL_HPP
2 #define INCLUDE_DLLIST_IMPL_HPP
3
4 #include "singleton_pool.hpp"
5
6 // DLList Class v1.0
7 // Simon Tindemans, 1/4/2008
8 // The DLList class provides a doubly-linked list that gets around a number of problems
9 // with the STL list class:
10 // 1) The STL class always copies the elements on insertion. DLList initializes them 'on the spot',
11 // making it suitable for non-copyable classes.
12 // 2) The STL class does not allow for conversion from data pointers to list element iterators. The
13 // DLList class makes this possible, leading to easier deletion (no need to store/retrieve iterators) and
14 // list traversal from within objects.
15 // 3) When transferring elements (single-element splice), the STL list invalidates iterators. DLList
16 // allows one to transfer elements without invalidating pointers.
17
18 // The DLList list can be constructed using two types of list elements.
19 // First, the DLBaseItem class should be used as a base class for the individual list
20 // items. This has the advantage that the pointers to the list container elements and
21 // the individual objects are the same.
22
23 // The second method is using DLContainerItem objects. These objects contain an object
24 // of the type that is to be stored. This has the advantage that the objects don't need
25 // to be compiled for list use.
26
27 // Lists of objects of type T are declared as follows:
28 // DLList<T> name; [for DLBaseItem-derived classes]
29 // DLList<DLContainerItem<T> > name; [for other classes]
30
31 template <class T, int AllocatorGranularity = 32>
32 class DLList
33 {
34 private:
35
36 int num_elements;
37 // This class keeps a pointer to both the first and the last element.
38 // This is not strictly necessary (first is enough), but this has the advantage
39 // that elements can be inserted in the back and iterated over from the front of
40 // the set. In the absence of deletions, this leads to a traversal of the elements
41 // in the order of creation - and hopefully in the order of memory allocation.
42 T* first_element;
43 T* last_element;
44
45 using MemPool = crts::SingletonPool<T>;
46
47 void unlink_element(T*);
48 T* link_element(T*);
49
50 public:
51
52 12 DLList():
53 12 num_elements{ 0 },
54 12 first_element{ nullptr },
55
11/23
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 1 times.
✗ Branch 32 not taken.
12 last_element{ nullptr } {};
56 ~DLList();
57
58
1/14
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
1 int size() { return num_elements; }
59
60
1/10
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
1 T* first() { return first_element; }
61
62
1/14
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
1 T* last() { return last_element; }
63
64 T* next(T* el) { return el->next_element; }
65
66 T* previous(T* el) { return el->previous_element; }
67
68 T* create();
69 template <class Q>
70 T* create(Q);
71 template <class Q, class R>
72 T* create(Q, R);
73 template <class Q, class R, class S>
74 T* create(Q, R, S);
75 void remove(T*);
76 void remove_all();
77 void import(DLList<T, AllocatorGranularity>&, T*);
78 void import_set(DLList<T, AllocatorGranularity>&, T*, T*);
79 };
80
81 template <class T, int I>
82 DLList<T, I>::~DLList()
83 {
84 12 remove_all();
85
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
12 }
86
87 template <class T, int I>
88 12 void DLList<T, I>::remove_all()
89 {
90
2/12
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
0/1
? Decision couldn't be analyzed.
55 while (first_element != nullptr)
91 {
92 ✗ remove(first_element);
93 }
94 12 }
95
96 template <class T, int I>
97 inline T* DLList<T, I>::link_element(T* new_element)
98 // Inserts a new element into the linked list.
99 // Insertion takes place at the back of the list for memory-traversal speed considerations.
100 {
101 43 num_elements++;
102 43 new_element->previous_element = last_element;
103 43 new_element->next_element = nullptr;
104
5/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 32 times.
✓ Branch 7 taken 7 times.
2/2
✓ Decision 'true' taken 32 times.
✓ Decision 'false' taken 10 times.
42 if (last_element != nullptr)
105 {
106 32 last_element->next_element = new_element;
107 }
108 else // if list was empty
109 {
110 11 first_element = new_element;
111 }
112 43 last_element = new_element;
113 return new_element;
114 }
115
116 template <class T, int I>
117 inline void DLList<T, I>::unlink_element(T* element)
118 // Removes an element from the linked list.
119 {
120 43 num_elements--;
121 ✗ if (element == first_element)
122 {
123 43 first_element = element->next_element;
124 }
125
2/8
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
2/2
✓ Decision 'true' taken 11 times.
✓ Decision 'false' taken 32 times.
43 if (element == last_element)
126 {
127 11 last_element = element->previous_element;
128 }
129
2/8
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
2/2
✓ Decision 'true' taken 32 times.
✓ Decision 'false' taken 11 times.
43 if (element->next_element != nullptr)
130 {
131 32 element->next_element->previous_element = element->previous_element;
132 }
133
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 43 times.
43 if (element->previous_element != nullptr)
134 {
135 ✗ element->previous_element->next_element = element->next_element;
136 }
137 return;
138 }
139
140 template <class T, int I>
141 ✗ inline void DLList<T, I>::remove(T* element)
142 // Removes a single element. No checking is performed on the pointer.
143 {
144 unlink_element(element);
145 ✗ element->~T();
146 // MemPool::free(element);
147 43 }
148
149 /// The following functions create a new element from the heap and insert it into the linked list.
150 // These differ only in the number of arguments that is passed to the constructor.
151 template <class T, int I>
152 39 inline T* DLList<T, I>::create()
153 {
154 39 T* new_element = new (MemPool::malloc()) T();
155 39 return link_element(new_element);
156 }
157
158 template <class T, int I>
159 template <class Q>
160 4 inline T* DLList<T, I>::create(Q par1)
161 {
162 4 T* new_element = new (MemPool::malloc()) T(par1);
163 4 return link_element(new_element);
164 }
165
166 template <class T, int I>
167 template <class Q, class R>
168 1 inline T* DLList<T, I>::create(Q par1, R par2)
169 {
170 1 T* new_element = new (MemPool::malloc()) T(par1, par2);
171 1 return link_element(new_element);
172 }
173
174 template <class T, int I>
175 template <class Q, class R, class S>
176 1 inline T* DLList<T, I>::create(Q par1, R par2, S par3)
177 {
178
0/4
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1 T* new_element = new (MemPool::malloc()) T(par1, par2, par3);
179 1 return link_element(new_element);
180 }
181
182 template <class T, int I>
183 // This functions transfers 'element' from 'oldList' to this list.
184 ✗ inline void DLList<T, I>::import(DLList<T, I>& old_list, T* element)
185 {
186 old_list.unlink_element(element);
187 link_element(element);
188 ✗ }
189
190 template <class T, int I>
191 inline void DLList<T, I>::import_set(DLList<T, I>& old_list, T* first_element, T* last_element)
192 // This functions transfers a number of elements from 'oldList' to this list.
193 // WARNING: not efficient at all!
194 {
195 T* current = first_element;
196 T* temp;
197
0/2
✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
✗ while (current != nullptr)
198 {
199 temp = current;
200 current = current->next();
201 ✗ import(old_list, temp);
202
0/2
✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
✗ if (temp == last_element)
203 {
204 break;
205 }
206 }
207 }
208
209 template <class T>
210 // this list item should be used as a base class [type 1 - see above]
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 class DLBaseItem
212 {
213 public:
214
215 template <class X, int I>
216 friend class DLList;
217
218 private:
219
220 T* next_element;
221 T* previous_element;
222
223 protected:
224
225 // make sure that this cannot be deleted...
226 virtual ~DLBaseItem() {};
227
228 public:
229
230
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
2 T* next() { return next_element; }
231
232
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 T* previous() { return previous_element; }
233 };
234
235 #endif // INCLUDE_DLLIST_IMPL_HPP
236