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_REFSET_H
     13 #define _ANDROID_UTILS_REFSET_H
     14 
     15 #include <android/utils/vector.h>
     16 
     17 /* this implements a set of addresses in memory.
     18  * NULL cannot be stored in the set.
     19  */
     20 
     21 typedef struct {
     22     AVECTOR_DECL(void*,buckets);
     23     int iteration;
     24 } ARefSet;
     25 
     26 AINLINED void
     27 arefSet_init( ARefSet*  s )
     28 {
     29     AVECTOR_INIT(s,buckets);
     30 }
     31 
     32 AINLINED void
     33 arefSet_done( ARefSet*  s )
     34 {
     35     AVECTOR_DONE(s,buckets);
     36 }
     37 
     38 AINLINED void
     39 arefSet_clear( ARefSet*  s )
     40 {
     41     AVECTOR_CLEAR(s,buckets);
     42     s->iteration = 0;
     43 }
     44 
     45 AINLINED int
     46 arefSet_count( ARefSet*  s )
     47 {
     48     return (int) AVECTOR_SIZE(s,buckets);
     49 }
     50 
     51 extern ABool  arefSet_has( ARefSet*  s, void*  item );
     52 extern void   arefSet_add( ARefSet*  s, void*  item );
     53 extern void   arefSet_del( ARefSet*  s, void*  item );
     54 
     55 extern void   _arefSet_removeDeferred( ARefSet* s );
     56 
     57 #define  AREFSET_DELETED ((void*)~(size_t)0)
     58 
     59 #define  AREFSET_FOREACH(_set,_item,_statement) \
     60     do { \
     61         int  __refset_nn = 0; \
     62         int  __refset_max = (_set)->max_buckets; \
     63         (_set)->iteration += 2; \
     64         for ( ; __refset_nn < __refset_max; __refset_nn++ ) { \
     65             void*  __refset_item = (_set)->buckets[__refset_nn]; \
     66             if (__refset_item == NULL || __refset_item == AREFSET_DELETED) \
     67                 continue; \
     68             _item = __refset_item; \
     69             _statement; \
     70         } \
     71         (_set)->iteration -= 2; \
     72         if ((_set)->iteration == 1) \
     73             _arefSet_removeDeferred(_set); \
     74     } while (0)
     75 
     76 #endif /* _ANDROID_UTILS_REFSET_H */
     77