示例#1
0
static inline void correct(int& x1, int& y1, int& x2, int& y2)
{
	if(x1>x2)
		iswap(x1, x2);
	if(y1>y2)
		iswap(y1, y2);
}
示例#2
0
文件: bz_sort.c 项目: Booley/nbis
/********************************************************
partition_dec()
Inputs a pivot element making comparisons and swaps with other elements in a list,
until pivot resides at its correct position in the list.
********************************************************/
static void partition_dec( struct cell v[], int *llen, int *rlen, int *ll, int *lr, int *rl, int *rr, int p, int l, int r )
{
#define iswap(a,b) { int itmp = (a); a = (b); b = itmp; }

*ll = l;
*rr = r;
while ( 1 ) {
	if ( l < p ) {
		if ( v[l].index < v[p].index ) {
			iswap( v[l].index, v[p].index )
			iswap( v[l].item,  v[p].item )
			p = l;
		} else {
			l++;
		}
	} else {
		if ( r > p ) {
			if ( v[r].index > v[p].index ) {
				iswap( v[r].index, v[p].index )
				iswap( v[r].item,  v[p].item )
				p = r;
				l++;
			} else {
				r--;
			}
		} else {
			*lr = p - 1;
			*rl = p + 1;
			*llen = *lr - *ll + 1;
			*rlen = *rr - *rl + 1;
			break;
		}
	}
}
}
示例#3
0
void 
GRectMapper::rotate(int count)
{
  int oldcode = code;
  switch (count & 0x3)
    {
    case 1:
      code ^= (code & SWAPXY) ? MIRRORY : MIRRORX;
      code ^= SWAPXY;
      break;
    case 2:
      code ^= (MIRRORX|MIRRORY);
      break;
    case 3:
      code ^= (code & SWAPXY) ? MIRRORX : MIRRORY;
      code ^= SWAPXY;
      break;
    }
  if ((oldcode ^ code) & SWAPXY)
    { 
      iswap(rectFrom.xmin, rectFrom.ymin);
      iswap(rectFrom.xmax, rectFrom.ymax);
      rw = rh = GRatio();
    }
}
示例#4
0
文件: quicksort.c 项目: faruzzy/misc
/* Partition a number to an array */
int partition(int a[], int num, int size)
{
    int l = 0, r = size;

    while(1) {

        for( ; r > num; r--) {
            if(a[r] < a[num]) {
                iswap(&a[r], &a[num]);
                num = r;
                break;
            }
        }

        for(; l < num; l++) {
            if(a[l] > a[num]) {
                iswap(&a[l], &a[num]);
                num = l;
                break;
            }
        }

        if(l == r)
            break;

    }

    return num;

}
示例#5
0
void 
GRectMapper::unmap(GRect &rect)
{
  unmap(rect.xmin, rect.ymin);
  unmap(rect.xmax, rect.ymax);
  if (rect.xmin >= rect.xmax)
    iswap(rect.xmin, rect.xmax);
  if (rect.ymin >= rect.ymax)
    iswap(rect.ymin, rect.ymax);
}
示例#6
0
void 
GRectMapper::set_input(const GRect &rect)
{
  if (rect.isempty())
    G_THROW( ERR_MSG("GRect.empty_rect1") );
  rectFrom = rect;
  if (code & SWAPXY)
  {
    iswap(rectFrom.xmin, rectFrom.ymin);
    iswap(rectFrom.xmax, rectFrom.ymax);
  }
  rw = rh = GRatio();
}
示例#7
0
void lupdcmp(double**a, int n, int *p)
{
	int i, j, k, pivot=0;
	double max = 0;

	/* start with identity permutation	*/
	for (i=0; i < n; i++)
		p[i] = i;

	for (k=0; k < n-1; k++)	 {
		max = 0;
		for (i = k; i < n; i++)	{
			if (fabs(a[i][k]) > max) {
				max = fabs(a[i][k]);
				pivot = i;
			}
		}	
		if (eq (max, 0))
			fatal ("singular matrix in lupdcmp\n");

		/* bring pivot element to position	*/
		iswap (&p[k], &p[pivot]);
		for (i=0; i < n; i++)
			dswap (&a[k][i], &a[pivot][i]);

		for (i=k+1; i < n; i++) {
			a[i][k] /= a[k][k];
			for (j=k+1; j < n; j++)
				a[i][j] -= a[i][k] * a[k][j];
		}
	}
}
示例#8
0
void HPRC4LikeCipher::Init(const uint8 *key, uint32 size)
{
    // align key to 32 bits
    std::vector<uint32> keycopy((size + sizeof(uint32) - 1) / sizeof(uint32));
    DEBUG(ASSERT(keycopy.size() * sizeof(uint32) >= size));
    // the last 3 bytes may be incomplete, null those before copying
    keycopy[keycopy.size() - 1] = 0;
    memcpy(&keycopy[0], key, size);

#if IS_BIG_ENDIAN
    for(uint32 i = 0; i < keycopy.size(); ++i)
        ToLittleEndian(keycopy[i]);
#endif

    MTRand mt;
    mt.seed((uint32*)&keycopy[0], keycopy.size());

    for(uint32 i = 0; i < 256; ++i)
        _sbox[i] = i | (mt.randInt() << 8); // lowest bit is always the exchange index, like in original RC4

    uint32 a = 0, b = 0;

    for(uint32 i = 0; i < std::max<uint32>(256, size); ++i)
    {
        b += key[a] + _sbox[i & 0xFF];
        iswap(_sbox[i & 0xFF], _sbox[b & 0xFF]);
        ++a;
        a %= size;
    }
}
示例#9
0
void siftDown(void **a, int start, int end) {
  // input:  end represents the limit of how far down the heap to sift.
  int root = start;
  int child, swapi;
  // (While the root has at least one child)
  while ( root * 2 + 1 <= end ) {
    child = root * 2 + 1; // (root*2 + 1 points to the left child)
    swapi = root; // (keeps track of child to swap with)
    // (check if root is smaller than left child)
    // if ( a[swapi] < a[child] ) 
    if ( compareXY(a[swapi], a[child]) < 0 ) 
      swapi = child;
    // (check if right child exists, and if it's bigger 
    // than what we're currently swapping with)
    // if ( child+1 <= end && a[swapi] < a[child+1] )
    if ( child+1 <= end && compareXY(a[swapi],a[child+1]) < 0 )
      swapi = child + 1;
    // (check if we need to swap at all)
    if ( swapi != root ) {
      // swap(a[root], a[swapi]);
      iswap(root, swapi, a);
      root = swapi; // (repeat to continue sifting down the child now)
    } else return;
  }
} // end siftDown
示例#10
0
文件: heap.c 项目: jesajx/experiments
int hp_pop(Heap* h) {
    assert(h->tail != 0);
    int val = h->v[0];
    iswap(&h->v[0], &h->v[h->tail-1]);
    --h->tail;
    hp_heapify_down(h, 0);
    return val;
}
示例#11
0
void qs(int* array, int n) {
    if (n > 1) {
        int pivot = array[n / 2];
        iswap(&array[n - 1], &array[n / 2]);
        int left = 0, right = n - 1;
        while (left < right) {
            if (array[left] <= pivot)
                ++left;
            else if (array[right - 1] > pivot)
                --right;
            else
                iswap(&array[left], &array[right - 1]);
        }
        iswap(&array[left], &array[n - 1]);
        qs(&array[0], left);
        qs(&array[left + 1], n - (left + 1));
    }
}
示例#12
0
文件: heap.c 项目: jesajx/experiments
void hp_heapify_up(Heap* h, size_t i) {
    if (i == 0) {
        return;
    }
    size_t j = hp_parent(i);
    if (h->v[i] < h->v[j]) {
        iswap(&h->v[i], &h->v[j]);
        hp_heapify_up(h, j);
    }
}
示例#13
0
void RC4Cipher::Init(const uint8 *key, uint32 size)
{
    uint8 a = 0, b = 0;
    for(uint32 i = 0; i < 256; ++i)
    {
        b += key[a] + _sbox[i];
        iswap(_sbox[i], _sbox[b]);
        ++a;
        a %= size;
    }
}
示例#14
0
void bond2inds(int N, int b, int *ind0, int *ind1){
    int factor = N*N;
    int level = b / factor;
    *ind0 = mod(b, factor);

    int x0, y0, x1, y1;
    ind2xy(N, *ind0, &x0, &y0);

    x1 = mod(x0 - 1*(level==0) + 1*(level==1), N);
    y1 = mod(y0 - 1*(level==2) + 1*(level==3), N);
    *ind1 = xy2ind(N, x1, y1);

    if (*ind0 > *ind1) iswap(*ind0, *ind1);
}
示例#15
0
文件: heap.c 项目: jesajx/experiments
static void hp_heapify_down(Heap* h, size_t i) {
    size_t l = hp_left(i),
        r = hp_right(i);
    if (l < h->tail) {
        int j = l;
        if (r < h->tail && h->v[r] < h->v[l]) {
            j = r;
        }
        if (h->v[j] < h->v[i]) {
            iswap(&h->v[j], &h->v[i]);
            hp_heapify_down(h, j);
        }
    }
}
示例#16
0
void RC4Cipher::Apply(uint8 *buf, uint32 size)
{
    uint8 x = _x, y = _y;
    uint8 *sbox = &_sbox[0];
    uint8 t;
    for(uint32 i = 0; i < size; ++i)
    {
        ++x;
        y += sbox[x];
        iswap(sbox[x], sbox[y]);
        t = sbox[x] + sbox[y];
        buf[i] ^= sbox[t];
    }
    _x = x;
    _y = y;
}
示例#17
0
void heapSort(void **a, int count) {
  // input:  an unordered array a of length count
  // first place a in max-heap order
  heapify(a, count);
  // in languages with zero-based arrays the children are 2*i+1 and 2*i+2
  int end = count-1; 
  while ( end > 0 ) {
    // (swap the root(maximum value) of the heap with 
    // the last element of the heap)
    // swap(a[end], a[0]);
    iswap(end, 0, a);
    // (decrease the size of the heap by one so that the 
    // previous max value will stay in its proper placement) 
    end = end - 1;
    // (put the heap back in max-heap order)
    siftDown(a, 0, end);
  }
} // end heapSort
示例#18
0
void 
GRectMapper::map(int &x, int &y)
{
  int mx = x;
  int my = y;
  // precalc
  if (! (rw.p && rh.p))
    precalc();
  // swap and mirror
  if (code & SWAPXY)
    iswap(mx,my);
  if (code & MIRRORX)
    mx = rectFrom.xmin + rectFrom.xmax - mx;
  if (code & MIRRORY)
    my = rectFrom.ymin + rectFrom.ymax - my;
  // scale and translate
  x = rectTo.xmin + (mx - rectFrom.xmin) * rw;
  y = rectTo.ymin + (my - rectFrom.ymin) * rh;
}
示例#19
0
void 
GRectMapper::unmap(int &x, int &y)
{
  // precalc 
  if (! (rw.p && rh.p))
    precalc();
  // scale and translate
  int mx = rectFrom.xmin + (x - rectTo.xmin) / rw;
  int my = rectFrom.ymin + (y - rectTo.ymin) / rh;
  //  mirror and swap
  if (code & MIRRORX)
    mx = rectFrom.xmin + rectFrom.xmax - mx;
  if (code & MIRRORY)
    my = rectFrom.ymin + rectFrom.ymax - my;
  if (code & SWAPXY)
    iswap(mx,my);
  x = mx;
  y = my;
}
示例#20
0
int inds2bond(int N, int ind0, int ind1){
    if (ind0 > ind1) iswap(ind0, ind1);

    int x0, y0, x1, y1;
    ind2xy(N, ind0, &x0, &y0);
    ind2xy(N, ind1, &x1, &y1);

    int level = -1;
    if (y0 == y1){
        if (x1-x0 == -1 || x1-x0 == N-1) level=0;
        if (x1-x0 ==  1 || x1-x0 == 1-N) level=1;
    } else {
        if (y1-y0 == -1 || y1-y0 == N-1) level=2;
        if (y1-y0 ==  1 || y1-y0 == 1-N) level=3;
    }
    if (level == -1)
        printf("Not a bond!\n");
    return ind0 + level*N*N;
}
示例#21
0
void iquicksort(int *s, int *ind, int left, int right){
	int i, j;
	int pivot;
	int middle;

	// Switch to Insertionsort if the array to be sorted is
	// small enough.

	if(left + LIMIT > right)
		iInsertionsort(s + left, ind + left, right - left + 1);
	else {

		// The pivot will be picked using the Median-of-Three strategy.

		middle = (left + right) / 2;
		if(s[middle] < s[left])
			iswap(s, ind, left, middle);
		if(s[right] < s[left])
			iswap(s, ind, right, left);
		if(s[right] < s[middle])
			iswap(s, ind, right, middle);

		// The pivot will now be placed in position right-1

		iswap(s, ind, right - 1, middle);

		pivot = s[right - 1];
		i = left;
		j = right - 1;

		for(;;) {
			while(s[++i] < pivot) {
			} // Don't modify ever!
			while(s[--j] > pivot) {
			} // Don't modify ever!

			if(i < j)
				iswap(s, ind, i, j);
			else
				break;
		}

		// Restore the pivot

		iswap(s, ind, right - 1, i);

		iquicksort(s, ind, left, i - 1);
		iquicksort(s, ind, i + 1, right);
	}
}
示例#22
0
void HPRC4LikeCipher::Apply(uint8 *buf, uint32 size)
{
    // remaining bytes from last round, to keep cipher consistent
    // this will probably invalidate proper memory alignment, though
    if(_rb)
    {
        if(_rb < size)
        {
            uint8 r = _rb;
            _DoRemainingBytes(buf, _rb);
            buf += r;
            size -= r;
        }
        else
        {
            _DoRemainingBytes(buf, (uint8)size);
            return;
        }
    }

    uint32 isize = size / 4;
    size -= (isize * 4); // remaining bytes

    uint32 *sbox = &_sbox[0];
    uint32 x = _x, y = _y;

    // process as much as possible as uint32 blocks
    // optimization: it seems that having x, y, t as uint32 and using & 0xFF everytime
    //               is more efficient then using uint8 and the fact that 0xFF overflows to 0x00
    if(isize)
    {
        uint32 *ibuf = (uint32*)buf;
        uint32 t;
        do
        {
            ++x;
            x &= 0xFF;
            y += sbox[x];
            y &= 0xFF;
            iswap(sbox[x], sbox[y]);
            t = sbox[x] + sbox[y];
            t &= 0xFF;
    #if IS_LITTLE_ENDIAN
            *ibuf++ ^= sbox[t];
    #else
            uint32 m = sbox[t];
            ToLittleEndian(m);
            *ibuf++ ^= m;
    #endif
            // leave lowest 8 bits intact (which are used for permutation), and mix the upper 24 bytes
            sbox[t] = (sbox[t] & 0xFF) | (0xFFFFFF00 & (sbox[t] ^ sbox[y] ^ sbox[x]));
        }
        while(--isize);

        buf = (uint8*)ibuf;
    }

    _x = (uint8)x;    
    _y = (uint8)y;

    // are there remaining bytes at the end of the buffer?
    if(size)
    {
        // we have to start a new round
        ++_x;
        _y += (uint8)sbox[_x];
        iswap(sbox[_x], sbox[_y]);
        _rb = sizeof(uint32); // after _DoRemainingBytes(), it will hold the amount of bytes to do in the next round
        _DoRemainingBytes(buf,(uint8)size);
    }
}
示例#23
0
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);
}
示例#24
0
void cut2S(int N, int M) {
	int I, J; // indices
	void *AI, *AJ; // array values
 Loop:
	if  ( M - N <= cut2SLimit ) { 
	  quicksort1(N, M);
	  return;
	}
	// Check for duplicates
        int sixth = (M - N + 1) / 6;
        int e1 = N  + sixth;
        int e5 = M - sixth;
        int e3 = (N+M) / 2; // The midpoint
        int e4 = e3 + sixth;
        int e2 = e3 - sixth;

        // Sort these elements using a 5-element sorting network
        void *ae1 = A[e1], *ae2 = A[e2], *ae3 = A[e3], *ae4 = A[e4], *ae5 = A[e5];
	void *t;
        // if (ae1 > ae2) { t = ae1; ae1 = ae2; ae2 = t; }
	if ( compareXY(ae1, ae2) > 0 ) { t = ae1; ae1 = ae2; ae2 = t; }
        if ( compareXY(ae4, ae5) > 0 ) { t = ae4; ae4 = ae5; ae5 = t; }
        if ( compareXY(ae1, ae3) > 0 ) { t = ae1; ae1 = ae3; ae3 = t; }
        if ( compareXY(ae2, ae3) > 0 ) { t = ae2; ae2 = ae3; ae3 = t; }
        if ( compareXY(ae1, ae4) > 0 ) { t = ae1; ae1 = ae4; ae4 = t; }
        if ( compareXY(ae3, ae4) > 0 ) { t = ae3; ae3 = ae4; ae4 = t; }
        if ( compareXY(ae2, ae5) > 0 ) { t = ae2; ae2 = ae5; ae5 = t; }
        if ( compareXY(ae2, ae3) > 0 ) { t = ae2; ae2 = ae3; ae3 = t; }
        if ( compareXY(ae4, ae5) > 0 ) { t = ae4; ae4 = ae5; ae5 = t; }

        A[e1] = ae1; A[e2] = ae2; A[e3] = ae3; A[e4] = ae4; A[e5] = ae5;

	// Fix end points
	if ( compareXY(ae1, A[N]) < 0 ) iswap(N, e1, A);
	if ( compareXY(A[M], ae5) < 0 ) iswap(M, e5, A);

	int duplicate = -1;
	// if ( ae1, ae5 ) { duplicate = e1; } else
	if ( compareXY(ae1, ae5) == 0 ) { duplicate = e1; } else
	if ( compareXY(ae1, ae4) == 0 ) { duplicate = e1; } else
	if ( compareXY(ae2, ae5) == 0 ) { duplicate = e2; } else
	if ( compareXY(ae1, ae3) == 0 ) { duplicate = e1; } else
	if ( compareXY(ae2, ae4) == 0 ) { duplicate = e2; } else
	if ( compareXY(ae3, ae5) == 0 ) { duplicate = e3; } else
	if ( compareXY(ae1, ae2) == 0 ) { duplicate = e1; } else
	if ( compareXY(ae2, ae3) == 0 ) { duplicate = e2; } else
	if ( compareXY(ae3, ae4) == 0 ) { duplicate = e3; } else
	if ( compareXY(ae4, ae5) == 0 ) { duplicate = e4; };
	
	if ( 0 <= duplicate ) {
	  void cut2S();
	  cut3duplicates(N, M, duplicate, cut2S, 0);
	  return;
	}

	register void *T = ae3; // pivot

	// initialize running indices
	I= N;
	J= M;
	// The left segment has elements < T
	// The right segment has elements >= T
	// /*
    Left:
	I = I + 1;
	AI = A[I];
	// if (AI < T) goto Left;
	if ( compareXY(AI, T) < 0 ) goto Left;
    Right:
	J = J - 1;
	AJ = A[J];
	// if ( T <= AJ ) goto Right;
	if ( compareXY(T, AJ) <= 0 ) goto Right;
	if ( I < J ) {
	    A[I] = AJ; A[J] = AI;
	    goto Left;
	}

	int left = I-N;
	int right = M-J;
	if ( left < right ) { // smallest one first
	    cut2S(N, J);
	    // cut2S(I, M);
	    N = I;
	    goto Loop;
	}
	cut2S(I, M);
	// cut2S(N, J);
	M = J;
	goto Loop;
    } // (*  OF cut2S; *) ... the brackets remind that this was Pascal code
示例#25
0
void cut2c(int N, int M, int depthLimit) {
  int L;
  Start:
  if ( depthLimit <= 0 ) {
    heapc(A, N, M);
    return;
  }
  L = M - N;
  if ( L < cut2Limit ) { 
    quicksort0c(N, M, depthLimit);
    return;
  }
  depthLimit--;
  
  // Check for duplicates
        int sixth = (M - N + 1) / 6;
        int e1 = N  + sixth;
        int e5 = M - sixth;
        int e3 = (N+M) / 2; // The midpoint
        int e4 = e3 + sixth;
        int e2 = e3 - sixth;

        // Sort these elements using a 5-element sorting network
        void *ae1 = A[e1], *ae2 = A[e2], *ae3 = A[e3], *ae4 = A[e4], *ae5 = A[e5];
	void *t;
        // if (ae1 > ae2) { t = ae1; ae1 = ae2; ae2 = t; }
	if ( compareXY(ae1, ae2) > 0 ) { t = ae1; ae1 = ae2; ae2 = t; }
        if ( compareXY(ae4, ae5) > 0 ) { t = ae4; ae4 = ae5; ae5 = t; }
        if ( compareXY(ae1, ae3) > 0 ) { t = ae1; ae1 = ae3; ae3 = t; }
        if ( compareXY(ae2, ae3) > 0 ) { t = ae2; ae2 = ae3; ae3 = t; }
        if ( compareXY(ae1, ae4) > 0 ) { t = ae1; ae1 = ae4; ae4 = t; }
        if ( compareXY(ae3, ae4) > 0 ) { t = ae3; ae3 = ae4; ae4 = t; }
        if ( compareXY(ae2, ae5) > 0 ) { t = ae2; ae2 = ae5; ae5 = t; }
        if ( compareXY(ae2, ae3) > 0 ) { t = ae2; ae2 = ae3; ae3 = t; }
        if ( compareXY(ae4, ae5) > 0 ) { t = ae4; ae4 = ae5; ae5 = t; }

        A[e1] = ae1; A[e2] = ae2; A[e3] = ae3; A[e4] = ae4; A[e5] = ae5;

	// Fix end points
	if ( compareXY(ae1, A[N]) < 0 ) iswap(N, e1, A);
	if ( compareXY(A[M], ae5) < 0 ) iswap(M, e5, A);

	int duplicate = -1;
	// if ( ae1, ae5 ) { duplicate = e1; } else
	if ( compareXY(ae1, ae5) == 0 ) { duplicate = e1; } else
	if ( compareXY(ae1, ae4) == 0 ) { duplicate = e1; } else
	if ( compareXY(ae2, ae5) == 0 ) { duplicate = e2; } else
	if ( compareXY(ae1, ae3) == 0 ) { duplicate = e1; } else
	if ( compareXY(ae2, ae4) == 0 ) { duplicate = e2; } else
	if ( compareXY(ae3, ae5) == 0 ) { duplicate = e3; } else
	if ( compareXY(ae1, ae2) == 0 ) { duplicate = e1; } else
	if ( compareXY(ae2, ae3) == 0 ) { duplicate = e2; } else
	if ( compareXY(ae3, ae4) == 0 ) { duplicate = e3; } else
	if ( compareXY(ae4, ae5) == 0 ) { duplicate = e4; };
	
	if ( 0 <= duplicate ) {
	  void cut2c();
	  cut3duplicates(N, M, duplicate, cut2c, depthLimit);
	  return;
	}

	register void *T = ae3; // pivot


	// test to play safe:
	// if ( T <= A[N] || A[M] < T ) {
	if ( compareXY(T, A[N]) <= 0 || compareXY(A[M], T) < 0 ) {
	  // give up because cannot find a good pivot
	  quicksort0c(N, M, depthLimit+1);
	  return;
	}

	register  int I, J; // indices
	register void *AI, *AJ; // array values


	// initialize running indices
	I= N;
	J= M;

	// The left segment has elements < T
	// The right segment has elements >= T
  Left:
	 I = I + 1;
	 AI = A[I];
	 // if (AI < T) goto Left;
	 if ( compareXY( AI,  T) < 0 ) goto Left;
  Right:
	 J = J - 1;
	 AJ = A[J];
	 // if ( T <= AJ ) goto Right;
	 if ( compareXY( T, AJ) <= 0 ) goto Right;
	 if ( I < J ) {
	   A[I] = AJ; A[J] = AI;
	   goto Left;
	 }
  	 if ( (I - N) < (M - J) ) { // smallest one first
	   cut2c(N, J, depthLimit);
	   N = I; 
	   goto Start;
	 }
	 cut2c(I, M, depthLimit);
	 M = J;
	 goto Start;
} // (*  OF cut2; *) ... the brackets reminds that this was Pascal code
示例#26
0
// Quicksort equipped with a defense against quadratic explosion;
// calling heapsort if depthlimit exhausted
void quicksort0c(int N, int M, int depthLimit) {
  // printf("Enter quicksort0c N: %d M: %d %d\n", N, M, depthLimit);
  while ( N < M ) {
    int L = M - N;
    if ( L <= 10 ) {
      insertionsort(N, M);
      return;
    }
    if ( depthLimit <= 0 ) {
      heapc(A, N, M);
      return;
    }
    depthLimit--;
    // 10 < L
    // grab median of 3 or 9 to get a good pivot
    int pn = N;
    int pm = M;
    int p0 = (pn+pm)/2;
    if ( 40 < L ) { // do median of 9
      int d = L/8;
      pn = med(A, pn, pn + d, pn + 2 * d, compareXY);
      p0 = med(A, p0 - d, p0, p0 + d, compareXY);
      pm = med(A, pm - 2 * d, pm - d, pm, compareXY);
      /* Activation of the check for duplicates gives a slow down of 
	 1/4% on uniform input.  If you suspect that duplicates
	 causing quadratic deterioration are not caught higher-up by cut3
	 you may want to experiment with this check::::
      if ( L < 100 ) { // check for duplicates
	int duplicate = -1;
	if ( compareXY(A[pn], A[pm]) == 0 ) { duplicate = pn; } else
	if ( compareXY(A[pn], A[p0]) == 0 ) { duplicate = pn; } else
	if ( compareXY(A[pm], A[p0]) == 0 ) { duplicate = pm; };
	if ( 0 < duplicate ) {
	  void quicksort0();
	  cut3duplicates(N, M, duplicate, quicksort0, depthLimit);
	  return;
	}
      }
      */
    }
    p0 = med(A, pn, p0, pm, compareXY); // p0 is index to 'best' pivot ...
    iswap(N, p0, A); // ... and is put in first position as required by quicksort0c

    register void *p = A[N]; // pivot
    register int i, j;
    i = N;
    j = M;
    register void *ai; void *aj;

    /* Split array A[N,M], N<M in two segments using pivot p; 
       construct a partition with A[N,i), A(i,M] and N <= i <= M, and
       N <= k <= i -> A[k] <= p  and  i < k <= M -> p < A[k];
       Allow the worse cases: N=i or i=M.
       Recurse on A[N,i) and A(i,M) (or in the reverse order).
       This code does NOT do swapping; instead it disposes 
       ai/aj in a hole created by setting aj/ai first.  
    */
    /* Start state:
	  |-------------------------------|
          N=i                           j=M
	  N = i < j = M
          N <= k < i -> A[k] <= p    
          N < j < k <= M -> p < A[k]
	  A[N] = p
          N < i -> p < A[i]
    */
    while ( i < j ) {
      /*
	  |-------o---------------[--------|
          N       i               j        M
	  N <= i < j <= M
          N <= k < i -> A[k] <= p    
          N < j < k <= M -> p < A[k]
	  A[N] <= p
          N < i -> p < A[i]
          p + A[N,i) + A(i,M] is a permutation of the input array
      */
      aj = A[j];
      while ( compareXY(p, aj) < 0 ) { 
        /*
	  |-------o---------------[--------|
          N       i               j        M
	  N = i < j <= M or N < i <= j <= M
          N <= k < i -> A[k] <= p    
          N < j <= k <= M -> p < A[k]
	  A[N] <= p
	  N < i -> p < A[i}
	  p + A[N,i) + A(i,M] is a permutation of the input array
          p < aj = A[j]
	*/
	j--; 
	aj = A[j]; 
      }
      /*
	  |-------o---------------[--------|
          N       i               j        M
	  N = i = j < M or N < i & = i-1 <= j <= M
          N <= k < i -> A[k] <= p    
          j < k <= M -> p < A[k]
	  A[N] <= p
	  N < i -> p < A[i}
	  p + A[N,i) + A(i,M] is a permutation of the input array
          aj = A[j] <= p
	*/
      if ( j <= i ) {
	/*
	  |-------o-----------------------|
          N       i                       M
	  N = i = j < M or N < i & = i-1 = j < M
          N <= k < i -> A[k] <= p    
          i < k <= M -> p < A[k]
	  A[N] <= p
	  p + A[N,i) + A(i,M] is a permutation of the input array
      */
	break;
      }
      // i < j 
      A[i] = aj; // fill hole !
      /*
	  |-------]---------------o--------|
          N       i               j        M
	  N <= i < j <= M
          N <= k <= i -> A[k] <= p    
          j < k <= M -> p < A[k]
	  A[N] <= p
	  p + A[N,j) + A(j,M] is a permutation of the input array
	  aj = A[j] <= p
      */
      i++; ai = A[i];
      while ( i < j && compareXY(ai, p) <= 0 ) {
      /*
	  |-------]---------------o--------|
          N       i               j        M
	  N < i < j <= M
          N <= k <= i -> A[k] <= p    
          j < k <= M -> p < A[k]
	  A[N] <= p
	  p + A[N,j) + A(j,M] is a permutation of the input array
	  aj = A[j] <= p
      */
	i++; ai = A[i]; 
      }
      if ( j <= i ) 
      /*
	  |----------------------o--------|
          N                     i=j       M
	  N < i = j <= M
          N <= k < i -> A[k] <= p    
          j < k <= M -> p < A[k]
	  A[N] <= p
	  p + A[N,j) + A(j,M] is a permutation of the input array
      */
	break;
      // i < j  & p < ai = A[i] 
      A[j] = ai;
      j--;
      /*
	  |--------o--------------[--------|
          N        i              j        M
	  N < i <= j <= M
          N <= k < i -> A[k] <= p    
          j < k <= M -> p < A[k]
	  A[N] <= p
          N < i -> p < ai = A[i]
	  p + A[N,i) + A(i,M] is a permutation of the input array
      */
    }
    A[i] = p;
    /*
	  |--------]----------------------|
          N        i                      M
	  N <= i <= M
          N <= k <= i -> A[k] <= p    
          i < k <= M -> p < A[k]
	  A[N] <= p
	  A[N,i] + A(i,M] is a permutation of the input array
    */
    // Recurse on the smallest one and iterate on the other one
    int ia = i-1; int ib = i+1; 
    if ( i-N < M-i ) { 
      if ( N < ia ) quicksort0c(N, ia, depthLimit);  
      N = ib; 
    } else { 
      if ( ib < M ) quicksort0c(ib, M, depthLimit);  
      M = ia; 
    }
  }
} // end of quicksort0c
示例#27
0
int set_project_data(ProjectUI *ui, Quark *q, void *caller)
{
    Project *pr = project_get_data(q);
    int retval = RETURN_SUCCESS;
    
    if (ui && pr) {
        GraceApp *gapp = gapp_from_quark(q);
        double jul;
    
        if (!caller || caller == ui->prec) {
            project_set_prec(q, GetSpinChoice(ui->prec));
        }
        if (!caller || caller == ui->description) {
            char *s = TextGetString(ui->description);
            project_set_description(q, s);
            xfree(s);
        }

        if (caller == ui->page_orient) {
            int wpp, hpp;
            int orientation = GetOptionChoice(ui->page_orient);
            project_get_page_dimensions(q, &wpp, &hpp);
            if ((orientation == PAGE_ORIENT_LANDSCAPE && wpp < hpp) ||
                (orientation == PAGE_ORIENT_PORTRAIT  && wpp > hpp)) {
                set_page_dimensions(gapp, hpp, wpp, TRUE);
            }
        }
        if (caller == ui->page_format) {
            int wpp, hpp;
            int orientation = GetOptionChoice(ui->page_orient);
            int format = GetOptionChoice(ui->page_format);
            GraceApp *gapp = gapp_from_quark(q);

            switch (format) {
            case PAGE_FORMAT_USLETTER:
                wpp = 792.0;
                hpp = 612.0;
                break;
            case PAGE_FORMAT_A4:
                wpp = 842.0;
                hpp = 595.0;
                break;
            default:
                return RETURN_SUCCESS;
            }
            
            if (orientation == PAGE_ORIENT_PORTRAIT) {
                iswap(&wpp, &hpp);
            }

            set_page_dimensions(gapp, wpp, hpp, TRUE);
        }
        
        if (!caller || caller == ui->page_x || caller == ui->page_y) {
            int page_units = GetOptionChoice(ui->page_size_unit);
            double factor, page_x, page_y;
            GraceApp *gapp = gapp_from_quark(q);

            if (xv_evalexpr(ui->page_x, &page_x) != RETURN_SUCCESS ||
                xv_evalexpr(ui->page_y, &page_y) != RETURN_SUCCESS) {
                errmsg("Invalid page dimension(s)");
                return RETURN_FAILURE;
            }

            switch (page_units) {
            case PAGE_UNITS_IN:
                factor = 72.0;
                break;
            case PAGE_UNITS_CM:
                factor = 72.0/CM_PER_INCH;
                break;
            default:
                factor = 1.0;
                break;
            }

            page_x *= factor;
            page_y *= factor;
            
            set_page_dimensions(gapp, (int) rint(page_x), (int) rint(page_y),
                TRUE);
        }

        if (!caller || caller == ui->bg_color) {
            pr->bgcolor = GetOptionChoice(ui->bg_color);
        }
        if (!caller || caller == ui->bg_fill) {
            pr->bgfill = GetToggleButtonState(ui->bg_fill);
        }

        if (!caller || caller == ui->fsize_scale) {
            pr->fscale = GetSpinChoice(ui->fsize_scale);
        }
        if (!caller || caller == ui->lwidth_scale) {
            pr->lscale = GetSpinChoice(ui->lwidth_scale);
        }

        if (!caller || caller == ui->refdate) {
            char *s = TextGetString(ui->refdate);
            if (parse_date_or_number(q, s, TRUE,
                    get_date_hint(gapp), &jul) == RETURN_SUCCESS) {
                pr->ref_date = jul;
            } else {
                errmsg("Invalid date");
                retval = RETURN_FAILURE;
            }
            xfree(s);
        }
        if (!caller || caller == ui->two_digits_years) {
            pr->two_digits_years = GetToggleButtonState(ui->two_digits_years);
        }
        if (!caller || caller == ui->wrap_year) {
            char *s = TextGetString(ui->wrap_year);
            pr->wrap_year = atoi(s);
            xfree(s);
        }

        quark_dirtystate_set(q, TRUE);
    }
    
    return retval;
}
示例#28
0
//Handle a command as received from GDB.
static int ATTR_GDBFN gdbHandleCommand(unsigned char *cmd, int len) {
	//Handle a command
	int i, j, k;
	unsigned char *data=cmd+1;
	if (cmd[0]=='g') {		//send all registers to gdb
		gdbPacketStart();
		gdbPacketHex(iswap(gdbstub_savedRegs.a0), 32);
		gdbPacketHex(iswap(gdbstub_savedRegs.a1), 32);
		for (i=2; i<16; i++) gdbPacketHex(iswap(gdbstub_savedRegs.a[i-2]), 32);
		gdbPacketHex(iswap(gdbstub_savedRegs.pc), 32);
		gdbPacketHex(iswap(gdbstub_savedRegs.sar), 32);
		gdbPacketHex(iswap(gdbstub_savedRegs.litbase), 32);
		gdbPacketHex(iswap(gdbstub_savedRegs.sr176), 32);
		gdbPacketHex(0, 32);
		gdbPacketHex(iswap(gdbstub_savedRegs.ps), 32);
		gdbPacketEnd();
	} else if (cmd[0]=='G') {	//receive content for all registers from gdb
		gdbstub_savedRegs.a0=iswap(gdbGetHexVal(&data, 32));
		gdbstub_savedRegs.a1=iswap(gdbGetHexVal(&data, 32));
		for (i=2; i<16; i++) gdbstub_savedRegs.a[i-2]=iswap(gdbGetHexVal(&data, 32));
		gdbstub_savedRegs.pc=iswap(gdbGetHexVal(&data, 32));
		gdbstub_savedRegs.sar=iswap(gdbGetHexVal(&data, 32));
		gdbstub_savedRegs.litbase=iswap(gdbGetHexVal(&data, 32));
		gdbstub_savedRegs.sr176=iswap(gdbGetHexVal(&data, 32));
		gdbGetHexVal(&data, 32);
		gdbstub_savedRegs.ps=iswap(gdbGetHexVal(&data, 32));
		gdbPacketStart();
		gdbPacketStr("OK");
		gdbPacketEnd();
	} else if (cmd[0]=='m') {	//read memory to gdb
		i=gdbGetHexVal(&data, -1);
		data++;
		j=gdbGetHexVal(&data, -1);
		gdbPacketStart();
		for (k=0; k<j; k++) {
			gdbPacketHex(readbyte(i++), 8);
		}
		gdbPacketEnd();
	} else if (cmd[0]=='M') {	//write memory from gdb
		i=gdbGetHexVal(&data, -1); //addr
		data++; //skip ,
		j=gdbGetHexVal(&data, -1); //length
		data++; //skip :
		if (validWrAddr(i) && validWrAddr(i+j)) {
			for (k=0; k<j; k++) {
				writeByte(i, gdbGetHexVal(&data, 8));
				i++;
			}
			//Make sure caches are up-to-date. Procedure according to Xtensa ISA document, ISYNC inst desc.
			asm volatile("ISYNC\nISYNC\n");
			gdbPacketStart();
			gdbPacketStr("OK");
			gdbPacketEnd();
		} else {