void QuickSort(int a[],int Left,int Right) { int i; int j; int k; int Center; const int Cutoff = 3; if (Left + Cutoff <= Right) { Center = Median3(a,Left,Right); i = Left; j = Right-1; while (1)//for() { while(a[++i] < Center){;}//下标右移 while(a[--j] > Center){;}//下标左移 if (i < j) swap_int(&a[i],&a[j]); else break; } swap_int(&a[i],&a[Right-1]); QuickSort(a,Left,i-1); QuickSort(a,i+1,Right); } else { InsertionSort(a+Left,Right-Left+1); } }
/* add the recently connected client to the game as a player */ void join_player(struct socket_node *socket) { struct packet_version packet; struct packet_welcome packet2; int i; struct action_node *action; struct socket_node *p; socket->player = get_free_player(); init_player(socket->player, INIT_PLAYER_HUMAN); /* send version */ packet.version = swap_int(PROGRAM_VERSION); send_socket(socket, PACKET_VERSION, &packet, sizeof(struct packet_version)); /* send welcome */ packet2.player = swap_int(socket->player); send_socket(socket, PACKET_WELCOME, &packet2, sizeof(struct packet_welcome)); /* send all previous players to the new client */ for (i = 0; i < NUM_PLAYERS; i++) { if (IS_ACTIVE(i) && i != socket->player) { send_player_info(socket, i, 0); /* send any already selected action */ action = find_action(i, current_turn); if (action && action->action != ACTION_NOTHING) send_action(socket, i, current_turn); } } /* send the new player to ALL clients */ for (p = socket_list; p != NULL; p = p->next) { if (p->type == SOCKET_CLIENT) send_player_info(p, socket->player, 1); } telegram("\n%s has\nconnected to the server.\n", GET_NAME(socket->player)); }
//快速排序 void QuickSort(int A[],int Left,int Right) { int i,j; int Pivot; const int Cutoff = 3; if (Left + Cutoff <= Right) { Pivot = Median3(A,Left,Right); i = Left; j = Right - 1; while (1) { while(A[++i] < Pivot){;} while(A[--j] > Pivot){;} if (i < j) swap_int(&A[i],&A[j]); else break; } swap_int(&A[i],&A[Right - 1]); QuickSort(A,Left,i - 1); QuickSort(A,i + 1,Right); } else { InsertionSort(A+Left,Right - Left + 1); } }
static void bresenham_line( SDL_Surface *surf, int ax, int ay, int bx, int by, Uint32 color) { int deltax, deltay; int error, y, ystep, x; int is_steep; /* boolean */ is_steep = abs(by - ay) > abs(bx - ax); if (is_steep) { swap_int(&ax, &ay); swap_int(&bx, &by); } if (ax > bx) { swap_int(&ax, &bx); swap_int(&ay, &by); } deltax = bx - ax; deltay = abs(by - ay); error = deltax >> 1; y = ay; ystep = (ay < by) ? 1 : -1; for (x = ax; x <= bx; x++) { if (!is_steep) { draw_pixel32(surf, x, y, color); } else { draw_pixel32(surf, y, x, color); } error -= deltay; if (error < 0) { y += ystep; error += deltax; } } }
internal void DebugLine(int x0, int y0, int x1, int y1, u32 color) { bool32 steep = false; if (Abs(x1 - x0) < Abs(y1 - y0)) { steep = true; swap_int(&x0, &y0); swap_int(&x1, &y1); } if (x0 > x1) { swap_int(&x0, &x1); swap_int(&y0, &y1); } int dx = x1 - x0; int dy = y1 - y0; int derror2 = Abs(dy) * 2; int error2 = 0; int y = y0; for (int x = x0; x <= x1; x++) { if (steep) SetPixel(y, x, color); else SetPixel(x, y, color); error2 += derror2; if (error2 > 1) { y += (y1 > y0 ? 1 : -1); error2 -= 2 * dx; } } }
static void swap_markerp_pft (MARKERP_PFT *data) { swap_int (&data->id); swap_short (&data->usage); swap_short (&data->tile_reference_identifier); swap_int (&data->entity_node_key); }
static void swap_hydline_lft (HYDLINE_LFT *data) { swap_int (&data->id); swap_short (&data->accuracy_category); swap_float (&data->contour_value); swap_short (&data->tile_reference_identifier); swap_int (&data->edge_primitive_key); }
int transform_coord(t_line *c, t_infos_mlx *infos_mlx) { if (c->x1 == c->x2 && c->y1 == c->y2) return (-1); (c->x1 > c->x2) ? (swap_int(&(c->y1), &(c->y2))) : (0); (c->x1 > c->x2) ? (swap_int(&(c->x1), &(c->x2))) : (0); return (0); }
void send_string(char *string) /* includefile */ { int len; len=strlen(string); if (swapout) swap_int(&len); fwrite(&len, sizeof(int), 1, output); if (swapout) swap_int(&len); fwrite(string, sizeof(char), len, output); /*fprintf(stderr,"%s\n",string);*/ }
//三数取中分割法 int Median3(int A[],int Left,int Right) { int Center = (Left + Right) / 2; if (A[Left] > A[Center]) swap_int(&A[Left],&A[Center]); if (A[Left] > A[Right]) swap_int(&A[Left],&A[Right]); if (A[Center] > A[Right]) swap_int(&A[Center],&A[Right]); swap_int(&A[Center],&A[Right - 1]); return A[Right - 1]; }
int Median3(int a[],int Left,int Right) { int Cent = (Left + Right) / 2; if (a[Left] > a[Cent]) swap_int(&a[Left],&a[Cent]); if (a[Left] > a[Right]) swap_int(&a[Left],&a[Right]); if (a[Cent] > a[Right]) swap_int(&a[Cent],&a[Right]); swap_int(&a[Cent],&a[Right-1]); return a[Right-1]; }
static void swap_dangera_aft (DANGERA_AFT *data) { swap_int (&data->id); swap_short (&data->accuracy_category); swap_short (&data->date); swap_short (&data->existence_category); swap_short (&data->hydrographic_depth_height_info); swap_float (&data->hydrographic_depth); swap_short (&data->value); swap_short (&data->vertical_reference_category); swap_short (&data->tile_reference_identifier); swap_int (&data->face_primitive_key); }
void send_socket(struct socket_node *socket, int type, void *data, int size) { struct packet_header header; if (socket->send_buf_size + sizeof(struct packet_header) + size > BUFFER_SIZE) do_error("Send buffer overflow"); /* shouldn't happen */ header.type =swap_int(type); header.size =swap_int(size); /* copy header to buffer */ memcpy(socket->send_buf + socket->send_buf_size, &header, sizeof(struct packet_header)); socket->send_buf_size += sizeof(struct packet_header); /* copy packet data to buffer */ memcpy(socket->send_buf + socket->send_buf_size, data, size); socket->send_buf_size += size; }
static void swap_pipelinel_lft (PIPELINEL_LFT *data) { swap_int (&data->id); swap_float (&data->depth_below_surface_level); swap_short (&data->existence_category); swap_float (&data->height_above_sea_bottom); swap_short (&data->location_category); swap_float (&data->overhead_clearance_category); swap_short (&data->over_water_obstruction); swap_short (&data->pipeline_type); swap_short (&data->product_category); swap_short (&data->tile_reference_identifier); swap_int (&data->edge_primitive_key); }
t_program *load_program(t_prog_param *prog, t_vm *vm) { t_program *program; int fd; if ((fd = open(prog->prog_name, O_RDONLY)) == 0) return (NULL); if ((program = my_malloc(sizeof(t_program), "Error: fail to malloc program...\n")) == 0) return (NULL); program->prog_param = prog; if ((program->header = my_malloc(sizeof(header_t), "Error: fail to malloc header...\n")) == 0) return (NULL); if (read(fd, program->header, sizeof(header_t)) <= 0) return (NULL); if (endian_cmp(program->header->magic, COREWAR_EXEC_MAGIC) == 0) { my_printf("%s: Invalide corewar file ...\n", prog->prog_name); return (NULL); } if (is_big_endian() == 0) program->header->prog_size = swap_int(program->header->prog_size); if (mem_reader(vm, fd, program) == 1) { my_printf("Error: invalid prog size expected %s\n", program->header->prog_size); return (NULL); } program->registre[0][REG_SIZE - 1] = (char)prog->prog_number; program->pc = prog->load_address; program->sleep = 0; program->active = 1; program->carry = 1; program->pid = prog->prog_number; vm->register_player[program->pid] = 1; return (program); }
int my_printf(void){ printf("Hello World\n"); int x; int num = MAXNUMBER; int big = 100; int small = 10; x = my_add(3,5); printf("get number: %d\n",x); printf("get max number: %d\n", num); // define macro test printf("the big number is %d , the small number is %d \n",big,small); swap_int(big,small); printf("after to swap.....\n"); printf("the big number is %d , the small number is %d \n",big,small); // define macro test printf("get the max number %d\n", max(big,small)); printf("get the max number of three is %d\n", max3(big,small,num)); return 0; }
//initializes thread_id_array with random thread id's based on "receiving threads ratio" void get_random_thread_selection(MContext *mc, int *thread_id_array, int *thread_id_array_sz) { int number_of_sharing_threads = mc->gopts->num_threads / (100 / mc->gopts->receiving_threads_ratio); //printf("number of sharing threads %d\n", number_of_sharing_threads); //get number_of_sharing_threads random thread id's from 0 to gopts->num_threads-1 //init thread_id_array with all possible thread id's and remove randomly *thread_id_array_sz = mc->gopts->num_threads; int i; for (i = 0; i < *thread_id_array_sz; ++i) { thread_id_array[i] = i; } for (i = 0; i < mc->gopts->num_threads - number_of_sharing_threads; ++i) { //randomly select a still existing thread id int id = get_rand_int_range(mc, 0, *thread_id_array_sz - 1); //swap element at position id with last element swap_int(&thread_id_array[id], &thread_id_array[*thread_id_array_sz - 1]); //"virtually" remove last element (*thread_id_array_sz)--; } }
static void swap_hydrol_lft (HYDROL_LFT *data) { swap_int (&data->id); swap_short (&data->accuracy_category); swap_float (&data->contour_value); swap_short (&data->hypsography_portrayal_category); }
/* Conver sudoku puzzle*/ void sudoku_to_problem(void){ int row,col,val; int i,j; // initialize row_index for(row=0; row<SIZE; ++row) row_index[row]=row; for(row=0; row<SIZE; ++row){ for(col=0; col<SIZE; ++col){ sudoku_modified[row][col]=EMPTY; } } for(row=0; row<SIZE; ++row){ for(col=0; col<SIZE; ++col){ if((val=sudoku[row][col])!=EMPTY){ sudoku_modified[val][row]=col; ++positive[val]; } } } for(i=0; i<SIZE-1; ++i){ for(j=i+1;j<SIZE; ++j){ if(positive[j]>positive[i]){ swap_row(sudoku_modified,i,j); swap_int(&row_index[i],&row_index[j]); } } } }
unsigned vax_to_local_int(const fort_int *i) { #if defined(WORDS_BIGENDIAN) return swap_int(*(unsigned *)i); #else return *i; #endif /* WORDS_BIGENDIAN */ }
int main(void) { int a = 3; int b = 5; printf("%d\t%d\n", a, b); swap_int(&a, &b ); printf("%d\t%d\n", a, b); char s1[] = "a"; char s2[] = "b"; printf("%s\t%s\n", &s1[0], &s2[0]); swap_char(s1, s2 ); printf("%s\t%s\n", &s1[0], &s2[0]); char s3[] = "abcdefg"; printf("%s\n", &s3[0]); reverseString(s3); printf("%s\n", &s3[0]); return 0; }
int main_swap_gen(int argc, char** argv) { swap_int(); swap_float(); return (EXIT_SUCCESS); }
void send_int(char *name, int integer) /* includefile */ { send_string(name); if (swapout) swap_int(&integer); fwrite(&integer,sizeof(int),1,output); /*fprintf(stderr,"%d\n",integer);*/ }
static void swap_hazarda_aft (HAZARDA_AFT *data) { swap_int (&data->id); swap_short (&data->accuracy_category); swap_short (&data->certainty); swap_short (&data->date); swap_short (&data->existence_category); swap_short (&data->hydrographic_depth_height_info); swap_float (&data->hydrographic_depth); swap_short (&data->location_category); swap_short (&data->sea_floor_feature_category); swap_short (&data->severity); swap_short (&data->value); swap_short (&data->vertical_reference_category); swap_short (&data->tile_reference_identifier); swap_int (&data->face_primitive_key); }
int32_t le_to_native_int(int32_t i) { if(set_endian() == BIG_ENDIAN) return swap_int(i); return i; }
/* server decides if selected actions are successful or not */ void server_prepare_actions() { int i, mt; struct action_node *action, *action2; struct packet_result packet; for (i = 0; i < NUM_PLAYERS; i++) { if (IS_ACTIVE(i)) { action = find_action(i, current_turn); if (action) { switch (action->action) { case ACTION_PROPAGANDA: action->success = randomize(1, 1); action->res_num = randomize(1, 10); /* population number */ break; case ACTION_WARHEAD_10MT: case ACTION_WARHEAD_20MT: case ACTION_WARHEAD_50MT: case ACTION_WARHEAD_100MT: mt = action->action - ACTION_WARHEAD_10MT; /* check target for defense */ action2 = find_action(action->target_player, current_turn); if (!action2) do_error("server_prepare_actions()"); /* shouldn't happen */ if (GET_MISSILE(i) > -1 && (action2->action == ACTION_LNDS || action2->action == ACTION_MEGA)) action->success = 0; else if (GET_BOMBER(i) > -1 && action2->action == ACTION_MEGA) action->success = 0; else action->success = 1; action->res_num = randomize(damage[mt * 2], damage[(mt * 2) + 1]); /* damage */ break; } /* send result to other players */ packet.turn = swap_int(current_turn); packet.player = swap_int(i); packet.success = swap_int(action->success); packet.res_num = swap_int(action->res_num); send_all_sockets(PACKET_RESULT, &packet, sizeof(struct packet_result)); } } } }
/*--------------------------------------------------------------------------*/ int writeInt(int _val, FILE* _pF, int _iEndian) { //reverse _val bytes and write int newVal = _val; if (_iEndian == BIG_ENDIAN) { newVal = swap_int(_val); } return fwrite(&newVal, sizeof(int), 1, _pF) != 1 ? -1 : 0; }
static void swap_lightsp_pft (LIGHTSP_PFT *data) { swap_int (&data->id); swap_short (&data->accuracy_category); swap_int (&data->broadcast_frequency[0]); swap_int (&data->broadcast_frequency[1]); swap_short (&data->color_code); swap_short (&data->elevation); swap_short (&data->hydrographic_light_type); swap_short (&data->IALA_aid_category); swap_short (&data->nominal_light_range); swap_short (&data->nav_system_type[0]); swap_short (&data->nav_system_type[1]); swap_float (&data->light_period); swap_short (&data->radar_reflector_attr); swap_short (&data->sound_signal_type); swap_short (&data->topmark_characteristic); swap_short (&data->tile_reference_identifier); swap_int (&data->entity_node_key); }
void local_to_vax_ints(fort_int *ia, const fort_int *n) { #if defined(WORDS_BIGENDIAN) int i; unsigned *uia = (unsigned *)ia; for (i = 0; i < *n; i++) { uia[i] = swap_int(uia[i]); } #endif /* WORDS_BIGENDIAN */ (void)ia; (void)n; // Avoid compiler warning return; }
int main(void) { int a; int b; a = 98; b = 402; printf("a=%d, b=%d\n", a, b); swap_int(&a, &b); printf("a=%d, b=%d\n", a, b); return (0); }