1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2005 - 2017 Christian Schoenebeck * |
* Copyright (C) 2005 - 2019 Christian Schoenebeck * |
4 |
* * |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
50 |
void add(T element) { |
void add(T element) { |
51 |
T* pNewArray = new T[iSize + 1]; |
T* pNewArray = new T[iSize + 1]; |
52 |
if (pData) { |
if (pData) { |
53 |
for (int i = 0; i < iSize; i++) |
for (ssize_t i = 0; i < iSize; i++) |
54 |
pNewArray[i] = pData[i]; |
pNewArray[i] = pData[i]; |
55 |
delete[] pData; |
delete[] pData; |
56 |
} |
} |
64 |
* |
* |
65 |
* @throws Exception - if \a iPosition is out of range |
* @throws Exception - if \a iPosition is out of range |
66 |
*/ |
*/ |
67 |
void remove(int iPosition) throw (Exception) { |
void remove(ssize_t iPosition) throw (Exception) { |
68 |
if (iPosition < 0 || iPosition >= iSize) |
if (iPosition < 0 || iPosition >= iSize) |
69 |
throw Exception("ArrayList::remove(): index out of range"); |
throw Exception("ArrayList::remove(): index out of range"); |
70 |
if (iSize == 1) clear(); |
if (iSize == 1) clear(); |
71 |
else if (pData) { |
else if (pData) { |
72 |
T* pNewArray = new T[iSize - 1]; |
T* pNewArray = new T[iSize - 1]; |
73 |
for (int iSrc = 0, iDst = 0; iSrc < iSize; iSrc++) { |
for (ssize_t iSrc = 0, iDst = 0; iSrc < iSize; iSrc++) { |
74 |
if (iSrc == iPosition) continue; |
if (iSrc == iPosition) continue; |
75 |
pNewArray[iDst] = pData[iSrc]; |
pNewArray[iDst] = pData[iSrc]; |
76 |
++iDst; |
++iDst; |
94 |
* Increase or decrease the size of this list to the new amount of |
* Increase or decrease the size of this list to the new amount of |
95 |
* elements given by \a cnt. |
* elements given by \a cnt. |
96 |
*/ |
*/ |
97 |
void resize(int cnt) { |
void resize(ssize_t cnt) { |
98 |
T* pNewArray = new T[cnt]; |
T* pNewArray = new T[cnt]; |
99 |
if (pData) { |
if (pData) { |
100 |
for (int i = 0; i < cnt; i++) |
for (ssize_t i = 0; i < cnt; i++) |
101 |
pNewArray[i] = pData[i]; |
pNewArray[i] = pData[i]; |
102 |
delete[] pData; |
delete[] pData; |
103 |
} |
} |
121 |
* |
* |
122 |
* @throws Exception - if \a element could not be found |
* @throws Exception - if \a element could not be found |
123 |
*/ |
*/ |
124 |
int find(const T& element) { |
ssize_t find(const T& element) { |
125 |
for (int i = 0; i < iSize; i++) |
for (ssize_t i = 0; i < iSize; i++) |
126 |
if (pData[i] == element) return i; |
if (pData[i] == element) return i; |
127 |
throw Exception("ArrayList::find(): could not find given element"); |
throw Exception("ArrayList::find(): could not find given element"); |
128 |
} |
} |
130 |
/** |
/** |
131 |
* Number of elements currently on the list. |
* Number of elements currently on the list. |
132 |
*/ |
*/ |
133 |
inline int size() const { |
inline ssize_t size() const { |
134 |
return iSize; |
return iSize; |
135 |
} |
} |
136 |
|
|
144 |
/** |
/** |
145 |
* Access element at \a iPosition. |
* Access element at \a iPosition. |
146 |
*/ |
*/ |
147 |
inline T& operator[](int iPosition) { |
inline T& operator[](ssize_t iPosition) { |
148 |
return pData[iPosition]; |
return pData[iPosition]; |
149 |
} |
} |
150 |
|
|
151 |
/** |
/** |
152 |
* Access element at \a iPosition. |
* Access element at \a iPosition. |
153 |
*/ |
*/ |
154 |
inline const T& operator[](int iPosition) const { |
inline const T& operator[](ssize_t iPosition) const { |
155 |
return pData[iPosition]; |
return pData[iPosition]; |
156 |
} |
} |
157 |
|
|
182 |
* given @a list to this list. |
* given @a list to this list. |
183 |
*/ |
*/ |
184 |
void copyFlatFrom(const ArrayList& list) { |
void copyFlatFrom(const ArrayList& list) { |
185 |
const int n = (size() < list.size()) ? size() : list.size(); |
const ssize_t n = (size() < list.size()) ? size() : list.size(); |
186 |
memcpy(pData, list.pData, n * sizeof(T)); |
memcpy(pData, list.pData, n * sizeof(T)); |
187 |
} |
} |
188 |
|
|
189 |
private: |
private: |
190 |
T* pData; |
T* pData; |
191 |
int iSize; |
ssize_t iSize; |
192 |
|
|
193 |
void copy(const ArrayList& list) { |
void copy(const ArrayList& list) { |
194 |
iSize = list.iSize; |
iSize = list.iSize; |
195 |
if (list.pData) { |
if (list.pData) { |
196 |
pData = new T[iSize]; |
pData = new T[iSize]; |
197 |
for (int i = 0 ; i < iSize ; i++) { |
for (ssize_t i = 0 ; i < iSize ; i++) { |
198 |
pData[i] = list.pData[i]; |
pData[i] = list.pData[i]; |
199 |
} |
} |
200 |
} else { |
} else { |