1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2005 Christian Schoenebeck * |
* Copyright (C) 2005 - 2014 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 |
|
|
26 |
namespace LinuxSampler { |
namespace LinuxSampler { |
27 |
|
|
51 |
if (pData) { |
if (pData) { |
52 |
for (int i = 0; i < iSize; i++) |
for (int i = 0; i < iSize; i++) |
53 |
pNewArray[i] = pData[i]; |
pNewArray[i] = pData[i]; |
54 |
delete pData; |
delete[] pData; |
55 |
} |
} |
56 |
pNewArray[iSize] = element; |
pNewArray[iSize] = element; |
57 |
pData = pNewArray; |
pData = pNewArray; |
61 |
/** |
/** |
62 |
* Remove the given element at \a iPosition from the list. |
* Remove the given element at \a iPosition from the list. |
63 |
* |
* |
64 |
* @throws LinuxSamplerException - if \a iPosition is out of range |
* @throws Exception - if \a iPosition is out of range |
65 |
*/ |
*/ |
66 |
void remove(int iPosition) throw (LinuxSamplerException) { |
void remove(int iPosition) throw (Exception) { |
67 |
if (iPosition < 0 || iPosition >= iSize) |
if (iPosition < 0 || iPosition >= iSize) |
68 |
throw LinuxSamplerException("ArrayList::remove(): index out of range"); |
throw Exception("ArrayList::remove(): index out of range"); |
69 |
if (iSize == 1) clear(); |
if (iSize == 1) clear(); |
70 |
else if (pData) { |
else if (pData) { |
71 |
T* pNewArray = new T[iSize - 1]; |
T* pNewArray = new T[iSize - 1]; |
74 |
pNewArray[iDst] = pData[iSrc]; |
pNewArray[iDst] = pData[iSrc]; |
75 |
++iDst; |
++iDst; |
76 |
} |
} |
77 |
delete pData; |
delete[] pData; |
78 |
pData = pNewArray; |
pData = pNewArray; |
79 |
--iSize; |
--iSize; |
80 |
} |
} |
83 |
/** |
/** |
84 |
* Remove the given \a element from the list. |
* Remove the given \a element from the list. |
85 |
* |
* |
86 |
* @throws LinuxSamplerException - if \a element could not be found |
* @throws Exception - if \a element could not be found |
87 |
*/ |
*/ |
88 |
void remove(const T& element) { |
void remove(const T& element) { |
89 |
remove(find(element)); |
remove(find(element)); |
90 |
} |
} |
91 |
|
|
92 |
/** |
/** |
93 |
|
* Increase or decrease the size of this list to the new amount of |
94 |
|
* elements given by \a cnt. |
95 |
|
*/ |
96 |
|
void resize(int cnt) { |
97 |
|
T* pNewArray = new T[cnt]; |
98 |
|
if (pData) { |
99 |
|
for (int i = 0; i < cnt; i++) |
100 |
|
pNewArray[i] = pData[i]; |
101 |
|
delete[] pData; |
102 |
|
} |
103 |
|
pData = pNewArray; |
104 |
|
iSize = cnt; |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
* Remove all elements from the list. |
* Remove all elements from the list. |
109 |
*/ |
*/ |
110 |
void clear() { |
void clear() { |
111 |
if (pData) { |
if (pData) { |
112 |
delete pData; |
delete[] pData; |
113 |
pData = NULL; |
pData = NULL; |
114 |
iSize = 0; |
iSize = 0; |
115 |
} |
} |
118 |
/** |
/** |
119 |
* Returns the index of the given \a element on the list. |
* Returns the index of the given \a element on the list. |
120 |
* |
* |
121 |
* @throws LinuxSamplerException - if \a element could not be found |
* @throws Exception - if \a element could not be found |
122 |
*/ |
*/ |
123 |
int find(const T& element) { |
int find(const T& element) { |
124 |
for (int i = 0; i < iSize; i++) |
for (int i = 0; i < iSize; i++) |
125 |
if (pData[i] == element) return i; |
if (pData[i] == element) return i; |
126 |
throw LinuxSamplerException("ArrayList::find(): could not find given element"); |
throw Exception("ArrayList::find(): could not find given element"); |
127 |
} |
} |
128 |
|
|
129 |
/** |
/** |
130 |
* Number of elements currently on the list. |
* Number of elements currently on the list. |
131 |
*/ |
*/ |
132 |
inline int size() { |
inline int size() const { |
133 |
return iSize; |
return iSize; |
134 |
} |
} |
135 |
|
|
136 |
/** |
/** |
137 |
* Returns true if the list is empty. |
* Returns true if the list is empty. |
138 |
*/ |
*/ |
139 |
inline bool empty() { |
inline bool empty() const { |
140 |
return (bool) !iSize; |
return (bool) !iSize; |
141 |
} |
} |
142 |
|
|
147 |
return pData[iPosition]; |
return pData[iPosition]; |
148 |
} |
} |
149 |
|
|
150 |
|
/** |
151 |
|
* Access element at \a iPosition. |
152 |
|
*/ |
153 |
|
inline const T& operator[](int iPosition) const { |
154 |
|
return pData[iPosition]; |
155 |
|
} |
156 |
|
|
157 |
|
/** |
158 |
|
* Copy constructor. |
159 |
|
*/ |
160 |
|
ArrayList(const ArrayList& list) { |
161 |
|
copy(list); |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
|
* Assignment. |
166 |
|
*/ |
167 |
|
ArrayList& operator=(const ArrayList& list) { |
168 |
|
if (this != &list) { |
169 |
|
clear(); |
170 |
|
copy(list); |
171 |
|
} |
172 |
|
return *this; |
173 |
|
} |
174 |
|
|
175 |
private: |
private: |
176 |
T* pData; |
T* pData; |
177 |
int iSize; |
int iSize; |
178 |
|
|
179 |
|
void copy(const ArrayList& list) { |
180 |
|
iSize = list.iSize; |
181 |
|
if (list.pData) { |
182 |
|
pData = new T[iSize]; |
183 |
|
for (int i = 0 ; i < iSize ; i++) { |
184 |
|
pData[i] = list.pData[i]; |
185 |
|
} |
186 |
|
} else { |
187 |
|
pData = NULL; |
188 |
|
} |
189 |
|
} |
190 |
}; |
}; |
191 |
|
|
192 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |