static int rebuild_shuffle_data(void) { if (g_list.cycle_mode != conf_cycle_random) return 0; g_shuffle.index = 0; g_shuffle.size = music_maxindex(); dbg_printf(d, "%s: rebuild shuffle data", __func__); if (g_shuffle.table != NULL) { free(g_shuffle.table); g_shuffle.table = NULL; } g_shuffle.table = build_array(music_maxindex()); shuffle(g_shuffle.table, g_shuffle.size); if (g_shuffle.first_time) { unsigned i; for (i = 0; i < g_shuffle.size; ++i) { if (g_shuffle.table[i] == 0) { swap(&g_shuffle.table[i], &g_shuffle.table[0]); break; } } g_shuffle.index = 1; g_shuffle.first_time = false; } return 0; }
/** * Builds a vertex buffer object containing the mesh vertex data. * * The way the VBO is constructed is affected by the current interleave * value (::interleave()) and the vbo usage hint (::vbo_usage()). */ void Mesh::build_vbo() { delete_array(); build_array(); int nvertices = vertices_.size(); attrib_data_ptr_.clear(); GLenum buffer_usage; if (vbo_usage_ == Mesh::VBOUsageStream) buffer_usage = GL_STREAM_DRAW; else if (vbo_usage_ == Mesh::VBOUsageDynamic) buffer_usage = GL_DYNAMIC_DRAW; else /* if (vbo_usage_ == Mesh::VBOUsageStatic) */ buffer_usage = GL_STATIC_DRAW; if (!interleave_) { /* Create a vbo for each attribute */ for (std::vector<std::pair<int, int> >::const_iterator ai = vertex_format_.begin(); ai != vertex_format_.end(); ai++) { float *data = vertex_arrays_[ai - vertex_format_.begin()]; GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, nvertices * ai->first * sizeof(float), data, buffer_usage); vbos_.push_back(vbo); attrib_data_ptr_.push_back(0); } vertex_stride_ = 0; } else { GLuint vbo; /* Create a single vbo to store all attribute data */ glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, nvertices * vertex_size_ * sizeof(float), vertex_arrays_[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); for (size_t i = 0; i < vertex_format_.size(); i++) { attrib_data_ptr_.push_back(reinterpret_cast<float *>(sizeof(float) * vertex_format_[i].second)); vbos_.push_back(vbo); } vertex_stride_ = vertex_size_ * sizeof(float); } delete_array(); }
Datum pgheal_ipix2ang_ring(PG_FUNCTION_ARGS) { q3c_ipix_t ipix; q3c_coord_t ra, dec, theta, phi; q3c_ipix_t nside = PG_GETARG_INT64(0); nside_check(nside); ipix = PG_GETARG_INT64(1); ipix_check(nside, ipix); pix2ang_ring(nside, ipix, &theta, &phi); get_ra_dec(theta, phi, &ra, &dec); PG_RETURN_ARRAYTYPE_P(build_array(ra,dec)); }
Datum array_agg_distinct_type_by_element(PG_FUNCTION_ARGS) { /* get element type for the dummy second parameter (anynonarray item) */ Oid element_type = get_fn_expr_argtype(fcinfo->flinfo, 1); CHECK_AGG_CONTEXT("count_distinct", fcinfo); /* return empty array if the state was not initialized */ if (PG_ARGISNULL(0)) PG_RETURN_DATUM(PointerGetDatum(construct_empty_array(element_type))); return build_array((element_set_t *)PG_GETARG_POINTER(0), element_type); }
void build_array(struct Value_array * array, struct Tokens * tokens){ while(tokens->tokens[tokens->counter].type != 'e'){ tokens->counter++; if(tokens->tokens[tokens->counter].type == 's'){ struct Value * val = malloc(sizeof(struct Value)); val->type = 's'; val->data.str.length = tokens->tokens[tokens->counter].data.length; strcpy(val->data.str.body, tokens->tokens[tokens->counter].data.body); array->values[array->size] = val; array->size++; } else if(tokens->tokens[tokens->counter].type == 'n'){ struct Value * val = malloc(sizeof(struct Value)); if(string_contains(dot_const, &tokens->tokens[tokens->counter].data)){ float content = atof(tokens->tokens[tokens->counter].data.body); val->type='f'; val->data.fl=content; } else { long content = atol(tokens->tokens[tokens->counter].data.body); val->type='l'; val->data.ln=content; } array->values[array->size] = val; array->size++; } else if(tokens->tokens[tokens->counter].type == 'k'){ struct Value * val = malloc(sizeof(struct Value)); val->type = 'k'; val->data.str.length = tokens->tokens[tokens->counter].data.length; strcpy(val->data.str.body, tokens->tokens[tokens->counter].data.body); array->values[array->size] = val; array->size++; } else if(tokens->tokens[tokens->counter].type == 'b'){ struct Value * val = malloc(sizeof(struct Value)); val->type = 'a'; val->data.array = malloc(sizeof(struct Value_array)); val->data.array->size = 0; build_array(val->data.array, tokens); array->values[array->size] = val; array->size++; // to advance beyond the inner closing bracket, otherwise it // will cause the outer array to stop building tokens->counter++; } } }
int qsort_line(){ int i ; Record my_array; build_array(&my_array); printf("\nBefore sorting the list is: \n"); for( i = 0 ; i < my_array.length; i++ ) { printf("%d ", my_array.keyword[i]); } qsort(my_array.keyword, my_array.length, sizeof(int), cmpfunc); printf("\nAfter sorting the list is: \n"); for( i = 0 ; i < my_array.length; i++ ) { printf("%d ", my_array.keyword[i]); } return(0); }
/** * Updates ranges of the vertex arrays. * * @param ranges the ranges of vertices to update */ void Mesh::update_array(const std::vector<std::pair<size_t, size_t> >& ranges) { /* If we don't have arrays to update, create them */ if (vertex_arrays_.empty()) { build_array(); return; } if (!interleave_) { for (size_t i = 0; i < vertex_arrays_.size(); i++) { update_single_array(ranges, i, vertex_format_[i].first, vertex_format_[i].second); } } else { update_single_array(ranges, 0, vertex_size_, 0); } }
static std::string build_one(const cJSON *json, TypedObject &to) { switch (json->type) { case 0: // cJSON_False case 1: // cJSON_True to.type = 0; to.data.bv = (json->type == cJSON_True); break; case 2: // cJSON_NULL to.type = 5; break; case 3: // cJSON_Number to.type = 1; to.data.nv = json->valuedouble; break; case 4: // cJSON_String to.type = 2; to.data.sv = json->valuestring; break; case 5: // cJSON_array to.type = 3; build_array(json->child, to.data.av); break; case 6: // cJSON_object to.type = 4; build_object(json->child, to.data.ov); break; } if (json->string) return json->string; else return ""; // 无名对象 .. }
static int do_plink(char *cwd, int cmd, int nbr, char *br[]) { int err, i, l; struct rlimit rlim; __nftw_func_t func; char *p; err = 0; switch (cmd) { case AuPlink_FLUSH: /*FALLTHROUGH*/ case AuPlink_CPUP: func = ftw_cpup; break; case AuPlink_LIST: func = ftw_list; break; default: errno = EINVAL; AuFin(NULL); func = NULL; /* never reach here */ } for (i = 0; i < nbr; i++) { //puts(br[i]); p = strchr(br[i], '='); if (strcmp(p + 1, AUFS_BRPERM_RW) && strcmp(p + 1, AUFS_BRPERM_RWNLWH)) continue; *p = 0; l = strlen(br[i]); p = malloc(l + sizeof(AUFS_WH_PLINKDIR) + 2); if (!p) AuFin("malloc"); sprintf(p, "%s/%s", br[i], AUFS_WH_PLINKDIR); //puts(p); err = build_array(p); if (err) AuFin("build_array"); free(p); } if (!ia.nino) goto out; if (cmd == AuPlink_LIST) { ia.p = ia.o; for (i = 0; i < ia.nino; i++) printf("%llu ", (unsigned long long)*ia.cur++); putchar('\n'); } err = getrlimit(RLIMIT_NOFILE, &rlim); if (err) AuFin("getrlimit"); nftw(cwd, func, rlim.rlim_cur - 10, FTW_PHYS | FTW_MOUNT | FTW_ACTIONRETVAL); /* ignore */ if (cmd == AuPlink_FLUSH) { au_clean_plink(); na.cur = na.o; for (i = 0; i < na.nname; i++) { Dpri("%s\n", na.cur); err = unlink(na.cur); if (err) AuFin("%s", na.cur); na.cur += strlen(na.cur) + 1; } } out: free(ia.o); free(na.o); return err; }
static int create_volume(int ac, char **av) { struct mfi_config_data *config; struct mfi_array *ar; struct mfi_ld_config *ld; struct config_id_state state; size_t config_size; char *p, *cfg_arrays, *cfg_volumes; int error, fd, i, raid_type; int narrays, nvolumes, arrays_per_volume; struct array_info *arrays; long stripe_size; #ifdef DEBUG int dump; #endif int ch, verbose; /* * Backwards compat. Map 'create volume' to 'create' and * 'create spare' to 'add'. */ if (ac > 1) { if (strcmp(av[1], "volume") == 0) { av++; ac--; } else if (strcmp(av[1], "spare") == 0) { av++; ac--; return (add_spare(ac, av)); } } if (ac < 2) { warnx("create volume: volume type required"); return (EINVAL); } bzero(&state, sizeof(state)); config = NULL; arrays = NULL; narrays = 0; error = 0; fd = mfi_open(mfi_unit); if (fd < 0) { error = errno; warn("mfi_open"); return (error); } if (!mfi_reconfig_supported()) { warnx("The current mfi(4) driver does not support " "configuration changes."); error = EOPNOTSUPP; goto error; } /* Lookup the RAID type first. */ raid_type = -1; for (i = 0; raid_type_table[i].name != NULL; i++) if (strcasecmp(raid_type_table[i].name, av[1]) == 0) { raid_type = raid_type_table[i].raid_type; break; } if (raid_type == -1) { warnx("Unknown or unsupported volume type %s", av[1]); error = EINVAL; goto error; } /* Parse any options. */ optind = 2; #ifdef DEBUG dump = 0; #endif verbose = 0; stripe_size = 64 * 1024; while ((ch = getopt(ac, av, "ds:v")) != -1) { switch (ch) { #ifdef DEBUG case 'd': dump = 1; break; #endif case 's': stripe_size = dehumanize(optarg); if ((stripe_size < 512) || (!powerof2(stripe_size))) stripe_size = 64 * 1024; break; case 'v': verbose = 1; break; case '?': default: error = EINVAL; goto error; } } ac -= optind; av += optind; /* Parse all the arrays. */ narrays = ac; if (narrays == 0) { warnx("At least one drive list is required"); error = EINVAL; goto error; } switch (raid_type) { case RT_RAID0: case RT_RAID1: case RT_RAID5: case RT_RAID6: case RT_CONCAT: if (narrays != 1) { warnx("Only one drive list can be specified"); error = EINVAL; goto error; } break; case RT_RAID10: case RT_RAID50: case RT_RAID60: if (narrays < 1) { warnx("RAID10, RAID50, and RAID60 require at least " "two drive lists"); error = EINVAL; goto error; } if (narrays > MFI_MAX_SPAN_DEPTH) { warnx("Volume spans more than %d arrays", MFI_MAX_SPAN_DEPTH); error = EINVAL; goto error; } break; } arrays = calloc(narrays, sizeof(*arrays)); if (arrays == NULL) { warnx("malloc failed"); error = ENOMEM; goto error; } for (i = 0; i < narrays; i++) { error = parse_array(fd, raid_type, av[i], &arrays[i]); if (error) goto error; } switch (raid_type) { case RT_RAID10: case RT_RAID50: case RT_RAID60: for (i = 1; i < narrays; i++) { if (arrays[i].drive_count != arrays[0].drive_count) { warnx("All arrays must contain the same " "number of drives"); error = EINVAL; goto error; } } break; } /* * Fetch the current config and build sorted lists of existing * array and volume identifiers. */ if (mfi_config_read(fd, &config) < 0) { error = errno; warn("Failed to read configuration"); goto error; } p = (char *)config->array; state.array_ref = 0xffff; state.target_id = 0xff; state.array_count = config->array_count; if (config->array_count > 0) { state.arrays = calloc(config->array_count, sizeof(int)); if (state.arrays == NULL) { warnx("malloc failed"); error = ENOMEM; goto error; } for (i = 0; i < config->array_count; i++) { ar = (struct mfi_array *)p; state.arrays[i] = ar->array_ref; p += config->array_size; } qsort(state.arrays, config->array_count, sizeof(int), compare_int); } else state.arrays = NULL; state.log_drv_count = config->log_drv_count; if (config->log_drv_count) { state.volumes = calloc(config->log_drv_count, sizeof(int)); if (state.volumes == NULL) { warnx("malloc failed"); error = ENOMEM; goto error; } for (i = 0; i < config->log_drv_count; i++) { ld = (struct mfi_ld_config *)p; state.volumes[i] = ld->properties.ld.v.target_id; p += config->log_drv_size; } qsort(state.volumes, config->log_drv_count, sizeof(int), compare_int); } else state.volumes = NULL; free(config); /* Determine the size of the configuration we will build. */ switch (raid_type) { case RT_RAID0: case RT_RAID1: case RT_RAID5: case RT_RAID6: case RT_CONCAT: case RT_JBOD: /* Each volume spans a single array. */ nvolumes = narrays; break; case RT_RAID10: case RT_RAID50: case RT_RAID60: /* A single volume spans multiple arrays. */ nvolumes = 1; break; default: /* Pacify gcc. */ abort(); } config_size = sizeof(struct mfi_config_data) + sizeof(struct mfi_ld_config) * nvolumes + MFI_ARRAY_SIZE * narrays; config = calloc(1, config_size); if (config == NULL) { warnx("malloc failed"); error = ENOMEM; goto error; } config->size = config_size; config->array_count = narrays; config->array_size = MFI_ARRAY_SIZE; /* XXX: Firmware hardcode */ config->log_drv_count = nvolumes; config->log_drv_size = sizeof(struct mfi_ld_config); config->spares_count = 0; config->spares_size = 40; /* XXX: Firmware hardcode */ cfg_arrays = (char *)config->array; cfg_volumes = cfg_arrays + config->array_size * narrays; /* Build the arrays. */ for (i = 0; i < narrays; i++) { build_array(fd, cfg_arrays, &arrays[i], &state, verbose); cfg_arrays += config->array_size; } /* Now build the volume(s). */ arrays_per_volume = narrays / nvolumes; for (i = 0; i < nvolumes; i++) { build_volume(cfg_volumes, arrays_per_volume, &arrays[i * arrays_per_volume], raid_type, stripe_size, &state, verbose); cfg_volumes += config->log_drv_size; } #ifdef DEBUG if (dump) dump_config(fd, config); #endif /* Send the new config to the controller. */ if (mfi_dcmd_command(fd, MFI_DCMD_CFG_ADD, config, config_size, NULL, 0, NULL) < 0) { error = errno; warn("Failed to add volume"); /* FALLTHROUGH */ } error: /* Clean up. */ free(config); free(state.volumes); free(state.arrays); for (i = 0; i < narrays; i++) free(arrays[i].drives); free(arrays); close(fd); return (error); }
int main() { int arr[6]; build_array(arr, 6); printf("%d\n", binsearch(3, arr, 6)); }