void leave_group(struct char_data *ch) { struct group_data *group; struct char_data *tch; struct iterator_data Iterator; bool found_pc = FALSE; if ((group = ch->group) == NULL) return; send_to_group(NULL, group, "%s has left the group.\r\n", GET_NAME(ch)); remove_from_list(ch, group->members); ch->group = NULL; if (group->members->iSize) { for (tch = (struct char_data *) merge_iterator(&Iterator, group->members); tch; tch = next_in_list(&Iterator)) if (!IS_NPC(tch)) found_pc = TRUE; remove_iterator(&Iterator); } if (!found_pc) SET_BIT(GROUP_FLAGS(group), GROUP_NPC); if (GROUP_LEADER(group) == ch && group->members->iSize) { group->leader = (struct char_data *) random_from_list(group->members); send_to_group(NULL, group, "%s has assumed leadership of the group.\r\n", GET_NAME(GROUP_LEADER(group))); } else if (group->members->iSize == 0) free_group(group); }
/* * ------------------------------------------------------------------------ * * "rcqpCmd_fdist2(SEXP inSubcorpus, SEXP inField1, SEXP inKey1, SEXP inField2, SEXP inKey2, SEXP inCutoff)" -- * * NB: * inField1 and inKey1 are the "source" parameters in compute_grouping() * inField2 and inKey2 are the "target" parameters in compute_grouping() * -> group NP matchend word by target lemma; * < target > < source > * corresponds to * -> cqi_fdist2("DICKENS:NP","target", "lemma", "matchend","word") * * ------------------------------------------------------------------------ */ SEXP rcqpCmd_fdist2(SEXP inSubcorpus, SEXP inField1, SEXP inKey1, SEXP inField2, SEXP inKey2, SEXP inCutoff) { SEXP result = R_NilValue; char *subcorpus, *att1, *att2; CorpusList * cl; int i, cutoff, size; Group * table; FieldType fieldtype1 = NoField; FieldType fieldtype2 = NoField; PROTECT(inSubcorpus); PROTECT(inField1); PROTECT(inKey1); PROTECT(inField2); PROTECT(inKey2); PROTECT(inCutoff); subcorpus = (char*)CHAR(STRING_ELT(inSubcorpus,0)); cl = cqi_find_corpus(subcorpus); if (cl == NULL) { UNPROTECT(6); rcqp_error_code(cqi_errno); } cutoff = asInteger(inCutoff); if (cutoff == NA_INTEGER) { UNPROTECT(6); error("invalid 'cutoff' value (too large or NA)"); } fieldtype1 = rcqp_get_field_type(inField1); fieldtype2 = rcqp_get_field_type(inField2); att1 = (char*)CHAR(STRING_ELT(inKey1,0)); att2 = (char*)CHAR(STRING_ELT(inKey2,0)); /* compute_grouping() returns tokens with f > cutoff, */ /* but CQi specifies f >= cutoff */ cutoff = (cutoff > 0) ? cutoff - 1 : 0; table = compute_grouping(cl, fieldtype1, 0, att1, fieldtype2, 0, att2, cutoff); if (table == NULL) { rcqp_error_code(CQI_CQP_ERROR_GENERAL); } else { size = table->nr_cells; result = PROTECT(allocMatrix(INTSXP, size, 3)); for (i=0; i < size; i++) { INTEGER(result)[i] = table->count_cells[i].s; INTEGER(result)[i+size] = table->count_cells[i].t; INTEGER(result)[i+(size*2)] = table->count_cells[i].freq; } free_group(&table); } UNPROTECT(7); return result; }
static void free_data(struct database *db) { struct group *g, *gnext; for (g = db->groups; g; g = gnext) { gnext = g->next; free_group(g); } }
static void test_group_readwrite(CuTest * tc) { faction * f; group *g; ally *al; int i; gamedata data; storage store; test_cleanup(); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); f = test_create_faction(0); new_group(f, "NW", 42); g = new_group(f, "Egoisten", 43); key_set(&g->attribs, 44); al = ally_add(&g->allies, f); al->status = HELP_GIVE; write_groups(&store, f); WRITE_INT(&store, 47); free_group(f->groups); free_group(g); f->groups = 0; data.strm.api->rewind(data.strm.handle); read_groups(&data, f); READ_INT(&store, &i); mstream_done(&data.strm); gamedata_done(&data); CuAssertIntEquals(tc, 47, i); CuAssertPtrNotNull(tc, f->groups); CuAssertIntEquals(tc, 42, f->groups->gid); CuAssertStrEquals(tc, "NW", f->groups->name); CuAssertPtrNotNull(tc, f->groups->next); CuAssertIntEquals(tc, 43, f->groups->next->gid); CuAssertStrEquals(tc, "Egoisten", f->groups->next->name); CuAssertPtrEquals(tc, 0, f->groups->allies); g = f->groups->next; CuAssertTrue(tc, key_get(g->attribs, 44)); CuAssertPtrNotNull(tc, g->allies); CuAssertPtrEquals(tc, 0, g->allies->next); CuAssertPtrEquals(tc, f, g->allies->faction); CuAssertIntEquals(tc, HELP_GIVE, g->allies->status); test_cleanup(); }
void unlink_group(Group *group) { Material *ma; Object *ob; Scene *sce; SceneRenderLayer *srl; ParticleSystem *psys; for(ma= G.main->mat.first; ma; ma= ma->id.next) { if(ma->group==group) ma->group= NULL; } for(ma= G.main->mat.first; ma; ma= ma->id.next) { if(ma->group==group) ma->group= NULL; } for (sce= G.main->scene.first; sce; sce= sce->id.next) { Base *base= sce->base.first; /* ensure objects are not in this group */ for(; base; base= base->next) { if(rem_from_group(group, base->object) && find_group(base->object, NULL)==NULL) { base->object->flag &= ~OB_FROMGROUP; base->flag &= ~OB_FROMGROUP; } } for(srl= sce->r.layers.first; srl; srl= srl->next) { if (srl->light_override==group) srl->light_override= NULL; } } for(ob= G.main->object.first; ob; ob= ob->id.next) { bActionStrip *strip; if(ob->dup_group==group) { ob->dup_group= NULL; /* duplicator strips use a group object, we remove it */ for(strip= ob->nlastrips.first; strip; strip= strip->next) { if(strip->object) strip->object= NULL; } } for(psys=ob->particlesystem.first; psys; psys=psys->next){ if(psys->part->dup_group==group) psys->part->dup_group= NULL; if(psys->part->eff_group==group) psys->part->eff_group= NULL; } } /* group stays in library, but no members */ free_group(group); group->id.us= 0; }
void free_list (list_t* list) { group_node_t* tmp = *list; while (*list != NULL) { tmp = *list; *list = tmp->next; free_group (&tmp->group); free (tmp); } }
/* * Free the whole list of group's in the global group_list. */ static void free_grouplist() { group_t *group = group_list; group_t *next; while (group) { next = group->next; free_group(group); group = next; } group_list = NULL; }
int delete_group(struct database *db, struct group *group) { struct group *g, **gprev; gprev = &db->groups; for (g = *gprev; g; gprev = &g->next, g = g->next) { if (g == group) { *gprev = g->next; free_group(g); return 0; } } db->dirty = 1; return -1; }
void close_inotify() { if ( xdata_inotify.fd>0 ) { close(xdata_inotify.fd); xdata_inotify.fd=0; } remove_xdata_from_beventloop(&xdata_inotify); remove_xdata_from_list(&xdata_inotify, 0); free_group(&group_watches_inotify, free_inotify_watch); }
/** remove the faction from memory. * this frees all memory that's only accessible through the faction, * but you should still call funhash and remove the faction from the * global list. */ static void free_faction(faction * f) { funhash(f); if (f->msgs) { free_messagelist(f->msgs->begin); free(f->msgs); } while (f->battles) { struct bmsg *bm = f->battles; f->battles = bm->next; if (bm->msgs) { free_messagelist(bm->msgs->begin); free(bm->msgs); } free(bm); } if (f->spellbook) { free_spellbook(f->spellbook); } while (f->groups) { group *g = f->groups; f->groups = g->next; free_group(g); } freelist(f->allies); free(f->email); free(f->name); if (f->seen_factions) { selist_free(f->seen_factions); f->seen_factions = 0; } while (f->attribs) { a_remove(&f->attribs, f->attribs); } i_freeall(&f->items); freelist(f->origin); }
static void increment_node_freq(LHALH1Decoder *decoder, uint16_t node_index) { Node *node, *other; node = &decoder->nodes[node_index]; other = &decoder->nodes[node_index - 1]; ++node->freq; // If the node is part of a group containing other nodes, it // must leave the group. if (node_index < NUM_TREE_NODES - 1 && node->group == decoder->nodes[node_index + 1].group) { // Next node in the group now becomes the leader. ++decoder->group_leader[node->group]; // The node must now either join the group to its // left, or start a new group. if (node->freq == other->freq) { node->group = other->group; } else { node->group = alloc_group(decoder); decoder->group_leader[node->group] = node_index; } } else { // The node is in a group of its own (single-node // group). It might need to join the group of the // node on its left if it has the same frequency. if (node->freq == other->freq) { free_group(decoder, node->group); node->group = other->group; } } }
static void test_group_readwrite(CuTest * tc) { faction * f; group *g; ally *al; storage store; FILE *F; stream strm; F = fopen("test.dat", "wb"); fstream_init(&strm, F); binstore_init(&store, &strm); test_cleanup(); test_create_world(); f = test_create_faction(0); g = new_group(f, "test", 42); al = ally_add(&g->allies, f); al->status = HELP_GIVE; write_groups(&store, f); binstore_done(&store); fstream_done(&strm); F = fopen("test.dat", "rb"); fstream_init(&strm, F); binstore_init(&store, &strm); f->groups = 0; free_group(g); read_groups(&store, f); binstore_done(&store); fstream_done(&strm); CuAssertPtrNotNull(tc, f->groups); CuAssertPtrNotNull(tc, f->groups->allies); CuAssertPtrEquals(tc, 0, f->groups->allies->next); CuAssertPtrEquals(tc, f, f->groups->allies->faction); CuAssertIntEquals(tc, HELP_GIVE, f->groups->allies->status); remove("test.dat"); test_cleanup(); }
int main(int argc, char* argv[]) { srand((unsigned)time(NULL)); int i, rank, size; int job_id; int num_slaves; // # of slaves int num_gen = 100; // # of generations int num_genes = 2; // # of gene segments int num_chrs = 8; // # of chromosomes double cross_rate = 0.95; // crossover rate double mutate_rate = 0.001; // mutation rate double fitness; unsigned char* genes = (unsigned char*)malloc(num_genes * sizeof(unsigned char)); Group* grp = init_group(num_genes, num_chrs, cross_rate, mutate_rate); MPI_Status stat; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); /* master is here */ if (0 == rank) { num_slaves = size - 1; job_id = 0; for (i = 1; i < size; i++) { // send next job i, send grp->chrs[i]->genes MPI_Send(grp->chrs[job_id]->genes, num_genes, MPI_UNSIGNED_CHAR, i, 0, MPI_COMM_WORLD); job_id++; } while (job_id < num_chrs) { i = job_id % num_slaves + 1; // get result s, recv grp->chrs[s]->genes MPI_Recv(&(grp->chrs[job_id-num_slaves]->fitness), 1, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, &stat); // send next job s, send grp->chrs[s]->genes MPI_Send(grp->chrs[job_id]->genes, num_genes, MPI_UNSIGNED_CHAR, i, 0, MPI_COMM_WORLD); job_id++; } int job_left = num_chrs % num_slaves; for (i = 0; i < num_genes; i++) { genes[i] = 0; } for (i = 1; i < size; i++) { int pid = (i + job_left - 1) % num_slaves + 1; // get result s, recv grp->chrs[s]->fitness MPI_Recv(&(grp->chrs[job_id-num_slaves]->fitness), 1, MPI_DOUBLE, pid, 0, MPI_COMM_WORLD, &stat); job_id++; // no more jobs s, send genes with all 0 values to represent no job MPI_Send(genes, num_genes, MPI_UNSIGNED_CHAR, i, 0, MPI_COMM_WORLD); } } else { /* slaves are here */ while (1) { // get next job s, recv grp->chrs[s]->genes MPI_Recv(genes, num_genes, MPI_UNSIGNED_CHAR, 0, 0, MPI_COMM_WORLD, &stat); // stop process if no job unsigned char sum = 0; for (i = 0; i < num_genes; i++) { sum += genes[i]; } // if sum of values of all gene segments is 0, // it means no job and should stop if (0 == sum) break; // do work fitness = update_fitness(num_genes, genes); // send result s, send fitness MPI_Send(&fitness, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); } } /* Genetic Algorithm Process done by master */ if (0 == rank) { for (i = 0; i < num_gen; i++) { update_fit_rate(grp); evolve(grp); } } free(genes); free_group(grp); MPI_Finalize(); return 0; }
/* used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c */ void free_libblock(ListBase *lb, void *idv) { ID *id= idv; switch( GS(id->name) ) { /* GetShort from util.h */ case ID_SCE: free_scene((Scene *)id); break; case ID_LI: free_library((Library *)id); break; case ID_OB: free_object((Object *)id); break; case ID_ME: free_mesh((Mesh *)id); break; case ID_CU: free_curve((Curve *)id); break; case ID_MB: free_mball((MetaBall *)id); break; case ID_MA: free_material((Material *)id); break; case ID_TE: free_texture((Tex *)id); break; case ID_IM: free_image((Image *)id); break; case ID_WV: /* free_wave(id); */ break; case ID_LT: free_lattice((Lattice *)id); break; case ID_LA: free_lamp((Lamp *)id); break; case ID_CA: free_camera((Camera*) id); break; case ID_IP: free_ipo((Ipo *)id); break; case ID_KE: free_key((Key *)id); break; case ID_WO: free_world((World *)id); break; case ID_SCR: free_screen((bScreen *)id); break; case ID_VF: free_vfont((VFont *)id); break; case ID_TXT: free_text((Text *)id); break; case ID_SCRIPT: //XXX free_script((Script *)id); break; case ID_SO: sound_free((bSound*)id); break; case ID_GR: free_group((Group *)id); break; case ID_AR: free_armature((bArmature *)id); break; case ID_AC: free_action((bAction *)id); break; case ID_NT: ntreeFreeTree((bNodeTree *)id); break; case ID_BR: free_brush((Brush *)id); break; case ID_PA: psys_free_settings((ParticleSettings *)id); break; case ID_WM: if(free_windowmanager_cb) free_windowmanager_cb(NULL, (wmWindowManager *)id); break; case ID_GD: free_gpencil_data((bGPdata *)id); break; } if (id->properties) { IDP_FreeProperty(id->properties); MEM_freeN(id->properties); } BLI_remlink(lb, id); MEM_freeN(id); }
Group * ComputeGroupInternally(Group *group) { ID_Count_Mapping node; ID_Count_Mapping *result; int i; size_t nr_nodes; int percentage, new_percentage; /* for ProgressBar */ int size = group->my_corpus->size; /* ---------------------------------------------------------------------- */ nr_nodes = 0; if (progress_bar) progress_bar_clear_line(); percentage = -1; EvaluationIsRunning = 1; for (i = 0; i < size; i++) { if (! EvaluationIsRunning) break; /* user abort (Ctrl-C) */ if (progress_bar) { new_percentage = floor(0.5 + (100.0 * i) / size); if (new_percentage > percentage) { percentage = new_percentage; progress_bar_percentage(1, 2, percentage); } } node.s = get_group_id(group, i, 0); /* source ID */ node.t = get_group_id(group, i, 1); /* target ID */ node.freq = 0; result = binsert_g(&node, (void **) &(group->count_cells), &nr_nodes, sizeof(ID_Count_Mapping), compare_st_cells); result->freq++; } if (EvaluationIsRunning) { group->nr_cells = sum_freqs(group->count_cells, nr_nodes, group->cutoff_frequency); if (progress_bar) progress_bar_clear_line(); if (group->nr_cells < nr_nodes) group->count_cells = cl_realloc(group->count_cells, (group->nr_cells * sizeof(ID_Count_Mapping))); } else { cqpmessage(Warning, "Group operation aborted by user."); if (which_app == cqp) install_signal_handler(); free_group(&group); /* sets return value to NULL to indicate failure */ } EvaluationIsRunning = 0; return group; }
void do_cqi_cqp_fdist_2(void) { char *subcorpus; CorpusList *cl; int cutoff; cqi_byte field1, field2; char *att1, *att2; Group *table; int i, size; char *fieldname1, *fieldname2; FieldType fieldtype1 = NoField, fieldtype2 = NoField; int fields_ok = 1; /* (both) fields valid? */ subcorpus = cqi_read_string(); cutoff = cqi_read_int(); field1 = cqi_read_byte(); att1 = cqi_read_string(); field2 = cqi_read_byte(); att2 = cqi_read_string(); /* not exactly the fastest way to do it ... */ fieldname1 = cqi_field_name(field1); if (fieldname1 == NULL) { fieldname1 = "<invalid field>"; fields_ok = 0; } else { fieldtype1 = field_name_to_type(fieldname1); } fieldname2 = cqi_field_name(field2); if (fieldname2 == NULL) { fieldname2 = "<invalid field>"; fields_ok = 0; } else { fieldtype2 = field_name_to_type(fieldname2); } if (server_debug) Rprintf( "CQi: CQI_CQP_FDIST_2('%s', %d, %s, %s, %s, %s)\n", subcorpus, cutoff, fieldname1, att1, fieldname2, att2); cl = cqi_find_corpus(subcorpus); if (cl == NULL) cqi_command(cqi_errno); else if (!fields_ok) cqi_command(CQI_CQP_ERROR_INVALID_FIELD); else { /* compute_grouping() returns tokens with f > cutoff, but CQi specifies f >= cutoff */ cutoff = (cutoff > 0) ? cutoff - 1 : 0; table = compute_grouping(cl, fieldtype1, 0, att1, fieldtype2, 0, att2, cutoff); if (table == NULL) { cqi_command(CQI_CQP_ERROR_GENERAL); } else { size = table->nr_cells; cqi_send_word(CQI_DATA_INT_TABLE); /* return table with 3 columns & <size> rows */ cqi_send_int(size); cqi_send_int(3); for (i=0; i < size; i++) { cqi_send_int(table->count_cells[i].s); cqi_send_int(table->count_cells[i].t); cqi_send_int(table->count_cells[i].freq); } cqi_flush(); free_group(&table); } } cl_free(subcorpus); cl_free(att1); cl_free(att2); }
/*============== main() =================*/ int main(int argc, char* argv[]) { srand((unsigned)time(NULL)); /* check arguments length */ int numArgv = 13; if (numArgv != argc) { print_usage(); // print usage and help info exit (1); } /* get values from command line */ int num_genes; // # of gene segments int num_players; // # of chromosomes/prisoners int num_gen; // # of generations int num_iters; // # of game iterations double cross_rate; // crossover rate double mutate_rate; // mutation rate // the order of arguments is arbitrary int i, j; for (i = 1; i < numArgv; i+=2) { if (0 == strcmp("-s",argv[i])) num_genes = atoi(argv[i+1]); else if (0 == strcmp("-p",argv[i])) num_players = atoi(argv[i+1]); else if (0 == strcmp("-g",argv[i])) num_gen = atoi(argv[i+1]); else if (0 == strcmp("-i",argv[i])) num_iters = atoi(argv[i+1]); else if (0 == strcmp("-c",argv[i])) cross_rate = atof(argv[i+1]); else if (0 == strcmp("-m",argv[i])) mutate_rate = atof(argv[i+1]); else { print_usage(); // print usage and help info exit (2); } } /* Prisoner's Dilemma Game */ Group* players = init_group(num_genes, num_players, cross_rate, mutate_rate); // run for num_gen generations for (i = 0; i < num_gen; i++) { // play PD game for num_iters times for (j = 0; j < num_iters; j++) { play_game(players); } update_fit_rate(players); evolve(players); } free_group(players); return 0; }
void initialize_inotify(unsigned int *error) { struct bevent_xdata_struct *xdata=NULL; int result=0, fd=0; /* create the inotify instance */ fd=inotify_init(); if (fd==-1) { *error=errno; logoutput("initialize_inotify: error creating inotify fd: %i.", errno); goto error; } /* add inotify to the main eventloop */ xdata=add_to_beventloop(fd, EPOLLIN | EPOLLPRI, &handle_inotify_fd, NULL, &xdata_inotify, NULL); if ( ! xdata ) { logoutput("initialize_inotify: error adding inotify fd to eventloop."); goto error; } else { logoutput("initialize_inotify: inotify fd %i added to eventloop", fd); add_xdata_to_list(xdata); } result=initialize_group(&group_watches_inotify, wd_hashfunction, 256, error); if (result<0) { *error=abs(result); logoutput("initialize_inotify: error %i adding inotify fd to eventloop", *error); goto error; } return; error: if (fd>0) { close(fd); fd=0; } if (xdata) { remove_xdata_from_beventloop(xdata); xdata=NULL; } free_group(&group_watches_inotify, free_inotify_watch); }
/* * A libdevinfo di_walk_node() callback that builds up the MPxIO group list. * * Every node encountered that's a client node is added into a group's client * list. Whenever a group doesn't already exist with a matching set of * related PHCIs, then a new group is constructed and put at the head of the * group list. */ static int build_groups(di_node_t dinode, void *arg) { int i = 0; int nphcis = 0; int *nclients = (int *)arg; phci_t *phcis; group_t *group; di_node_t phcinode; di_path_t dipath = DI_PATH_NIL; /* Safety check */ if (nclients == NULL) return (DI_WALK_TERMINATE); /* * Build a sorted array of PHCIs pertaining to the client. */ while ((dipath = di_path_client_next_path(dinode, dipath)) != DI_PATH_NIL) nphcis++; /* Skip non-clients. */ if (nphcis == 0) return (DI_WALK_CONTINUE); if ((phcis = (phci_t *)calloc(nphcis, sizeof (phci_t))) == NULL) { rcm_log_message(RCM_ERROR, "MPXIO: failed to allocate client's PHCIs (%s).\n", strerror(errno)); return (DI_WALK_TERMINATE); } while ((dipath = di_path_client_next_path(dinode, dipath)) != DI_PATH_NIL) { phcinode = di_path_phci_node(dipath); if (phcinode == DI_NODE_NIL) { free_phcis(i, phcis); /* free preceeding PHCIs */ rcm_log_message(RCM_ERROR, "MPXIO: client appears to have no PHCIs.\n"); return (DI_WALK_TERMINATE); } if ((phcis[i].path = get_rsrcname(phcinode)) == NULL) { free_phcis(i, phcis); return (DI_WALK_TERMINATE); } phcis[i].state = di_path_state(dipath); i++; } qsort(phcis, nphcis, sizeof (phci_t), compare_phci); /* * Compare that PHCI set to each existing group's set. We just add * the client to the group and exit successfully once a match is made. * Falling out of this loop means no match was found. */ for (group = group_list; group != NULL; group = group->next) { /* There is no match if the number of PHCIs is inequal */ if (nphcis != group->nphcis) continue; /* Compare the PHCIs linearly (which is okay; they're sorted) */ for (i = 0; i < nphcis; i++) if (strcmp(phcis[i].path, group->phcis[i].path) != 0) break; /* * If the loop above completed, we have a match. Add the client * to the group's disk array in that case, and return * successfully. */ if (i == nphcis) { free_phcis(nphcis, phcis); if ((group->clients[group->nclients] = get_rsrcname(dinode)) == NULL) return (DI_WALK_TERMINATE); group->nclients++; return (DI_WALK_CONTINUE); } } /* The loop above didn't find a match. So build a new group. */ if ((group = (group_t *)calloc(1, sizeof (*group))) == NULL) { rcm_log_message(RCM_ERROR, "MPXIO: failed to allocate PHCI group (%s).\n", strerror(errno)); free_phcis(nphcis, phcis); return (DI_WALK_TERMINATE); } if ((group->clients = (char **)calloc(*nclients, sizeof (char *))) == NULL) { free(group); free_phcis(nphcis, phcis); return (DI_WALK_TERMINATE); } group->nphcis = nphcis; group->phcis = phcis; if ((group->clients[0] = get_rsrcname(dinode)) == NULL) { free_group(group); return (DI_WALK_TERMINATE); } group->nclients = 1; /* Link the group into the group list and return successfully. */ group->next = group_list; group_list = group; return (DI_WALK_CONTINUE); }