コード例 #1
0
Bool Vector_printf(Vector *v, Allocator allocator, char const *format, ...)
{
	size_t const speculative_result_length = strlen(format) * 2;
	va_list args;
	ptrdiff_t printed;

	if (!Vector_resize(v, speculative_result_length, allocator))
	{
		return False;
	}

	va_start(args, format);
	printed = vsnprintf(Vector_data(v), Vector_size(v), format, args);
	assert(printed >= 0);
	if ((size_t)printed == Vector_size(v))
	{
		if (!Vector_resize(v, (size_t)printed + 1, allocator))
		{
			va_end(args);
			return False;
		}
		printed = vsnprintf(Vector_data(v), Vector_size(v), format, args);
		assert((size_t)printed == Vector_size(v));
	}
	Vector_resize_smaller(v, (size_t)printed + 1);
	va_end(args);
	return True;
}
コード例 #2
0
Bool Vector_append_binary_file(Vector *v, Allocator v_allocator, FILE *in)
{
	size_t const original_size = Vector_size(v);
	size_t buffer_size = 8192;
	for (;;)
	{
		size_t const offset_to_read_to = Vector_size(v);
		size_t const max_size_t = ~(size_t)0;
		size_t resizing = original_size + buffer_size;
		size_t read_size;
		size_t actually_read;
		if (resizing > max_size_t)
		{
			resizing = max_size_t;
		}
		if (!Vector_resize(v, resizing, v_allocator))
		{
			Vector_resize_smaller(v, original_size);
			return False;
		}
		read_size = resizing - offset_to_read_to;
		actually_read = fread(Vector_data(v) + offset_to_read_to, 1, read_size, in);
		if (actually_read < read_size)
		{
			Vector_resize_smaller(v, offset_to_read_to + actually_read);
			return True;
		}
	}
}
コード例 #3
0
static void test_vector(void)
{
	MemoryManager memory = create_standard_memory_manager();

	{
		Vector v;
		Vector_init(&v);
		TEST(Vector_empty(&v));
		TEST(Vector_size(&v) == 0);
		Vector_free(&v, memory.deallocator);
	}

	{
		Vector v;
		Vector_init(&v);
		TEST(Vector_reserve(&v, 100, memory.allocator));
		TEST(Vector_reserve(&v, 200, memory.allocator));
		TEST(Vector_reserve(&v, 50, memory.allocator));
		TEST(Vector_reserve(&v, 0, memory.allocator));
		Vector_free(&v, memory.deallocator);
	}

	{
		Vector v;
		Vector_init(&v);
		TEST(Vector_resize(&v, 100, memory.allocator));
		TEST(Vector_resize(&v, 200, memory.allocator));
		TEST(Vector_resize(&v, 50, memory.allocator));
		TEST(Vector_resize(&v, 0, memory.allocator));
		Vector_free(&v, memory.deallocator);
	}

	{
		size_t i;
		size_t const count = 1000;
		Vector v;
		Vector_init(&v);
		for (i = 0; i < count; ++i)
		{
			char c = (char)('0' + (i % 10));
			ASSERT(Vector_push_back(&v, &c, 1, memory.allocator));
		}
		for (i = 0; i < count; ++i)
		{
			char c = (char)('0' + (i % 10));
			ASSERT(Vector_data(&v)[i] == c);
		}
		Vector_free(&v, memory.deallocator);
	}
}
コード例 #4
0
ファイル: day12.c プロジェクト: Jassob/advent_of_code_2016
void run(Vector v, int *r){
  int size = Vector_size(v);
  struct Inst *instrs = Vector_data(v);
  for(int i = 0;i < size; ++i){
    struct Inst *inst = &instrs[i];
    switch(inst->op_code){
      case cpy:
        r[inst->y.x] = to_value(r, inst->x);
        break;
      case inc:
        ++r[inst->x.x];
        break;
      case dec:
        --r[inst->x.x];
        break;
      case jnz:
        if (to_value(r, inst->x)) i += to_value(r, inst->y)-1;
        break;
      default:
        printf("Error bad instruction\n");
    }
  }
}