1/* Copyright (C) 2009 The Android Open Source Project
2**
3** This software is licensed under the terms of the GNU General Public
4** License version 2, as published by the Free Software Foundation, and
5** may be copied, distributed, and modified under those terms.
6**
7** This program is distributed in the hope that it will be useful,
8** but WITHOUT ANY WARRANTY; without even the implied warranty of
9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10** GNU General Public License for more details.
11*/
12#ifndef _ANDROID_UTILS_VECTOR_H
13#define _ANDROID_UTILS_VECTOR_H
14
15#include "android/utils/assert.h"
16#include "android/utils/compiler.h"
17#include "android/utils/system.h"
18
19ANDROID_BEGIN_HEADER
20
21#define  AVECTOR_DECL(ctype,name)  \
22    ctype*    name; \
23    unsigned  num_##name; \
24    unsigned  max_##name \
25
26#define  AVECTOR_SIZE(obj,name)  \
27    (obj)->num_##name
28
29#define  AVECTOR_INIT(obj,name)   \
30    do { \
31        (obj)->name = NULL; \
32        (obj)->num_##name = 0; \
33        (obj)->max_##name = 0; \
34    } while (0)
35
36#define  AVECTOR_INIT_ALLOC(obj,name,count) \
37    do { \
38        AARRAY_NEW0( (obj)->name, (count) ); \
39        (obj)->num_##name = 0; \
40        (obj)->max_##name = (count); \
41    } while (0)
42
43#define  AVECTOR_DONE(obj,name)  \
44    do { \
45        AFREE((obj)->name); \
46        (obj)->num_##name = 0; \
47        (obj)->max_##name = 0; \
48    } while (0)
49
50#define  AVECTOR_CLEAR(obj,name) \
51    do { \
52        (obj)->num_##name = 0; \
53    } while (0)
54
55#define  AVECTOR_AT(obj,name,index)  \
56    (&(obj)->name[(index)])
57
58#define  AVECTOR_REALLOC(obj,name,newMax) \
59    do { \
60        AARRAY_RENEW((obj)->name,newMax); \
61        (obj)->max_##name = (newMax); \
62    } while(0)
63
64#define  AVECTOR_ENSURE(obj,name,newCount) \
65    do { \
66        unsigned  _newCount = (newCount); \
67        if (_newCount > (obj)->max_##name) \
68            AASSERT_LOC(); \
69            _avector_ensure( (void**)&(obj)->name, sizeof((obj)->name[0]), \
70                             &(obj)->max_##name, _newCount ); \
71    } while (0);
72
73extern void  _avector_ensure( void**  items, size_t  itemSize,
74                              unsigned*  pMaxItems, unsigned  newCount );
75
76#define  AVECTOR_FOREACH(obj,name,itemptr,statement) \
77    do { \
78        unsigned __vector_nn = 0; \
79        unsigned __vector_max = (obj)->num_##name; \
80        for ( ; __vector_nn < __vector_max; __vector_nn++ ) { \
81            itemptr = &(obj)->name[__vector_nn]; \
82            statement; \
83        } \
84    } while (0);
85
86ANDROID_END_HEADER
87
88#endif /* _ANDROID_UTILS_VECTOR_H */
89