/* Draw a one-pixel-wide line. */ int gz_draw_line_fixed(fixed ixf, fixed iyf, fixed itoxf, fixed itoyf, const gx_device_color *pdevc, gs_state *pgs) { int ix = fixed2int_var(ixf); int iy = fixed2int_var(iyf); int itox = fixed2int_var(itoxf); int itoy = fixed2int_var(itoyf); gx_device *dev; gp_check_interrupts(); if ( itoy == iy ) /* horizontal line */ { return (ix <= itox ? gz_fill_rectangle(ix, iy, itox - ix + 1, 1, pdevc, pgs) : gz_fill_rectangle(itox, iy, ix - itox + 1, 1, pdevc, pgs) ); } if ( itox == ix ) /* vertical line */ { return (iy <= itoy ? gz_fill_rectangle(ix, iy, 1, itoy - iy + 1, pdevc, pgs) : gz_fill_rectangle(ix, itoy, 1, iy - itoy + 1, pdevc, pgs) ); } if ( color_is_pure(pdevc) && (dev = pgs->device->info, (*dev->procs->draw_line)(dev, ix, iy, itox, itoy, pdevc->color1)) >= 0 ) return 0; { fixed h = itoyf - iyf; fixed w = itoxf - ixf; fixed tf; #define fswap(a, b) tf = a, a = b, b = tf if ( (w < 0 ? -w : w) <= (h < 0 ? -h : h) ) { if ( h < 0 ) fswap(ixf, itoxf), fswap(iyf, itoyf), h = -h; return gz_fill_trapezoid_fixed(ixf - fixed_half, fixed_1, iyf, itoxf - fixed_half, fixed_1, h, 0, pdevc, pgs); } else { if ( w < 0 ) fswap(ixf, itoxf), fswap(iyf, itoyf), w = -w; return gz_fill_trapezoid_fixed(iyf - fixed_half, fixed_1, ixf, itoyf - fixed_half, fixed_1, w, 1, pdevc, pgs); } #undef fswap } }
int main(){ //声明指针 int *p; int x = 13; p = &x;//获取变量的存储地址 printf("The value of p is: %d\n", p); printf("The size of p is: %d\n", sizeof(p)); //指针类型转换 char *q = (char *)p; printf("The value of q is: %d\n", q);//返回的值和p一样 printf("The value of *q is: %d\n", *q);//返回值为13,说明系统按小端序存放数据 //声明一个函数指针fswap, 函数签名与swap一样. void (*fswap)(int *, int *); fswap = swap;//函数指针fswap赋值 int a = 3, b = 5; //函数指针调用 fswap(&a, &b); printf("after swap: a is %d, b is %d\n", a, b); }
int ssd_reverse(Quark *q) { ss_data *ssd = ssd_get_data(q); int nrows, ncols, i, j, k; if (!ssd) { return RETURN_FAILURE; } nrows = ssd_get_nrows(q); ncols = ssd_get_ncols(q); for (k = 0; k < ncols; k++) { ss_column *col = &ssd->cols[k]; if (col->format == FFORMAT_STRING) { char **s = col->data; for (i = 0; i < nrows/2; i++) { j = (nrows - 1) - i; sswap(&s[i], &s[j]); } } else { double *x = col->data; for (i = 0; i < nrows/2; i++) { j = (nrows - 1) - i; fswap(&x[i], &x[j]); } } } quark_dirtystate_set(q, TRUE); return RETURN_SUCCESS; }
/* Draw a one-pixel-wide line. */ int gz_draw_line_fixed(fixed ixf, fixed iyf, fixed itoxf, fixed itoyf, gx_device_color *pdevc, gs_state *pgs) { int ix = fixed2int(ixf); int iy = fixed2int(iyf); int itox = fixed2int(itoxf); int itoy = fixed2int(itoyf); if ( itoy == iy ) /* horizontal line */ { if ( ix <= itox ) gz_fill_rectangle(ix, iy, fixed2int_ceiling(itoxf) - ix, 1, pdevc, pgs); else gz_fill_rectangle(itox, iy, fixed2int_ceiling(ixf) - itox, 1, pdevc, pgs); } else { gx_device *dev = pgs->device->info; fixed h, w, tf; #define fswap(a, b) tf = a, a = b, b = tf if ( color_is_pure(pdevc) && (*dev->procs->draw_line)(dev, ix, iy, itox, itoy, pdevc->color1) >= 0 ) return 0; h = itoyf - iyf; w = itoxf - ixf; #define fixed_eps (fixed)1 if ( (w < 0 ? -w : w) <= (h < 0 ? -h : h) ) { if ( h < 0 ) fswap(ixf, itoxf), fswap(iyf, itoyf), h = -h; gz_fill_trapezoid_fixed(ixf, fixed_eps, iyf, itoxf, fixed_eps, h, 0, pdevc, pgs); } else { if ( w < 0 ) fswap(ixf, itoxf), fswap(iyf, itoyf), w = -w; gz_fill_trapezoid_fixed(iyf, fixed_eps, ixf, itoyf, fixed_eps, w, 1, pdevc, pgs); } #undef fixed_eps #undef fswap } return 0; }
ibMtx4& ibMtx4::Transpose() { fswap( data.mtx._01, data.mtx._10 ); fswap( data.mtx._02, data.mtx._20 ); fswap( data.mtx._03, data.mtx._30 ); fswap( data.mtx._12, data.mtx._21 ); fswap( data.mtx._13, data.mtx._31 ); fswap( data.mtx._23, data.mtx._32 ); return *this; }
/* void test() { struct Data data1, data2; data1.x = 1, data1.y = -7; data2.x = 0, data2.y = 12; int fd = open("thith.test", O_RDWR, 0600); if (writestruct(fd, &data1) == -1) printf("shit happens\n"); if (writestruct(fd, &data2) == -1) printf("shit happens\n"); close(fd); } */ int main(int argc, char **argv) { // test(); ///* int fd = open(argv[1], O_RDWR); int32_t a = strtol(argv[2], NULL, 10); struct Data data; ssize_t cur = 0; size_t size = 0; // calculate number of strictires in file for (; cur != -1; size++) cur = readstruct(fd, &data); size--; if (size == 0) return 0; for (size_t i = 0; i * 2 < size; i++) fswap(fd, i, size - i - 1, a); close(fd); return 0; //*/ }
static void fallbackQSort3 ( UInt32* fmap, UInt32* eclass, Int32 loSt, Int32 hiSt ) { Int32 unLo, unHi, ltLo, gtHi, n, m; Int32 sp, lo, hi; UInt32 med, r, r3; Int32 stackLo[FALLBACK_QSORT_STACK_SIZE]; Int32 stackHi[FALLBACK_QSORT_STACK_SIZE]; r = 0; sp = 0; fpush ( loSt, hiSt ); while (sp > 0) { AssertH ( sp < FALLBACK_QSORT_STACK_SIZE, 1004 ); fpop ( lo, hi ); if (hi - lo < FALLBACK_QSORT_SMALL_THRESH) { fallbackSimpleSort ( fmap, eclass, lo, hi ); continue; } /* Random partitioning. Median of 3 sometimes fails to avoid bad cases. Median of 9 seems to help but looks rather expensive. This too seems to work but is cheaper. Guidance for the magic constants 7621 and 32768 is taken from Sedgewick's algorithms book, chapter 35. */ r = ((r * 7621) + 1) % 32768; r3 = r % 3; if (r3 == 0) med = eclass[fmap[lo]]; else if (r3 == 1) med = eclass[fmap[(lo+hi)>>1]]; else med = eclass[fmap[hi]]; unLo = ltLo = lo; unHi = gtHi = hi; while (1) { while (1) { if (unLo > unHi) break; n = (Int32)eclass[fmap[unLo]] - (Int32)med; if (n == 0) { fswap(fmap[unLo], fmap[ltLo]); ltLo++; unLo++; continue; }; if (n > 0) break; unLo++; } while (1) { if (unLo > unHi) break; n = (Int32)eclass[fmap[unHi]] - (Int32)med; if (n == 0) { fswap(fmap[unHi], fmap[gtHi]); gtHi--; unHi--; continue; }; if (n < 0) break; unHi--; } if (unLo > unHi) break; fswap(fmap[unLo], fmap[unHi]); unLo++; unHi--; } AssertD ( unHi == unLo-1, "fallbackQSort3(2)" ); if (gtHi < ltLo) continue; n = fmin(ltLo-lo, unLo-ltLo); fvswap(lo, unLo-n, n); m = fmin(hi-gtHi, gtHi-unHi); fvswap(unLo, hi-m+1, m); n = lo + unLo - ltLo - 1; m = hi - (gtHi - unHi) + 1; if (n - lo > hi - m) { fpush ( lo, n ); fpush ( m, hi ); } else { fpush ( m, hi ); fpush ( lo, n ); } }
int fft(double *real_data, double *imag_data, int n_pts, int nu, int inv) { int n2, i, ib, mm, k; int sgn, tstep; double tr, ti, arg; /* intermediate values in calcs. */ double c, s; /* cosine & sine components of Fourier trans. */ static double *sintab = NULL; static int last_n = 0; n2 = n_pts / 2; if (n_pts != last_n) { /* allocate new sin table */ arg = 2*M_PI/n_pts; last_n = 0; sintab = xrealloc(sintab, n_pts*SIZEOF_DOUBLE); if (sintab == NULL) { return RETURN_FAILURE; } for (i = 0; i < n_pts; i++) { sintab[i] = sin(arg*i); } last_n = n_pts; } /* * sign change for inverse transform */ sgn = inv ? 1:-1; /* do bit reversal of data in advance */ for (k = 0; k != n_pts; k++) { ib = bit_swap(k, nu); if (ib > k) { fswap((real_data + k), (real_data + ib)); fswap((imag_data + k), (imag_data + ib)); } } /* * Calculate the componets of the Fourier series of the function */ tstep = n2; for (mm = 1; mm < n_pts; mm *= 2) { int sinidx = 0, cosidx = n_pts/4; for (k=0; k<mm; k++) { c = sintab[cosidx]; s = sgn*sintab[sinidx]; sinidx += tstep; cosidx += tstep; if (sinidx >= n_pts) { sinidx -= n_pts; } if (cosidx >= n_pts) { cosidx -= n_pts; } for (i = k; i < n_pts; i += mm*2) { double re1, re2, im1, im2; re1 = real_data[i]; im1 = imag_data[i]; re2 = real_data[i + mm]; im2 = imag_data[i + mm]; tr = re2*c + im2*s; ti = im2*c - re2*s; real_data[i+mm] = re1 - tr; imag_data[i+mm] = im1 - ti; real_data[i] = re1 + tr; imag_data[i] = im1 + ti; } } tstep /= 2; } return RETURN_SUCCESS; }
void inverty(int gno) { fswap(&g[gno].v.yv1, &g[gno].v.yv2); }
void invertx(int gno) { fswap(&g[gno].v.xv1, &g[gno].v.xv2); }
void flipxy(int gno) { int i, j; tickmarks t; double *x, *y; for (i = 0; i < MAXAXES; i += 2) { memcpy(&t, &g[gno].t[i], sizeof(tickmarks)); memcpy(&g[gno].t[i], &g[gno].t[i + 1], sizeof(tickmarks)); memcpy(&g[gno].t[i + 1], &t, sizeof(tickmarks)); if (g[gno].t[i].t_op == RIGHT) { g[gno].t[i].t_op = TOP; } else if (g[gno].t[i].t_op == LEFT) { g[gno].t[i].t_op = BOTTOM; } if (g[gno].t[i].tl_op == RIGHT) { g[gno].t[i].tl_op = TOP; } else if (g[gno].t[i].tl_op == LEFT) { g[gno].t[i].tl_op = BOTTOM; } if (g[gno].t[i + 1].t_op == TOP) { g[gno].t[i + 1].t_op = RIGHT; } else if (g[gno].t[i + 1].t_op == BOTTOM) { g[gno].t[i + 1].t_op = LEFT; } if (g[gno].t[i + 1].tl_op == TOP) { g[gno].t[i + 1].tl_op = RIGHT; } else if (g[gno].t[i + 1].tl_op == BOTTOM) { g[gno].t[i + 1].tl_op = LEFT; } } if (g[gno].type == LOGX) { g[gno].type = LOGY; } else if (g[gno].type == LOGY) { g[gno].type = LOGX; } fswap(&g[gno].w.xg1, &g[gno].w.yg1); fswap(&g[gno].w.xg2, &g[gno].w.yg2); fswap(&g[gno].dsx, &g[gno].dsy); iswap(&g[gno].fx, &g[gno].fy); iswap(&g[gno].px, &g[gno].py); for (i = 0; i < g[gno].maxplot; i++) { if (isactive(gno, i)) { x = getx(gno, i); /* TODO really need to just swap pointers */ y = gety(gno, i); for (j = 0; j < getsetlength(gno, i); j++) { fswap(&x[j], &y[j]); } updatesetminmax(gno, i); } } update_all(gno); }
void fft(double *real_data, double *imag_data, int n_pts, int nu, int inv) { int n2, i, ib ,mm, k; int sgn, tstep; double tr, ti, arg; /* intermediate values in calcs. */ double c, s; /* cosine & sine components of Fourier trans. */ static double *sintab = NULL; static int last_n = 0; n2 = n_pts / 2; if(n_pts==0) { if(sintab) XCFREE(sintab); sintab=NULL; last_n=0; return; /* just deallocate memory if called with zero points */ } else if (n_pts != last_n) { /* allocate new sin table */ arg=2*M_PI/n_pts; last_n=0; if(sintab) XCFREE(sintab); sintab=(double *) xcalloc(n_pts,sizeof(double)); if(sintab == NULL) return; /* out of memory! */ for(i=0; i<n_pts; i++) sintab[i] = sin(arg*i); last_n=n_pts; } /* * sign change for inverse transform */ sgn = inv ? -1 : 1; /* do bit reversal of data in advance */ for (k = 0; k != n_pts; k++) { ib = bit_swap(k, nu); if (ib > k) { fswap((real_data + k), (real_data + ib)); fswap((imag_data + k), (imag_data + ib)); } } /* * Calculate the componets of the Fourier series of the function */ tstep=n2; for (mm = 1; mm < n_pts; mm*=2) { int sinidx=0, cosidx=n_pts/4; for(k=0; k<mm; k++) { c = sintab[cosidx]; s = sgn * sintab[sinidx]; sinidx += tstep; cosidx += tstep; if(sinidx >= n_pts) sinidx -= n_pts; if(cosidx >= n_pts) cosidx -= n_pts; for (i = k; i < n_pts; i+=mm*2) { double re1, re2, im1, im2; re1=real_data[i]; re2=real_data[i+mm]; im1=imag_data[i]; im2=imag_data[i+mm]; tr = re2 * c + im2 * s; ti = im2 * c - re2 * s; real_data[i+mm] = re1 - tr; imag_data[i+mm] = im1 - ti; real_data[i] = re1 + tr; imag_data[i] = im1 + ti; } } tstep /= 2; } /* * If calculating the inverse transform, must divide the data by the number of * data points. */ if (inv) { double fac = 1.0 / n_pts; for (k = 0; k != n_pts; k++) { *(real_data + k) *= fac; *(imag_data + k) *= fac; } } }