Пример #1
0
int main(void) {
    DynArr dynarr;
    int i;

    dynarr_create(&dynarr);

    printf("Enter numbers! Stop input by entering not a number.\n");
    for (;;) {
        int num;
        printf("Enter number: ");
        if (scanf("%d", &num) == 1) {
            if (dynarr_append(&dynarr, num) == 0) {
                printf("\n\nError!\n");
                return 1;
            }
        } else {
            break;
        }
    }

    printf("You have entered:");
    for (i = 0; i < dynarr_length(&dynarr); ++i) {
        printf(" %d", dynarr_get(&dynarr, i));
    }
    printf("\n");

    dynarr_destroy(&dynarr);

    return 0;
}
Пример #2
0
NdbCCurve * nodedb_c_curve_create(NodeCurve *node, VLayerID curve_id, const char *name, uint8 dimensions)
{
	NdbCCurve	*curve;

	if(node == NULL || name == NULL || dimensions > 4)
		return NULL;
	if(node->curves == NULL)
	{
		node->curves = dynarr_new(sizeof (NdbCCurve), 4);
		dynarr_set_default_func(node->curves, cb_curve_default, NULL);
	}
	if(curve_id == (VLayerID) ~0)
		curve = dynarr_append(node->curves, NULL, NULL);
	else
		curve = dynarr_set(node->curves, curve_id, NULL);
	if(curve != NULL)
	{
		curve->id = curve_id;
		stu_strncpy(curve->name, sizeof curve->name, name);
		curve->dimensions = dimensions;
		curve->keys = NULL;
		printf("Curve curve %u.%u %s created, dim=%u\n", node->node.id, curve_id, name, curve->dimensions);
	}
	return curve;
}
Пример #3
0
/* ------------------ */
int dynarr_insert(struct dynarr *array, unsigned int pos, dynarr_info_t info)
{
    if (pos > array->size)
        return 0;
    else if (pos == array->size)
        return dynarr_append(array, info);

    int alloc = dynarr_resize_up(array, ++array->size);

    int move_size = array->size - pos - 1;    /* Old size, so -1 */
    memmove(array->arr + pos + 1, array->arr + pos, move_size * sizeof(dynarr_info_t));
    array->arr[pos] = info;

    return alloc;
}
Пример #4
0
NdbCKey * nodedb_c_key_create(NdbCCurve *curve, uint32 key_id,
				     real64 pos, const real64 *value,
				     const uint32 *pre_pos, const real64 *pre_value,
				     const uint32 *post_pos, const real64 *post_value)
{
	NdbCKey	*key;
	int	insert = 0, sort = 0, i;

	if(curve->keys == NULL)
	{
		curve->keys = dynarr_new(sizeof *key, 8);
		dynarr_set_default_func(curve->keys, cb_key_default, NULL);
	}
	if(curve->keys == NULL)
		return NULL;
	if(key_id == ~0u)
	{
		/* If creating a new key, make sure it's not clobbering an existing position. Search. */
		List	*old;

		if((old = list_find_sorted(curve->curve, &pos, cb_key_compare_find)) != NULL)
			key = list_data(old);	/* Just re-use the same slot, since we're not changing position. */
		else
		{
			key = dynarr_append(curve->keys, NULL, NULL);
			insert = 1;
		}
	}
	else
	{
		key = dynarr_set(curve->keys, key_id, NULL);
		sort = 1;	/* We might re-sort. Turned off if new pos equals old, below. */
	}
	if(key == NULL)
		return NULL;
	key->id = key_id;
	if(sort)
		sort = key->pos != pos;	/* Only resort if new position differs from the one we're reusing. */
	key->pos = pos;
	for(i = 0; i < curve->dimensions; i++)
	{
		key->value[i]      = value[i];
		key->pre.pos[i]    = pre_pos[i];
		key->pre.value[i]  = pre_value[i];
		key->post.pos[i]   = post_pos[i];
		key->post.value[i] = post_value[i];
	}
	if(insert)
	{
		printf("inserting key in list\n");
		curve->curve = list_insert_sorted(curve->curve, key, cb_key_compare);
	}
	else if(sort)
	{
		List	*nl = NULL, *iter;

		printf("resorting list\n");
		/* This is considered a rare event, so we can be a bit expensive. */
		nl = list_insert_sorted(nl, key, cb_key_compare);	/* Get the new one in there. */
		for(iter = curve->curve; iter != NULL; iter = list_next(iter))
			nl = list_insert_sorted(nl, list_data(iter), cb_key_compare);
		list_destroy(curve->curve);
		curve->curve = nl;
		printf("key list resorted\n");
	}
	return key;
}