|
|
|
This file is part of LinuxSampler, which is licensed under the GNU GPL with the exception that USAGE of the source code, libraries and applications FOR COMMERCIAL HARDWARE OR SOFTWARE PRODUCTS IS NOT ALLOWED without prior written permission by the LinuxSampler authors. If you have questions on the subject, that are not yet covered by the FAQ, please contact us.
1 schoenebeck 1.1 /*************************************************************************** 2 * * 3 * Copyright (C) 2005 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 schoenebeck 1.1 #define __LS_ARRAYLIST_H__ 23 24 #include "LinuxSamplerException.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 schoenebeck 1.1 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 1.2 delete[] pData; | ||
55 schoenebeck 1.1 } 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 LinuxSamplerException - if \a iPosition is out of range 65 */ 66 void remove(int iPosition) throw (LinuxSamplerException) { 67 if (iPosition < 0 || iPosition >= iSize) 68 throw LinuxSamplerException("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 schoenebeck 1.1 } | ||
77 persson 1.2 delete[] pData; | ||
78 schoenebeck 1.1 pData = pNewArray; 79 --iSize; 80 } 81 } 82 83 /** 84 * Remove the given \a element from the list. 85 * 86 * @throws LinuxSamplerException - if \a element could not be found 87 */ 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 1.2 delete[] pData; | ||
98 schoenebeck 1.1 pData = NULL; 99 iSize = 0; 100 } 101 } 102 103 /** 104 * Returns the index of the given \a element on the list. 105 * 106 * @throws LinuxSamplerException - if \a element could not be found 107 */ 108 int find(const T& element) { 109 for (int i = 0; i < iSize; i++) 110 if (pData[i] == element) return i; 111 throw LinuxSamplerException("ArrayList::find(): could not find given element"); 112 } 113 114 /** 115 * Number of elements currently on the list. 116 */ 117 inline int size() { 118 return iSize; 119 schoenebeck 1.1 } 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 private: 136 T* pData; 137 int iSize; 138 }; 139 140 schoenebeck 1.1 } // namespace LinuxSampler 141 142 #endif // __ARRAYLIST_H__ |
| LinuxSampler Developers |
Powered by ViewCVS |