/* initialize tasknames collection */ void init_tasknames_collection(void) { assert(libraryStatus == uninitialized); actual_code = code_of_mainTask; exist_nonspecified_tasknames = False; libraryStatus = initialized; all_tasknames = RBTreeCreate ( "taskname", "taskname_code", tasknameComp, tasknameDest, taskcodeDest, tasknamePrint, taskcodePrint, NULL, NULL); nonspecified_tasknames = RBTreeCreate ( "taskname", "taskname_code", tasknameComp, tasknameDest, taskcodeDest, tasknamePrint, taskcodePrint, NULL, NULL); }
void db_connect() { int ret, ix; // initialize some database handlers g_db.dbList = (DB**)malloc(sizeof(DB*) * GROWBY); memset(g_db.dbList, 0, sizeof(DB*) * GROWBY); g_db.sz = GROWBY; g_db.current = 0; // we have a special root db if ((ret = db_create (&g_db.root, NULL, 0)) != 0) { fprintf (stderr, "db_create: %s\n", db_strerror (ret)); } if ((ret = g_db.root->open (g_db.root, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) { g_db.root->err (g_db.root, ret, "%s", DATABASE); } // rb tree setup for(ix = 0; ix < BINMAX; ix++) { tree[ix] = RBTreeCreate(IntComp,IntDest,InfoDest,IntPrint,InfoPrint); } }
rb_red_blk_tree* buildEventMap() { printf("--- Generating Event Rule Map ... \n"); rb_red_blk_tree* EventTree = RBTreeCreate(Compare_EventType,DestroyEventType,DestroyInfoEventKey,PrintEventKey,PrintInfoEventKey); if(!EventTree) { printf("Error Building the Event Rule Map.\n"); return NULL; } int i=0; term_t a0 = PL_new_term_refs(3); term_t b0 = PL_new_term_refs(2); static predicate_t p; static functor_t event_functor; char myEvents[256][256]; int arity; eventType* temp=NULL; if ( !event_functor ) event_functor = PL_new_functor(PL_new_atom("event"), 2); PL_cons_functor(a0+1,event_functor,b0,b0+1); if ( !p ) p = PL_predicate("trClause", 3, NULL); qid_t qid = PL_open_query(NULL, PL_Q_NORMAL, p, a0); while(PL_next_solution(qid) != FALSE) { //termToString(b0,myEvents[i]); atom_t name; PL_get_name_arity(b0, &name, &arity); sprintf(myEvents[i],"%s",PL_atom_chars(name)); temp=(eventType*)calloc(1,sizeof(eventType)); trClause* trc=(trClause*)calloc(1,sizeof(trClause)); strcpy(temp->name,PL_atom_chars(name)); temp->arity = arity; RBTreeInsert(EventTree,temp,trc); temp=NULL; trc=NULL; padding(' ',4); printf("+New Event Signature : %s/%d\n",myEvents[i],arity); i++; } PL_close_query(qid); #if DEBUG RBTreePrint(EventTree); #endif printf("--- Done!\n"); return EventTree; }
int dhcp_cache_init(time_t nodes_ttl) { int err; if( (err = pthread_rwlock_init(&cache_lock, NULL) ) ) { log_wr(CLOG, "Can't init rw_lock for DHCP cache: '%s'", strerror(err)); return FAIL; } cache_node_ttl = nodes_ttl; cache_last_flush = time(NULL); cache = RBTreeCreate(cmp_nodes, node_destroy, info_destroy, node_print, info_print); return cache ? OK : FAIL; }
/* called at every kthread_create(). Assumes cfs_init() has already been * called */ void cfs_kthread_init(kthread_t *k_ctx) { checkpoint("k%d: CFS: init kthread", k_ctx->cpuid); gt_spin_lock(&scheduler.lock); cfs_kthread_t *cfs_kthread = cfs_get_kthread(k_ctx); gt_spinlock_init(&cfs_kthread->lock); cfs_kthread->k_ctx = k_ctx; cfs_kthread->current_cfs_uthread = NULL; cfs_kthread->cfs_uthread_count = 0; cfs_kthread->latency = CFS_DEFAULT_LATENCY_us; cfs_kthread->min_vruntime = 0; cfs_kthread->tree = RBTreeCreate(&cfs_rb_compare_key, &cfs_rb_destroy_key, &cfs_rb_destroy_info, &cfs_rb_print_key, &cfs_rb_print_info); cfs_data_t *cfs_data = SCHED_DATA; cfs_data->cfs_kthread_count++; gt_spin_unlock(&scheduler.lock); }
int main(int argc, char** argv) { int option=0; int64_t newKey,newKey2; rb_red_blk_node* newNode; rb_red_blk_tree* tree; int64_t* array = 0; int64_t* array2 = 0; unsigned int N = 65536; //total number of elements to insert unsigned int M = 16384; //number of elements to delete from the beginning unsigned int M2 = 16384; //number of elements to delete from the end int i; unsigned int j; time_t t1 = time(0); unsigned int seed = t1; double par = 2.5; for(i=1;i<argc;i++) if(argv[i][0] == '-') switch(argv[i][1]) { case 'N': N = atoi(argv[i+1]); break; case 'M': M = atoi(argv[i+1]); if(i+2 < argc) { if(isdigit(argv[i+2][0])) M2 = atoi(argv[i+2]); else M2 = M; } else M2 = M; break; case 's': seed = atoi(argv[i+1]); break; case 'p': par = atof(argv[i+1]); break; default: fprintf(stderr,"unrecognized parameter: %s!\n",argv[i]); break; } if(M + M2 >= N) { fprintf(stderr,"Error: number of elements to delete (%u + %u) is more than the total number of elements (%u)!\n", M,M2,N); return 1; } tree=RBTreeCreate(CmpInt64,NullFunction,NullFunction,NullFunction,NullFunction,DFInt64,&par); array = SafeMalloc(sizeof(int64_t)*N); for(j=0;j<N;j++) { array[j] = ((int64_t)rand())*((int64_t)rand())*((int64_t)rand()); RBTreeInsert(tree,(void*)(array[j]),0); } for(j=0;j<M;j++) { newNode = RBExactQuery(tree,(void*)(array[j])); if(!newNode) { fprintf(stderr,"Error: node not found!\n"); goto rbt_end; } RBDelete(tree,newNode); } for(j=N-M2;j<N;j++) { newNode = RBExactQuery(tree,(void*)(array[j])); if(!newNode) { fprintf(stderr,"Error: node not found!\n"); goto rbt_end; } RBDelete(tree,newNode); } N = N-M2-M; array2 = array+M; quicksort(array2,0,N); j=0; newNode = TreeFirst(tree); double cdf = 0.0; do { int64_t v1 = (int64_t)(newNode->key); if(v1 != array2[j]) { fprintf(stderr,"error: %d != %d!\n",v1,array2[j]); break; } double cdf2 = GetNodeRank(tree,newNode); double diff = fabs(cdf2-cdf); if(diff > EPSILON) { fprintf(stderr,"wrong cdf value: %g != %g (diff: %g)!\n",cdf,cdf2,diff); break; } j++; cdf += DFInt64((void*)array2[j],&par); newNode = TreeSuccessor(tree,newNode); } while(newNode != tree->nil && j<N); if( !(newNode == tree->nil && j == N) ) { fprintf(stderr,"error: tree or array too short / long!\n"); } rbt_end: RBTreeDestroy(tree); free(array); time_t t2 = time(0); fprintf(stderr,"runtime: %u\n",(unsigned int)(t2-t1)); return 0; }
static SparseMatrix get_overlap_graph(int dim, int n, real *x, real *width, int check_overlap_only){ /* if check_overlap_only = TRUE, we only check whether there is one overlap */ scan_point *scanpointsx, *scanpointsy; int i, k, neighbor; SparseMatrix A = NULL, B = NULL; rb_red_blk_node *newNode, *newNode0, *newNode2 = NULL; rb_red_blk_tree* treey; real one = 1; A = SparseMatrix_new(n, n, 1, MATRIX_TYPE_REAL, FORMAT_COORD); scanpointsx = N_GNEW(2*n,scan_point); for (i = 0; i < n; i++){ scanpointsx[2*i].node = i; scanpointsx[2*i].x = x[i*dim] - width[i*dim]; scanpointsx[2*i].status = INTV_OPEN; scanpointsx[2*i+1].node = i+n; scanpointsx[2*i+1].x = x[i*dim] + width[i*dim]; scanpointsx[2*i+1].status = INTV_CLOSE; } qsort(scanpointsx, 2*n, sizeof(scan_point), comp_scan_points); scanpointsy = N_GNEW(2*n,scan_point); for (i = 0; i < n; i++){ scanpointsy[i].node = i; scanpointsy[i].x = x[i*dim+1] - width[i*dim+1]; scanpointsy[i].status = INTV_OPEN; scanpointsy[i+n].node = i; scanpointsy[i+n].x = x[i*dim+1] + width[i*dim+1]; scanpointsy[i+n].status = INTV_CLOSE; } treey = RBTreeCreate(NodeComp,NodeDest,InfoDest,NodePrint,InfoPrint); for (i = 0; i < 2*n; i++){ #ifdef DEBUG_RBTREE fprintf(stderr," k = %d node = %d x====%f\n",(scanpointsx[i].node)%n, (scanpointsx[i].node), (scanpointsx[i].x)); #endif k = (scanpointsx[i].node)%n; if (scanpointsx[i].status == INTV_OPEN){ #ifdef DEBUG_RBTREE fprintf(stderr, "inserting..."); treey->PrintKey(&(scanpointsy[k])); #endif RBTreeInsert(treey, &(scanpointsy[k]), NULL); /* add both open and close int for y */ #ifdef DEBUG_RBTREE fprintf(stderr, "inserting2..."); treey->PrintKey(&(scanpointsy[k+n])); #endif RBTreeInsert(treey, &(scanpointsy[k+n]), NULL); } else { real bsta, bbsta, bsto, bbsto; int ii; assert(scanpointsx[i].node >= n); newNode = newNode0 = RBExactQuery(treey, &(scanpointsy[k + n])); ii = ((scan_point *)newNode->key)->node; assert(ii < n); bsta = scanpointsy[ii].x; bsto = scanpointsy[ii+n].x; #ifdef DEBUG_RBTREE fprintf(stderr, "poping..%d....yinterval={%f,%f}\n", scanpointsy[k + n].node, bsta, bsto); treey->PrintKey(newNode->key); #endif assert(treey->nil != newNode); while ((newNode) && ((newNode = TreePredecessor(treey, newNode)) != treey->nil)){ neighbor = (((scan_point *)newNode->key)->node)%n; bbsta = scanpointsy[neighbor].x; bbsto = scanpointsy[neighbor+n].x;/* the y-interval of the node that has one end of the interval lower than the top of the leaving interval (bsto) */ #ifdef DEBUG_RBTREE fprintf(stderr," predecessor is node %d y = %f\n", ((scan_point *)newNode->key)->node, ((scan_point *)newNode->key)->x); #endif if (neighbor != k){ if (ABS(0.5*(bsta+bsto) - 0.5*(bbsta+bbsto)) < 0.5*(bsto-bsta) + 0.5*(bbsto-bbsta)){/* if the distance of the centers of the interval is less than sum of width, we have overlap */ A = SparseMatrix_coordinate_form_add_entries(A, 1, &neighbor, &k, &one); #ifdef DEBUG_RBTREE fprintf(stderr,"====================================== %d %d\n",k,neighbor); #endif if (check_overlap_only) goto check_overlap_RETURN; } } else { newNode2 = newNode; } } #ifdef DEBUG_RBTREE fprintf(stderr, "deleteing..."); treey->PrintKey(newNode0->key); #endif if (newNode0) RBDelete(treey,newNode0); if (newNode2 && newNode2 != treey->nil && newNode2 != newNode0) { #ifdef DEBUG_RBTREE fprintf(stderr, "deleteing2..."); treey->PrintKey(newNode2->key); #endif if (newNode0) RBDelete(treey,newNode2); } } } check_overlap_RETURN: FREE(scanpointsx); FREE(scanpointsy); RBTreeDestroy(treey); B = SparseMatrix_from_coordinate_format(A); SparseMatrix_delete(A); A = SparseMatrix_symmetrize(B, FALSE); SparseMatrix_delete(B); if (Verbose) fprintf(stderr, "found %d clashes\n", A->nz); return A; }
void recreate_black() { RBTreeDestroy(black); black = RBTreeCreate(IntComp,IntDest,InfoDest,IntPrint,InfoPrint); }
ssd_cache_handle_t sstack_ssd_cache_init(char *path, int64_t size, log_ctx_t *ctx) { ssd_cache_handle_t handle; ssd_cache_struct_t cache_struct; ssd_cachedev_info_t *info = NULL; int ret = -1; // Parameter validation if (NULL == path || size < 0) { sfs_log(ctx, SFS_ERR, "%s: Invalid parameter specified \n", __FUNCTION__); errno = EINVAL; return -1; } // NOTE: // Populate local data structure and copy this to ssd_caches. // This is to use pthread spinlock instead of mutex and limit // critical section cache_struct.handle = handle; info = ssd_create_cachedev(path, size, ctx); if (NULL == info) { sfs_log(ctx, SFS_ERR, "%s: Failed to create cachedev. Error = %d \n", __FUNCTION__, errno); return -1; } cache_struct.stats.inuse = 0; // Get number of inodes in the file system cache_struct.stats.num_cachelines = info->num_cachelines; strncpy((char *) &cache_struct.mountpt, info->mntpnt, PATH_MAX); // Get cache handle handle = get_next_ssd_cache_handle(); if (handle == -1) { sfs_log(ctx, SFS_ERR, "%s: get_next_ssd_cache_handle returned error. " "Either max cache devices are under use or fatal " "error.\n", __FUNCTION__); errno = ENOENT; return -1; } // Create ssdmd tree and LRU tree for the SSD cache device cache_struct.md_tree = RBTreeCreate(md_comp_func, md_destroy_func, NULL, NULL, NULL); if (NULL == cache_struct.md_tree) { sfs_log(ctx, SFS_ERR, "%s: Failed to allocate memory for md_tree \n", __FUNCTION__); // FIXME: // Implement and call free_ssd_cache_handle(handle); return -1; } pthread_spin_init(&cache_struct.md_lock, PTHREAD_PROCESS_PRIVATE); cache_struct.lru_tree = RBTreeCreate(lru_comp_func, lru_destroy_func, NULL, NULL, NULL); if (NULL == cache_struct.lru_tree) { sfs_log(ctx, SFS_ERR, "%s: Failed to allocate memory for lru_tree \n", __FUNCTION__); RBTreeDestroy(cache_struct.md_tree); pthread_spin_destroy(&cache_struct.md_lock); // FIXME: // Implement and call free_ssd_cache_handle(handle); return -1; } pthread_spin_init(&cache_struct.lru_lock, PTHREAD_PROCESS_PRIVATE); // Create ce_bitmap cache_struct.ce_bitmap = sfs_init_bitmap( cache_struct.stats.num_cachelines, ctx); if (ret == -1) { RBTreeDestroy(cache_struct.md_tree); pthread_spin_destroy(&cache_struct.md_lock); pthread_spin_destroy(&cache_struct.lru_lock); // FIXME: // Implement and call free_ssd_cache_handle(handle); return -1; } pthread_spin_lock(&cache_list_lock); memcpy((void *) &ssd_caches[handle], &cache_struct, sizeof(ssd_cache_struct_t)); pthread_spin_init(&ssd_caches[handle].stats.lock, PTHREAD_PROCESS_PRIVATE); pthread_spin_unlock(&cache_list_lock); return 0; }
rb_red_blk_tree *create_circ_tree_raw() { return RBTreeCreate(circ_compare, circ_destroy, dummy_fun, dummy_fun, dummy_fun); }
int main() { stk_stack* enumResult; int option=0; int newKey,newKey2; int* newInt; rb_red_blk_node* newNode; rb_red_blk_tree* tree; tree=RBTreeCreate(IntComp,IntDest,InfoDest,IntPrint,InfoPrint); while(option!=8) { printf("choose one of the following:\n"); printf("(1) add to tree\n(2) delete from tree\n(3) query\n"); printf("(4) find predecessor\n(5) find sucessor\n(6) enumerate\n"); printf("(7) print tree\n(8) quit\n"); do option=fgetc(stdin); while(-1 != option && isspace(option)); option-='0'; switch(option) { case 1: { printf("type key for new node\n"); scanf("%i",&newKey); newInt=(int*) malloc(sizeof(int)); *newInt=newKey; RBTreeInsert(tree,newInt,0); } break; case 2: { printf("type key of node to remove\n"); scanf("%i",&newKey); if ( ( newNode=RBExactQuery(tree,&newKey ) ) ) RBDelete(tree,newNode);/*assignment*/ else printf("key not found in tree, no action taken\n"); } break; case 3: { printf("type key of node to query for\n"); scanf("%i",&newKey); if ( ( newNode = RBExactQuery(tree,&newKey) ) ) {/*assignment*/ printf("data found in tree at location %i\n",(int)newNode); } else { printf("data not in tree\n"); } } break; case 4: { printf("type key of node to find predecessor of\n"); scanf("%i",&newKey); if ( ( newNode = RBExactQuery(tree,&newKey) ) ) {/*assignment*/ newNode=TreePredecessor(tree,newNode); if(tree->nil == newNode) { printf("there is no predecessor for that node (it is a minimum)\n"); } else { printf("predecessor has key %i\n",*(int*)newNode->key); } } else { printf("data not in tree\n"); } } break; case 5: { printf("type key of node to find successor of\n"); scanf("%i",&newKey); if ( (newNode = RBExactQuery(tree,&newKey) ) ) { newNode=TreeSuccessor(tree,newNode); if(tree->nil == newNode) { printf("there is no successor for that node (it is a maximum)\n"); } else { printf("successor has key %i\n",*(int*)newNode->key); } } else { printf("data not in tree\n"); } } break; case 6: { printf("type low and high keys to see all keys between them\n"); scanf("%i %i",&newKey,&newKey2); enumResult=RBEnumerate(tree,&newKey,&newKey2); while ( (newNode = StackPop(enumResult)) ) { tree->PrintKey(newNode->key); printf("\n"); } free(enumResult); } break; case 7: { RBTreePrint(tree); } break; case 8: { RBTreeDestroy(tree); return 0; } break; default: printf("Invalid input; Please try again.\n"); } } return 0; }
static SparseMatrix get_overlap_graph(int dim, int n, real *x, real *width){ scan_point *scanpointsx, *scanpointsy; int i, k, neighbor; SparseMatrix A = NULL, B = NULL; rb_red_blk_node *newNode, *newNode0; rb_red_blk_tree* treey; real one = 1; A = SparseMatrix_new(n, n, 1, MATRIX_TYPE_REAL, FORMAT_COORD); scanpointsx = N_GNEW(2*n,scan_point); for (i = 0; i < n; i++){ scanpointsx[2*i].node = i; scanpointsx[2*i].x = x[i*dim] - width[i*dim]; scanpointsx[2*i].status = INTV_OPEN; scanpointsx[2*i+1].node = i+n; scanpointsx[2*i+1].x = x[i*dim] + width[i*dim]; scanpointsx[2*i+1].status = INTV_CLOSE; } qsort(scanpointsx, 2*n, sizeof(scan_point), comp_scan_points); scanpointsy = N_GNEW(2*n,scan_point); for (i = 0; i < n; i++){ scanpointsy[i].node = i; scanpointsy[i].x = x[i*dim+1] - width[i*dim+1]; scanpointsy[i].status = INTV_OPEN; scanpointsy[i+n].node = i; scanpointsy[i+n].x = x[i*dim+1] + width[i*dim+1]; scanpointsy[i+n].status = INTV_CLOSE; } treey = RBTreeCreate(NodeComp,NodeDest,InfoDest,NodePrint,InfoPrint); for (i = 0; i < 2*n; i++){ #ifdef DEBUG_RBTREE fprintf(stderr," k = %d node = %d x====%f\n",(scanpointsx[i].node)%n, (scanpointsx[i].node), (scanpointsx[i].x)); #endif k = (scanpointsx[i].node)%n; if (scanpointsx[i].status == INTV_OPEN){ #ifdef DEBUG_RBTREE fprintf(stderr, "inserting..."); treey->PrintKey(&(scanpointsy[k])); #endif RBTreeInsert(treey, &(scanpointsy[k]), NULL); /* add both open and close int for y */ #ifdef DEBUG_RBTREE fprintf(stderr, "inserting2..."); treey->PrintKey(&(scanpointsy[k+n])); #endif RBTreeInsert(treey, &(scanpointsy[k+n]), NULL); } else { assert(scanpointsx[i].node >= n); newNode = newNode0 = RBExactQuery(treey, &(scanpointsy[k + n])); #ifdef DEBUG_RBTREE fprintf(stderr, "poping..%d....", scanpointsy[k + n].node); treey->PrintKey(newNode->key); #endif assert(treey->nil != newNode); while ((newNode) && ((newNode = TreePredecessor(treey, newNode)) != treey->nil) && ((scan_point *)newNode->key)->node != k){ neighbor = (((scan_point *)newNode->key)->node)%n; A = SparseMatrix_coordinate_form_add_entries(A, 1, &neighbor, &k, &one); #ifdef DEBUG_RBTREE fprintf(stderr,"%d %d\n",k,neighbor); #endif } #ifdef DEBUG_RBTREE fprintf(stderr, "deleteing..."); treey->PrintKey(newNode0->key); #endif if (newNode0) RBDelete(treey,newNode0); if (newNode != treey->nil && newNode != newNode0) { #ifdef DEBUG_RBTREE fprintf(stderr, "deleting2..."); treey->PrintKey(newNode->key) #endif if (newNode0) RBDelete(treey,newNode); } } }