1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2005 Christian Schoenebeck * |
* Copyright (C) 2005 - 2017 Christian Schoenebeck * |
4 |
* * |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
21 |
#ifndef __LS_ARRAYLIST_H__ |
#ifndef __LS_ARRAYLIST_H__ |
22 |
#define __LS_ARRAYLIST_H__ |
#define __LS_ARRAYLIST_H__ |
23 |
|
|
24 |
#include "LinuxSamplerException.h" |
#include "Exception.h" |
25 |
|
#include <string.h> // for memcpy() |
26 |
|
|
27 |
namespace LinuxSampler { |
namespace LinuxSampler { |
28 |
|
|
62 |
/** |
/** |
63 |
* Remove the given element at \a iPosition from the list. |
* Remove the given element at \a iPosition from the list. |
64 |
* |
* |
65 |
* @throws LinuxSamplerException - if \a iPosition is out of range |
* @throws Exception - if \a iPosition is out of range |
66 |
*/ |
*/ |
67 |
void remove(int iPosition) throw (LinuxSamplerException) { |
void remove(int iPosition) throw (Exception) { |
68 |
if (iPosition < 0 || iPosition >= iSize) |
if (iPosition < 0 || iPosition >= iSize) |
69 |
throw LinuxSamplerException("ArrayList::remove(): index out of range"); |
throw Exception("ArrayList::remove(): index out of range"); |
70 |
if (iSize == 1) clear(); |
if (iSize == 1) clear(); |
71 |
else if (pData) { |
else if (pData) { |
72 |
T* pNewArray = new T[iSize - 1]; |
T* pNewArray = new T[iSize - 1]; |
84 |
/** |
/** |
85 |
* Remove the given \a element from the list. |
* Remove the given \a element from the list. |
86 |
* |
* |
87 |
* @throws LinuxSamplerException - if \a element could not be found |
* @throws Exception - if \a element could not be found |
88 |
*/ |
*/ |
89 |
void remove(const T& element) { |
void remove(const T& element) { |
90 |
remove(find(element)); |
remove(find(element)); |
91 |
} |
} |
92 |
|
|
93 |
/** |
/** |
94 |
|
* 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 |
* Remove all elements from the list. |
* Remove all elements from the list. |
110 |
*/ |
*/ |
111 |
void clear() { |
void clear() { |
119 |
/** |
/** |
120 |
* Returns the index of the given \a element on the list. |
* Returns the index of the given \a element on the list. |
121 |
* |
* |
122 |
* @throws LinuxSamplerException - if \a element could not be found |
* @throws Exception - if \a element could not be found |
123 |
*/ |
*/ |
124 |
int find(const T& element) { |
int find(const T& element) { |
125 |
for (int i = 0; i < iSize; i++) |
for (int i = 0; i < iSize; i++) |
126 |
if (pData[i] == element) return i; |
if (pData[i] == element) return i; |
127 |
throw LinuxSamplerException("ArrayList::find(): could not find given element"); |
throw Exception("ArrayList::find(): could not find given element"); |
128 |
} |
} |
129 |
|
|
130 |
/** |
/** |
131 |
* Number of elements currently on the list. |
* Number of elements currently on the list. |
132 |
*/ |
*/ |
133 |
inline int size() { |
inline int size() const { |
134 |
return iSize; |
return iSize; |
135 |
} |
} |
136 |
|
|
137 |
/** |
/** |
138 |
* Returns true if the list is empty. |
* Returns true if the list is empty. |
139 |
*/ |
*/ |
140 |
inline bool empty() { |
inline bool empty() const { |
141 |
return (bool) !iSize; |
return (bool) !iSize; |
142 |
} |
} |
143 |
|
|
148 |
return pData[iPosition]; |
return pData[iPosition]; |
149 |
} |
} |
150 |
|
|
151 |
|
/** |
152 |
|
* Access element at \a iPosition. |
153 |
|
*/ |
154 |
|
inline const T& operator[](int iPosition) const { |
155 |
|
return pData[iPosition]; |
156 |
|
} |
157 |
|
|
158 |
|
/** |
159 |
|
* Copy constructor. |
160 |
|
*/ |
161 |
|
ArrayList(const ArrayList& list) { |
162 |
|
copy(list); |
163 |
|
} |
164 |
|
|
165 |
|
/** |
166 |
|
* 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 |
|
*/ |
172 |
|
ArrayList& operator=(const ArrayList& list) { |
173 |
|
if (this != &list) { |
174 |
|
clear(); |
175 |
|
copy(list); |
176 |
|
} |
177 |
|
return *this; |
178 |
|
} |
179 |
|
|
180 |
|
/** |
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 |
private: |
private: |
190 |
T* pData; |
T* pData; |
191 |
int iSize; |
int iSize; |
192 |
|
|
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 |
}; |
}; |
205 |
|
|
206 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |