Home | History | Annotate | Download | only in utils
      1 /* Copyright (C) 2011 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_INTMAP_H
     13 #define _ANDROID_UTILS_INTMAP_H
     14 
     15 /* A simple container that can hold a simple mapping from integers to
     16  * references. I.e. a dictionary where keys are integers, and values
     17  * are liberal pointer values (NULL is allowed).
     18  */
     19 
     20 typedef struct AIntMap  AIntMap;
     21 
     22 /* Create new integer map */
     23 AIntMap*  aintMap_new(void);
     24 
     25 /* Returns the number of keys stored in the map */
     26 int       aintmap_getCount( AIntMap* map );
     27 
     28 /* Returns TRUE if the map has a value for the 'key'. Necessary because
     29  * NULL is a valid value for the map.
     30  */
     31 int       aintmap_has( AIntMap*  map, int key );
     32 
     33 /* Get the value associated with a 'key', or NULL if not in map */
     34 void*     aintMap_get( AIntMap*  map, int  key );
     35 
     36 /* Get the value associated with a 'key', or 'def' if not in map */
     37 void*     aintMap_getWithDefault( AIntMap*  map, int key, void*  def );
     38 
     39 /* Set the value associated to a 'key', return the old value, if any, or NULL */
     40 void*     aintMap_set( AIntMap* map, int key, void* value );
     41 
     42 /* Delete a given value associated to a 'key', return the old value, or NULL */
     43 void*     aintMap_del( AIntMap* map, int key );
     44 
     45 /* Destroy a given integer map */
     46 void      aintMap_free( AIntMap*  map );
     47 
     48 /* Integer map iterator. First call aintMapIterator_init(), then call
     49  * aintMapIterator_next() until it returns 0. Then finish with
     50  * aintMapIterator_done().
     51  *
     52  * Example:
     53  *    AIntMapIterator  iter[1];
     54  *    aintMapIterator_init(iter, map);
     55  *    while (aintMapIterator_next(iter, &key, &value)) {
     56  *        // do something
     57  *    }
     58  *    aintMapIterator_done(iter);
     59  */
     60 typedef struct AIntMapIterator {
     61     int    key;
     62     void*  value;
     63     void*  magic[4];
     64 } AIntMapIterator;
     65 
     66 /* Initialize iterator. Returns -1 if the map is empty, or 0 otherwise
     67  * On success, the first (key,value) pair can be read from the iterator
     68  * directly.
     69  */
     70 void aintMapIterator_init( AIntMapIterator* iter, AIntMap* map );
     71 
     72 /* Read the next (key,value) pair with an iterator, returns -1 when
     73  * there isn't anything more, or 0 otherwise. On success, the key and
     74  * value can be read directly from the iterator.
     75  */
     76 int  aintMapIterator_next( AIntMapIterator* iter );
     77 
     78 /* Finalize an iterator. This only needs to be called if you stop
     79  * the iteration before aintMapIterator_init() or aintMapIterator_next()
     80  * return -1.
     81  */
     82 void aintMapIterator_done( AIntMapIterator* iter );
     83 
     84 #define AINTMAP_FOREACH_KEY(map, keyvarname, stmnt) \
     85     do { \
     86         AIntMapIterator  __aintmap_foreach_iter[1]; \
     87         aintMapIterator_init(__aintmap_foreach_iter, (map)); \
     88         while (aintMapIterator_next(__aintmap_foreach_iter)) { \
     89             int keyvarname = __aintmap_foreach_iter->key; \
     90             stmnt; \
     91         } \
     92         aintMapIterator_done(__aintmap_foreach_iter); \
     93     } while (0)
     94 
     95 #define AINTMAP_FOREACH_VALUE(map, valvarname, stmnt) \
     96     do { \
     97         AIntMapIterator  __aintmap_foreach_iter[1]; \
     98         aintMapIterator_init(__aintmap_foreach_iter, (map)); \
     99         while (aintMapIterator_next(__aintmap_foreach_iter)) { \
    100             void* valvarname = __aintmap_foreach_iter->value; \
    101             stmnt; \
    102         } \
    103         aintMapIterator_done(__aintmap_foreach_iter); \
    104     } while (0)
    105 
    106 #endif /* _ANDROID_UTILS_INTMAP_H */
    107