-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSList.h
More file actions
210 lines (177 loc) · 4.93 KB
/
Copy pathSList.h
File metadata and controls
210 lines (177 loc) · 4.93 KB
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
/*
* The MIT License(MIT)
* Copyright(c) 2016 Lorenzo Delana, https://searchathing.com
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef _SEARCHATHING_ARDUINO_UTILS_SLIST_H
#define _SEARCHATHING_ARDUINO_UTILS_SLIST_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif
#include "DebugMacros.h"
namespace SearchAThing
{
namespace Arduino
{
// Templated simple linked-list node element.
// Store templated element `T' into a node object that allow to
// follow using a `next' pointer to the next one in the list.
// The given template `T' must support default constructor for
// internal purpose.
template<class T>
class SListNode
{
public:
// Constructor stores a copy of given element into internal data.
SListNode<T>(const T& _data) { data = _data; }
// Element data.
T data;
// Pointer to the next node in simple linked list.
SListNode<T> *next = NULL;
};
// Templated simple linked-list.
// Store templated element `T' into a simple linked list.
// The given template `T' must support default constructor for
// internal purpose.
template<class T>
class SList
{
uint16_t size = 0;
SListNode<T> *first = NULL;
SListNode<T> *last = NULL;
public:
// Default constructor.
SList()
{
}
// Copy constructor.
SList(const SList& other)
{
*this = other;
}
// Assign operator. Creates a copy of the given `other' list.
SList& operator = (const SList& other)
{
Clear();
auto node = other.first;
while (node)
{
Add(node->data);
node = node->next;
}
return *this;
}
// Destructor. Deallocates memory used for nodes and thus calls
// destructor of stored templated objects.
~SList()
{
Clear();
}
// Current list size.
uint16_t Size() const { return size; }
// Adds given templated object `data' to the list.
// If DEBUG and DEBUG_ASSERT are enabled a report about out-of-
// memory will be reported if unable to allocate more nodes.
T& Add(const T& data)
{
auto node = new SListNode<T>(data);
if (node == NULL)
{
#if defined DEBUG && defined DEBUG_ASSERT
DPrint(F("* Fatal: SList alloc of node out of memory"));
#endif
}
if (first == NULL)
first = last = node;
else
{
last->next = node;
last = node;
}
++size;
return node->data;
}
// Clear the list destroying each nodes thus calling the
// destructor of contained templated data objects.
void Clear()
{
if (first == NULL) return;
SListNode<T> *node = first;
while (node != NULL)
{
SListNode<T> *tmp = node->next;
delete node;
node = tmp;
--size;
}
first = last = NULL;
}
// Remove the node by idx ( 0 is the first ).
// It does nothing if invalid index out of bounds.
void Remove(uint16_t idx)
{
if (idx >= size) return; // arg exception
if (idx == 0) // delete first
{
SListNode<T> *tmp = first->next;
delete first;
first = tmp;
}
else
{
SListNode<T> *before = GetNode(idx - 1);
if (idx == size - 1) // delete last
{
delete last;
before->next = NULL;
last = before;
}
else
{
SListNode<T> *after = before->next->next;
delete before->next;
before->next = after;
}
}
--size;
}
// Retrieve a reference of the template object at the given `idx'
// in the node list. Note: don't use `auto' pointer of the
// returned object will be copied instead of referenced.
T& Get(int idx) const
{
return GetNode(idx)->data;
}
// Retrieve a pointer to the node at the given `idx'
// ( 0 is start ). If an invalid index was given returns NULL.
SListNode<T> *GetNode(int idx) const
{
if (idx == size - 1) return last;
if (idx >= size) return NULL;
SListNode<T> *res = first;
while (idx--) { res = res->next; }
return res;
}
};
}
}
#endif