~vanvugt/ubuntu/oneiric/mediatomb/fix-770964-784431

« back to all changes in this revision

Viewing changes to src/zmmf/base_array.h

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2008-02-02 01:42:48 UTC
  • Revision ID: james.westby@ubuntu.com-20080202014248-cjouolddb8gi2zkz
Tags: upstream-0.10.0.dfsg1
ImportĀ upstreamĀ versionĀ 0.10.0.dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*MT*
 
2
    
 
3
    MediaTomb - http://www.mediatomb.cc/
 
4
    
 
5
    base_array.h - this file is part of MediaTomb.
 
6
    
 
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
 
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
 
9
    
 
10
    Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
 
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
 
12
                            Leonhard Wimmer <leo@mediatomb.cc>
 
13
    
 
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.
 
17
    
 
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.
 
22
    
 
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.
 
26
    
 
27
    $Id$
 
28
*/
 
29
 
 
30
/// \file base_array.h
 
31
 
 
32
#ifndef __ZMMF_BASE_ARRAY_H__
 
33
#define __ZMMF_BASE_ARRAY_H__
 
34
 
 
35
#include "zmm/zmm.h"
 
36
#include "memory.h"
 
37
 
 
38
#define DEFAULT_ARRAY_CAPACITY 16
 
39
 
 
40
namespace zmm
 
41
{
 
42
 
 
43
template <class T>
 
44
class BaseArray : public Object
 
45
{
 
46
public:
 
47
    BaseArray() : Object()
 
48
    {
 
49
        _init(DEFAULT_ARRAY_CAPACITY);
 
50
    }
 
51
    
 
52
    BaseArray(int capacity) : Object()
 
53
    {
 
54
        _init(capacity);
 
55
    }
 
56
    
 
57
    void _init(int capacity)
 
58
    {
 
59
        this->capacity = capacity;
 
60
        siz = 0;
 
61
        arr = (T *)MALLOC(capacity * sizeof(T));
 
62
    }
 
63
    
 
64
    ~BaseArray()
 
65
    {
 
66
        if (arr)
 
67
            FREE(arr);
 
68
    }
 
69
    
 
70
    void append(T el)
 
71
    {
 
72
        resize(siz+1);
 
73
        arr[siz++] = el;
 
74
    }
 
75
    
 
76
    void set(T el, int index)
 
77
    {
 
78
        arr[index] = el;
 
79
    }
 
80
    
 
81
    T get(int index)
 
82
    {
 
83
        return arr[index];
 
84
    }
 
85
    
 
86
    void remove(int index, int count=1)
 
87
    {
 
88
        if (index < 0 || index >= siz) // index beyond size
 
89
            return;
 
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
 
92
            max = siz;
 
93
        if (max <= index) // if nothing to remove
 
94
            return;
 
95
        int move = siz - max;
 
96
        if (move) // if there is anything to shift
 
97
        {
 
98
            memmove(
 
99
                (void *)(arr + index),
 
100
                (void *)(arr + index + count),
 
101
                move * sizeof(T)
 
102
            );
 
103
        }
 
104
        siz -= count;
 
105
    }
 
106
    
 
107
    void removeUnordered(int index)
 
108
    {
 
109
        if (index < 0 || index >= siz) // index beyond size
 
110
            return;
 
111
        arr[index] = arr[--siz];
 
112
    }
 
113
    
 
114
    void insert(int index, T el)
 
115
    {
 
116
        resize(siz + 1);
 
117
        memmove(
 
118
            (void *)(arr + (index + 1)),
 
119
            (void *)(arr + index),
 
120
            (siz - index) * sizeof(T)
 
121
        );
 
122
        arr[index] = el;
 
123
        siz++;
 
124
    }
 
125
    
 
126
    int size()
 
127
    {
 
128
        return siz;
 
129
    }
 
130
    
 
131
    void resize(int requiredSize)
 
132
    {
 
133
        if(requiredSize > capacity)
 
134
        {
 
135
            int newCapacity = siz + (siz / 2);
 
136
            if(requiredSize > newCapacity)
 
137
                newCapacity = requiredSize;
 
138
            capacity = newCapacity;
 
139
            arr = (T *)REALLOC(arr, capacity * sizeof(T));
 
140
        }
 
141
    }
 
142
    
 
143
    
 
144
    /*
 
145
    void optimize()
 
146
    {
 
147
        
 
148
    }
 
149
    
 
150
    Object **getObjectArray()
 
151
    {
 
152
        return base.arr;
 
153
    }
 
154
    */
 
155
    
 
156
protected:
 
157
    T* arr;
 
158
    int siz;
 
159
    int capacity;
 
160
};
 
161
 
 
162
class IntArray : public BaseArray<int>
 
163
{
 
164
public:
 
165
    String toCSV(char sep = ',')
 
166
    {
 
167
        Ref<StringBuffer> buf(new StringBuffer());
 
168
        for (int i = 0; i < siz; i++)
 
169
            *buf << sep << get(i);
 
170
        if (buf->length() <= 0)
 
171
            return _("");
 
172
        return buf->toString(1);
 
173
    }
 
174
    
 
175
    void addCSV(String csv, char sep = ',')
 
176
    {
 
177
        char *data = csv.c_str();
 
178
        char *dataEnd = data + csv.length();
 
179
        while (data < dataEnd)
 
180
        {
 
181
            char *endptr;
 
182
            int val = (int)strtol(data, &endptr, 10);
 
183
            if (endptr == data)
 
184
                throw _Exception(_("illegal csv given to IntArray"));
 
185
            append(val);
 
186
            if (endptr >= dataEnd)
 
187
                break;
 
188
            if (*endptr == sep)
 
189
                data = endptr + 1;
 
190
            else
 
191
                throw _Exception(_("illegal csv given to IntArray"));
 
192
        }
 
193
    }
 
194
};
 
195
 
 
196
} // namespace
 
197
 
 
198
#endif // __ZMMF_BASE_ARRAY_H__