1 /* 2 * Copyright (c) 2007, Novell Inc. 3 * 4 * This program is licensed under the BSD license, read LICENSE.BSD 5 * for further information 6 */ 7 #ifndef LIBSOLV_DIRPOOL_H 8 #define LIBSOLV_DIRPOOL_H 9 10 11 #include "pooltypes.h" 12 #include "util.h" 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 typedef struct _Dirpool { 19 Id *dirs; 20 int ndirs; 21 Id *dirtraverse; 22 } Dirpool; 23 24 void dirpool_init(Dirpool *dp); 25 void dirpool_free(Dirpool *dp); 26 27 void dirpool_make_dirtraverse(Dirpool *dp); 28 Id dirpool_add_dir(Dirpool *dp, Id parent, Id comp, int create); 29 30 /* return the parent directory of child did */ 31 static inline Id dirpool_parent(Dirpool *dp, Id did) 32 { 33 if (!did) 34 return 0; 35 while (dp->dirs[--did] > 0) 36 ; 37 return -dp->dirs[did]; 38 } 39 40 /* return the next child entry of child did */ 41 static inline Id 42 dirpool_sibling(Dirpool *dp, Id did) 43 { 44 /* if this block contains another entry, simply return it */ 45 if (did + 1 < dp->ndirs && dp->dirs[did + 1] > 0) 46 return did + 1; 47 /* end of block reached, rewind to get to the block's 48 * dirtraverse entry */ 49 while (dp->dirs[--did] > 0) 50 ; 51 /* need to special case did == 0 to prevent looping */ 52 if (!did) 53 return 0; 54 if (!dp->dirtraverse) 55 dirpool_make_dirtraverse(dp); 56 return dp->dirtraverse[did]; 57 } 58 59 /* return the first child entry of directory did */ 60 static inline Id 61 dirpool_child(Dirpool *dp, Id did) 62 { 63 if (!dp->dirtraverse) 64 dirpool_make_dirtraverse(dp); 65 return dp->dirtraverse[did]; 66 } 67 68 static inline void 69 dirpool_free_dirtraverse(Dirpool *dp) 70 { 71 solv_free(dp->dirtraverse); 72 dp->dirtraverse = 0; 73 } 74 75 static inline Id 76 dirpool_compid(Dirpool *dp, Id did) 77 { 78 return dp->dirs[did]; 79 } 80 81 #ifdef __cplusplus 82 } 83 #endif 84 85 #endif /* LIBSOLV_DIRPOOL_H */ 86