long TMAVectorOrdered::addElement(PMAObject obj) { // Si no hay elementos, insertamos directamente. // if (size()==0) return add_element(obj,0); // En otro caso ordenamos. // long index_izq=0,index_der=il_count-1,index=0; while (index_izq<=index_der) { index = (index_izq + index_der)/2; PMAObject obj1 = CItems[index]; switch (io_comp->compare(*obj,*obj1)) { case -1: // obj<obj1 index_der=index-1; break; case 0: // obj=obj1 index_izq=index+1; break; case 1: // obj>obj1 index_izq=index+1; break; } } if (index>il_count) index=il_count; if (index<0) index=0; return add_element(obj,index); }
struct LList *parse(char *source, char *deliminator){ char buffer[strlen(source)+1]; unsigned long delim_len = strlen(deliminator); struct LList *list = new_list(); char *tmp; char c; int i, j, k=0, index; for (i=0; i < strlen(source); i++){ if (source[i] == deliminator[0]){ index = i; for (j=0; source[index+j] == deliminator[j]; j++){ if (j+1 == delim_len){ buffer[k] = '\0'; tmp = malloc(sizeof(char)*strlen(buffer)+1); strcpy(tmp, buffer); add_element(tmp, list); k = 0; i += delim_len; break; } } } c = source[i]; buffer[k] = c; k++; } buffer[k] = '\0'; tmp = malloc(sizeof(char)*strlen(buffer)+1); strcpy(tmp, buffer); add_element(tmp, list); return list; }
int main(int argc, char* argv[]) { glutInit(&argc, argv); display_init(); comm_initialize(); /* Call backs */ glutDisplayFunc( display); glutReshapeFunc( reshape); glutKeyboardFunc( keyboard); glutIdleFunc( comm_read); satellite = add_element("Satellite"); satellite_camera = new Camera(); current_camera = satellite_camera; planet = add_element("Ball"); for (int ii=0; ii<3 ; ii++) { planet->pos[ii] = 0.0; } glutMainLoop(); return 0; }
static int handle_boolean(void *ctx, int boolVal) { state_t *st = (state_t *)ctx; if (boolVal) { add_element(st, enif_make_atom(st->env, "true")); } else { add_element(st, enif_make_atom(st->env, "false")); } return 1; }
void bfs(lptr svd) //creates dictionary in disk { qptr q; FILE *dic; lptr l1,l2,l3;// q=(qptr)malloc(sizeof(struct queue)); initialize(q); add_element(q,svd); dic=fopen("dictionary","wb+"); //printf("%s\n",svd->key); //WRITE IN FILE fwrite(svd,sizeof(struct node),1,dic); while(!empty(q)) { l1=remove_element(q); l1->visited=1; if(l1->lchild!=NULL) { if(l1->lchild->visited!=1) { add_element(q,l1->lchild); //printf("%s \n",l1->lchild->key); fwrite(l1->lchild,sizeof(struct node),1,dic); } else { //printf("%s \n",l1->lchild->key); fwrite(l1->lchild,sizeof(struct node),1,dic); } } if(l1->rchild!=NULL) { if(l1->rchild->visited!=1) { add_element(q,l1->rchild); //printf("%s \n",l1->rchild->key); fwrite(l1->rchild,sizeof(struct node),1,dic); } else { //printf("%s \n",l1->rchild->key); fwrite(l1->rchild,sizeof(struct node),1,dic); } } } fclose(dic); dic=fopen("dictionary","rb"); printf("--------Dictionary contents--------\n"); while(fread(l1,sizeof(struct node),1,dic)) printf("\n%s",l1->key); printf("\n--------------------------------\n"); //reconstruct(); fclose(dic); }
ErrorStack ArrayMetadataSerializer::save(tinyxml2::XMLElement* element) const { CHECK_ERROR(save_base(element)); CHECK_ERROR(add_element(element, "payload_size_", "", data_casted_->payload_size_)); CHECK_ERROR(add_element( element, "snapshot_drop_volatile_pages_threshold_", "", data_casted_->snapshot_drop_volatile_pages_threshold_)); CHECK_ERROR(add_element(element, "array_size_", "", data_casted_->array_size_)); return kRetOk; }
static void fill_table(table_t *table, char *arg, char option) { char *p = strtok(arg, ", "); if (p == NULL) Die(gettext("invalid argument for -%c\n"), option); add_element(table, (long)Atoi(p)); while (p = strtok(NULL, ", ")) add_element(table, (long)Atoi(p)); }
void filter::build_index() { index_.clear(); tools::substring all(&values_[0], &values_[0] + values_.size()); for ( tools::substring sub; tools::split(all, ',', sub, all); ) { add_element(sub.begin(), sub.end()); } add_element(all.begin(), all.end()); std::sort(index_.begin(), index_.end(), sort_caseless()); }
/** * mempool_resize - resize an existing memory pool * @pool: pointer to the memory pool which was allocated via * mempool_create(). * @new_min_nr: the new minimum number of elements guaranteed to be * allocated for this pool. * * This function shrinks/grows the pool. In the case of growing, * it cannot be guaranteed that the pool will be grown to the new * size immediately, but new mempool_free() calls will refill it. * This function may sleep. * * Note, the caller must guarantee that no mempool_destroy is called * while this function is running. mempool_alloc() & mempool_free() * might be called (eg. from IRQ contexts) while this function executes. */ int mempool_resize(mempool_t *pool, int new_min_nr) { void *element; void **new_elements; unsigned long flags; BUG_ON(new_min_nr <= 0); might_sleep(); spin_lock_irqsave(&pool->lock, flags); if (new_min_nr <= pool->min_nr) { while (new_min_nr < pool->curr_nr) { element = remove_element(pool); spin_unlock_irqrestore(&pool->lock, flags); pool->free(element, pool->pool_data); spin_lock_irqsave(&pool->lock, flags); } pool->min_nr = new_min_nr; goto out_unlock; } spin_unlock_irqrestore(&pool->lock, flags); /* Grow the pool */ new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements), GFP_KERNEL); if (!new_elements) return -ENOMEM; spin_lock_irqsave(&pool->lock, flags); if (unlikely(new_min_nr <= pool->min_nr)) { /* Raced, other resize will do our work */ spin_unlock_irqrestore(&pool->lock, flags); kfree(new_elements); goto out; } memcpy(new_elements, pool->elements, pool->curr_nr * sizeof(*new_elements)); kfree(pool->elements); pool->elements = new_elements; pool->min_nr = new_min_nr; while (pool->curr_nr < pool->min_nr) { spin_unlock_irqrestore(&pool->lock, flags); element = pool->alloc(GFP_KERNEL, pool->pool_data); if (!element) goto out; spin_lock_irqsave(&pool->lock, flags); if (pool->curr_nr < pool->min_nr) { add_element(pool, element); } else { spin_unlock_irqrestore(&pool->lock, flags); pool->free(element, pool->pool_data); /* Raced */ goto out; } } out_unlock: spin_unlock_irqrestore(&pool->lock, flags); out: return 0; }
/* store_result() - store record from database in B-tree * this function is also a valid callback for use with sqlite3_exec() * pextra is again unused * * needs to return zero (nonzero aborts SQL query) */ int store_result(void * pextra, int nfields, char ** arrvalues, char ** arrfieldnames) { int n; /* allocate record on heap */ struct s_record * prec = alloc_record(); prec->irecord = record_count+1; for (n = 0; n < nfields; n++) { if (strcasecmp(arrfieldnames[n], "MovieTitle") == 0) prec->name = strdup(arrvalues[n]); /* key */ else if (strcasecmp(arrfieldnames[n], "MovieCategory") == 0) prec->category = strdup(arrvalues[n]); else if (strcasecmp(arrfieldnames[n], "ProductionYear") == 0) prec->year = atoi(arrvalues[n]); else if (strcasecmp(arrfieldnames[n], "Format") == 0) prec->format = strdup(arrvalues[n]); else if (strcasecmp(arrfieldnames[n], "Language") == 0) prec->language = strdup(arrvalues[n]); else if (strcasecmp(arrfieldnames[n], "Web") == 0) prec->url = strdup(arrvalues[n]); } /* add record to B-tree */ if (add_element(prec->name, prec) != NULL) { /* element already exists -- don't add record */ printf("Duplicate record exists: "); /* diagnostic value only */ display_record(prec, stdout); free_record(prec); } return 0; }
/** * mempool_fill - fill a memory pool * <at> pool: memory pool to fill * <at> gfp_mask: allocation mask to use * * Allocate new elements with <at> gfp_mask and fill <at> pool so that it has * <at> pool->min_nr elements. Returns 0 on success, -errno on failure. */ static int mempool_fill(mempool_t *pool, gfp_t gfp_mask) { /* * If curr_nr == min_nr is visible, we're correct regardless of * locking. */ while (pool->curr_nr < pool->min_nr) { void *elem; unsigned long flags; elem = pool->alloc(gfp_mask, pool->pool_data); if (unlikely(!elem)) return -ENOMEM; spin_lock_irqsave(&pool->lock, flags); if (pool->curr_nr < pool->min_nr) { add_element(pool, elem); elem = NULL; } spin_unlock_irqrestore(&pool->lock, flags); if (elem) { pool->free(elem, pool->pool_data); return 0; } } return 0; }
/* _VMKLNX_CODECHECK_: mempool_free */ void mempool_free(void *element, mempool_t *pool) { unsigned long flags; #if defined(__VMKLNX__) VMK_ASSERT(vmk_PreemptionIsEnabled() == VMK_FALSE); #endif smp_mb(); if (pool->curr_nr < pool->min_nr) { spin_lock_irqsave(&pool->lock, flags); if (pool->curr_nr < pool->min_nr) { add_element(pool, element); spin_unlock_irqrestore(&pool->lock, flags); wake_up(&pool->wait); return; } spin_unlock_irqrestore(&pool->lock, flags); } #if defined(__VMKLNX__) VMKAPI_MODULE_CALL_VOID(pool->module_id, pool->free, element, pool->pool_data); #else /* !defined(__VMKLNX__) */ pool->free(element, pool->pool_data); #endif /* defined(__VMKLNX__) */ }
/* @obj_size :缓存对象大小 @max_cache_size :最多缓存元素个数 memcache原理: 初始化时缓存max_cache_size/2个元素, 后续分配从cache中分, 如果超过cache部分, 则malloc释放回来的时候, 也最多装满max_cache_size个元素, 多的部分free掉 max_cache_size的选择应该选择大部分时候使用的量, 不要选择极端情形下的内存数字 */ struct memcache *memcache_create(size_t obj_size, int max_cache_size) { struct memcache *cache; size_t size = sizeof(struct memcache) + max_cache_size * sizeof(void *); assert(max_cache_size >= 2); cache = (struct memcache *)malloc(size); if (NULL == cache) return NULL; cache->elements = (void **)((char *)cache + sizeof(struct memcache)); cache->obj_size = obj_size; cache->cache_size = max_cache_size; cache->curr = 0; max_cache_size >>= 2; while (cache->curr < max_cache_size) { void *element = malloc(cache->obj_size); if (NULL == element) { free_pool(cache); return NULL; } add_element(cache, element); } return cache; }
mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn, mempool_free_t *free_fn, void *pool_data, int node_id) { mempool_t *pool; pool = kmalloc_node(sizeof(*pool), GFP_KERNEL | __GFP_ZERO, node_id); if (!pool) return NULL; pool->elements = kmalloc_node(min_nr * sizeof(void *), GFP_KERNEL, node_id); if (!pool->elements) { kfree(pool); return NULL; } spin_lock_init(&pool->lock); pool->min_nr = min_nr; pool->pool_data = pool_data; init_waitqueue_head(&pool->wait); pool->alloc = alloc_fn; pool->free = free_fn; /* * First pre-allocate the guaranteed number of buffers. */ while (pool->curr_nr < pool->min_nr) { void *element; element = pool->alloc(GFP_KERNEL, pool->pool_data); if (unlikely(!element)) { free_pool(pool); return NULL; } add_element(pool, element); } return pool; }
bool RStarTreeNode::insert(const BoundedObjectPtr element) { Uint32 i; if (get_leaf()) { return add_element(element); } else { for (i = 0; i < get_count(); i++) { if (get_element_bounding_box(i).contains( element->get_bounding_box())) { if (get_node(i)->insert(element)) { return true; } } } return false; } }
/* Function to read in a file of entrants */ void read_entrants(FILE *file, event *e) { entrant *entrant_data; list_node *new_element; course *course; int status; while (!feof(file)) { entrant_data = malloc(sizeof(entrant)); new_element = malloc(sizeof(list_node)); status = fscanf(file, "%d %c %[a-zA-Z ]\n", &entrant_data->number, &entrant_data->course, entrant_data->name); if(status != EOF) { entrant_data->state.type = NOT_STARTED; entrant_data->start_time = 0; entrant_data->end_time = 0; course = find_course(e->courselist, entrant_data->course); entrant_data->current_track = course->tracks.head; entrant_data->state.nodes_visited = 0; entrant_data->state.location_ref = 0; entrant_data->state.type = NOT_STARTED; entrant_data->state.late = 0; entrant_data->mc_time_delay = 0; new_element->data = entrant_data; new_element->next = NULL; add_element(&e->entrantlist, new_element); } } }
/* ** @brief create a list from a string ** ** It will store our string converted in our mwrite_buffer struct with fd ** ** @param server main structure ** @param fd file descriptor ** @return 0 if no error else 1 */ static int _split_msg_list(t_server *server, int fd, char *msg) { int begin; int len; char *tmp; int error; begin = 0; error = 0; len = strlen(msg); while (begin < len && error == 0) { if (!(tmp = strndup(&msg[begin], MAX_MSG))) error = 1; if (add_element(&(server->write_buffer[fd]), my_strdup(tmp), strlen(tmp))) error = 2; free(tmp); begin += MAX_MSG; } if (error == 0) return (0); else if (error == 2) free(tmp); return (1); }
/** * Inserts new element in the list. If the index is past the end of * the list * it creates the list and add the element as first element of list. * @param new_element the element to be add in the list. * @return 32-bit integer on the success returns 1. */ OSCL_EXPORT_REF int32 Oscl_Linked_List_Base::insert_element(const OsclAny* new_element, int index) { if (index >= num_elements) { return add_element(new_element); } else if (index == 0) { return add_to_front(new_element); } else { OsclAny *tmp, *next, *newelem; int32 ii; if (index < 0) return 0; for (tmp = head, ii = 0; ii < index - 1; ++ii, tmp = pOpaqueType->get_next(tmp)) { } next = pOpaqueType->get_next(tmp); newelem = pOpaqueType->allocate(sizeof_T); if (newelem == NULL) return 0; pOpaqueType->construct(newelem, new_element); pOpaqueType->set_next(tmp, newelem); pOpaqueType->set_next(newelem, next); ++num_elements; return 1; } return 0; }
void* ecriture(void* arg) { pthread_t tid = pthread_self () ; srand ((int) tid) ; // on effectue soit un ajout soit une suppression int val = rand()%2; if (val) { val = rand()%2 + 1; printf("Rédacteur %lu, ajoute la valeur %d dans la liste\n", (unsigned long) tid, val); struct linked_list *l; l = malloc(sizeof(struct linked_list)); l->nb = val; l->next = NULL; add_element(&liste, l); } else { val = rand()%2 + 1; struct linked_list *l; if ((l = remove_element(&liste, val)) != NULL) { printf("Rédacteur %lu, supprime la valeur %d dans la liste si elle existe : réussi\n", (unsigned long) tid, val); free(l); } else { printf("Rédacteur %lu, supprime la valeur %d dans la liste si elle existe : raté\n", (unsigned long) tid, val); } } return NULL; }
TEST(molecular_mass_test_case, molecular_mass_test) { auto subs = ChemicalSubstance::from_string("H2"); ASSERT_NEAR(subs.get_molecular_mass(), 2., 0.1); subs.add_element(Element(2), 3); ASSERT_NEAR(subs.get_molecular_mass(), 14., 0.1); }
void TMAVectorBase::fillEmpty(long numelems) { while (numelems>0) { add_element(NULL,il_count); numelems--; } }
ErrorStack MasstreeMetadataSerializer::save(tinyxml2::XMLElement* element) const { CHECK_ERROR(save_base(element)); CHECK_ERROR(add_element( element, "border_early_split_threshold_", "", data_casted_->border_early_split_threshold_)); CHECK_ERROR(add_element( element, "snapshot_drop_volatile_pages_layer_threshold_", "", data_casted_->snapshot_drop_volatile_pages_layer_threshold_)); CHECK_ERROR(add_element( element, "snapshot_drop_volatile_pages_btree_levels_", "", data_casted_->snapshot_drop_volatile_pages_btree_levels_)); CHECK_ERROR(add_element(element, "min_layer_hint_", "", data_casted_->min_layer_hint_)); return kRetOk; }
void fancy_set::add_elements(INT *elts, INT nb) { INT i; for (i = 0; i < nb; i++) { add_element(elts[i]); } }
static void *hashtest(void *data) { int my_els_removed = 0; int my_els_added = 0; int my_els_lookedup = 0; int my_els_found = 0; int my_els_traversals = 0; int my_testno = testno++; int its; /* data will be a random number == use as a seed for random numbers */ unsigned long seed = (unsigned long)data; printf("hashtest thread created... test beginning\n"); /* main test routine-- a global hashtab exists, pound it like crazy */ for(its=0;its<100000;its++) { void *seed2 = &seed; int op = my_rand(0,100, seed2); if (op<60) { my_els_lookedup++; #ifdef DEBUG printf("%d[%d]: LOOKUP\n", my_testno, its); #endif if ((my_els_lookedup%1000)==0) { printf("."); fflush(stdout); } if (lookup_element(seed2)) my_els_found++; } else if (op < 61) { /* make this 61 and it'll take 16 minutes to run */ #ifdef DEBUG printf("%d[%d]: TRAVERSE\n", my_testno, its); #endif traverse_elements(); my_els_traversals++; } else if (op < 80) { #ifdef DEBUG printf("%d[%d]: REMOVE\n", my_testno, its); #endif if (del_element(seed2)) my_els_removed++; } else { my_els_added++; #ifdef DEBUG printf("%d[%d]: ADD\n", my_testno, its); #endif add_element(); } } printf("\nhashtest thread %d exiting.... lookups=%d/%d, added=%d, removed=%d, traversals=%d;\n", my_testno, my_els_found, my_els_lookedup, my_els_added, my_els_removed, my_els_traversals); printf("\ntotals..................... lookups=%d/%d, added=%d, removed=%d; traversals=%d\n", els_found, els_lookedup, els_added, els_removed, els_traversals); pthread_exit(0); return NULL; }
void reset(lptr svd) //make visited to 0 { qptr q; FILE *dic; lptr l1,l2,l3;// q=(qptr)malloc(sizeof(struct queue)); initialize(q); add_element(q,svd); while(!empty(q)) { l1=remove_element(q); l1->visited=0; if(l1->lchild!=NULL) { if(l1->lchild->visited!=0) { add_element(q,l1->lchild); //printf("%s \n",l1->lchild->key); //fwrite(l1->lchild,sizeof(struct node),1,dic); } else { //printf("%s \n",l1->lchild->key); //fwrite(l1->lchild,sizeof(struct node),1,dic); } } if(l1->rchild!=NULL) { if(l1->rchild->visited!=0) { add_element(q,l1->rchild); //printf("%s \n",l1->rchild->key); //fwrite(l1->rchild,sizeof(struct node),1,dic); } else { //printf("%s \n",l1->rchild->key); //fwrite(l1->rchild,sizeof(struct node),1,dic); } } } }
void Metalink4Writer::write_file(const MetalinkFile& file) { start(wxT("file")); add_attr(wxT("name"), file.get_filename()); close_start(); add_element(wxT("identity"), file.get_identity()); add_element(wxT("description"), file.get_description()); add_element(wxT("version"), file.get_version()); add_element(wxT("size"), file.get_size()); write_hashes(file); write_piece_hash(file); const std::vector<MetalinkSource>& sources = file.get_sources(); for(std::vector<MetalinkSource>::const_iterator i = sources.begin(), eoi = sources.end(); i != eoi; ++i) { write_source(*i); } end(wxT("file")); }
static char * test_is_ignored_file_ko() { struct search_t *search; search = create_search(); add_element(&search->ignore, "rules"); mu_assert("test_is_ignored_file_ko failed", is_ignored_file(search, "Rules") == 0); free_search(search); return 0; }
static int handle_end(void *ctx, int array) { state_t *st = (state_t *)ctx; container_t *c = st->c; /* unlink container struct from state */ st->c = c->next; /* create and add container term */ if (array) { add_element(st, enif_make_tuple_from_array(st->env, c->array, c->count)); } else { add_element(st, enif_make_list_from_array(st->env, c->array, c->count)); } /* decallocate used container struct */ enif_free(st->env, c); return 1; }
static char * test_is_extension_good_ko() { struct search_t *search; search = create_search(); add_element(&search->extension, ".cpp"); mu_assert("test_is_extension_good_ko failed", is_ignored_file(search, "file.c") == 0); free_search(search); return 0; }
static char * test_is_specific_file_ko() { struct search_t *search; search = create_search(); add_element(&search->specific_file, "Makefile"); mu_assert("test_is_specific_file_ko failed", is_specific_file(search, "makefile") == 0); free_search(search); return 0; }