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;
}
Example #2
0
void *Hashmap_delete(Hashmap *map, void *key)
{
    uint32_t hash = 0;
    DArray *bucket = Hashmap_find_bucket(map, key, 0, &hash);
    if(!bucket) {
        return NULL;
    }

    int i = Hashmap_get_node(map, hash, bucket, key);
    if(i == -1) {
        return NULL;
    }

    HashmapNode *node = DArray_get(bucket, i);
    void *data = node->data;
    free(node);

    HashmapNode *ending = DArray_pop(bucket);

    if(ending != node) {
        // alright looks like it's not the last one, swap it.
        DArray_set(bucket, i, ending);
    }

    return data;
}
Example #3
0
void 
CloseNodes_Destroy(CloseNodes *close)
{
    if (close == NULL)
        return;

    while (DArray_count(close->close_nodes) > 0)
    {
        free(DArray_pop(close->close_nodes));
    }

    DArray_destroy(close->close_nodes);
    free(close);
}
Example #4
0
static inline void TSTree_empty_content(DArray *darray)
{

  check(darray != NULL, "DArray can't be NULL.");

   int i = 0;
    if(!DArray_empty(darray)) {
        for(i = 0; i <= DArray_count(darray); i++) {
            DArray_pop(darray);
        }
    }

error:
  return;
}
Example #5
0
char *test_push_pop() {
    int i = 0;
    for(i=0; i<1000; i++) {
        int *val = DArray_new(array);
        *val = i*333;
        DArray_push(array, val);
    }

    mu_assert(array->max == 1201, "Wrong max size");

    for(i=999; i>=0; i--) {
        int *val = DArray_pop(array);
        mu_assert(val != NULL, "Should not be NULL");
        mu_assert(*val == i*333, "Wrong value");
        DArray_free(val);
    }

    return NULL;
}
Example #6
0
void* Hashmap_delete(Hashmap* map, void* key) {
    uint32_t hash = 9;
    DArray* bucket = Hashmap_find_bucket(map, key, 0, &hash);
    if(!bucket) return NULL;

    int i = Hashmap_get_node(map, hash, bucket, key);
    if(i == -1) return NULL;

    HashmapNode* node = DArray_get(bucket, i);
    void* data = node->data;
    free(node);

    HashmapNode* ending = DArray_pop(bucket);

    if(ending != node) {
        DArray_set(bucket, i, ending);
    }

    return data;
}
Example #7
0
char * test_delete()
{
    DArray * array = DB_get_name("select db");
    Snippet * snippet = NULL;
    snippet = DArray_pop(array);
    mu_assert(snippet != NULL, "DB empty.");
    int old_id = snippet->id;

    int rc = DB_delete(snippet);
    snippet_destroy(snippet);
    DArray_clear_destroy(array,snippet_destroy);
    mu_assert(rc == 0, "Failed to delete.");
    log_info("snippet * = %p",snippet);
    snippet = NULL;
    snippet = DB_get(old_id);
    mu_assert(snippet == NULL, "This snippet Shouldn't exist.");
    log_info("about to quite test_delete");
    if(snippet != NULL)
        snippet_destroy(snippet);
        
    return NULL;
}
char *test_push_pop()
{
	int i = 0;
	for (i = 0; i < 1000; i++) {
		int *val = DArray_new(array);
		*val = i * 333;
		DArray_push(array, val);
		//int valuepushed = *( (int*)DArray_get(array, i));
		//printf("%d", valuepushed);
		//debug("%d: At address %x, we have %d",i, DArray_get(array, i), 
	//			*((int*)DArray_get(array, i)) );
	}

	mu_assert(array->max == 1201, "Wrong max size.");

	for (i = 999; i >= 0; i--) {
		int *val = DArray_pop(array);
		mu_assert(val != NULL, "Shouldn't get a NULL");
		mu_assert(*val == i * 333, "Wrong Value.");
		DArray_free(val);
	}
	return NULL;

}
Example #9
0
File: object.c Project: txus/shitdb
void
Object_destroy(Object *object)
{
  switch(object->type) {
    case tString:
      bdestroy(object->value.as_string);
      break;
    case tArray: {
      DArray *ary = object->value.as_array;
      Object *obj = NULL;
      while((obj = (Object*)DArray_pop(ary)) != NULL) {
        Object_destroy(obj);
      }
      break;
    }
    case tHash: {
      Hashmap_destroy(object->value.as_hash);
    }
    default:
      break;
  }

  free(object);
}
Example #10
0
/* My own implemenatation of quicksort */
int DArray_quicksort(DArray *array, DArray_compare cmp) {
	debug( ">>> DArray_quicksort\n");
	check(array != NULL, "array is NULL\n");
	debug( "array: %p\n", array);

	// declare here for error handling
	DArray *less;
	DArray *more;

	if(DArray_count(array) <= 1) {
	// base-case: array is empty or singleton
		debug( "array less/equal 1\n");
		return 0;

	} else {
		// recursive-case: array is longer than 1
		debug( "array greater than 1\n");
		
		// pick a pivot
		void *pivot = DArray_pop(array);
		check(pivot != NULL, "pivot is NULL");
		debug("length of array: %d", DArray_count(array));	

		// make a list of larger and smaller elements
		less = DArray_create(sizeof(void *), _quicksort_INITIAL_MAX);
		more = DArray_create(sizeof(void *), _quicksort_INITIAL_MAX);
		debug("create less and more");
		void *cur;
		int i;

		/*
		for(i = 0, cur = DArray_first(array); 
			i < DArray_count(array);
			i++, cur = DArray_get(array, i)) {
		*/

		for(cur = DArray_pop(array); cur != NULL; cur = DArray_pop(array)) {

			check(cur != NULL, "cur is NULL");
			debug("length of array: %d; i: %d", DArray_count(array), i);

			debug("comparing cur to pivot");
			debug("cur: %s", (char *)cur);
			debug("pivot: %s", (char *)pivot);
			
			debug("address of comparator: %p", cmp);
			debug("cmp(&pivot, &cur): %d", cmp(&pivot, &cur));

			if(cmp(&pivot, &cur) > 0){
				debug("cur is less than the pivot");
				DArray_push(less, cur);
			}else{
				debug("cur is more than the pivot");
				DArray_push(more, cur);
			}
		}

		check(DArray_count(array) == 0, "array not empty");

		// get lengths before
		int len_less_before = DArray_count(less);
		int len_more_before = DArray_count(more);

		// sort the sublists
		int rc1 = DArray_quicksort(less, cmp);
		int rc2 = DArray_quicksort(more, cmp);
		check(rc1 == 0, "failed to sort less sublist");
		check(rc2 == 0, "failed to sort more sublist");

		// check lengths
		check(len_less_before == DArray_count(less), "length of less changes");
		debug("old len(more): %d; new len(more): %d", len_more_before, DArray_count(more));
		check(len_more_before == DArray_count(more), "length of more changes");
		
		// at this point less and more are already sorted

		// write all elements back into the old array
		for( i = 0, cur = DArray_first(less); 
			i < DArray_count(less); 
			i++, cur = DArray_get(less, i)) {

			debug("pushing from less, length of less: %d; i: %d", DArray_count(less), i);
			DArray_push(array, cur);
		}

		DArray_push(array, pivot);
			debug("pushing from pivot");

		for(i = 0, cur = DArray_first(more); 
			i < DArray_count(more); 
			i++, cur = DArray_get(more, i)) {

			debug("pushing from more");
			DArray_push(array, cur);
		}

		// check size
		int s_array = DArray_count(array);
		int s_less = DArray_count(less);
		int s_more = DArray_count(more);
		check(s_array = (s_less + s_more + 1), "number of elements changes");

		// destroy the temporary arrays
		DArray_destroy(less);
		DArray_destroy(more);

		// give feedback
		#ifndef NDEBUG
		int k;
		for(k=0; k<DArray_count(array); k++) {
			debug("element %d: %s", k, DArray_get(array, k));
		}
		#endif

		return 0;
	}

error:
// free all allocated memory
if(array) DArray_destroy(array);
if(less) DArray_destroy(less);
if(more) DArray_destroy(more);

return 1;
}