char *test_iterationForward_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_next(array, &index) == 1 && "Expected to continue to next element.");
    assert(index == 0 && "Expected index was not set by iteration.");

    assert(DArray_iterator_next(array, &index) == 1 && "Expected to continue to next element.");
    assert(index == 1 && "Expected index was not set by iteration.");

    assert(DArray_iterator_next(array, &index) == 1 && "Expected to continue to next element.");
    assert(index == 2 && "Expected index was not set by iteration.");

    assert(DArray_iterator_next(array, &index) == 0 && "Expected to end interation.");

    /* while (DArray_iterator_next(array, &index)) { */
    /*     intptr_t element = (intptr_t) DArray_get(array, index); */

    /*     collected_elements[index] = element; */
    /* } */

    /* assert(collected_elements[0] == 60 && "Expected value at index not found."); */
    /* assert(collected_elements[1] == 670 && "Expected value at index not found."); */
    /* assert(collected_elements[2] == 90 && "Expected value at index not found."); */

    DArray_destroy(array);

    return NULL;
}
 char *test_push_pop()
 {
     DArray *array = DArray_create(sizeof(intptr_t), 5);

     DArray_push(array, (void *) (intptr_t) 30);
     DArray_push(array, (void *) (intptr_t) 20);
     DArray_push(array, (void *) (intptr_t) 10);

     intptr_t value = (intptr_t) DArray_pop(array);
     assert(value == 10 && "pop'ed wrong value from array.");

     value = (intptr_t) DArray_pop(array);
     assert(value == 20 && "pop'ed wrong value from array.");

     value = (intptr_t) DArray_pop(array);
     assert(value == 30 && "pop'ed wrong value from array.");

     DArray_push(array, (void *) (intptr_t) 60);
     value = (intptr_t) DArray_get(array, 0);
     assert(value == 60 && "Getting value by index after pop'ing all values from array failed.");

     DArray_destroy(array);

     return NULL;
}
Ejemplo n.º 3
0
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;
}
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;
}
Ejemplo n.º 5
0
TileMapParseStatus TileMap_parse_map(xmlTextReaderPtr reader, Engine *engine,
                                     TileMap *map) {
  TileMapParseStatus status = TILEMAP_PARSE_OK;
  
  xmlChar *name = xmlTextReaderName(reader);
  if (!(streq(name, "map") &&
        xmlTextReaderNodeType(reader) == XML_ELEMENT_NODE)) {
    return TILEMAP_PARSE_INVALID_FORMAT;
  }
  
  while (xmlTextReaderMoveToNextAttribute(reader)) {
    xmlChar *attrName = xmlTextReaderName(reader);
    xmlChar *attrVal = xmlTextReaderValue(reader);
    
    if (streq(attrName, "orientation")) {
      if (!streq(attrVal, "orthogonal")) {
        return TILEMAP_PARSE_INVALID_ORIENTATION;
      }
    } else if (streq(attrName, "width")) {
      map->cols = atoi((const char *)attrVal);
    } else if (streq(attrName, "height")) {
      map->rows = atoi((const char *)attrVal);
    } else if (streq(attrName, "tilewidth")) {
      map->tile_size.w = atoi((const char *)attrVal);
    } else if (streq(attrName, "tileheight")) {
      map->tile_size.h = atoi((const char *)attrVal);
    }
  }
  
  while (xmlTextReaderRead(reader)) {
    xmlChar *childName = xmlTextReaderName(reader);
    if (xmlTextReaderNodeType(reader) == XML_ELEMENT_DECL &&
        streq(childName, "map")) {
      break;
    } else if (streq(childName, "tileset")) {
      Tileset *tileset = NULL;
      status = TileMap_parse_tileset(reader, engine, map, &tileset);
      if (status != TILEMAP_PARSE_OK) return status;
      DArray_push(map->tilesets, tileset);
    } else if (streq(childName, "layer")) {
      TileMapLayer *layer = NULL;
      status = TileMap_parse_layer(reader, map, &layer);
      if (status != TILEMAP_PARSE_OK) return status;
      DArray_push(map->layers, layer);
    }
  }
  
  return status;
}
Ejemplo n.º 6
0
int gen_keys(DArray *keys, int num_keys) {
	int i = 0;
	FILE *urand = fopen("/dev/urandom", "r");
	check(urand != NULL, "Failed to open /dev/urandom");

	struct bStream *stream = bsopen((bNread)fread, urand);
	check(stream != NULL, "Failed to open /dev/urandom");

	bstring key = bfromcstr("");
	int rc = 0;

	// FNVla histogram
	for(i = 0; i < num_keys; i++) {
		rc = bsread(key, stream, BUFFER_LEN);
		check(rc >= 0, "Failed to read from /dev/urandom.");

		DArray_push(keys, bstrcpy(key));
	}

	bsclose(stream);
	fclose(urand);
	return 0;

error:
	return -1;
}
Ejemplo n.º 7
0
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;
}
Ejemplo n.º 8
0
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;
}
Ejemplo n.º 9
0
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;
}
Ejemplo n.º 10
0
int gen_keys(DArray * keys, int num_keys)
{
    int i = 0;
    FILE *urand = fopen("/dev/urandom", "r");
    check(urand != NULL, "Failed to open /dev/urandom");
    int result = -1; // default to failure condition
    int rc = 0;

    struct bStream *stream = bsopen((bNread) fread, urand);
    check(stream != NULL, "Failed to open /dev/urandom");

    bstring key = bfromcstr("");

    // FNV1a histogram
    for (i = 0; i < num_keys; i++) {
        rc = bsread(key, stream, BUFFER_LEN);
        check(rc >= 0, "Failed to read from /dev/urandom.");

        DArray_push(keys, bstrcpy(key));
    }

    result = 0; // all good

error: // fallthrough
    if(stream) bsclose(stream);
    if(urand) fclose(urand);
    if(key) bdestroy(key);
    return result;
}
Ejemplo n.º 11
0
Archivo: object.c Proyecto: txus/shitdb
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;
}
char *test_contains()
{
    DArray *array = DArray_create(sizeof(intptr_t), 3);

    DArray_push(array, (void *) (intptr_t) 60);
    DArray_push(array, (void *) (intptr_t) 90);

    assert(DArray_contains(array, (void *) (intptr_t) 90, _test_predicate) == 1 && "Expected to find value in array.");
    assert(DArray_contains(array, (void *) (intptr_t) 60, _test_predicate) == 1 && "Expected to find value in array.");
    assert(DArray_contains(array, (void *) (intptr_t) 100, _test_predicate) == 0 && "Expected to not find value in array.");
    assert(DArray_contains(array, NULL, _test_predicate) == 0 && "Expected to not find value in array.");

    DArray_destroy(array);

    return NULL;
}
Ejemplo n.º 13
0
static void _TSTree_collect_sub_nodes(TSTree *node, DArray *matches, char *cur_match, size_t len)
{
  if (!node) {
    return;
  }

  len += 1;
  char *match = malloc(sizeof(char) * len);
  sprintf(match, "%s%c", cur_match, node->splitchar);

  if (node->low) {
    _TSTree_collect_sub_nodes(node->low, matches, match, len);
  }

  if (node->equal) {
    _TSTree_collect_sub_nodes(node->equal, matches, match, len);
  }

  if (node->high) {
    _TSTree_collect_sub_nodes(node->high, matches, match, len);
  }

  if (node->value) {
    DArray_push(matches, match);
  } else {
    free(match);
  }
}
Ejemplo n.º 14
0
int 
CloseNodes_Add(CloseNodes *close, Node *node)
{
    assert(close != NULL && "NULL CloseNodes pointer");
    assert(node != NULL && "NULL Node pointer");

    Distance distance = Hash_Distance(&close->id, &node->id);
    int i = FindIndex(close->close_nodes, &distance);

    if (i < 0)
        return 0;

    struct CloseNode *close_node = malloc(sizeof(struct CloseNode));
    check_mem(close_node);

    close_node->node = node;
    close_node->distance = distance;

    if (DArray_count(close->close_nodes) < BUCKET_K)
        DArray_push(close->close_nodes, NULL);

    ShiftFrom(close->close_nodes, i);
    DArray_set(close->close_nodes, i, close_node);

    return 0;
error:
    return -1;
}
Ejemplo n.º 15
0
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;
}
Ejemplo n.º 16
0
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;
}
Ejemplo n.º 17
0
void addNSToList(NodeAddress *nsAddress) {
	int pos = checkNSExists(nsList, nsAddress->ip, nsAddress->portNumber);

	if (pos == -1) {
		DArray_push(nsList, nsAddress);
		nsCount++;
	}
}
Ejemplo n.º 18
0
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;
}
Ejemplo n.º 19
0
char *test_each()
{
  DArray *ary = DArray_create(sizeof(VALUE), 10);
  VALUE one   = Number_new(state, 1);
  VALUE two   = Number_new(state, 2);
  VALUE three = Number_new(state, 3);
  DArray_push(ary, one);
  DArray_push(ary, two);
  DArray_push(ary, three);

  VALUE vector = Vector_new(state, ary);

  __block int counter = 0;

  Vector_each(vector, ^ void (VALUE val) {
    counter = counter + VAL2NUM(val);
  });
Ejemplo n.º 20
0
int Parallax_add_layer(Parallax *parallax, ParallaxLayer *layer) {
    check(parallax != NULL, "No parallax to add to");
    check(layer != NULL, "No parallax layer to add");

    DArray_push(parallax->layers, layer);

    return 1;
error:
    return 0;
}
Ejemplo n.º 21
0
void add_feature(FeatureGroup group, char* value, Hashmap *featuremap, DArray *list) {
    struct FeatureKey ro;
    ro.grp = group;
    ro.value = value;

    FeatureValue fv;
    fv = (FeatureValue) Hashmap_get(featuremap, &ro);

    if (fv != NULL)
        DArray_push(list, &(fv->feature_id));
}
Ejemplo n.º 22
0
DArray *create_words()
{
    DArray *result = DArray_create(0, 5);
    char *words[] = {"asdfasfd", "werwar", "13234", "asdfasfd", "oioj"};

    for(int i = 0; i < 5; i++) {
        DArray_push(result, words[i]);
    }

    return result;
}
char *test_destroy_arrayContainingMallocedValues()
{
    DArray *array = DArray_create(sizeof(intptr_t), 100);

    char *test_str_1 = malloc(sizeof(char) * TEST_STR_1_LEN);
    strncpy(test_str_1, TEST_STR_1, TEST_STR_1_LEN);

    char *test_str_2 = malloc(sizeof(char) * TEST_STR_2_LEN);
    strncpy(test_str_2, TEST_STR_2, TEST_STR_2_LEN);

    DArray_push(array, test_str_1);
    DArray_push(array, test_str_2);

    DArray_destroy(array);

    free(test_str_1);
    free(test_str_2);

    return NULL;
}
Ejemplo n.º 24
0
DArray *create_words()
{
	DArray *result = DArray_create(0, 7);
	char *words[] = { "werwar", "asdasd", "13234", "oioj", "gthht", "lkluil", "xswcde" };
	int i = 0;

	for (i = 0; i < 7; i++) {
		DArray_push(result, words[i]);
	}

	return result;
}
Ejemplo n.º 25
0
DArray DArray_find(DArray array, DArray_find_fn *filter_fn, void* args)
{
  if (DArray_is_empty(array)) return NULL;

  DArray filtered = DArray_create(array->element_size, array->size);

  for (int i = 0; i < DArray_count(array); i++) {
    void *item = DArray_get(array, i);

    if (filter_fn(item, args))
      DArray_push(filtered, item);
  }

  return filtered;
}
Ejemplo n.º 26
0
int Hashmap_set(Hashmap* map, void* key, void* data) {
    uint32_t hash = 0;
    DArray* bucket = Hashmap_find_bucket(map, key, 1, &hash);
    check(bucket, "Can't create bucket.");

    HashmapNode* node = Hashmap_node_create(hash, key, data);
    check_mem(node);

    DArray_push(bucket, node);

    return 0;

error:
    return 01;
}
Ejemplo n.º 27
0
int Hashmap_set(Hashmap *map, void *key, void *data)//finds a bucket create node add node to bucket
{
    uint32_t hash = 0;
    DArray *bucket = Hashmap_find_bucket(map, key, 1, &hash);//int 1 tells fn to create bucket if none found
    check(bucket, "Error can't create bucket.");

    HashmapNode *node = Hashmap_node_create(hash, key, data);
    check_mem(node);

    DArray_push(bucket, node);

    return 0;

error:
    return -1;
}
Ejemplo n.º 28
0
static inline DArray *TSTree_all_values(TSTree *node, DArray *values)
{


  if(node == NULL) return NULL;

  if(values == NULL) values = DArray_create(DEFAULT_DARRAY_SIZE, DEFAULT_DARRAY_SIZE);

  if(!DArray_empty(node->values)) DArray_push(values, DArray_first(node->values));

  if(node->low) TSTree_all_values(node->low, values);
  if(node->equal) TSTree_all_values(node->equal, values);
  if(node->high) TSTree_all_values(node->high, values);

  return values;
}
Ejemplo n.º 29
0
Object*
Array_new(Object **contents, int count) {
  DArray *array = DArray_create(sizeof(Object*), count || 1);
  int i=0;
  for(i=0; i < count; i++) {
    retain((Object*)contents[i]);
    DArray_push(array, contents[i]);
  }

  Object *object      = Object_new();
  object->type        = tArray;
  object->value.other = array;

  Object_define_native_method(object, bfromcstr("[]"), Primitive_Array_at, 1);

  return object;
}
Ejemplo n.º 30
0
DArray* _merge_array(DArray* left, DArray* right, DArray_compare cmp)
{
    DArray* result = DArray_create(left->element_size,
            left->max > right->max ? left->max : right->max);
    DArray_check(left);
    DArray_check(right);
    DArray_check(result);
    void* element = NULL;
    while(left->end > 0 || right->end > 0) {
        int i = 0;
        DArray_debug(left);
        DArray_debug(right);
        debug("Left end: %d\tRight end: %d", left->end, right->end);
        if(left->end > 0 && right->end > 0) {
            check(left->contents[i] != NULL,
                        "left->contents[%i] is NULL!", i);
            check(right->contents[i] != NULL,
                        "right->contents[%i] is NULL!", i);
            if(cmp(&left->contents[i], &right->contents[i]) <= 0) {
                debug("Fire left!");
                element = DArray_fpop(left);
            } else {
                debug("Fire right!");
                element = DArray_fpop(right);
            }
        } else if(left->end > 0) {
            debug("Fire left?");
            element = DArray_fpop(left);
        } else if(right->end > 0) {
            debug("Fire right?");
            element = DArray_fpop(right);
        } else sentinel("_merge_array shouldn't get here.");
        check_mem(element);
        DArray_push(result, element);
        i++;
    }

    DArray_destroy(left);
    DArray_destroy(right);
    return result;
error:
    DArray_destroy(left);
    DArray_destroy(right);
    if(result) DArray_destroy(result);
    return NULL;
}