]>
git.ipfire.org Git - thirdparty/squid.git/blob - include/Array.h
2 * $Id: Array.h,v 1.10 2003/01/23 00:36:46 robertc Exp $
4 * AUTHOR: Alex Rousskov
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 /* see Array.c for more documentation */
48 /* iterator support */
49 template <class C
> class VectorIteratorBase
{
52 VectorIteratorBase(C
&);
53 VectorIteratorBase(size_t, C
&);
54 VectorIteratorBase
& operator =(VectorIteratorBase
const &);
55 bool operator != (VectorIteratorBase
const &rhs
);
56 bool operator == (VectorIteratorBase
const &rhs
);
57 VectorIteratorBase
& operator ++();
58 VectorIteratorBase
operator ++(int);
59 typename
C::value_type
& operator *() const;
60 typename
C::value_type
* operator -> () const;
61 ssize_t
operator - (VectorIteratorBase
const &rhs
) const;
62 bool incrementable() const;
73 typedef VectorIteratorBase
<Vector
<E
> > iterator
;
74 typedef VectorIteratorBase
<Vector
<E
> const> const_iterator
;
76 void *operator new (size_t);
77 void operator delete (void *);
81 Vector(Vector
const &);
82 Vector
&operator = (Vector
const &);
84 void reserve (size_t capacity
);
88 void preAppend(int app_count
);
92 const_iterator
begin () const;
94 const_iterator
end () const;
101 typedef Vector
<void *> Array
;
105 SQUIDCEXTERN Array
*arrayCreate(void);
106 SQUIDCEXTERN
void arrayInit(Array
* s
);
107 SQUIDCEXTERN
void arrayClean(Array
* s
);
108 SQUIDCEXTERN
void arrayDestroy(Array
* s
);
109 SQUIDCEXTERN
void arrayAppend(Array
* s
, void *obj
);
110 SQUIDCEXTERN
void arrayPreAppend(Array
* s
, int app_count
);
116 Vector
<E
>::operator new(size_t size
)
118 return xmalloc (size
);
123 Vector
<E
>::operator delete (void *address
)
129 Vector
<E
>::Vector() : capacity (0), count(0), items (NULL
)
143 /* could also warn if some objects are left */
150 /* grows internal buffer to satisfy required minimal capacity */
153 Vector
<E
>::reserve(size_t min_capacity
)
155 const int min_delta
= 16;
157 if (capacity
>= min_capacity
)
159 delta
= min_capacity
;
160 /* make delta a multiple of min_delta */
161 delta
+= min_delta
- 1;
166 delta
= min_capacity
- capacity
;
167 E
*newitems
= new E
[capacity
+ delta
];
168 for (size_t counter
= 0; counter
< size(); ++counter
) {
169 newitems
[counter
] = items
[counter
];
178 Vector
<E
>::push_back(E obj
)
180 if (size() >= capacity
)
181 reserve (size() + 1);
182 items
[count
++] = obj
;
187 Vector
<E
>::pop_back()
190 value_type result
= items
[--count
];
191 items
[count
] = value_type();
200 return items
[size() - 1];
203 /* if you are going to append a known and large number of items, call this first */
206 Vector
<E
>::preAppend(int app_count
)
208 if (size() + app_count
> capacity
)
209 reserve(size() + app_count
);
214 Vector
<E
>::operator = (Vector
const &old
)
217 reserve (old
.size());
218 for (size_t counter
= 0; counter
< old
.size(); ++counter
)
219 push_back (old
.items
[counter
]);
225 Vector
<E
>::empty() const
232 Vector
<E
>::size() const
241 return iterator (0, *this);
248 return iterator(size(), *this);
252 Vector
<E
>::const_iterator
253 Vector
<E
>::begin() const
255 return const_iterator (0, *this);
259 Vector
<E
>::const_iterator
260 Vector
<E
>::end() const
262 return const_iterator(size(), *this);
267 VectorIteratorBase
<C
>::VectorIteratorBase() : pos(0), theVector(NULL
)
272 VectorIteratorBase
<C
>::VectorIteratorBase(C
&container
) : pos(container
.begin()), theVector(&container
)
277 VectorIteratorBase
<C
>::VectorIteratorBase(size_t aPos
, C
&container
) : pos(aPos
), theVector(&container
) {}
280 bool VectorIteratorBase
<C
>:: operator != (VectorIteratorBase
const &rhs
)
283 return pos
!= rhs
.pos
;
287 bool VectorIteratorBase
<C
>:: operator == (VectorIteratorBase
const &rhs
)
290 return pos
== rhs
.pos
;
295 VectorIteratorBase
<C
>::incrementable() const
298 return pos
!= theVector
->size();
302 VectorIteratorBase
<C
> & VectorIteratorBase
<C
>:: operator ++()
305 if (!incrementable())
306 fatal ("domain error");
312 VectorIteratorBase
<C
> VectorIteratorBase
<C
>:: operator ++(int)
314 VectorIteratorBase
result(*this);
320 typename
C::value_type
& VectorIteratorBase
<C
>::operator *() const
322 return theVector
->items
[pos
];
326 typename
C::value_type
* VectorIteratorBase
<C
>::operator -> () const
328 return &theVector
->items
[pos
];
332 VectorIteratorBase
<C
>&
333 VectorIteratorBase
<C
>::operator =(VectorIteratorBase
const &old
) {
335 theVector
= old
.theVector
;
341 VectorIteratorBase
<C
>::operator - (VectorIteratorBase
const &rhs
) const
343 assert(theVector
== rhs
.theVector
);
344 return pos
- rhs
.pos
;
348 #endif /* SQUID_ARRAY_H */