double sem_cov_ab(VARIOGRAM *v, DPOINT *a, DPOINT *b, int sem) /* * return Cov(a,b) or Sem(a,b), * taking care of IS_BLOCK(a) and IS_BLOCK(b): */ { static DATA *Discr_a = NULL, *Discr_b = NULL; static DPOINT *block_p = NULL; DPOINT *tmp; if (block_p == NULL) block_p = get_block_p(); if (a == b) { if (IS_POINT(a)) return sem_cov_blocks(v, NULL, NULL, sem); Discr_a = block_discr(Discr_a, block_p, a); return sem_cov_blocks(v, Discr_a, Discr_a, sem); } /* * if one of them IS_BLOCK, make sure it's a: * (because block_discr() will otherwise store block * discretisations in both Discr_a and Discr_b) */ if (IS_POINT(a) && IS_BLOCK(b)) { tmp = a; a = b; b = tmp; /* swap a and b */ } Discr_a = block_discr(Discr_a, block_p, a); Discr_b = block_discr(Discr_b, block_p, b); return sem_cov_blocks(v, Discr_a, Discr_b, sem); }
Datum cube_subset(PG_FUNCTION_ARGS) { NDBOX *c = PG_GETARG_NDBOX(0); ArrayType *idx = PG_GETARG_ARRAYTYPE_P(1); NDBOX *result; int size, dim, i; int *dx; if (array_contains_nulls(idx)) ereport(ERROR, (errcode(ERRCODE_ARRAY_ELEMENT_ERROR), errmsg("cannot work with arrays containing NULLs"))); dx = (int32 *) ARR_DATA_PTR(idx); dim = ARRNELEMS(idx); if (dim > CUBE_MAX_DIM) ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("array is too long"), errdetail("A cube cannot have more than %d dimensions.", CUBE_MAX_DIM))); size = IS_POINT(c) ? POINT_SIZE(dim) : CUBE_SIZE(dim); result = (NDBOX *) palloc0(size); SET_VARSIZE(result, size); SET_DIM(result, dim); if (IS_POINT(c)) SET_POINT_BIT(result); for (i = 0; i < dim; i++) { if ((dx[i] <= 0) || (dx[i] > DIM(c))) { pfree(result); ereport(ERROR, (errcode(ERRCODE_ARRAY_ELEMENT_ERROR), errmsg("Index out of bounds"))); } result->x[i] = c->x[dx[i] - 1]; if (!IS_POINT(c)) result->x[i + dim] = c->x[dx[i] + DIM(c) - 1]; } PG_FREE_IF_COPY(c, 0); PG_RETURN_NDBOX(result); }
/* Add a dimension to an existing cube */ Datum cube_c_f8_f8(PG_FUNCTION_ARGS) { NDBOX *cube = PG_GETARG_NDBOX(0); double x1 = PG_GETARG_FLOAT8(1); double x2 = PG_GETARG_FLOAT8(2); NDBOX *result; int size; int i; if (DIM(cube) + 1 > CUBE_MAX_DIM) ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("can't extend cube"), errdetail("A cube cannot have more than %d dimensions.", CUBE_MAX_DIM))); if (IS_POINT(cube) && (x1 == x2)) { size = POINT_SIZE((DIM(cube) + 1)); result = (NDBOX *) palloc0(size); SET_VARSIZE(result, size); SET_DIM(result, DIM(cube) + 1); SET_POINT_BIT(result); for (i = 0; i < DIM(cube); i++) result->x[i] = cube->x[i]; result->x[DIM(result) - 1] = x1; } else { size = CUBE_SIZE((DIM(cube) + 1)); result = (NDBOX *) palloc0(size); SET_VARSIZE(result, size); SET_DIM(result, DIM(cube) + 1); for (i = 0; i < DIM(cube); i++) { result->x[i] = LL_COORD(cube, i); result->x[DIM(result) + i] = UR_COORD(cube, i); } result->x[DIM(result) - 1] = x1; result->x[2 * DIM(result) - 1] = x2; } PG_FREE_IF_COPY(cube, 0); PG_RETURN_NDBOX(result); }
static bool cube_is_point_internal(NDBOX *cube) { int i; if (IS_POINT(cube)) return true; /* * Even if the point-flag is not set, all the lower-left coordinates might * match the upper-right coordinates, so that the value is in fact a * point. Such values don't arise with current code - the point flag is * always set if appropriate - but they might be present on-disk in * clusters upgraded from pre-9.4 versions. */ for (i = 0; i < DIM(cube); i++) { if (LL_COORD(cube, i) != UR_COORD(cube, i)) return false; } return true; }
int ini_read_ini(s_ini_handle* hdl) { unsigned int i, current_line; s_ini_section* current_section = 0; char* current_section_name = 0; char* current_key_name = 0; char* current_value = 0; unsigned int name_start, value_start; int current_state; if (hdl == 0) return -1; if (hdl->buffer == 0) return -1; /* printf("ini_read_ini\n"); */ current_state = STATE_EMPTY; name_start = 0; value_start = 0; current_line = 1; for (i = 0; i < hdl->buffer_size; ++i) { switch (current_state) { case STATE_EMPTY: switch (hdl->buffer[i]) { case '[': name_start = i; current_state = STATE_SECTION_NAME; if (current_section_name != 0) { free(current_section_name); current_section_name = 0; } break; case ' ': case '\t': case '\n': case '\r': /* Skip white spaces */ break; case '#': /* Skip comment line */ current_state = STATE_SKIP; break; default: if ( IS_ALPHA_NUMERIC(hdl->buffer[i]) ) { current_state = STATE_KEYNAME; name_start = i; if (current_key_name != 0) { free(current_key_name); current_key_name = 0; } } else { printf("Unkown char %c found at line %d\n", hdl->buffer[i], current_line); } break; } break; case STATE_SECTION_NAME: switch(hdl->buffer[i]) { case '\r': case '\n': printf("Misformated INI file (missing ']') at line %d\n", current_line); case ']': { unsigned int section_name_len = i - (name_start); current_section_name = (char*)malloc(section_name_len); memcpy(current_section_name, &hdl->buffer[name_start+1], section_name_len); current_section_name[section_name_len-1] = 0x00; /* printf("Found section: '%s' at line %d\n", current_section_name, current_line); */ current_section = ini_add_section(hdl, current_section_name); current_section_name = 0; /* prevent current_section_name from being freed */ current_state = STATE_EMPTY; } break; } break; case STATE_KEYNAME: if (hdl->buffer[i] == '=') { unsigned int key_name_len = i - name_start + 1; current_key_name = (char*)malloc(key_name_len); memcpy(current_key_name, &hdl->buffer[name_start], key_name_len); current_key_name[key_name_len-1] = 0x00; /* printf("Found key: '%s' at line %d\n", current_key_name, current_line); */ value_start = i; if (current_value != 0) { free(current_value); current_value = 0; } current_state = STATE_VALUE; } else if (hdl->buffer[i] == '\n' || hdl->buffer[i] == '\r') { printf("Missing '=' at line %d\n", current_line); current_state = STATE_SKIP; } #if 0 else if ( IS_ALPHA_NUMERIC(hdl->buffer[i]) || IS_POINT(hdl->buffer[i]) || hdl->buffer[i] == '[' || hdl->buffer[i] == ']' ) { /* Do nothing */ } else { printf("Unkown char %c found at line %d\n", hdl->buffer[i], current_line); current_state = STATE_SKIP; } #endif break; case STATE_VALUE: if (hdl->buffer[i] == '\n' || hdl->buffer[i] == '\r') { unsigned int value_len = i - value_start; current_value = (char*)malloc(value_len); memcpy(current_value, &hdl->buffer[value_start+1], value_len); current_value[value_len-1] = 0x00; /* printf("Value: %s\n", current_value); */ current_state = STATE_EMPTY; ini_add_parameter(hdl, current_section, current_key_name, current_value); current_key_name = 0; current_value = 0; } #if 0 else if ( IS_ALPHA_NUMERIC(hdl->buffer[i]) || IS_POINT(hdl->buffer[i]) || hdl->buffer[i] == '[' || hdl->buffer[i] == ']' ) { /* Do nothing */ } else { printf("Unkown char %c found at line %d\n", hdl->buffer[i], current_line); current_state = STATE_SKIP; } #endif break; case STATE_SKIP: if (hdl->buffer[i] == '\n' || hdl->buffer[i] == '\r') { current_state = STATE_EMPTY; } break; } if (hdl->buffer[i] == '\n') { ++current_line; } } /* Free used memory */ if (current_section_name != 0) { free(current_section_name); } if (current_value != 0) { free(current_value); } if (current_key_name != 0) { free(current_key_name); } return 0; }