本文整理汇总了C++中DArray_create函数的典型用法代码示例。如果您正苦于以下问题:C++ DArray_create函数的具体用法?C++ DArray_create怎么用?C++ DArray_create使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DArray_create函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: DArray_group_by
DArray DArray_group_by(DArray array, DArray_group_by_fn *group_by_fn)
{
if (DArray_is_empty(array) || !group_by_fn) return NULL;
DArray groups = DArray_create(sizeof(DArray), 10);
DArray group;
int idx;
void *el;
size_t count = (size_t) DArray_count(array);
for (size_t i = 0; i < count; i++) {
el = DArray_remove(array, i);
if (!el) continue;
idx = group_by_fn(el);
if (idx == -1) continue;
group = DArray_get(groups, idx);
if (!group) {
group = DArray_create(array->element_size, DArray_count(array) + 1);
DArray_ensure_capacity(groups, idx + 1);
DArray_set(groups, idx, group);
}
DArray_push(group, el);
}
return groups;
}
示例2: DArray_create
char *test_copy() {
DArray *orig = DArray_create(sizeof(int), 11);
DArray *dest = DArray_create(sizeof(int), 11);
int i = 0;
for(i = 0; i < 10; i++) {
int *el_to_add = DArray_new(orig);
*el_to_add = i;
mu_assert(DArray_push(orig, el_to_add) == 0, "Pushing to DArray failed.");
}
int rc = DArray_copy(orig, dest);
mu_assert(rc == 0, "Copy failed.")
mu_assert(orig->max == dest->max, "max did not copy properly.");
mu_assert(orig->end == dest->end, "end did not copy properly.");
mu_assert(orig->element_size == dest->element_size,
"element_size did not copy properly.");
mu_assert(orig->expand_rate == dest->expand_rate,
"expand_rate did not copy properly.");
for(i = 0; i < 10; i++) {
int *val = DArray_get(dest, i);
mu_assert(val != NULL, "Got NULL from copy.");
mu_assert(*val == i,
"Failed to copy contents correctly.");
}
return NULL;
}
示例3: _merge_sort
DArray* _merge_sort(DArray* array, DArray_compare cmp)
{
DArray_check(array);
if(array->end <= 1)
return array;
DArray* left = DArray_create(array->element_size, array->max);
check_mem(left);
DArray* right = DArray_create(array->element_size, array->max);
check_mem(right);
int middle = array->end/2;
int i = 0;
for(i = 0; i < array->end; i++) {
if(i < middle) {
DArray_push(left, DArray_get(array, i));
} else {
DArray_push(right, DArray_get(array, i));
}
}
DArray* sort_left = _merge_sort(left, cmp);
check(left != NULL, "Error in left merge sort");
DArray* sort_right = _merge_sort(right, cmp);
check(right != NULL, "Error in right merge sort");
if(sort_left != left) DArray_destroy(left);
if(sort_right != right) DArray_destroy(right);
return _merge_array(sort_left, sort_right, cmp);
error:
return NULL;
}
示例4: expose_VM
static inline void
expose_VM(STATE, VALUE lobby)
{
VALUE vm = Value_new(state, ObjectType);
Value_set(state, lobby, "VM", vm);
// VM.primitives map
DArray *prims = DArray_create(sizeof(VALUE), 10);
VALUE primitives = Map_new(state, prims);
Value_set(state, vm, "primitives", primitives);
// Object
DEFPRIM("to_s", Primitive_to_s);
DEFPRIM("prototype", Primitive_prototype);
DEFPRIM("or", Primitive_or);
DEFPRIM("equals", Primitive_equals);
DEFPRIM("is", Primitive_is);
DEFPRIM("print", Primitive_print);
DEFPRIM("puts", Primitive_puts);
DEFPRIM("require", Primitive_require);
DEFPRIM("clone", Primitive_clone);
// Vector
DEFPRIM("vector_[]", Primitive_Vector_at);
DEFPRIM("vector_push", Primitive_Vector_push);
DEFPRIM("vector_to_map", Primitive_Vector_to_map);
DEFPRIM("vector_each", Primitive_Vector_each);
DEFPRIM("vector_each_with_index", Primitive_Vector_each_with_index);
// Number
DEFPRIM("number_+", Primitive_Number_add);
DEFPRIM("number_-", Primitive_Number_sub);
DEFPRIM("number_*", Primitive_Number_mul);
DEFPRIM("number_/", Primitive_Number_div);
DEFPRIM("number_<", Primitive_Number_lt);
DEFPRIM("number_>", Primitive_Number_gt);
// String
DEFPRIM("string_+", Primitive_String_concat);
// Map
DEFPRIM("map_each", Primitive_Map_each);
// VM.types map
DArray *ts = DArray_create(sizeof(VALUE), 10);
VALUE types = Map_new(state, ts);
Value_set(state, vm, "types", types);
DEFVALUE("object", Object_bp);
DEFVALUE("number", Number_bp);
DEFVALUE("string", String_bp);
DEFVALUE("vector", Vector_bp);
DEFVALUE("map", Map_bp);
DEFVALUE("closure", Closure_bp);
}
示例5: kernel_files
static inline DArray*
kernel_files(STATE)
{
DArray *entries = DArray_create(sizeof(bstring), 10);
bstring kernel_relative_path = bfromcstr("../kernel");
bstring absolute_path = resolve_path(state, kernel_relative_path);
struct dirent **namelist;
int n = scandir(bdata(absolute_path), &namelist, 0, alphasort);
if (n < 0) {
perror("scandir");
} else {
for(int i = 0; i < n; i++) {
if(strcmp(namelist[i]->d_name, ".") != 0 && strcmp(namelist[i]->d_name, "..") != 0) {
DArray_push(entries, bfromcstr(namelist[i]->d_name));
}
free(namelist[i]);
}
free(namelist);
}
bdestroy(absolute_path);
bdestroy(kernel_relative_path);
return entries;
}
示例6: malloc
Parallax *Parallax_create() {
Parallax *parallax = malloc(sizeof(Parallax));
check(parallax != NULL, "Could not create parallax");
parallax->layers = DArray_create(sizeof(ParallaxLayer), 8);
parallax->camera = NULL;
GfxSize level = {1, 1};
parallax->level_size = level;
parallax->sky_color.rgba.r = 0.5;
parallax->sky_color.rgba.g = 0.5;
parallax->sky_color.rgba.b = 1.0;
parallax->sky_color.rgba.a = 1.0;
parallax->sea_color.rgba.r = 0.0;
parallax->sea_color.rgba.g = 0.5;
parallax->sea_color.rgba.b = 0.0;
parallax->sea_color.rgba.a = 1.0;
parallax->y_wiggle = 0.0;
parallax->sea_level = 1.0;
return parallax;
error:
if (parallax) free(parallax);
return NULL;
}
示例7: calloc
static inline TSTree *TSTree_insert_base(TSTree *root, TSTree *node,
const char *key, size_t len, void *value)
{
if(node == NULL) {
node = (TSTree *) calloc(1, sizeof(TSTree));
check(node != NULL, "Failed creating node");
node->values = DArray_create(DEFAULT_DARRAY_SIZE, DEFAULT_DARRAY_SIZE);
if(root == NULL) root = node;
node->splitchar = *key;
}
if(*key < node->splitchar) {
node->low = TSTree_insert_base(root, node->low, key, len, value);
} else if(*key == node->splitchar) {
if(len > 1) {
node->equal = TSTree_insert_base(root, node->equal, key + 1, len - 1, value);
} else {
DArray_push(node->values, value);
return node;
// assert(node->value == NULL && "Duplicate insert into tst.");
}
} else {
node->high = TSTree_insert_base(root, node->high, key, len, value);
}
return node;
error:
return NULL;
}
示例8: DArray_create
char *test_iterationBackward_arrayContainingContinousElements()
{
DArray *array = DArray_create(sizeof(intptr_t), 3);
DArray_push(array, (void *) (intptr_t) 60);
DArray_push(array, (void *) (intptr_t) 670);
DArray_push(array, (void *) (intptr_t) 90);
int index = -1;
assert(DArray_iterator_prev(array, &index) == 1 && "Expected to continue to prev element.");
assert(index == 2 && "Expected index was not set by iteration.");
assert(DArray_iterator_prev(array, &index) == 1 && "Expected to continue to prev element.");
assert(index == 1 && "Expected index was not set by iteration.");
assert(DArray_iterator_prev(array, &index) == 1 && "Expected to continue to prev element.");
assert(index == 0 && "Expected index was not set by iteration.");
assert(DArray_iterator_prev(array, &index) == 0 && "Expected to end interation.");
DArray_destroy(array);
return NULL;
}
示例9: DArray_create
char *test_distribution()
{
int i = 0;
int stats[3][BUCKETS] = { {0} };
DArray *keys = DArray_create(0, NUM_KEYS);
mu_assert(gen_keys(keys, NUM_KEYS) == 0,
"Failed to generate random keys.");
fill_distribution(stats[ALGO_FNV1A], keys, Hashmap_fnv1a_hash);
fill_distribution(stats[ALGO_ADLER32], keys, Hashmap_adler32_hash);
fill_distribution(stats[ALGO_DJB], keys, Hashmap_djb_hash);
fprintf(stderr, "FNV\tA32\tDJB\n");
for (i = 0; i < BUCKETS; i++) {
fprintf(stderr, "%d\t%d\t%d\n",
stats[ALGO_FNV1A][i],
stats[ALGO_ADLER32][i], stats[ALGO_DJB][i]);
}
destroy_keys(keys);
return NULL;
}
示例10: CloseNodes_GetNodes
DArray *
CloseNodes_GetNodes(CloseNodes *close)
{
assert(close != NULL && "NULL CloseNodes pointer");
DArray *nodes = DArray_create(sizeof(Node *),
DArray_max(close->close_nodes));
check(nodes != NULL, "DArray_create failed");
int i = 0;
for (i = 0; i < DArray_count(close->close_nodes); i++)
{
struct CloseNode *close_node = DArray_get(close->close_nodes, i);
check(close_node != NULL, "NULL CloseNodes entry");
check(close_node->node != NULL, "NULL Node pointer in CloseNodes entry");
int rc = DArray_push(nodes, close_node->node);
check(rc == 0, "DArray_push failed");
}
return nodes;
error:
DArray_destroy(nodes);
return NULL;
}
示例11: check
static inline DArray *Hashmap_find_bucket(Hashmap * map, void *key,
int create,
uint32_t * hash_out)
{
uint32_t hash = map->hash(key);
int bucket_n = hash % DEFAULT_NUMBER_OF_BUCKETS;
check(bucket_n >= 0, "Invalid bucket found: %d", bucket_n);
// store it for the return so the caller can use it
*hash_out = hash;
DArray *bucket = DArray_get(map->buckets, bucket_n);
if (!bucket && create) {
// new bucket, set it up
bucket = DArray_create(
sizeof(void *), DEFAULT_NUMBER_OF_BUCKETS);
check_mem(bucket);
DArray_set(map->buckets, bucket_n, bucket);
}
return bucket;
error:
return NULL;
}
示例12: DArray_create
DArray *TSTree_collect(TSTree *root, const char *prefix, size_t len)
{
DArray *matches = DArray_create(sizeof(char *), 100);
TSTree *node = root;
size_t i = 0;
while (i < len && node) {
if (prefix[i] < node->splitchar) {
node = node->low;
} else if (prefix[i] > node->splitchar) {
node = node->high;
} else {
assert(prefix[i] == node->splitchar && "Prefix should == splitchar");
i++;
if (i < len) {
node = node->equal;
}
}
}
// node is the node that matches the prefix; all nodes in the tree will match
char *partial_match = malloc(sizeof(char) * (len - 1));
memcpy(partial_match, prefix, (len - 1));
_TSTree_collect_sub_nodes(node, matches, partial_match, len);
return matches;
}
示例13: String_to_object
Object*
String_to_object(bstring string)
{
Object *obj = NULL;
if (bchar(string, 0) == '"') {
int len = blength(string) - 2;
obj = Object_create_string(bmidstr(string, 1, len));
} else if (bchar(string, 0) == '[') {
int strlen = blength(string) - 2;
bstring inner_str = bmidstr(string, 1, strlen);
struct bstrList *elements = bsplit(inner_str, ',');
int len = elements->qty;
int i = 0;
DArray *array = DArray_create(sizeof(Object*), len);
bstring *ptr = elements->entry;
for(i = 0; i < len; i++) {
btrimws(*ptr);
DArray_push(array, String_to_object(*ptr));
ptr++;
}
obj = Object_create_array(array);
} else {
int value = atoi(bdata(string));
if (value != 0) {
obj = Object_create_integer(atoi(bdata(string)));
} else {
return NULL;
}
}
return obj;
}
示例14: DArray_mergesort
int DArray_mergesort(DArray *array, DArray_compare cmp)
{
int count = array->end;
if (count > 1) {
int pivot = count / 2;
int after_pivot = count - pivot;
DArray *left = DArray_create(array->element_size, pivot);
DArray *right = DArray_create(array->element_size, after_pivot);
for (int i = 0; i < pivot; i++) {
DArray_push(left, DArray_get(array, i));
}
for (int i = pivot; i < count; i++) {
DArray_push(right, DArray_get(array, i));
}
DArray_mergesort(left, cmp);
DArray_mergesort(left, cmp);
int i = 0;
int l_ptr = 0;
int r_ptr = 0;
for (i = 0; i < count; i++) {
if (l_ptr >= pivot) {
DArray_set(array, i, DArray_get(right, r_ptr));
r_ptr++;
} else if (r_ptr >= after_pivot) {
DArray_set(array, i, DArray_get(left, l_ptr));
l_ptr++;
} else if (cmp(DArray_get(left, l_ptr), DArray_get(right, r_ptr)) < 0) {
DArray_set(array, i, DArray_get(left, l_ptr));
l_ptr++;
} else {
DArray_set(array, i, DArray_get(right, r_ptr));
r_ptr++;
}
}
DArray_destroy(left);
DArray_destroy(right);
}
return 0;
}
示例15: DArray_create
char *test_create() {
array = DArray_create(sizeof(int), 100);
mu_assert(array != NULL, "DArray_create failed");
mu_assert(array->contents != NULL, "contents wrong in darray");
mu_assert(array->end == 0, "end isn't at the right spot");
mu_assert(array->element_size == sizeof(int), "element size is wrong");
mu_assert(array->max == 100, "wrong max length on initial size");
return NULL;
}