/[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 2500 - (hide annotations) (download) (as text)
Fri Jan 10 12:20:05 2014 UTC (5 years, 11 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 6130 byte(s)
* Added support for multiple MIDI input ports per sampler channel (added
  various new C++ API methods for this new feature/design, old C++ API
  methods are now marked as deprecated but should still provide full
  behavior backward compatibility).
* LSCP Network interface: Added the following new LSCP commands for the new
  feature mentioned above: "ADD CHANNEL MIDI_INPUT",
  "REMOVE CHANNEL MIDI_INPUT" and "LIST CHANNEL MIDI_INPUTS". As with the
  C++ API changes, the old LSCP commands for MIDI input management are now
  marked as deprecated, but are still there and should provide full behavior
  backward compatibility.
* New LSCP specification document (LSCP v1.6).
* AbstractEngine::GSCheckSum(): don't allocate memory on the stack (was
  unsafe and caused compilation error with old clang 2.x).
* Bumped version (1.0.0.svn25).

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     * 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 schoenebeck 2500 inline int size() const {
118 schoenebeck 458 return iSize;
119     }
120    
121     /**
122     * Returns true if the list is empty.
123     */
124 schoenebeck 2500 inline bool empty() const {
125 schoenebeck 458 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 schoenebeck 2500 * Access element at \a iPosition.
137     */
138     inline const T& operator[](int iPosition) const {
139     return pData[iPosition];
140     }
141    
142     /**
143 persson 2055 * Copy constructor.
144     */
145     ArrayList(const ArrayList& list) {
146     copy(list);
147     }
148    
149     /**
150     * Assignment.
151     */
152     ArrayList& operator=(const ArrayList& list) {
153     if (this != &list) {
154     clear();
155     copy(list);
156     }
157     return *this;
158     }
159    
160 schoenebeck 458 private:
161     T* pData;
162     int iSize;
163 persson 2055
164     void copy(const ArrayList& list) {
165     iSize = list.iSize;
166     if (list.pData) {
167     pData = new T[iSize];
168     for (int i = 0 ; i < iSize ; i++) {
169     pData[i] = list.pData[i];
170     }
171     } else {
172     pData = NULL;
173     }
174     }
175 schoenebeck 458 };
176    
177     } // namespace LinuxSampler
178    
179     #endif // __ARRAYLIST_H__

  ViewVC Help
Powered by ViewVC