/[svn]/linuxsampler/trunk/src/common/ArrayList.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/common/ArrayList.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3557 - (show annotations) (download) (as text)
Sun Aug 18 00:06:04 2019 UTC (4 years, 8 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 7341 byte(s)
* NKSP: Introducing 64 bit support for NKSP integer scripts
  variables (declare $foo).
* Require C++11 compiler support.
* Autoconf: Added m4/ax_cxx_compile_stdcxx.m4 macro which is used
  for checking in configure for C++11 support (as mandatory
  requirement) and automatically adds compiler argument if required
  (e.g. -std=C++11).
* Bumped version (2.1.1.svn3).

1 /***************************************************************************
2 * *
3 * Copyright (C) 2005 - 2019 Christian Schoenebeck *
4 * *
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 #include "Exception.h"
25 #include <string.h> // for memcpy()
26
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 (ssize_t i = 0; i < iSize; i++)
54 pNewArray[i] = pData[i];
55 delete[] pData;
56 }
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 * @throws Exception - if \a iPosition is out of range
66 */
67 void remove(ssize_t iPosition) throw (Exception) {
68 if (iPosition < 0 || iPosition >= iSize)
69 throw Exception("ArrayList::remove(): index out of range");
70 if (iSize == 1) clear();
71 else if (pData) {
72 T* pNewArray = new T[iSize - 1];
73 for (ssize_t iSrc = 0, iDst = 0; iSrc < iSize; iSrc++) {
74 if (iSrc == iPosition) continue;
75 pNewArray[iDst] = pData[iSrc];
76 ++iDst;
77 }
78 delete[] pData;
79 pData = pNewArray;
80 --iSize;
81 }
82 }
83
84 /**
85 * Remove the given \a element from the list.
86 *
87 * @throws Exception - if \a element could not be found
88 */
89 void remove(const T& element) {
90 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(ssize_t cnt) {
98 T* pNewArray = new T[cnt];
99 if (pData) {
100 for (ssize_t 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.
110 */
111 void clear() {
112 if (pData) {
113 delete[] pData;
114 pData = NULL;
115 iSize = 0;
116 }
117 }
118
119 /**
120 * Returns the index of the given \a element on the list.
121 *
122 * @throws Exception - if \a element could not be found
123 */
124 ssize_t find(const T& element) {
125 for (ssize_t i = 0; i < iSize; i++)
126 if (pData[i] == element) return i;
127 throw Exception("ArrayList::find(): could not find given element");
128 }
129
130 /**
131 * Number of elements currently on the list.
132 */
133 inline ssize_t size() const {
134 return iSize;
135 }
136
137 /**
138 * Returns true if the list is empty.
139 */
140 inline bool empty() const {
141 return (bool) !iSize;
142 }
143
144 /**
145 * Access element at \a iPosition.
146 */
147 inline T& operator[](ssize_t iPosition) {
148 return pData[iPosition];
149 }
150
151 /**
152 * Access element at \a iPosition.
153 */
154 inline const T& operator[](ssize_t 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 ssize_t n = (size() < list.size()) ? size() : list.size();
186 memcpy(pData, list.pData, n * sizeof(T));
187 }
188
189 private:
190 T* pData;
191 ssize_t iSize;
192
193 void copy(const ArrayList& list) {
194 iSize = list.iSize;
195 if (list.pData) {
196 pData = new T[iSize];
197 for (ssize_t i = 0 ; i < iSize ; i++) {
198 pData[i] = list.pData[i];
199 }
200 } else {
201 pData = NULL;
202 }
203 }
204 };
205
206 } // namespace LinuxSampler
207
208 #endif // __ARRAYLIST_H__

  ViewVC Help
Powered by ViewVC