static PyObject * Simulator_get_population(Simulator *self) { int err; unsigned int j; PyObject *ret = NULL; PyObject *l = NULL; PyObject *py_ind = NULL; avl_tree_t *pop = NULL; avl_node_t *node; uint64_t id; uintptr_t int_ptr; individual_t *ind; if (Simulator_check_sim(self) != 0) { goto out; } pop = PyMem_Malloc(sizeof(avl_tree_t)); if (pop == NULL) { PyErr_NoMemory(); goto out; } err = sim_get_population(self->sim, pop); if (err != 0) { handle_library_error(err); goto out; } l = PyList_New(avl_count(pop)); if (l == NULL) { goto out; } j = 0; for (node = pop->head; node != NULL; node = node->next) { id = *((uint64_t *) node->item); int_ptr = (uintptr_t) id; ind = (individual_t *) int_ptr; py_ind = Simulator_individual_to_python(self, ind); if (py_ind == NULL) { goto out; } if (PyList_SetItem(l, j, py_ind) != 0) { Py_DECREF(py_ind); goto out; } j++; } ret = l; l = NULL; out: if (pop != NULL) { sim_free_population(self->sim, pop); PyMem_Free(pop); } Py_XDECREF(l); return ret; }
/*Numero de produtos por cliente ou numero de produtos por tipo de compra*/ NUM_OCORRENCIAS_CODE avl_count(AVL c, char* code, int tipo, char mode) { NUM_OCORRENCIAS_CODE res; if(c==NULL) res=0; else if(tipo==Compras_Ord_CC) { Comp info=(Comp) c->info; if(strncmp(info->codigo_cliente, code, 5)>0) res=avl_count(c->left, code, tipo, mode); else if(strncmp(info->codigo_cliente, code, 5)<0) res=avl_count(c->right, code, tipo, mode); else res=1+avl_count(c->right, code, tipo, mode) + avl_count(c->left, code, tipo,mode); } else { Comp info=(Comp) c->info; if(strncmp(info->codigo_produto, code, 6)>0) res=avl_count(c->left, code, tipo, mode); else if(strncmp(info->codigo_produto, code, 6)<0) res=avl_count(c->right, code, tipo, mode); else if(strncmp(info->codigo_produto, code, 6)==0 && mode==info->tipo) res=1+avl_count(c->right, code, tipo, mode)+avl_count(c->left,code,tipo,mode); else res=avl_count(c->right, code, tipo, mode)+avl_count(c->left,code,tipo,mode); } return res; }
hx_container_t* hx_idmap_strings ( hx_idmap* map ) { size_t size = avl_count( map->id2string ); hx_container_t* c = hx_new_container( 'S', size ); struct avl_traverser iter; avl_t_init( &iter, map->id2string ); hx_idmap_item* item; while ((item = (hx_idmap_item*) avl_t_next( &iter )) != NULL) { int len = strlen( item->string ) + 1; char* string = calloc( len, sizeof(char) ); strcpy(string, item->string ); hx_container_push_item( c, string ); } return c; }
/** * @brief Updates the archive fields returned by the getters. */ static void coco_archive_update(coco_archive_t *archive) { if (!archive->is_up_to_date) { avl_node_t *node, *left_node; coco_archive_avl_item_t *node_item, *left_node_item; double *node_objectives, *left_node_objectives; /* Updates number_of_solutions */ archive->number_of_solutions = avl_count(archive->tree); /* Updates hypervolume */ node = archive->tree->head; archive->hypervolume = 0; /* Hypervolume of the extreme point equals 0 */ while (node->next) { /* Add hypervolume contributions of the other points that are within ROI */ left_node = node->next; node_item = (coco_archive_avl_item_t *) node->item; left_node_item = (coco_archive_avl_item_t *) left_node->item; node_objectives = coco_archive_node_item_get_vector(node_item); left_node_objectives = coco_archive_node_item_get_vector(left_node_item); if (mo_solution_is_within_ROI(left_node_objectives, archive->ideal, archive->nadir, archive->number_of_objectives)) { if (mo_solution_is_within_ROI(node_objectives, archive->ideal, archive->nadir, archive->number_of_objectives)) archive->hypervolume += (node_item->f1 - left_node_item->f1) * (archive->nadir[1] - left_node_item->f2); else archive->hypervolume += (archive->nadir[0] - left_node_item->f1) * (archive->nadir[1] - left_node_item->f2); } coco_free_memory(node_objectives); coco_free_memory(left_node_objectives); node = left_node; } /* Performs normalization */ archive->hypervolume /= ((archive->nadir[0] - archive->ideal[0]) * (archive->nadir[1] - archive->ideal[1])); archive->is_up_to_date = 1; archive->current_solution = NULL; archive->extremes_already_returned = 0; } }
/** * @brief Updates the archive fields returned by the getters. */ static void coco_archive_update(coco_archive_t *archive) { double hyp; if (!archive->is_up_to_date) { avl_node_t *node, *left_node; coco_archive_avl_item_t *node_item, *left_node_item; /* Updates number_of_solutions */ archive->number_of_solutions = avl_count(archive->tree); /* Updates hypervolume */ node = archive->tree->head; archive->hypervolume = 0; /* Hypervolume of the extreme point equals 0 */ while (node->next) { /* Add hypervolume contributions of the other points that are within ROI */ left_node = node->next; node_item = (coco_archive_avl_item_t *) node->item; left_node_item = (coco_archive_avl_item_t *) left_node->item; if (mo_is_within_ROI(left_node_item->normalized_y, archive->number_of_objectives)) { hyp = 0; if (mo_is_within_ROI(node_item->normalized_y, archive->number_of_objectives)) hyp = (node_item->normalized_y[0] - left_node_item->normalized_y[0]) * (1 - left_node_item->normalized_y[1]); else hyp = (1 - left_node_item->normalized_y[0]) * (1 - left_node_item->normalized_y[1]); assert(hyp >= 0); archive->hypervolume += hyp; } node = left_node; } archive->is_up_to_date = 1; archive->current_solution = NULL; archive->extremes_already_returned = 0; } }
int main(int argc, char ** argv) { unsigned int i; pthread_t * threads; unsigned int * ids; double maxval; double curval; struct s_rule_link * root; struct s_rule_link * prevlink; struct s_rule_link * curlink; struct s_rule_link * tmplink; struct s_rule_link * maxlink; int maxrulelen = 500; avl_tree_t * oldcoverage; avl_node_t * scoverage; avl_node_t * scoverage2; BLOOM_TYPE * bloom; uint64_t curindex; uint64_t nbleft; double wordlistProcessingTime, candidateProcessingTime; char * endptr; setlimits(); if(argc<6) usage(); matchlimit = atoi(argv[1]); if(matchlimit == 0) usage(); nbthreads = atoi(argv[2]); if(nbthreads == 0) nbthreads = 1; wordlistProcessingTime = strtod(argv[3], &endptr); // weak check if(endptr == argv[3]) { fprintf(stderr, "Could not parse wordlistProcessingTime\n"); usage(); } candidateProcessingTime = strtod(argv[4], &endptr); if(endptr == argv[4]) { fprintf(stderr, "Could not parse candidateProcessingTime: %s\n", argv[4]); usage(); } fprintf(stderr, "Wordlist processing time: %es / Candidate processing time: %es/candidate\n", wordlistProcessingTime, candidateProcessingTime); nbfiles = argc-5; threads = xmalloc(sizeof(pthread_t)*nbthreads); memset(threads, 0, sizeof(pthread_t)*nbthreads); ids = xmalloc(sizeof(unsigned int)*nbthreads); rulejob = xmalloc(sizeof(struct s_rulejob)*nbfiles); bloom = xmalloc(BLOOM_STORAGE); for(i=0;i<nbfiles;i++) { rulejob[i].root = NULL; rulejob[i].tail = NULL; rulejob[i].done_by = -1; rulejob[i].filename = argv[i+5]; } for(i=0;i<nbthreads;i++) { ids[i] = i; if(pthread_create(&threads[i], NULL, load_rules, &ids[i])) { fprintf(stderr, "error, could not create thread for file %s\n", argv[i+4]); perror("pthread_create"); return 1; } } for(i=0;i<nbthreads;i++) pthread_join( threads[i], NULL ); root = NULL; for(i=0;i<nbfiles;i++) { if(rulejob[i].tail == NULL) continue; rulejob[i].tail->next = root; root = rulejob[i].root; } free(rulejob); fprintf(stderr, "start crunching\n"); oldcoverage = NULL; while(1) { maxval = 0; curlink = root; prevlink = NULL; maxlink = NULL; nbleft = 0; while(curlink) { if(curlink->coverage == NULL) curval = 1e50; else { if(oldcoverage) { /* coverage cleanup */ scoverage = curlink->coverage->head; while(scoverage) { curindex = (uint64_t) scoverage->item; if(GETBIT(bloom, curindex) && avl_search(oldcoverage, (void *) curindex)) { scoverage2 = scoverage->next; avl_delete_node(curlink->coverage, scoverage); scoverage = scoverage2; } else { nbleft++; scoverage = scoverage->next; } } } curval = ((double) avl_count(curlink->coverage)) / ( ((double) curlink->pwtested )*candidateProcessingTime + wordlistProcessingTime) ; } if( (curlink->coverage == NULL) || (avl_count(curlink->coverage) <matchlimit)) { if(curlink->rule) { free(curlink->rule); } if(curlink->coverage) { avl_free_tree(curlink->coverage); } if(prevlink) prevlink->next = curlink->next; else root = curlink->next; tmplink = curlink; curlink = curlink->next; free(tmplink); continue; } if(curval>maxval || (curval == maxval && strlen(curlink->rule) < maxrulelen ) ) { maxrulelen = strlen(curlink->rule); maxval = curval; maxlink = curlink; } prevlink = curlink; curlink = curlink->next; } if(maxlink == NULL) break; if(oldcoverage) avl_free_tree(oldcoverage); oldcoverage = maxlink->coverage; /* build bloom filter */ memset(bloom, 0, BLOOM_STORAGE); scoverage = oldcoverage->head; while(scoverage) { //pcurindex = scoverage->item; //curindex = *pcurindex; curindex = (uint64_t) scoverage->item; SETBIT(bloom, curindex); scoverage = scoverage->next; } maxlink->coverage = NULL; printf("%s NBPWD=%d [%f]\n", maxlink->rule, avl_count(oldcoverage), maxval); fprintf(stderr, "%s NBPWD=%d [%f] NBRULES=%ld\n", maxlink->rule, avl_count(oldcoverage), maxval, nbleft); } return 0; }
int32_t stSortedSet_size(stSortedSet *sortedSet) { return avl_count(sortedSet->sortedSet); }
/* Checks that |tree| is well-formed and verifies that the values in |array[]| are actually in |tree|. There must be |n| elements in |array[]| and |tree|. Returns nonzero only if no errors detected. */ static int verify_tree (struct avl_table *tree, int array[], size_t n) { int okay = 1; /* Check |tree|'s bst_count against that supplied. */ if (avl_count (tree) != n) { printf (" Tree count is %lu, but should be %lu.\n", (unsigned long) avl_count (tree), (unsigned long) n); okay = 0; } if (okay) { /* Recursively verify tree structure. */ size_t count; int height; recurse_verify_tree (tree->avl_root, &okay, &count, 0, INT_MAX, &height); if (count != n) { printf (" Tree has %lu nodes, but should have %lu.\n", (unsigned long) count, (unsigned long) n); okay = 0; } } if (okay) { /* Check that all the values in |array[]| are in |tree|. */ size_t i; for (i = 0; i < n; i++) if (avl_find (tree, &array[i]) == NULL) { printf (" Tree does not contain expected value %d.\n", array[i]); okay = 0; } } if (okay) { /* Check that |avl_t_first()| and |avl_t_next()| work properly. */ struct avl_traverser trav; size_t i; int prev = -1; int *item; for (i = 0, item = avl_t_first (&trav, tree); i < 2 * n && item != NULL; i++, item = avl_t_next (&trav)) { if (*item <= prev) { printf (" Tree out of order: %d follows %d in traversal\n", *item, prev); okay = 0; } prev = *item; } if (i != n) { printf (" Tree should have %lu items, but has %lu in traversal\n", (unsigned long) n, (unsigned long) i); okay = 0; } } if (okay) { /* Check that |avl_t_last()| and |avl_t_prev()| work properly. */ struct avl_traverser trav; size_t i; int next = INT_MAX; int *item; for (i = 0, item = avl_t_last (&trav, tree); i < 2 * n && item != NULL; i++, item = avl_t_prev (&trav)) { if (*item >= next) { printf (" Tree out of order: %d precedes %d in traversal\n", *item, next); okay = 0; } next = *item; } if (i != n) { printf (" Tree should have %lu items, but has %lu in reverse\n", (unsigned long) n, (unsigned long) i); okay = 0; } } if (okay) { /* Check that |avl_t_init()| works properly. */ struct avl_traverser init, first, last; int *cur, *prev, *next; avl_t_init (&init, tree); avl_t_first (&first, tree); avl_t_last (&last, tree); cur = avl_t_cur (&init); if (cur != NULL) { printf (" Inited traverser should be null, but is actually %d.\n", *cur); okay = 0; } next = avl_t_next (&init); if (next != avl_t_cur (&first)) { printf (" Next after null should be %d, but is actually %d.\n", *(int *) avl_t_cur (&first), *next); okay = 0; } avl_t_prev (&init); prev = avl_t_prev (&init); if (prev != avl_t_cur (&last)) { printf (" Previous before null should be %d, but is actually %d.\n", *(int *) avl_t_cur (&last), *prev); okay = 0; } avl_t_next (&init); } return okay; }