File: Array.h

package info (click to toggle)
squid3 3.0.PRE5-5%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 21,188 kB
  • ctags: 20,388
  • sloc: cpp: 119,851; ansic: 30,259; sh: 10,465; makefile: 3,289; perl: 1,267; awk: 84; xml: 58
file content (401 lines) | stat: -rw-r--r-- 8,222 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * $Id: Array.h,v 1.24 2005/11/21 22:43:41 wessels Exp $
 *
 * AUTHOR: Alex Rousskov
 *
 * SQUID Web Proxy Cache          http://www.squid-cache.org/
 * ----------------------------------------------------------
 *
 *  Squid is the result of efforts by numerous individuals from
 *  the Internet community; see the CONTRIBUTORS file for full
 *  details.   Many organizations have provided support for Squid's
 *  development; see the SPONSORS file for full details.  Squid is
 *  Copyrighted (C) 2001 by the Regents of the University of
 *  California; see the COPYRIGHT file for full details.  Squid
 *  incorporates software developed and/or copyrighted by other
 *  sources; see the CREDITS file for full details.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *  
 */

#ifndef SQUID_ARRAY_H
#define SQUID_ARRAY_H

#include "fatal.h"
#include "util.h"

/* iterator support */

template <class C>

class VectorIteratorBase
{

public:
    VectorIteratorBase();
    VectorIteratorBase(C &);
    VectorIteratorBase(size_t, C &);
    VectorIteratorBase & operator =(VectorIteratorBase const &);
    bool operator != (VectorIteratorBase const &rhs);
    bool operator == (VectorIteratorBase const &rhs);
    VectorIteratorBase & operator ++();
    VectorIteratorBase operator ++(int);
    typename C::value_type & operator *() const
    {
        return theVector->items[pos];
    }

    typename C::value_type * operator -> () const
    {
        return &theVector->items[pos];
    }

    ssize_t operator - (VectorIteratorBase const &rhs) const;
    bool incrementable() const;

private:
    size_t pos;
    C * theVector;
};

template<class E>

class Vector
{

public:
    typedef E value_type;
    typedef E* pointer;
    typedef VectorIteratorBase<Vector<E> > iterator;
    typedef VectorIteratorBase<Vector<E> const> const_iterator;

    void *operator new (size_t);
    void operator delete (void *);

    Vector();
    ~Vector();
    Vector(Vector const &);
    Vector &operator = (Vector const &);
    void clean();
    void reserve (size_t capacity);
    void push_back (E);
    Vector &operator += (E item) {push_back(item); return *this;};

    void insert (E);
    E &back();
    E pop_back();
    E shift();         // aka pop_front
    void preAppend(int app_count);
    bool empty() const;
    size_t size() const;
    iterator begin();
    const_iterator begin () const;
    iterator end();
    const_iterator end () const;
    E& operator [] (unsigned i);

    /* Do not change these, until the entry C struct is removed */
    size_t capacity;
    size_t count;
    E *items;
};

template<class E>
void *
Vector<E>::operator new(size_t size)
{
    return xmalloc (size);
}

template<class E>
void
Vector<E>::operator delete (void *address)
{
    xfree (address);
}

template<class E>
Vector<E>::Vector() : capacity (0), count(0), items (NULL)
{}

template<class E>
Vector<E>::~Vector()
{
    clean();
}

template<class E>
void
Vector<E>::clean()
{
    /* could also warn if some objects are left */
    delete[] items;
    items = NULL;
    capacity = 0;
    count = 0;
}

/* grows internal buffer to satisfy required minimal capacity */
template<class E>
void
Vector<E>::reserve(size_t min_capacity)
{
    const int min_delta = 16;
    int delta;

    if (capacity >= min_capacity)
        return;

    delta = min_capacity;

    /* make delta a multiple of min_delta */
    delta += min_delta - 1;

    delta /= min_delta;

    delta *= min_delta;

    /* actual grow */
    if (delta < 0)
        delta = min_capacity - capacity;

    E*newitems = new E[capacity + delta];

    for (size_t counter = 0; counter < size(); ++counter) {
        newitems[counter] = items[counter];
    }

    capacity += delta;
    delete[]items;
    items = newitems;
}

template<class E>
void
Vector<E>::push_back(E obj)
{
    if (size() >= capacity)
        reserve (size() + 1);

    items[count++] = obj;
}

template<class E>
void
Vector<E>::insert(E obj)
{
    if (size() >= capacity)
        reserve (size() + 1);

    int i;

    for (i = count; i > 0; i--)
        items[i] = items[i - 1];

    items[i] = obj;

    count += 1;
}

template<class E>
E
Vector<E>::shift()
{
    assert (size());
    value_type result = items[0];

    for (unsigned int i = 1; i < count; i++)
        items[i-1] = items[i];

    count--;

    return result;
}

template<class E>
E
Vector<E>::pop_back()
{
    assert (size());
    value_type result = items[--count];
    items[count] = value_type();
    return result;
}

template<class E>
E &
Vector<E>::back()
{
    assert (size());
    return items[size() - 1];
}

/* if you are going to append a known and large number of items, call this first */
template<class E>
void
Vector<E>::preAppend(int app_count)
{
    if (size() + app_count > capacity)
        reserve(size() + app_count);
}

template<class E>
Vector<E>::Vector (Vector<E> const &rhs)
{
    items = NULL;
    capacity = 0;
    count = 0;
    reserve (rhs.size());

    for (size_t counter = 0; counter < rhs.size(); ++counter)
        push_back (rhs.items[counter]);
}

template<class E>
Vector<E> &
Vector<E>::operator = (Vector<E> const &old)
{
    clean();
    reserve (old.size());

    for (size_t counter = 0; counter < old.size(); ++counter)
        push_back (old.items[counter]);

    return *this;
}

template<class E>
bool
Vector<E>::empty() const
{
    return size() == 0;
}

template<class E>
size_t
Vector<E>::size() const
{
    return count;
}

template<class E>
typename Vector<E>::iterator
Vector<E>::begin()
{
    return iterator (0, *this);
}

template<class E>
typename Vector<E>::iterator
Vector<E>::end()
{
    return iterator(size(), *this);
}

template<class E>
typename Vector<E>::const_iterator
Vector<E>::begin() const
{
    return const_iterator (0, *this);
}

template<class E>
typename Vector<E>::const_iterator
Vector<E>::end() const
{
    return const_iterator(size(), *this);
}

template<class E>
E &
Vector<E>::operator [] (unsigned i)
{
    assert (size() > i);
    return items[i];
}

template<class C>
VectorIteratorBase<C>::VectorIteratorBase() : pos(0), theVector(NULL)
{}

template<class C>
VectorIteratorBase<C>::VectorIteratorBase(C &container) : pos(container.begin()), theVector(&container)
{}

template<class C>
VectorIteratorBase<C>::VectorIteratorBase(size_t aPos, C &container) : pos(aPos), theVector(&container) {}

template<class C>
bool VectorIteratorBase<C>:: operator != (VectorIteratorBase<C> const &rhs)
{
    assert (theVector);
    return pos != rhs.pos;
}

template<class C>
bool VectorIteratorBase<C>:: operator == (VectorIteratorBase<C> const &rhs)
{
    assert (theVector);
    return pos == rhs.pos;
}

template<class C>
bool
VectorIteratorBase<C>::incrementable() const
{
    assert (theVector);
    return pos != theVector->size();
}

template<class C>
VectorIteratorBase<C> & VectorIteratorBase<C>:: operator ++()
{
    assert (theVector);

    if (!incrementable())
        fatal ("domain error");

    ++pos;

    return *this;
}

template<class C>
VectorIteratorBase<C> VectorIteratorBase<C>:: operator ++(int)
{
    VectorIteratorBase result(*this);
    ++*this;
    return result;
}

template<class C>
VectorIteratorBase<C>&
VectorIteratorBase<C>::operator =(VectorIteratorBase<C> const &old)
{
    pos = old.pos;
    theVector = old.theVector;
    return *this;
}

template<class C>
ssize_t
VectorIteratorBase<C>::operator - (VectorIteratorBase<C> const &rhs) const
{
    assert(theVector == rhs.theVector);
    return pos - rhs.pos;
}

#endif /* SQUID_ARRAY_H */