Beispiel #1
0
static void new_heapSize_twoCount(void **state){
    heap_t * newHeap = heap_new(228,30);
    assert_int_equal(heap_getSize(newHeap), 228);
    heap_t * newSecondHeap = heap_new(300,30);
    assert_int_equal(heap_getSize(newSecondHeap), 300);
    heap_delete(newHeap);
    heap_delete(newSecondHeap);
}
Beispiel #2
0
static void new_viewMemory_twoData(void **state){
    heap_t * newHeap = heap_new(100,30);
    memory_t * newMemory = heap_interactionMemory(newHeap, 50);
    memory_check(newMemory, "Hello World!");
    assert_int_equal(strcmp(memory_view(newMemory), "Hello World!"), 0);

    heap_t * newSecondHeap = heap_new(100,31);
    memory_t * newSecondMemory = heap_interactionMemory(newSecondHeap, 30);
    memory_check(newSecondMemory, "Hello C\C++!");
    assert_int_equal(strcmp(memory_view(newSecondMemory), "Hello C\C++!"), 0);
    heap_delete(newHeap);
    heap_delete(newSecondHeap);
}
Beispiel #3
0
static void new_viewMemory_invalidData(void **state){
    heap_t * newHeap = heap_new(100,30);
    memory_t * newMemory = heap_interactionMemory(newHeap, 20);
    memory_check(newMemory, "Hello World!");
    assert_int_equal(strcmp(memory_view(newMemory), "Zdrastvuyte"), -1);
    heap_delete(newHeap);
}
Beispiel #4
0
static void new_checkMemory_normal(void **state){
    heap_t * newHeap = heap_new(100,30);
    memory_t * newMemory = heap_interactionMemory(newHeap, 50);
    memory_check(newMemory, "I am normal string!");
    assert_int_equal(memory_status(newMemory), M_OK);
    heap_delete(newHeap);
}
Beispiel #5
0
static void new_checkMemory_empty(void **state){
    heap_t * newHeap = heap_new(100,30);
    memory_t * newMemory = heap_interactionMemory(newHeap, 50);
    memory_check(newMemory, "");
    assert_int_equal(memory_status(newMemory), M_EMPTY);
    heap_delete(newHeap);
}
void
main(void)
{
    int i;
    int arr[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
    int *heap_size = malloc(sizeof(int));
    *heap_size = sizeof(arr) / sizeof(int) - 1;
    HEAP A = {arr, heap_size};
    build_max_heap(A);
    heap_increase_key(A, 8, 15);
    for (i = 0; i <= *(A.heap_size); i++)
	printf("%d\t", A.arr[i]);
    printf("\n%d\n", *(A.heap_size));
    heap_extract_max(A);
    printf("%d\n", *(A.heap_size));
    printf("\n");
    for (i = 0; i <= *(A.heap_size); i++)
	printf("%d\t", A.arr[i]);
    printf("\n");
    A = max_heap_insert(A, 16);
    printf("%d\n", *(A.heap_size));
    for (i = 0; i <= *(A.heap_size); i++)
	printf("%d\t", A.arr[i]);
    printf("\n");
    A = heap_delete(A, 3);
    for (i = 0; i <= *(A.heap_size); i++)
	printf("%d\t", A.arr[i]);
    printf("\n");
}
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
        n = 0;
        heap.resize(buildings.size());
        id2heap.resize(buildings.size());

        vector<pair<int, int>> results;
        vector<pair<int, int>> points;

        for (int i = 0; i < buildings.size(); i++) {
            points.push_back(make_pair(buildings[i][0], i+1));
            points.push_back(make_pair(buildings[i][1], -(i+1)));
        }
        Comparator compare(buildings);
        sort(points.begin(), points.end(), compare);

        for (auto& p : points) {
            pair<int, int> res;
            if (p.second > 0) {
                int i = p.second-1;
                heap_insert(buildings[i][2], i);
                res = make_pair(p.first, heap_max());
            } else {
                int i = -p.second-1;
                heap_delete(i);
                res = make_pair(p.first, heap_max());
            }
            if (results.empty() || results.back().second != res.second)
                results.push_back(res);
        }

        return results;
    }
Beispiel #8
0
void delete_dungeon(dungeon_t *d)
{
  free(d->rooms);
  heap_delete(&d->events);
  memset(d->character_map, 0, sizeof (d->character_map));
  destroy_objects(d);
}
void new_characters(heap_t *h, cell_t *cell_arr,
		    character **c_arr, room_t *room_arr,
		    int t_char, int num_room)
{
  int i;

  for(i = 1; i < t_char; i++)
    {
      delete (character *)(*(c_arr +i));
    }

  heap_delete(h);
  
  heap_init(h, character_cmp, 0);

  ((pc *)(*c_arr))->place_pc(cell_arr, room_arr, num_room);

  heap_insert(h, *(c_arr + 0));

  for(i = 1; i < t_char; i++)
    {
      *(c_arr + i) = new npc(cell_arr, room_arr, num_room, i); 

      heap_insert(h, *(c_arr + i));
    }
}
Beispiel #10
0
/*
 * Main function for timer thread.
 */
static void watch_timers(void *arg)
{
    Timerset *set;
    long top_time;
    long now;

    set = arg;

    while (!set->stopping) {
        lock(set);

	now = time(NULL);

	while (set->heap->len > 0 && set->heap->tab[0]->elapses <= now) {
	    elapse_timer(set->heap->tab[0]);
	    heap_delete(set->heap, 0);
	}

	/*
	 * Now sleep until the next timer elapses.  If there isn't one,
	 * then just sleep very long.  We will get woken up if the
	 * top of the heap changes before we wake.
	 */

        if (set->heap->len == 0) {
            unlock(set);
            gwthread_sleep(1000000.0);
        } else {
	    top_time = set->heap->tab[0]->elapses;
	    unlock(set);
	    gwthread_sleep(top_time - now);
	}
    }
}
Beispiel #11
0
static void new_memory_fullSize(void **state){
    heap_t * newHeap = heap_new(50,30);
    memory_t * memory = heap_interactionMemory(newHeap, 100);
    assert_int_equal(heap_status(newHeap), H_FULL);
    assert_int_equal(memory_status(memory), M_EMPTY);
    heap_delete(newHeap);
}
int main() {

  int heap_size =10;
  int a[2*heap_size];
  for (int i=0; i<heap_size; i++)
    a[i] = i;
  p(a, heap_size);

  build_heap(a, heap_size);
  p(a, heap_size);

  printf("delete index = 3\n");
  heap_delete(a, 3, &heap_size);
  p(a, heap_size);
  printf("delete index = 3\n");
  heap_delete(a, 3, &heap_size);
  p(a, heap_size);
}
Beispiel #13
0
int ticker_delete(struct ticker *ticker, struct async_call *call)
{
	int old = cpu_interrupt_set(0);
	spinlock_acquire(&ticker->lock);
	int r = heap_delete(&ticker->heap, call);
	call->queue = 0;
	spinlock_release(&ticker->lock);
	cpu_interrupt_set(old);
	return r;
}
Beispiel #14
0
int workqueue_delete(struct workqueue *wq, struct async_call *call)
{
	ASSERT(call);
	int old = cpu_interrupt_set(0);
	spinlock_acquire(&wq->lock);
	int r = heap_delete(&wq->tasks, call);
	call->queue = 0;
	spinlock_release(&wq->lock);
	cpu_interrupt_set(old);
	return r;
}
Beispiel #15
0
static void
heap_remove(RemovalPolicy * policy, StoreEntry * entry,
    RemovalPolicyNode * node)
{
    HeapPolicyData *heap = policy->_data;
    heap_node *hnode = node->data;
    if (!hnode)
	return;
    heap_delete(heap->heap, hnode);
    node->data = NULL;
    heap->count -= 1;
}
Beispiel #16
0
status heap_delete_int(heap * const p_H,
                        size_t const index,
                        int * const p_i)
{
    int * p_temp;
    if (heap_delete(p_H, index, (generic_ptr *)&p_temp) == ERROR)
        return ERROR;

    *p_i = *p_temp;
    free(p_temp);
    return OK;
}//heap_delete_int
Beispiel #17
0
void load() 
{
    char str[100];
    struct stat s;

    printf("Loading, enter file name : ");
    scanf("%s", str);
    if (stat(str, &s) != 0) {
        printf("File does not exist\n");
        return;
    }
    heap_delete(&myheap);
    FILE *myfile = fopen(str, "r");
    heap_load(&myheap, myfile);
    fclose(myfile);
    heap_print(&myheap);
}
Beispiel #18
0
/* 
 * Returns the data of the node with the largest KEY value and removes that
 * node from the heap.  Returns NULL if the heap was empty.
 */
heap_t
heap_extractmin(heap * hp)
{
    heap_t data;

    if (hp->last <= 0)
	return NULL;

    mutex_lock(hp->lock);

    data = hp->nodes[0]->data;
    heap_delete(hp, hp->nodes[0]);	/* Delete the root */

    mutex_unlock(hp->lock);

    return data;
}
void delete_characters(heap_t *h, character **c_arr, int t_char)
{
  int i;
  character *curr;
  pc *player;

  player = (pc *)(*c_arr);
  player->inv.delete_inventory();
  delete player;

  for(i = 1; i < t_char; i++)
    {
      curr = (character *)(*(c_arr +i));
      delete curr;
    }

  heap_delete(h);
}
Beispiel #20
0
/*Test code. Tests if a path is correct and it has the given value */
int main()
{
	printf("<heaptest>\n");
	HEAP heap;
	ITEM p[number_of_nodes];
	ITEM *res;

	p[0].v=123;
	p[1].v=23;
	p[2].v=0;
	p[3].v=22;
	p[4].v=255;
	p[5].v=1;
	p[6].v=10;
	p[7].v=3;
	p[8].v=101;
	p[9].v =102;

	heap_init(&heap, number_of_nodes);

	printf("\nAdding nodes to the heap\nLabel\tCost\n");
	for (int i=0;i<number_of_nodes;i++) {
		printf("<iter i=%d>\n");
		p[i].label = i;
		//p[i].v = (int)(rand()/1000000);
		printf("%d\t%.2f\n",p[i].label,p[i].v);
		heap_addItem(&heap,&(p[i]));
		heap_print(&heap);
		printf("</iter>\n");
	}

	heap_print(&heap);

	/*
	printf("\n\nExtracting the nodes from the heap\nLabel\tCost\n");
	while (!heap_isEmpty(&heap)) {
		res = heap_extractMin(&heap);
		printf("%d\t%.2f\n",res->label,res->v);
	}
	*/
	heap_delete(&heap);
	printf("</heaptest>\n");
	return 0;
}
Beispiel #21
0
void gw_timer_stop(Timer *timer)
{
    gw_assert(timer != NULL);
    lock(timer->timerset);

    /*
     * If the timer is active, make it inactive and remove it from
     * the heap.
     */
    if (timer->elapses > 0) {
        timer->elapses = -1;
        gw_assert(timer->timerset->heap->tab[timer->index] == timer);
        heap_delete(timer->timerset->heap, timer->index);
    }

    abort_elapsed(timer);

    unlock(timer->timerset);
}
Beispiel #22
0
/*
 * Delete an element from the heap, at the given index
 */
void*
heapDelete(HEAP heap,int idx)
{
    HeapElement * he;
    void*	  data;

    DBG(debug("heapDelete(heap=%p,idx=%d)\n",heap,idx));

    /* Remove the element from the heap */
    he = heap_delete((Heap*)heap,idx);

    XLOG(he);
    data = he ? he->heData : NULL;

    if (he) { STDFREE(he); }

    LLOG(((Heap*)heap)->hpFilled);

    return data;
}
static void connect_rooms(cell_t *cell_arr, room_t *room_arr, int num_room)
{
  int i, x, y;
  path_node_c_t *path_arr, *curr_p;
  heap_t *h;
  room_t *room;

  h = (heap_t *) malloc(sizeof(*h));

  for(i = 1; i < num_room; i++)
    {
      room = room_arr + i;
      x = random_number(room->x, room->x + room->width - 1);
      y = random_number(room->y, room->y + room->height - 1);
   
      heap_init(h, gen_path_node_cmp, NULL);
      path_arr = (path_node_c_t *) malloc(MAP_HEIGHT * MAP_WIDTH *
				  sizeof(*path_arr));
      initialize_heap(h, path_arr, cell_arr);
           
      curr_p = path_arr + (MAP_WIDTH * y) + x;
      curr_p->weight = 1;
      curr_p->distance = 0;
      curr_p->previous = NULL;
      heap_decrease_key_no_replace(h, curr_p->heap_node);

      create_corridor(h, path_arr);

      room = room_arr + i - 1;
      x = random_number(room->x, room->x + room->width - 1);
      y = random_number(room->y, room->y + room->height - 1);
      curr_p = path_arr + (MAP_WIDTH * y) + x;

      place_corridor(curr_p, cell_arr);

      heap_delete(h);
      free(path_arr);
    }
  
  free(h);
}
Beispiel #24
0
void dijkstra(graph* g,node* s)
{
	initialize(g,s) ;
	int i ;
	for(i=1; i <= (g->vertices) ; ++i)
	{
		heap_insert(newheapnode(g->v[i])) ;
	}
	while(n != 0)
	{
		node* u = heap_delete() ;
		//printf("## poped from pq--> %d ,with d value= %d\n" , u->idx , u->d) ;
		int i ;
		edge* curedge = g->adjlist[u->idx]->firstedge ;
		for( ; curedge != NULL ; curedge = curedge->next)
		{
			if(g->v[curedge->v]->color == black)
				continue ;		
			relax(u,g->v[curedge->v],curedge->wt) ;
		}
		u->color = black ;
	}
}
Beispiel #25
0
int main() {
	const uint32_t len = 16 * 1024 * 1024;
	uint32_t i;

	uint8_t* p = (uint8_t*)heap_new(len);
	if (!p) {
		printf("out of memory!\n");
		return 1;
	}

	printf("Test 16bit stores\n");
	for (i = 0;i < len;i += 2)	
		*(uint16_t*)&p[i] = i & 0xffff;

	printf("Test 32bit stores\n");
	for (i = 0;i < len;i += 4)	
		*(uint32_t*)&p[i] = i;

	printf("Test 64bit stores\n");
	for (i = 0;i < len;i += 8)	
		*(uint64_t*)&p[i] = i;
		

	printf("Test 128bit loads/stores\n");

	__m128 v ;
	
 	for (i = 0;i < len;i += 16)	{
		v = _mm_load_ps((float*)(p + i));
		_mm_store_ps((float*)(p + i),v);
	}

	heap_delete(p);

	printf("Got no unaligned addr exception!\n");
	return 0;
}
Beispiel #26
0
List *gw_timer_break(Timerset *set)
{
	List *ret = NULL;

    lock(set);

    if (set->heap->len == 0) {
        unlock(set);
    	return NULL;
    }

    ret = gwlist_create();

    /* Stop all timers. */
    while (set->heap->len > 0) {
    	Timer *timer = set->heap->tab[0];

    	gwlist_append(ret, timer);

        /*
         * If the timer is active, make it inactive and remove it from
         * the heap.
         */
        if (timer->elapses > 0) {
            timer->elapses = -1;
            gw_assert(set->heap->tab[timer->index] == timer);
            heap_delete(set->heap, timer->index);
        }

        abort_elapsed(timer);
    }

    unlock(set);

    return ret;
}
Beispiel #27
0
int turn_delete()
{
	heap_delete(h);
	return 0;
}
Beispiel #28
0
void dijkstra(dungeon_t *d)
{
  /* Currently assumes that monsters only move on floors.  Will *
   * need to be modified for tunneling and pass-wall monsters.  */

  heap_t h;
  uint32_t x, y;
  static path_t p[DUNGEON_Y][DUNGEON_X], *c;
  static uint32_t initialized = 0;

  if (!initialized) {
    initialized = 1;
    dungeon = d;
    for (y = 0; y < DUNGEON_Y; y++) {
      for (x = 0; x < DUNGEON_X; x++) {
        p[y][x].pos[dim_y] = y;
        p[y][x].pos[dim_x] = x;
      }
    }
  }

  for (y = 0; y < DUNGEON_Y; y++) {
    for (x = 0; x < DUNGEON_X; x++) {
      d->pc_distance[y][x] = 255;
    }
  }
  d->pc_distance[d->pc.position[dim_y]][d->pc.position[dim_x]] = 0;

  heap_init(&h, dist_cmp, NULL);

  for (y = 0; y < DUNGEON_Y; y++) {
    for (x = 0; x < DUNGEON_X; x++) {
      if (mapxy(x, y) >= ter_floor) {
        p[y][x].hn = heap_insert(&h, &p[y][x]);
      }
    }
  }

  while ((c = heap_remove_min(&h))) {
    c->hn = NULL;
    if ((p[c->pos[dim_y] - 1][c->pos[dim_x] - 1].hn) &&
        (d->pc_distance[c->pos[dim_y] - 1][c->pos[dim_x] - 1] >
         d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1)) {
      d->pc_distance[c->pos[dim_y] - 1][c->pos[dim_x] - 1] =
        d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1;
      heap_decrease_key_no_replace(&h,
                                   p[c->pos[dim_y] - 1][c->pos[dim_x] - 1].hn);
    }
    if ((p[c->pos[dim_y] - 1][c->pos[dim_x]    ].hn) &&
        (d->pc_distance[c->pos[dim_y] - 1][c->pos[dim_x]    ] >
         d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1)) {
      d->pc_distance[c->pos[dim_y] - 1][c->pos[dim_x]    ] =
        d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1;
      heap_decrease_key_no_replace(&h,
                                   p[c->pos[dim_y] - 1][c->pos[dim_x]    ].hn);
    }
    if ((p[c->pos[dim_y] - 1][c->pos[dim_x] + 1].hn) &&
        (d->pc_distance[c->pos[dim_y] - 1][c->pos[dim_x] + 1] >
         d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1)) {
      d->pc_distance[c->pos[dim_y] - 1][c->pos[dim_x] + 1] =
        d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1;
      heap_decrease_key_no_replace(&h,
                                   p[c->pos[dim_y] - 1][c->pos[dim_x] + 1].hn);
    }
    if ((p[c->pos[dim_y]    ][c->pos[dim_x] - 1].hn) &&
        (d->pc_distance[c->pos[dim_y]    ][c->pos[dim_x] - 1] >
         d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1)) {
      d->pc_distance[c->pos[dim_y]    ][c->pos[dim_x] - 1] =
        d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1;
      heap_decrease_key_no_replace(&h,
                                   p[c->pos[dim_y]    ][c->pos[dim_x] - 1].hn);
    }
    if ((p[c->pos[dim_y]    ][c->pos[dim_x] + 1].hn) &&
        (d->pc_distance[c->pos[dim_y]    ][c->pos[dim_x] + 1] >
         d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1)) {
      d->pc_distance[c->pos[dim_y]    ][c->pos[dim_x] + 1] =
        d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1;
      heap_decrease_key_no_replace(&h,
                                   p[c->pos[dim_y]    ][c->pos[dim_x] + 1].hn);
    }
    if ((p[c->pos[dim_y] + 1][c->pos[dim_x] - 1].hn) &&
        (d->pc_distance[c->pos[dim_y] + 1][c->pos[dim_x] - 1] >
         d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1)) {
      d->pc_distance[c->pos[dim_y] + 1][c->pos[dim_x] - 1] =
        d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1;
      heap_decrease_key_no_replace(&h,
                                   p[c->pos[dim_y] + 1][c->pos[dim_x] - 1].hn);
    }
    if ((p[c->pos[dim_y] + 1][c->pos[dim_x]    ].hn) &&
        (d->pc_distance[c->pos[dim_y] + 1][c->pos[dim_x]    ] >
         d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1)) {
      d->pc_distance[c->pos[dim_y] + 1][c->pos[dim_x]    ] =
        d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1;
      heap_decrease_key_no_replace(&h,
                                   p[c->pos[dim_y] + 1][c->pos[dim_x]    ].hn);
    }
    if ((p[c->pos[dim_y] + 1][c->pos[dim_x] + 1].hn) &&
        (d->pc_distance[c->pos[dim_y] + 1][c->pos[dim_x] + 1] >
         d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1)) {
      d->pc_distance[c->pos[dim_y] + 1][c->pos[dim_x] + 1] =
        d->pc_distance[c->pos[dim_y]][c->pos[dim_x]] + 1;
      heap_decrease_key_no_replace(&h,
                                   p[c->pos[dim_y] + 1][c->pos[dim_x] + 1].hn);
    }
  }
  heap_delete(&h);
}
Beispiel #29
0
void _st_del_sleep_q(_st_thread_t *thread)
{
  heap_delete(thread);
  thread->flags &= ~_ST_FL_ON_SLEEPQ;
}
Beispiel #30
0
void delete_dungeon(dungeon_t *d)
{
  free(d->rooms);
  heap_delete(&d->next_turn);
  memset(d->charmap, 0, sizeof (d->charmap));
}