3
MediaTomb - http://www.mediatomb.cc/
5
base_array.h - this file is part of MediaTomb.
7
Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
8
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
10
Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
11
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
12
Leonhard Wimmer <leo@mediatomb.cc>
14
MediaTomb is free software; you can redistribute it and/or modify
15
it under the terms of the GNU General Public License version 2
16
as published by the Free Software Foundation.
18
MediaTomb is distributed in the hope that it will be useful,
19
but WITHOUT ANY WARRANTY; without even the implied warranty of
20
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
GNU General Public License for more details.
23
You should have received a copy of the GNU General Public License
24
version 2 along with MediaTomb; if not, write to the Free Software
25
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
30
/// \file base_array.h
32
#ifndef __ZMMF_BASE_ARRAY_H__
33
#define __ZMMF_BASE_ARRAY_H__
38
#define DEFAULT_ARRAY_CAPACITY 16
44
class BaseArray : public Object
47
BaseArray() : Object()
49
_init(DEFAULT_ARRAY_CAPACITY);
52
BaseArray(int capacity) : Object()
57
void _init(int capacity)
59
this->capacity = capacity;
61
arr = (T *)MALLOC(capacity * sizeof(T));
76
void set(T el, int index)
86
void remove(int index, int count=1)
88
if (index < 0 || index >= siz) // index beyond size
90
int max = index + count; // max is the last element to remove + 1
91
if (max > siz) // if remove block is beyond size, cut it
93
if (max <= index) // if nothing to remove
96
if (move) // if there is anything to shift
99
(void *)(arr + index),
100
(void *)(arr + index + count),
107
void removeUnordered(int index)
109
if (index < 0 || index >= siz) // index beyond size
111
arr[index] = arr[--siz];
114
void insert(int index, T el)
118
(void *)(arr + (index + 1)),
119
(void *)(arr + index),
120
(siz - index) * sizeof(T)
131
void resize(int requiredSize)
133
if(requiredSize > capacity)
135
int newCapacity = siz + (siz / 2);
136
if(requiredSize > newCapacity)
137
newCapacity = requiredSize;
138
capacity = newCapacity;
139
arr = (T *)REALLOC(arr, capacity * sizeof(T));
150
Object **getObjectArray()
162
class IntArray : public BaseArray<int>
165
String toCSV(char sep = ',')
167
Ref<StringBuffer> buf(new StringBuffer());
168
for (int i = 0; i < siz; i++)
169
*buf << sep << get(i);
170
if (buf->length() <= 0)
172
return buf->toString(1);
175
void addCSV(String csv, char sep = ',')
177
char *data = csv.c_str();
178
char *dataEnd = data + csv.length();
179
while (data < dataEnd)
182
int val = (int)strtol(data, &endptr, 10);
184
throw _Exception(_("illegal csv given to IntArray"));
186
if (endptr >= dataEnd)
191
throw _Exception(_("illegal csv given to IntArray"));
198
#endif // __ZMMF_BASE_ARRAY_H__