/[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 2581 - (hide annotations) (download) (as text)
Fri May 30 12:48:05 2014 UTC (9 years, 11 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 schoenebeck 458 /***************************************************************************
2     * *
3 schoenebeck 2500 * Copyright (C) 2005 - 2014 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 schoenebeck 2581 * 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 schoenebeck 458 * Remove all elements from the list.
109     */
110     void clear() {
111     if (pData) {
112 persson 835 delete[] pData;
113 schoenebeck 458 pData = NULL;
114     iSize = 0;
115     }
116     }
117    
118     /**
119     * Returns the index of the given \a element on the list.
120     *
121 schoenebeck 880 * @throws Exception - if \a element could not be found
122 schoenebeck 458 */
123     int find(const T& element) {
124     for (int i = 0; i < iSize; i++)
125     if (pData[i] == element) return i;
126 schoenebeck 880 throw Exception("ArrayList::find(): could not find given element");
127 schoenebeck 458 }
128    
129     /**
130     * Number of elements currently on the list.
131     */
132 schoenebeck 2500 inline int size() const {
133 schoenebeck 458 return iSize;
134     }
135    
136     /**
137     * Returns true if the list is empty.
138     */
139 schoenebeck 2500 inline bool empty() const {
140 schoenebeck 458 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 persson 2055 /**
151 schoenebeck 2500 * Access element at \a iPosition.
152     */
153     inline const T& operator[](int iPosition) const {
154     return pData[iPosition];
155     }
156    
157     /**
158 persson 2055 * 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 schoenebeck 458 private:
176     T* pData;
177     int iSize;
178 persson 2055
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 schoenebeck 458 };
191    
192     } // namespace LinuxSampler
193    
194     #endif // __ARRAYLIST_H__

  ViewVC Help
Powered by ViewVC