/* build Sector's LineDefs into sorted polygon(s) */ Bool BuildPolygons( BCINT lines[], BCINT sector, BCINT *end) { BCINT m, l; /* build list of all LineDefs that make up the Sector */ for (m = 0; m < NumLineDefs; m++) { l = SideDefs[ LineDefs[ m].sidedef1].sector; if (l != sector && LineDefs[ m].sidedef2 != -1) l = SideDefs[ LineDefs[ m].sidedef2].sector; if (l == sector) { if (*end < MAXLNS) /* safety check */ lines[(*end)++] = m + 1; /* allow LineDef 0's orientation to be */ else /* reversed too (eg. E3M6 sector 4) */ return FALSE; } } /* sort list for consecutive LineDefs on outer and any inner polygons (eg. E2M1 sector 3 has 4 inner polygons); negating "reverse" LineDefs */ l = 0; while (l < *end) { m = FindStartLine( lines, l, *end); /* put it at front */ if (l != m) exch( lines[l], lines[m]); m = SortPolygon( lines, l, end); l = m + 1; } return TRUE; }
void sort(int* l, size_t len) { int i, j, min, max; min = l[0]; max = min; for (i = 1; i < len; i++) { if (min != l[i]) { if (min > l[i]) { min = l[i]; } else if (max < l[i]) { max = l[i]; } break; } } i = -1; j = len; while (1) { while (l[++i] == min) { if (i == len - 1) break; } while (l[--j] == max) { if (j == 0) break; } if (i >= j) break; exch(l, i, j); } }
void fixDown(int *arr, int f, int t) { int i, j, flag; while (3*f-1 <= t) { // the children's index of f are 3f-1, 3f, 3f+1 i = flag = 3*f-1; for (j=2; j>0; j--) { if (i+1<=t) { if (arr[flag]>arr[i+1]) { flag = i+1; } i += 1; } } if (arr[f] <= arr[flag]) { break; } exch(arr[f], arr[flag]); f = flag; } return; }
void TLevelWriterMov::save(const TImageP &img, int frameIndex) { TRasterImageP ri(img); if (!img) throw TImageException(getFilePath(), "Unsupported image type"); TRasterP ras(ri->getRaster()); int lx = ras->getLx(), ly = ras->getLy(), pixSize = ras->getPixelSize(); if (pixSize != 4) throw TImageException(getFilePath(), "Unsupported pixel type"); int size = lx * ly * pixSize; // Send messages QLocalSocket socket; tipc::startSlaveConnection(&socket, t32bitsrv::srvName(), -1, t32bitsrv::srvCmdline()); tipc::Stream stream(&socket); tipc::Message msg; // Send the write message. stream << (msg << QString("$LWMovImageWrite") << m_id << frameIndex << lx << ly); // Send the data through a shared memory segment { t32bitsrv::RasterExchanger<TPixel32> exch(ras); tipc::writeShMemBuffer(stream, msg << tipc::clr, size, &exch); } if (tipc::readMessage(stream, msg) != "ok") throw TImageException(getFilePath(), "Couln't save image"); }
void TLevelWriterMov::saveSoundTrack(TSoundTrack *st) { if (st == 0) return; // Prepare connection QLocalSocket socket; tipc::startSlaveConnection(&socket, t32bitsrv::srvName(), -1, t32bitsrv::srvCmdline()); unsigned int size = st->getSampleSize() * st->getSampleCount(); // Send the saveSoundTract command to the server tipc::Stream stream(&socket); tipc::Message msg; stream << (msg << QString("$LWMovSaveSoundTrack") << m_id << st->getSampleRate() << st->getBitPerSample() << st->getChannelCount() << st->getSampleCount() << st->getFormat().m_signedSample); t32bitsrv::BufferExchanger exch((UCHAR *)st->getRawData()); tipc::writeShMemBuffer(stream, msg << tipc::clr, size, &exch); QString res(tipc::readMessage(stream, msg)); assert(res == "ok"); }
static void transpose_path(gx_path *path) { segment *s = (segment *)path->first_subpath; exch(path->bbox.p.x, path->bbox.p.y); exch(path->bbox.q.x, path->bbox.q.y); for (; s; s = s->next) { if (s->type == s_curve) { curve_segment *c = (curve_segment *)s; exch(c->p1.x, c->p1.y); exch(c->p2.x, c->p2.y); } exch(s->pt.x, s->pt.y); } }
void sortprog(int *A, int l, int r) { int diff=0; int is_sorted = 0; #ifdef DEBUG printf("[odd-even before] "); for(int i=l; i<=r; i++)printf("%d ", A[i]); printf("\n"); #endif while(is_sorted!=3){ is_sorted |= (1<<diff); #pragma omp parallel for for(int i=l+diff; i<r; i+=2){ if(greater(A[i], A[i+1])){ exch(A[i], A[i+1]); is_sorted &= ~(1<<diff); } } #ifdef DEBUG printf("[odd-even after] "); for(int i=l; i<=r; i++)printf("%d ", A[i]); printf("\n"); #endif } }
int main() { int a[LEN] = ARRAY; int h = 1; while(h < LEN / FI) h = FI * h + 1; display(a); while(h >= 1) { for(int i = h; i < LEN; i++) { for(int j = i; j >= h; j -= h) { int flag = less(a[j], a[j-h]); if(flag < 0) { exch(a, j, j-h); } TIME++; } } h = h / FI; } printf("TIME: %d\n", TIME); }
void reverseArray(void *buf, size_t size, size_t total) { int i; for(i = 0; i < total / 2; i++) { exch(buf, i, total - i - 1, size); } }
void generate (int N) { int n, t, M; assert (N <= MAX); for (n = 0; n < N; ++n) { p[n] = n; c[n] = 0; } doit(N); for (n = 0; n < N; ) { if (c[n] < n) { exch(n % 2 ? 0 : c[n], N-1); c[n]++; n = 0; doit(N); } else c[n++] = 0; } }
/*! \brief partition function \param a : vector to be sorted \param l : first value to be considered \param r : last value to be considered */ int vpRobust::partition(vpColVector &a, int l, int r) { int i = l-1; int j = r; double v = a[(unsigned int)r]; for (;;) { while (a[(unsigned int)++i] < v) ; while (v < a[(unsigned int)--j]) if (j == l) break; if (i >= j) break; exch(a[(unsigned int)i], a[(unsigned int)j]); } exch(a[(unsigned int)i], a[(unsigned int)r]); return i; }
static void fix_up(Item a[], int k) { while (k > 1 && less(a[k/2], a[k])) { exch(a[k/2], a[k]); k /= 2; } }
Item getmax() { int max = 0; for (int j = 1; j < N; j++) if (pq[max] < pq[j]) max = j; exch (pq[max], pq[N-1]); return pq[--N]; }
Item pq_delmax() { assert(N > 0); exch(pq[1], pq[N]); fix_down(pq, 1, N - 1); return pq[N--]; }
void Heap::swim(int k) { while(k>1&&less(k/2,k)) { exch(k/2,k); k=k/2; } }
void fixUp(Item a[], int k) { while (k > 1 && less(a[k/2], a[k])) { exch(a[k], a[k/2]); k = k/2; } }
//restore the heap condition from the bottom up void fixUp(int k) { while (k>1 && (pq[k/2].freq > pq[k].freq)) //while there's more than 2 items in queue & the item is less than its child { exch(k, (k/2)); //send the values to exchange k = (k/2); //examine the parent next } }
fixDown(Item a[], int k, int N) { int j; while (2*k <= N) { j = 2*k; if (j < N && less(a[j], a[j+1])) j++; if (!less(a[k], a[j])) break; exch(a[k], a[j]); k = j; } }
void sink(int n){ while (2 * n <= N){ int j = 2 * n; if (j < N&&!less(j, j+1))j++;//选取两个子节点中较的的一个,换到上面成为父节点之后才能比下面两个子节点都大 if (!less(n, j))break; exch(n, j); n = j; } }
void heapsort(int *data, int N) { for(int k = N/2-1; k >= 0; k--) { sink(data, k, N); } while( N > 1 ) { exch(data, 0, N-1); sink(data, 0, --N); } }
static void fixDown(int k, double prty[]) { int j; while (2*k <= N) { j = 2*k; if (j < N && maior(j, j+1)) j++; if (!maior( k, j)) break; exch(k, j); k = j; } }
void selection(Item a[], int l, int r) { int i, j; for (i = l; i < r; i++) { int min = i; for ( j = i+1; j <= r; j++) if (less(a[j], a[min])) min = j; exch(a[i], a[min]); } }
Remotes unmap_owners( Mesh* old_mesh, Int ent_dim, LOs new_ents2old_ents, LOs old_ents2new_ents) { auto old_copies2old_owners = old_mesh->ask_dist(ent_dim); auto old_owners2old_copies = old_copies2old_owners.invert(); auto old_copies2new_owners = old_owners2old_copies.exch(old_ents2new_ents, 1); auto new_ents2new_owners = unmap(new_ents2old_ents, old_copies2new_owners, 1); auto old_own_ranks = old_mesh->ask_owners(ent_dim).ranks; auto new_own_ranks = unmap(new_ents2old_ents, old_own_ranks, 1); return Remotes(new_own_ranks, new_ents2new_owners); }
int Heap::delMax() { int max=hp[1]; exch(1,N--); hp[N+1]=NULL; hp.pop_back(); sink(1); return max; }
static void presort_flexible(unsigned int a[], int N) { int i; int j; int temp; int min; for(i = 0; i < N-double_presort_count; i+=double_presort_count) { insertion(&a[i], presort_count); insertion_reverse(&a[i+presort_count], presort_count); } /* do a selection sort for the next 4, if they exist */ temp = i+presort_count; if (N <= temp) temp = N; for(; i < temp-1; i++) { min = i; for(j = i+1; j < temp; j++) { if(less(a[j],a[min])) { min = j; } } exch(a[i], a[min]); } /* do a reverse sort for the remaining ones, if they exist */ for(i = temp; i < N-1; i++) { min = i; for(j = i+1; j < N; j++) { if(!less(a[j],a[min])) { min = j; } } exch(a[i], a[min]); } }
void MySort::Shuffle(int* a, int N) { int r=0; srand(time(NULL)); for (int i = 0; i < N; i++) { r=rand()%(i+1); exch(a,i,r); } }
void node_heap::sink(node *n) { node *w = largerChild(n); if (w == 0) return; if ((*w) < (*n)) { exch(n, w); sink(n); } else { return; } }
void node_heap::swim(node *n) { node *p = parentNode(n); if (p == 0) return; if ((*n) < (*p)) { exch(n, p); swim(n); } else { return; } }
int main() { int a[10], i, j; for (i = 0; i < 10; i++) scanf("%d", &a[i]); for (i = 0; i < 9; i++) for (j = 0; j < 9; j++) if (a[j] > a[j + 1]) exch(a, j, j + 1); for (i = 0; i < 10; i++) printf("%d ", a[i]); return 0; }
int MySort::partition(int* a, int lo,int hi) { int i=lo, j=hi+1; while (true) { while (less(a[++i], a[lo])) { if(i==hi) break; } while (less(a[lo], a[--j])) { if(j==lo) break; } if (i>=j) break; exch(a,i,j); } exch(a,lo,j); return j; }