int main() { int i, *ptr; const void *val; struct rbtree *rb; srand(getpid()); if ((rb=rbinit(compare, NULL))==NULL) { fprintf(stderr, "insufficient memory\n"); exit(1); } for (i = 0; i < 12; i++) { ptr = (int *)xmalloc(sizeof(int)); *ptr = rand()&0xff; val = rbsearch((void *)ptr, rb); if(val == NULL) { fprintf(stderr, "insufficient memory\n"); exit(1); } } for(val=rblookup(RB_LUFIRST, NULL, rb); val!=NULL; val=rblookup(RB_LUNEXT, val, rb)) { printf("%6d\n", *(int *)val); } rbdestroy(rb); return 0; }
static void * int_dict_loookup( int mode, int_dict_t *dict, intkey_t *key, void *value) { int_dict_node_t target; int_dict_node_t *node; assert(dict); assert(dict->tree); if (key != NULL) { target.key = *key; } READ_LOCK(&dict->mutex); node = (int_dict_node_t *)rblookup(mode, &target, dict->tree); if (node == NULL) { RW_MUTEX_UNLOCK(&dict->mutex); return NULL; } if (key != NULL) { *key = node->key; } if (value != NULL) { memcpy(value, node->value, dict->value_size); } RW_MUTEX_UNLOCK(&dict->mutex); return node->value; }
static void FS_Stats_f (void) { #if BTREE_SEARCH || MAGIC_BTREE int i; int j; int k; const void *val; #endif #ifdef HASH_CACHE fscache_t *temp; #endif #if BTREE_SEARCH || MAGIC_BTREE i = 0; j = 1; k = 1; for(val=rblookup(RB_LUFIRST, NULL, rb); val!=NULL; val=rblookup(RB_LUNEXT, val, rb)) i++; while (j < i) { k++; j <<= 1; } Com_Printf ("%d entries in binary search tree cache (est. height %d).\n", LOG_GENERAL, i, k); #endif #ifdef HASH_CACHE temp = &fscache; temp = temp->next; i = 0; if (temp) { while (temp->next) { i++; temp = temp->next; } } Com_Printf ("%d entries in linked list hash cache.\n", LOG_GENERAL, i); #endif }
/** * scan an iTunes xml music database file, augmenting * the metainfo with that found in the xml file * * @param filename xml file to parse * @returns TRUE if playlist parsed successfully, FALSE otherwise */ int scan_xml_playlist(char *filename) { char *working_base; const void *val; int retval=TRUE; SCAN_XML_RB *lookup_ptr; SCAN_XML_RB lookup_val; RXMLHANDLE xml_handle; MAYBEFREE(scan_xml_itunes_version); MAYBEFREE(scan_xml_itunes_base_path); MAYBEFREE(scan_xml_itunes_decoded_base_path); MAYBEFREE(scan_xml_real_base_path); scan_xml_file = filename; /* initialize the redblack tree */ if((scan_xml_db = rbinit(scan_xml_rb_compare,NULL)) == NULL) { DPRINTF(E_LOG,L_SCAN,"Could not initialize red/black tree\n"); return FALSE; } /* find the base dir of the itunes playlist itself */ working_base = strdup(filename); if(strrchr(working_base,'/')) { *(strrchr(working_base,'/') + 1) = '\x0'; scan_xml_real_base_path = strdup(working_base); } else { scan_xml_real_base_path = strdup("/"); } free(working_base); DPRINTF(E_SPAM,L_SCAN,"Parsing xml file: %s\n",filename); if(!rxml_open(&xml_handle,filename,scan_xml_handler,NULL)) { DPRINTF(E_LOG,L_SCAN,"Error opening xml file %s: %s\n", filename,rxml_errorstring(xml_handle)); } else { if(!rxml_parse(xml_handle)) { retval=FALSE; DPRINTF(E_LOG,L_SCAN,"Error parsing xml file %s: %s\n", filename,rxml_errorstring(xml_handle)); } } rxml_close(xml_handle); /* destroy the redblack tree */ val = rblookup(RB_LUFIRST,NULL,scan_xml_db); while(val) { lookup_val.itunes_index = ((SCAN_XML_RB*)val)->itunes_index; lookup_ptr = (SCAN_XML_RB *)rbdelete((void*)&lookup_val,scan_xml_db); if(lookup_ptr) free(lookup_ptr); val = rblookup(RB_LUFIRST,NULL,scan_xml_db); } rbdestroy(scan_xml_db); MAYBEFREE(scan_xml_itunes_version); MAYBEFREE(scan_xml_itunes_base_path); MAYBEFREE(scan_xml_itunes_decoded_base_path); MAYBEFREE(scan_xml_real_base_path); return retval; }