Ejemplo n.º 1
0
BOOL intersect_vis_rectangles( struct bitblt_coords *dst, struct bitblt_coords *src )
{
    RECT rect;

    /* intersect the rectangles */

    if ((src->width == dst->width) && (src->height == dst->height)) /* no stretching */
    {
        offset_rect( &src->visrect, dst->x - src->x, dst->y - src->y );
        if (!intersect_rect( &rect, &src->visrect, &dst->visrect )) return FALSE;
        src->visrect = dst->visrect = rect;
        offset_rect( &src->visrect, src->x - dst->x, src->y - dst->y );
    }
    else  /* stretching */
    {
        /* map source rectangle into destination coordinates */
        rect = src->visrect;
        offset_rect( &rect, -min( src->x, src->x + src->width + 1),
                     -min( src->y, src->y + src->height + 1) );
        rect.left   = dst->x + rect.left * dst->width / abs(src->width);
        rect.top    = dst->y + rect.top * dst->height / abs(src->height);
        rect.right  = dst->x + rect.right * dst->width / abs(src->width);
        rect.bottom = dst->y + rect.bottom * dst->height / abs(src->height);
        if (rect.left > rect.right) swap_ints( &rect.left, &rect.right );
        if (rect.top > rect.bottom) swap_ints( &rect.top, &rect.bottom );

        /* avoid rounding errors */
        rect.left--;
        rect.top--;
        rect.right++;
        rect.bottom++;
        if (!intersect_rect( &dst->visrect, &rect, &dst->visrect )) return FALSE;

        /* map destination rectangle back to source coordinates */
        rect = dst->visrect;
        offset_rect( &rect, -min( dst->x, dst->x + dst->width + 1),
                     -min( dst->y, dst->y + dst->height + 1) );
        rect.left   = src->x + rect.left * src->width / abs(dst->width);
        rect.top    = src->y + rect.top * src->height / abs(dst->height);
        rect.right  = src->x + rect.right * src->width / abs(dst->width);
        rect.bottom = src->y + rect.bottom * src->height / abs(dst->height);
        if (rect.left > rect.right) swap_ints( &rect.left, &rect.right );
        if (rect.top > rect.bottom) swap_ints( &rect.top, &rect.bottom );

        /* avoid rounding errors */
        rect.left--;
        rect.top--;
        rect.right++;
        rect.bottom++;
        if (!intersect_rect( &src->visrect, &rect, &src->visrect )) return FALSE;
    }
    return TRUE;
}
Ejemplo n.º 2
0
void sort_nums(int* number_array, size_t number_array_size)
{
    if (number_array_size == 0)
        return;
  
  
    ///5, 7, 8, 1, 7, 10
    ///5, 7, 8, 1, 7, 10
    ///^--------------^ find the minimum in this range
    ///         ^ minimum
    ///1, 7, 8, 5, 7, 10
    ///   ^-----------^ find the minimum in this range
    ///     ....
  
    for (int i = 0; i < number_array_size - 1; ++i)
    {
        ///loop through everything until the last item
        
        ///pick the item at i
        
        ///find the item in the range [i,end), that is the minimum
        size_t minimum_value_index = i + find_min_index(number_array + i, number_array_size - i);
        
        swap_ints(number_array+i, number_array+minimum_value_index);

        //fprintf(stdout, "%d\n", number_array[i]);

    }
    
}
Ejemplo n.º 3
0
Archivo: main.cpp Proyecto: CCJY/coliru
int main() {
    using_overload(swap_ints, std::swap<int>);
    
    
    int a = 0, b = 1;
    swap_ints(a, b);
    ASSERT(a == 1 && b == 0);
}
void shuffle_ints(int N, int* array)
{
    int i;
    for (i = N - 1; i >= 0; i--)
    {
        int j = rand() % (i + 1);
        swap_ints(array + i, array + j);
    }
}
Ejemplo n.º 5
0
void place_negatives_first(int *arr, unsigned int length) {
  int *current_left = arr;
  int *current_right = arr + length - 1;
  while (current_left < current_right) {
    while (current_left < current_right && *current_left < 0)
      current_left++;
    while (current_left < current_right && *current_right > 0)
      current_right--;
    swap_ints(current_left, current_right);
  }
}
Ejemplo n.º 6
0
int find_median(int *data, int l, int r, int sz) {
	int groups_cnt = (r-l+1)/5;
	printf("groups_cnt=%d, l=%d, r=%d\n", groups_cnt, l, r);
	print_data("before compute 5", data, sz);
	for (int j = 0; j < groups_cnt; j++) {
		swap_ints(data, l+j, compute_median_of_five(data, l + 5*j));
		/*data[l+j] = compute_median_of_five(data, l + 5*j);*/
		printf("start = %d, ", l+5*j);
		print_data("after compute 5", data, sz);
	}
	return select_median(data, l, l + groups_cnt, groups_cnt/2);
}
Ejemplo n.º 7
0
/* 
 * inet_htonl: Converts each element of `data' from host to network order. If
 * `family' is equal to AF_INET6, the array is swapped too (on big endian
 * machine).
 */
void inet_htonl(u_int *data, int family)
{
#if BYTE_ORDER == LITTLE_ENDIAN
	if(family==AF_INET) {
		data[0]=htonl(data[0]);
	} else {
		int i;
		swap_ints(MAX_IP_INT, data, data);
		for(i=0; i<MAX_IP_INT; i++)
			data[i]=htonl(data[i]);
	}
#endif
}
Ejemplo n.º 8
0
int select_median(int *data, int l, int r, int kth) {
	int idx, minValue, sz = r - l;
	printf("select_median: r=%d, l=%d, kth=%d\n", l, r, kth);
	for (int i = 0; i <= kth; i++) {
		idx = i;
		minValue = data[i];
		for (int j = i+1; j <= sz; ++j) {
			if (minValue > data[j]) {
				idx = j;
				minValue = data[j];
			}
		}
		swap_ints(data, i, idx);
	}
	return data[kth];
}
Ejemplo n.º 9
0
static int *alloc_random_data(int sz) {
	srandom(random());
	int *data = malloc(sz*sizeof(int));

	for (int i = 0; i < sz; ++i) {
		data[i] = i;
	}

	/* Shuffle by Fisher-Yates algorithm */
	int j;
	for (int i = sz - 1; i > 0; --i) {
		swap_ints(data, (int)(i-1) * drand48(), i);
		print_data("swapped", data, sz);
	}

	return data;
}
int GCD(int a, int b)
{ 
    int r;
    if (a < b){
        swap_ints(&a ,&b );
    }
    r = a%b;
    if (r == 0)
    {
        return b;
    }
        
    a = b;
    b = r;
    
    return GCD(a,b);
}
Ejemplo n.º 11
0
void swap_rows(int *arr, unsigned int row1, unsigned int row2,
               unsigned int rows, unsigned int columns) {
  for (unsigned int col = 1; col <= columns; col++)
    swap_ints(&arr[mat_to_arr(row1, col, columns)],
              &arr[mat_to_arr(row2, col, columns)]);
}
Ejemplo n.º 12
0
static Game *
create_game (const char *name,
             unsigned    width,
             unsigned    height)

{
  Game *game = dsk_malloc (sizeof (Game));
  unsigned usize;
  unsigned i;

  game->name = dsk_strdup (name);
  game->next_game = all_games;
  all_games = game;
  game->universe_width = width;
  game->universe_height = height;
  usize = width * height;
  game->h_walls = generate_ones (usize);
  game->v_walls = generate_ones (usize);
  for (i = 0; i < N_OBJECT_TYPES; i++)
    game->objects[i] = NULL;
  game->generators = NULL;
  game->cells = dsk_malloc0 (sizeof (Cell) * width * height);
  game->latest_update = 0;
  game->wrap = DSK_TRUE;
  game->diag_bullets_bounce = DSK_TRUE;
  game->bullet_kills_player = DSK_TRUE;
  game->bullet_kills_generator = DSK_TRUE;
  game->pending_updates = NULL;

  /* Generate with Modified Kruskals Algorithm, see 
   *    http://en.wikipedia.org/wiki/Maze_generation_algorithm
   */
  TmpWall *tmp_walls = dsk_malloc (sizeof (TmpWall) * usize * 2);
  TmpSetInfo *sets = dsk_malloc (sizeof (TmpSetInfo) * usize);

  /* connect the walls together in random order */
  unsigned *scramble;
  scramble = dsk_malloc (sizeof (unsigned) * usize * 2);
  for (i = 0; i < usize * 2; i++)
    scramble[i] = i;
  for (i = 0; i < usize * 2; i++)
    swap_ints (scramble + random_int_range (usize * 2), scramble + random_int_range (usize * 2));

  TmpWall *wall_list = NULL;
  for (i = 0; i < usize * 2; i++)
    {
      unsigned e = scramble[i];
      unsigned h = e % 2;
      unsigned x = (e / 2) % width;
      unsigned y = e / (width * 2);
      if (!game->wrap)
        {
          if ((h && y == 0) || (!h && x == 0))
            continue;
        }
      tmp_walls[e].prev = NULL;
      tmp_walls[e].next = wall_list;
      if (wall_list)
        wall_list->prev = tmp_walls + e;
      wall_list = tmp_walls + e;
    }

  for (i = 0; i < usize; i++)
    {
      sets[i].set_number = i;
      sets[i].next_in_set = sets + i;
    }

  while (wall_list != NULL)
    {
      /* Invariants:
           - the sets are in a ring by set number.
           - The wall_list only consists of walls that separate distinct sets.
       */

      /* remove wall */
      unsigned e = wall_list - tmp_walls;
      unsigned h = e % 2;
      unsigned x = (e / 2) % width;
      unsigned y = e / (width * 2);
      TmpSetInfo *si = sets + e / 2;
      TmpSetInfo *osi;
      if (h)
        {
          if (y == 0)
            osi = si + (height - 1) * width;
          else
            osi = si - width;
          game->h_walls[x + y * width] = 0;
        }
      else
        {
          if (x == 0)
            osi = si + width - 1;
          else
            osi = si - 1;
          game->v_walls[x + y * width] = 0;
        }
      dsk_assert (osi->set_number != si->set_number);
      TmpSetInfo *kring = osi->set_number < si->set_number ? osi : si;              /* ring to keep */
      TmpSetInfo *dring = osi->set_number < si->set_number ? si : osi;              /* ring to change */
      TmpSetInfo *dring_start = dring;

      /* combine sets (removing any walls that no longer separate different sets
         from the list of walls to remove) */
      unsigned set = kring->set_number;
      do
        {
          unsigned x = (dring - sets) % width;
          unsigned y = (dring - sets) / width;
          int wall_idx;
          dring->set_number = set;

#if 0
          if (wall_list)
            {
              dsk_assert (wall_list->prev == NULL);
              TmpWall *t;
              for (t = wall_list; t; t = t->next)
                if (t->next)
                  dsk_assert (t->next->prev == t);
            }
#endif

          /* Maybe remove left wall from candidate set of walls. */
          wall_idx = -1;
          if (x > 0 && (dring-1)->set_number == set)
            wall_idx = 2 * (x + y * width);
          else if (x == 0 && game->wrap && (dring+width-1)->set_number == set)
            wall_idx = 2 * (x + y * width);
          if (wall_idx >= 0)
            remove_tmp_wall (tmp_walls, wall_idx, &wall_list);

          /* Maybe remove right wall from candidate set of walls. */
          wall_idx = -1;
          if (x < width - 1 && (dring+1)->set_number == set)
            wall_idx = 2 * ((x+1) + y * width);
          else if (x == width - 1 && game->wrap && (dring-width+1)->set_number == set)
            wall_idx = 2 * (0 + y * width);
          if (wall_idx >= 0)
            remove_tmp_wall (tmp_walls, wall_idx, &wall_list);

          /* Maybe remove top wall from candidate set of walls. */
          wall_idx = -1;
          if (y > 0 && (dring-width)->set_number == dring->set_number)
            wall_idx = 2 * (x + y * width) + 1;
          else if (y == 0 && game->wrap && (dring+width*(height-1))->set_number == set)
            wall_idx = 2 * (x + y * width) + 1;
          if (wall_idx >= 0)
            remove_tmp_wall (tmp_walls, wall_idx, &wall_list);

          /* Maybe remove bottom wall from candidate set of walls. */
          wall_idx = -1;
          if (y < height - 1 && (dring+width)->set_number == set)
            wall_idx = 2 * (x + (y+1) * width) + 1;
          else if (y == height - 1 && game->wrap && (dring-(height-1)*width)->set_number == set)
            wall_idx = 2 * (x + 0 * width) + 1;
          if (wall_idx >= 0)
            remove_tmp_wall (tmp_walls, wall_idx, &wall_list);

          dring = dring->next_in_set;
        }
      while (dring != dring_start);

      /* Merge the rings */
      TmpSetInfo *old_dring_next = dring->next_in_set;
      dring->next_in_set = kring->next_in_set;
      kring->next_in_set = old_dring_next;

    }

  dsk_free (tmp_walls);
  dsk_free (sets);
  dsk_free (scramble);

  /* generate generators */
  unsigned n_generators = 12 + rand () % 6;
  dsk_warning ("%u generators", n_generators);
  i = 0;
  while (i < n_generators)
    {
      unsigned idx = random_int_range (usize);
      Cell *cell = game->cells + idx;
      if (cell->generator == NULL)
        {
          cell->generator = dsk_malloc (sizeof (Generator));
          cell->generator->game = game;
          cell->generator->x = (idx % width) * CELL_SIZE + CELL_SIZE/2;
          cell->generator->y = (idx / width) * CELL_SIZE + CELL_SIZE/2;
          dsk_warning ("created generator at %u,%u",cell->generator->x ,cell->generator->y);
          cell->generator->generator_prob = 0.01;
          cell->generator->next_in_game = game->generators;
          cell->generator->prev_in_game = NULL;
          if (game->generators)
            game->generators->prev_in_game = cell->generator;
          game->generators = cell->generator;

          i++;
        }
    }

  game->timer = dsk_main_add_timer_millis (update_period_msecs,
                                    (DskTimerFunc) game_update_timer_callback,
                                    game);
  return game;
}