Home | History | Annotate | Download | only in utils
      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/system.h"
     16 #include "android/utils/assert.h"
     17 
     18 #define  AVECTOR_DECL(ctype,name)  \
     19     ctype*    name; \
     20     unsigned  num_##name; \
     21     unsigned  max_##name \
     22 
     23 #define  AVECTOR_SIZE(obj,name)  \
     24     (obj)->num_##name
     25 
     26 #define  AVECTOR_INIT(obj,name)   \
     27     do { \
     28         (obj)->name = NULL; \
     29         (obj)->num_##name = 0; \
     30         (obj)->max_##name = 0; \
     31     } while (0)
     32 
     33 #define  AVECTOR_INIT_ALLOC(obj,name,count) \
     34     do { \
     35         AARRAY_NEW0( (obj)->name, (count) ); \
     36         (obj)->num_##name = 0; \
     37         (obj)->max_##name = (count); \
     38     } while (0)
     39 
     40 #define  AVECTOR_DONE(obj,name)  \
     41     do { \
     42         AFREE((obj)->name); \
     43         (obj)->num_##name = 0; \
     44         (obj)->max_##name = 0; \
     45     } while (0)
     46 
     47 #define  AVECTOR_CLEAR(obj,name) \
     48     do { \
     49         (obj)->num_##name = 0; \
     50     } while (0)
     51 
     52 #define  AVECTOR_AT(obj,name,index)  \
     53     (&(obj)->name[(index)])
     54 
     55 #define  AVECTOR_REALLOC(obj,name,newMax) \
     56     do { \
     57         AARRAY_RENEW((obj)->name,newMax); \
     58         (obj)->max_##name = (newMax); \
     59     } while(0)
     60 
     61 #define  AVECTOR_ENSURE(obj,name,newCount) \
     62     do { \
     63         unsigned  _newCount = (newCount); \
     64         if (_newCount > (obj)->max_##name) \
     65             AASSERT_LOC(); \
     66             _avector_ensure( (void**)&(obj)->name, sizeof((obj)->name[0]), \
     67                              &(obj)->max_##name, _newCount ); \
     68     } while (0);
     69 
     70 extern void  _avector_ensure( void**  items, size_t  itemSize,
     71                               unsigned*  pMaxItems, unsigned  newCount );
     72 
     73 #define  AVECTOR_FOREACH(obj,name,itemptr,statement) \
     74     do { \
     75         unsigned __vector_nn = 0; \
     76         unsigned __vector_max = (obj)->num_##name; \
     77         for ( ; __vector_nn < __vector_max; __vector_nn++ ) { \
     78             itemptr = &(obj)->name[__vector_nn]; \
     79             statement; \
     80         } \
     81     } while (0);
     82 
     83 /* */
     84 
     85 #endif /* _ANDROID_UTILS_VECTOR_H */
     86