1 |
schoenebeck |
458 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
schoenebeck |
3293 |
* Copyright (C) 2005 - 2017 Christian Schoenebeck * |
4 |
schoenebeck |
458 |
* * |
5 |
|
|
* This program is free software; you can redistribute it and/or modify * |
6 |
|
|
* it under the terms of the GNU General Public License as published by * |
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
8 |
|
|
* (at your option) any later version. * |
9 |
|
|
* * |
10 |
|
|
* This program is distributed in the hope that it will be useful, * |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
|
|
* GNU General Public License for more details. * |
14 |
|
|
* * |
15 |
|
|
* You should have received a copy of the GNU General Public License * |
16 |
|
|
* along with this program; if not, write to the Free Software * |
17 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
18 |
|
|
* MA 02111-1307 USA * |
19 |
|
|
***************************************************************************/ |
20 |
|
|
|
21 |
|
|
#ifndef __LS_ARRAYLIST_H__ |
22 |
|
|
#define __LS_ARRAYLIST_H__ |
23 |
|
|
|
24 |
schoenebeck |
880 |
#include "Exception.h" |
25 |
schoenebeck |
3293 |
#include <string.h> // for memcpy() |
26 |
schoenebeck |
458 |
|
27 |
|
|
namespace LinuxSampler { |
28 |
|
|
|
29 |
|
|
/** |
30 |
|
|
* Very simple container with array implementation which ensures a constant |
31 |
|
|
* access time of Theta(1). We could have used std::vector instead, but due |
32 |
|
|
* to paranoia in regards of possible implementation differences, we better |
33 |
|
|
* rely on this class instead in parts where RT stability is mandatory. |
34 |
|
|
*/ |
35 |
|
|
template<typename T> |
36 |
|
|
class ArrayList { |
37 |
|
|
public: |
38 |
|
|
ArrayList() { |
39 |
|
|
pData = NULL; |
40 |
|
|
iSize = 0; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
~ArrayList() { |
44 |
|
|
clear(); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
/** |
48 |
|
|
* Add a new element to the end of the list. |
49 |
|
|
*/ |
50 |
|
|
void add(T element) { |
51 |
|
|
T* pNewArray = new T[iSize + 1]; |
52 |
|
|
if (pData) { |
53 |
|
|
for (int i = 0; i < iSize; i++) |
54 |
|
|
pNewArray[i] = pData[i]; |
55 |
persson |
835 |
delete[] pData; |
56 |
schoenebeck |
458 |
} |
57 |
|
|
pNewArray[iSize] = element; |
58 |
|
|
pData = pNewArray; |
59 |
|
|
++iSize; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
/** |
63 |
|
|
* Remove the given element at \a iPosition from the list. |
64 |
|
|
* |
65 |
schoenebeck |
880 |
* @throws Exception - if \a iPosition is out of range |
66 |
schoenebeck |
458 |
*/ |
67 |
schoenebeck |
880 |
void remove(int iPosition) throw (Exception) { |
68 |
schoenebeck |
458 |
if (iPosition < 0 || iPosition >= iSize) |
69 |
schoenebeck |
880 |
throw Exception("ArrayList::remove(): index out of range"); |
70 |
schoenebeck |
458 |
if (iSize == 1) clear(); |
71 |
|
|
else if (pData) { |
72 |
|
|
T* pNewArray = new T[iSize - 1]; |
73 |
|
|
for (int iSrc = 0, iDst = 0; iSrc < iSize; iSrc++) { |
74 |
|
|
if (iSrc == iPosition) continue; |
75 |
|
|
pNewArray[iDst] = pData[iSrc]; |
76 |
|
|
++iDst; |
77 |
|
|
} |
78 |
persson |
835 |
delete[] pData; |
79 |
schoenebeck |
458 |
pData = pNewArray; |
80 |
|
|
--iSize; |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
/** |
85 |
|
|
* Remove the given \a element from the list. |
86 |
|
|
* |
87 |
schoenebeck |
880 |
* @throws Exception - if \a element could not be found |
88 |
schoenebeck |
458 |
*/ |
89 |
|
|
void remove(const T& element) { |
90 |
|
|
remove(find(element)); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
/** |
94 |
schoenebeck |
2581 |
* Increase or decrease the size of this list to the new amount of |
95 |
|
|
* elements given by \a cnt. |
96 |
|
|
*/ |
97 |
|
|
void resize(int cnt) { |
98 |
|
|
T* pNewArray = new T[cnt]; |
99 |
|
|
if (pData) { |
100 |
|
|
for (int i = 0; i < cnt; i++) |
101 |
|
|
pNewArray[i] = pData[i]; |
102 |
|
|
delete[] pData; |
103 |
|
|
} |
104 |
|
|
pData = pNewArray; |
105 |
|
|
iSize = cnt; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
/** |
109 |
schoenebeck |
458 |
* Remove all elements from the list. |
110 |
|
|
*/ |
111 |
|
|
void clear() { |
112 |
|
|
if (pData) { |
113 |
persson |
835 |
delete[] pData; |
114 |
schoenebeck |
458 |
pData = NULL; |
115 |
|
|
iSize = 0; |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* Returns the index of the given \a element on the list. |
121 |
|
|
* |
122 |
schoenebeck |
880 |
* @throws Exception - if \a element could not be found |
123 |
schoenebeck |
458 |
*/ |
124 |
|
|
int find(const T& element) { |
125 |
|
|
for (int i = 0; i < iSize; i++) |
126 |
|
|
if (pData[i] == element) return i; |
127 |
schoenebeck |
880 |
throw Exception("ArrayList::find(): could not find given element"); |
128 |
schoenebeck |
458 |
} |
129 |
|
|
|
130 |
|
|
/** |
131 |
|
|
* Number of elements currently on the list. |
132 |
|
|
*/ |
133 |
schoenebeck |
2500 |
inline int size() const { |
134 |
schoenebeck |
458 |
return iSize; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
/** |
138 |
|
|
* Returns true if the list is empty. |
139 |
|
|
*/ |
140 |
schoenebeck |
2500 |
inline bool empty() const { |
141 |
schoenebeck |
458 |
return (bool) !iSize; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
/** |
145 |
|
|
* Access element at \a iPosition. |
146 |
|
|
*/ |
147 |
|
|
inline T& operator[](int iPosition) { |
148 |
|
|
return pData[iPosition]; |
149 |
|
|
} |
150 |
|
|
|
151 |
persson |
2055 |
/** |
152 |
schoenebeck |
2500 |
* Access element at \a iPosition. |
153 |
|
|
*/ |
154 |
|
|
inline const T& operator[](int iPosition) const { |
155 |
|
|
return pData[iPosition]; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
/** |
159 |
persson |
2055 |
* Copy constructor. |
160 |
|
|
*/ |
161 |
|
|
ArrayList(const ArrayList& list) { |
162 |
|
|
copy(list); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
/** |
166 |
schoenebeck |
3293 |
* Generalized assignment with size adjustment and deep copy of |
167 |
|
|
* elements. Note that this method/operator re-allocates, so it is |
168 |
|
|
* not appropriate for being used in a real-time context! |
169 |
|
|
* |
170 |
|
|
* @see copyFlatFrom() for real-time safe copying |
171 |
persson |
2055 |
*/ |
172 |
|
|
ArrayList& operator=(const ArrayList& list) { |
173 |
|
|
if (this != &list) { |
174 |
|
|
clear(); |
175 |
|
|
copy(list); |
176 |
|
|
} |
177 |
|
|
return *this; |
178 |
|
|
} |
179 |
|
|
|
180 |
schoenebeck |
3293 |
/** |
181 |
|
|
* Real-time safe copying elements (flat, no deep copy) from |
182 |
|
|
* given @a list to this list. |
183 |
|
|
*/ |
184 |
|
|
void copyFlatFrom(const ArrayList& list) { |
185 |
|
|
const int n = (size() < list.size()) ? size() : list.size(); |
186 |
|
|
memcpy(pData, list.pData, n * sizeof(T)); |
187 |
|
|
} |
188 |
|
|
|
189 |
schoenebeck |
458 |
private: |
190 |
|
|
T* pData; |
191 |
|
|
int iSize; |
192 |
persson |
2055 |
|
193 |
|
|
void copy(const ArrayList& list) { |
194 |
|
|
iSize = list.iSize; |
195 |
|
|
if (list.pData) { |
196 |
|
|
pData = new T[iSize]; |
197 |
|
|
for (int i = 0 ; i < iSize ; i++) { |
198 |
|
|
pData[i] = list.pData[i]; |
199 |
|
|
} |
200 |
|
|
} else { |
201 |
|
|
pData = NULL; |
202 |
|
|
} |
203 |
|
|
} |
204 |
schoenebeck |
458 |
}; |
205 |
|
|
|
206 |
|
|
} // namespace LinuxSampler |
207 |
|
|
|
208 |
|
|
#endif // __ARRAYLIST_H__ |