/[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 2581 - (show annotations) (download) (as text)
Fri May 30 12:48:05 2014 UTC (9 years, 10 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 6621 byte(s)
* (WIP) Implemented parser and VM for upcoming new real-time instrument
  script support. It needs yet to be integrated into the sampler's
  sampler engines. You can toy around for now with the command line tool
  "ls_instr_script" and i.e. examples showing the core language features
  under src/scriptvm/examples/.
* Bumped version (1.0.0.svn41).

1 /***************************************************************************
2 * *
3 * Copyright (C) 2005 - 2014 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
26 namespace LinuxSampler {
27
28 /**
29 * Very simple container with array implementation which ensures a constant
30 * access time of Theta(1). We could have used std::vector instead, but due
31 * to paranoia in regards of possible implementation differences, we better
32 * rely on this class instead in parts where RT stability is mandatory.
33 */
34 template<typename T>
35 class ArrayList {
36 public:
37 ArrayList() {
38 pData = NULL;
39 iSize = 0;
40 }
41
42 ~ArrayList() {
43 clear();
44 }
45
46 /**
47 * Add a new element to the end of the list.
48 */
49 void add(T element) {
50 T* pNewArray = new T[iSize + 1];
51 if (pData) {
52 for (int i = 0; i < iSize; i++)
53 pNewArray[i] = pData[i];
54 delete[] pData;
55 }
56 pNewArray[iSize] = element;
57 pData = pNewArray;
58 ++iSize;
59 }
60
61 /**
62 * Remove the given element at \a iPosition from the list.
63 *
64 * @throws Exception - if \a iPosition is out of range
65 */
66 void remove(int iPosition) throw (Exception) {
67 if (iPosition < 0 || iPosition >= iSize)
68 throw Exception("ArrayList::remove(): index out of range");
69 if (iSize == 1) clear();
70 else if (pData) {
71 T* pNewArray = new T[iSize - 1];
72 for (int iSrc = 0, iDst = 0; iSrc < iSize; iSrc++) {
73 if (iSrc == iPosition) continue;
74 pNewArray[iDst] = pData[iSrc];
75 ++iDst;
76 }
77 delete[] pData;
78 pData = pNewArray;
79 --iSize;
80 }
81 }
82
83 /**
84 * Remove the given \a element from the list.
85 *
86 * @throws Exception - if \a element could not be found
87 */
88 void remove(const T& element) {
89 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.
109 */
110 void clear() {
111 if (pData) {
112 delete[] pData;
113 pData = NULL;
114 iSize = 0;
115 }
116 }
117
118 /**
119 * Returns the index of the given \a element on the list.
120 *
121 * @throws Exception - if \a element could not be found
122 */
123 int find(const T& element) {
124 for (int i = 0; i < iSize; i++)
125 if (pData[i] == element) return i;
126 throw Exception("ArrayList::find(): could not find given element");
127 }
128
129 /**
130 * Number of elements currently on the list.
131 */
132 inline int size() const {
133 return iSize;
134 }
135
136 /**
137 * Returns true if the list is empty.
138 */
139 inline bool empty() const {
140 return (bool) !iSize;
141 }
142
143 /**
144 * Access element at \a iPosition.
145 */
146 inline T& operator[](int iPosition) {
147 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:
176 T* pData;
177 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
193
194 #endif // __ARRAYLIST_H__

  ViewVC Help
Powered by ViewVC