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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2055 - (hide annotations) (download) (as text)
Sat Jan 30 10:30:02 2010 UTC (14 years, 3 months ago) by persson
File MIME type: text/x-c++hdr
File size: 5921 byte(s)
* sfz engine: added support for v2 multiple stage envelope generators
* sfz engine: added a fine-tuned v1 envelope generator instead of
  using the one from the gig engine

1 schoenebeck 458 /***************************************************************************
2     * *
3 persson 2055 * Copyright (C) 2005 - 2010 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 458
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 persson 835 delete[] pData;
55 schoenebeck 458 }
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 schoenebeck 880 * @throws Exception - if \a iPosition is out of range
65 schoenebeck 458 */
66 schoenebeck 880 void remove(int iPosition) throw (Exception) {
67 schoenebeck 458 if (iPosition < 0 || iPosition >= iSize)
68 schoenebeck 880 throw Exception("ArrayList::remove(): index out of range");
69 schoenebeck 458 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 persson 835 delete[] pData;
78 schoenebeck 458 pData = pNewArray;
79     --iSize;
80     }
81     }
82    
83     /**
84     * Remove the given \a element from the list.
85     *
86 schoenebeck 880 * @throws Exception - if \a element could not be found
87 schoenebeck 458 */
88     void remove(const T& element) {
89     remove(find(element));
90     }
91    
92     /**
93     * Remove all elements from the list.
94     */
95     void clear() {
96     if (pData) {
97 persson 835 delete[] pData;
98 schoenebeck 458 pData = NULL;
99     iSize = 0;
100     }
101     }
102    
103     /**
104     * Returns the index of the given \a element on the list.
105     *
106 schoenebeck 880 * @throws Exception - if \a element could not be found
107 schoenebeck 458 */
108     int find(const T& element) {
109     for (int i = 0; i < iSize; i++)
110     if (pData[i] == element) return i;
111 schoenebeck 880 throw Exception("ArrayList::find(): could not find given element");
112 schoenebeck 458 }
113    
114     /**
115     * Number of elements currently on the list.
116     */
117     inline int size() {
118     return iSize;
119     }
120    
121     /**
122     * Returns true if the list is empty.
123     */
124     inline bool empty() {
125     return (bool) !iSize;
126     }
127    
128     /**
129     * Access element at \a iPosition.
130     */
131     inline T& operator[](int iPosition) {
132     return pData[iPosition];
133     }
134    
135 persson 2055 /**
136     * Copy constructor.
137     */
138     ArrayList(const ArrayList& list) {
139     copy(list);
140     }
141    
142     /**
143     * Assignment.
144     */
145     ArrayList& operator=(const ArrayList& list) {
146     if (this != &list) {
147     clear();
148     copy(list);
149     }
150     return *this;
151     }
152    
153 schoenebeck 458 private:
154     T* pData;
155     int iSize;
156 persson 2055
157     void copy(const ArrayList& list) {
158     iSize = list.iSize;
159     if (list.pData) {
160     pData = new T[iSize];
161     for (int i = 0 ; i < iSize ; i++) {
162     pData[i] = list.pData[i];
163     }
164     } else {
165     pData = NULL;
166     }
167     }
168 schoenebeck 458 };
169    
170     } // namespace LinuxSampler
171    
172     #endif // __ARRAYLIST_H__

  ViewVC Help
Powered by ViewVC