Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * @file lib.LinkedNode.hpp | ||
3 | * @author Sergey Baigudin, sergey@baigudin.software | ||
4 | * @copyright 2016-2022, Sergey Baigudin, Baigudin Software | ||
5 | */ | ||
6 | #ifndef LIB_LINKEDNODE_HPP_ | ||
7 | #define LIB_LINKEDNODE_HPP_ | ||
8 | |||
9 | #include "lib.NonCopyable.hpp" | ||
10 | |||
11 | namespace eoos | ||
12 | { | ||
13 | namespace lib | ||
14 | { | ||
15 | |||
16 | /** | ||
17 | * @class LinkedNode<T,A> | ||
18 | * @brief Element for linked lists. | ||
19 | * | ||
20 | * @tparam T Data type of element. | ||
21 | * @tparam A Heap memory allocator class. | ||
22 | */ | ||
23 | template <typename T, class A = Allocator> | ||
24 | class LinkedNode : public NonCopyable<A> | ||
25 | { | ||
26 | typedef NonCopyable<A> Parent; | ||
27 | |||
28 | public: | ||
29 | |||
30 | /** | ||
31 | * @brief Constructor. | ||
32 | * | ||
33 | * @note A passed element will be copied to the internal data | ||
34 | * structure by calling a copy constructor so that the element | ||
35 | * might be invalidated after the function called. | ||
36 | * | ||
37 | * @param element An user element of this node. | ||
38 | */ | ||
39 | 214 | LinkedNode(T const& element) | |
40 | : NonCopyable<A>() | ||
41 | 214 | , prev_(this) | |
42 | 214 | , next_(this) | |
43 | 214 | , index_(0) | |
44 | 214 | , element_(element) { | |
45 | 214 | } | |
46 | |||
47 | /** | ||
48 | * @brief Destructor. | ||
49 | */ | ||
50 | 444 | virtual ~LinkedNode() | |
51 | { | ||
52 | 222 | LinkedNode* node( this->next_ ); | |
53 |
2/2✓ Branch 0 taken 94 times.
✓ Branch 1 taken 111 times.
|
410 | while(node->index_ != 0) |
54 | { | ||
55 | 188 | node->index_--; ///< SCA MISRA-C++:2008 Defected Rule 5-2-10 | |
56 | 188 | node = node->next_; | |
57 | } | ||
58 | 222 | next_->prev_ = prev_; | |
59 | 222 | prev_->next_ = next_; | |
60 | 222 | prev_ = this; | |
61 | 222 | next_ = this; | |
62 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
666 | } |
63 | |||
64 | /** | ||
65 | * @brief Inserts a new element after this. | ||
66 | * | ||
67 | * Function links a node after this and reindexes | ||
68 | * chain of nodes starts from given node. | ||
69 | * | ||
70 | * @param node Pointer to inserted node. | ||
71 | */ | ||
72 | 115 | void insertAfter(LinkedNode<T,A>* node) | |
73 | { | ||
74 | 115 | link(node); | |
75 | 115 | node->index_ = index_; | |
76 | do | ||
77 | { | ||
78 | 129 | node->index_++; ///< SCA MISRA-C++:2008 Defected Rule 5-2-10 | |
79 | 129 | node = node->next_; | |
80 | } | ||
81 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 59 times.
|
129 | while(node->index_ != 0); |
82 | 115 | } | |
83 | |||
84 | /** | ||
85 | * @brief Inserts a new element before this. | ||
86 | * | ||
87 | * Function links a node before this and reindexes | ||
88 | * chain of nodes starts from this node. | ||
89 | * | ||
90 | * @param node Pointer to inserted node. | ||
91 | */ | ||
92 | 12 | void insertBefore(LinkedNode<T,A>* node) | |
93 | { | ||
94 | 12 | prev_->link(node); | |
95 | 12 | node->index_ = index_; | |
96 | 12 | node = this; | |
97 | do | ||
98 | { | ||
99 | 20 | node->index_++; ///< SCA MISRA-C++:2008 Defected Rule 5-2-10 | |
100 | 20 | node = node->next_; | |
101 | } | ||
102 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
|
20 | while(node->index_ != 0); |
103 | } | ||
104 | |||
105 | /** | ||
106 | * @brief Returns previous element. | ||
107 | * | ||
108 | * @return Previous element. | ||
109 | */ | ||
110 | 180 | LinkedNode<T,A>* getPrevious() ///< SCA MISRA-C++:2008 Justified Rule 9-3-3 | |
111 | { | ||
112 | 180 | return prev_; | |
113 | } | ||
114 | |||
115 | /** | ||
116 | * @brief Returns previous element. | ||
117 | * | ||
118 | * @return Previous element. | ||
119 | */ | ||
120 | LinkedNode<T,A> const* getPrevious() const | ||
121 | { | ||
122 | return prev_; | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * @brief Returns next element. | ||
127 | * | ||
128 | * @return The next element. | ||
129 | */ | ||
130 | 634 | LinkedNode<T,A>* getNext() ///< SCA MISRA-C++:2008 Justified Rule 9-3-3 | |
131 | { | ||
132 | 634 | return next_; | |
133 | } | ||
134 | |||
135 | /** | ||
136 | * @brief Returns next element. | ||
137 | * | ||
138 | * @return The next element. | ||
139 | */ | ||
140 | LinkedNode<T,A> const* getNext() const | ||
141 | { | ||
142 | return next_; | ||
143 | } | ||
144 | |||
145 | /** | ||
146 | * @brief Returns the element. | ||
147 | * | ||
148 | * @return The next element. | ||
149 | */ | ||
150 | 486 | T& getElement() | |
151 | { | ||
152 | 486 | return element_; ///< SCA MISRA-C++:2008 Justified Rule 9-3-2 | |
153 | } | ||
154 | |||
155 | /** | ||
156 | * @brief Returns the element. | ||
157 | * | ||
158 | * @return The next element. | ||
159 | */ | ||
160 | T const& getElement() const | ||
161 | { | ||
162 | return element_; | ||
163 | } | ||
164 | |||
165 | /** | ||
166 | * @brief Returns the element index. | ||
167 | * | ||
168 | * @return An element index. | ||
169 | */ | ||
170 | 2207 | int32_t getIndex() const | |
171 | { | ||
172 | 2207 | return index_; | |
173 | } | ||
174 | |||
175 | private: | ||
176 | |||
177 | /** | ||
178 | * @brief Links a given node after this. | ||
179 | * | ||
180 | * @param node Pointer to linking node. | ||
181 | */ | ||
182 | 127 | void link(LinkedNode<T,A>* node) | |
183 | { | ||
184 | 127 | next_->prev_ = node; | |
185 | 127 | node->next_ = next_; | |
186 | 127 | next_ = node; | |
187 | 127 | node->prev_ = this; | |
188 | 127 | } | |
189 | |||
190 | /** | ||
191 | * @brief Previous node. | ||
192 | */ | ||
193 | LinkedNode* prev_; | ||
194 | |||
195 | /** | ||
196 | * @brief Next node. | ||
197 | */ | ||
198 | LinkedNode* next_; | ||
199 | |||
200 | /** | ||
201 | * @brief Index of the node. | ||
202 | */ | ||
203 | int32_t index_; | ||
204 | |||
205 | /** | ||
206 | * @brief Containing element. | ||
207 | */ | ||
208 | T element_; | ||
209 | |||
210 | }; | ||
211 | |||
212 | } // namespace lib | ||
213 | } // namespace eoos | ||
214 | #endif // LIB_LINKEDNODE_HPP_ | ||
215 |