QList<double> QuickSort::Sort(){ curList = getSequence(); if ( curList.count() > 0 ){ sortIt(0,curList.count()-1); } return curList; }
void QuickSort::sortIt(int left, int right){ int i,j; double pivot; i = (left + right) / 2; pivot = curList[i]; curList[i] = curList[right]; for(j = i = left; i < right; i++){ if(curList[i] < pivot){ curList.swap(i, j); j++; } } curList[right] = curList[j]; curList[j] = pivot; if(left < j - 1) sortIt(left, j - 1); if(j + 1 < right) sortIt(j + 1, right); }
void Radixsort(union Int32 *a, int n) { int npos, nneg, i, c; union Int32* pos = (union Int32*)malloc(n * sizeof(union Int32)); union Int32* neg = (union Int32*)malloc(n * sizeof(union Int32)); npos = 0; nneg = 0; for (i = 0; i < n; i++) { if (a[i].x >= 0) { pos[npos] = a[i]; npos++; } else { neg[nneg].x = (-1)*a[i].x; nneg++; } } sortIt(pos,npos); sortIt(neg, nneg); c = 0; for (i = nneg - 1; i >=0; i--) { a[c].x = (-1)*neg[i].x; c++; } for (i = 0; i < npos; i++) { a[c] = pos[i]; c++; } free(pos); free(neg); }
bool providesSort(const CanonicalQuery& query, const BSONObj& kp) { BSONObjIterator sortIt(query.getParsed().getSort()); BSONObjIterator kpIt(kp); while (sortIt.more() && kpIt.more()) { // We want the field name to be the same as well (so we pass true). // TODO: see if we can pull a reverse sort out... if (0 != sortIt.next().woCompare(kpIt.next(), true)) { return false; } } // every elt in sort matched kp return !sortIt.more(); }
void Table<V,A>::setup() const { if (isReady) return; if (v.size() <= 1) throw TableError("Trying to use a null Table (need at least 2 entries)"); sortIt(); lastIndex = 1; // Start back at the beginning for the next search. // See if arguments are equally spaced // ...within this fractional error: const double tolerance = 0.01; dx = (v.back().arg - v.front().arg) / (v.size()-1); if (dx == 0.) throw TableError("First and last Table entry are equal."); equalSpaced = true; for (int i=1; i<int(v.size()); i++) { if ( std::abs( ((v[i].arg-v[0].arg)/dx - i)) > tolerance) equalSpaced = false; if (v[i].arg == v[i-1].arg) throw TableError("Table has repeated arguments."); } switch (iType) { case linear: interpolate = &Table<V,A>::linearInterpolate; break; case spline : setupSpline(); interpolate = &Table<V,A>::splineInterpolate; break; case floor: interpolate = &Table<V,A>::floorInterpolate; break; case ceil: interpolate = &Table<V,A>::ceilInterpolate; break; default: throw TableError("interpolation method not yet implemented"); } isReady = true; }
Status LiteParsedQuery::init(const string& ns, int ntoskip, int ntoreturn, int queryOptions, const BSONObj& queryObj, const BSONObj& proj, bool fromQueryMessage) { _ns = ns; _ntoskip = ntoskip; _ntoreturn = ntoreturn; _options = queryOptions; _proj = proj.getOwned(); if (_ntoskip < 0) { return Status(ErrorCodes::BadValue, "bad skip value in query"); } if (_ntoreturn == std::numeric_limits<int>::min()) { // _ntoreturn is negative but can't be negated. return Status(ErrorCodes::BadValue, "bad limit value in query"); } if (_ntoreturn < 0) { // _ntoreturn greater than zero is simply a hint on how many objects to send back per // "cursor batch". A negative number indicates a hard limit. _wantMore = false; _ntoreturn = -_ntoreturn; } if (fromQueryMessage) { BSONElement queryField = queryObj["query"]; if (!queryField.isABSONObj()) { queryField = queryObj["$query"]; } if (queryField.isABSONObj()) { _filter = queryField.embeddedObject().getOwned(); Status status = initFullQuery(queryObj); if (!status.isOK()) { return status; } } else { // TODO: Does this ever happen? _filter = queryObj.getOwned(); } } else { // This is the debugging code path. _filter = queryObj.getOwned(); } _hasReadPref = queryObj.hasField("$readPreference"); if (!_sort.isEmpty()) { if (!isValidSortOrder(_sort)) { return Status(ErrorCodes::BadValue, "bad sort specification"); } _sort = normalizeSortOrder(_sort); } // Min and Max objects must have the same fields. if (!_min.isEmpty() && !_max.isEmpty()) { if (!_min.isFieldNamePrefixOf(_max) || (_min.nFields() != _max.nFields())) { return Status(ErrorCodes::BadValue, "min and max must have the same field names"); } } // Can't combine a normal sort and a $meta projection on the same field. BSONObjIterator projIt(_proj); while (projIt.more()) { BSONElement projElt = projIt.next(); if (isTextScoreMeta(projElt)) { BSONElement sortElt = _sort[projElt.fieldName()]; if (!sortElt.eoo() && !isTextScoreMeta(sortElt)) { return Status(ErrorCodes::BadValue, "can't have a non-$meta sort on a $meta projection"); } } } // All fields with a $meta sort must have a corresponding $meta projection. BSONObjIterator sortIt(_sort); while (sortIt.more()) { BSONElement sortElt = sortIt.next(); if (isTextScoreMeta(sortElt)) { BSONElement projElt = _proj[sortElt.fieldName()]; if (projElt.eoo() || !isTextScoreMeta(projElt)) { return Status(ErrorCodes::BadValue, "must have $meta projection for all $meta sort keys"); } } } return Status::OK(); }
void landgen(void) { int level = 5, i; static int arraySize = pow(2.0, MAXLEVEL + 1); if (landGenFirst) { ptptr = 0; landGenFirst = 0; landpt = new Vector3[arraySize]; srand(time(NULL)); ptptr = 0; mountain(MAXLEVEL - 1, 0.0, 0.0, INITHEIGHT, LANDW, LANDL, 0.0); mountain(MAXLEVEL - 1, 0.0, 0.0, INITHEIGHT, 0.0, LANDL, 0.0); mountain(MAXLEVEL - 1, 0.0, 0.0, INITHEIGHT, -1. * LANDW, LANDL, 0.0); mountain(MAXLEVEL - 1, 0.0, 0.0, INITHEIGHT, -1. * LANDW, 0.0, 0.0); mountain(MAXLEVEL - 1, 0.0, 0.0, INITHEIGHT, -1. * LANDW, -1. * LANDL, 0.0); mountain(MAXLEVEL - 1, 0.0, 0.0, INITHEIGHT, 0.0, -1. * LANDL, 0.0); mountain(MAXLEVEL - 1, 0.0, 0.0, INITHEIGHT, LANDW, -1. * LANDL, 0.0); mountain(MAXLEVEL - 1, 0.0, 0.0, INITHEIGHT, LANDW, 0.0, 0.0); sortIt(arraySize); } for (i = 0; i<arraySize / 8 * 7; i++) { if ((i + 1) % (arraySize / 8) == 0) continue; glBegin(GL_TRIANGLE_STRIP); glVertex3f(landpt[i].x, landpt[i].y, landpt[i].z); glVertex3f(landpt[i + arraySize / 8].x, landpt[i + arraySize / 8].y, landpt[i + arraySize / 8].z); glVertex3f(landpt[i + 1].x, landpt[i + 1].y, landpt[i + 1].z); glVertex3f(landpt[i + arraySize / 8 + 1].x, landpt[i + arraySize / 8 + 1].y, landpt[i + arraySize / 8 + 1].z); glEnd(); } for (i = arraySize / 8 * 7; i<arraySize - 1; i++) { glBegin(GL_TRIANGLE_STRIP); glVertex3f(landpt[i].x, landpt[i].y, landpt[i].z); glVertex3f(landpt[i - arraySize / 8 * 7].x, landpt[i - arraySize / 8 * 7].y, landpt[i - arraySize / 8 * 7].z); glVertex3f(landpt[i + 1].x, landpt[i + 1].y, landpt[i + 1].z); glVertex3f(landpt[i - arraySize / 8 * 7 + 1].x, landpt[i - arraySize / 8 * 7 + 1].y, landpt[i - arraySize / 8 * 7 + 1].z); glEnd(); } glBegin(GL_TRIANGLE_FAN); glVertex3f(0.0, 0.0, INITHEIGHT); glVertex3f(landpt[0].x, landpt[0].y, landpt[0].z); glVertex3f(landpt[arraySize / 8].x, landpt[arraySize / 8].y, landpt[arraySize / 8].z); glVertex3f(landpt[arraySize / 4].x, landpt[arraySize / 4].y, landpt[arraySize / 4].z); glVertex3f(landpt[arraySize / 8 * 3].x, landpt[arraySize / 8 * 3].y, landpt[arraySize / 8 * 3].z); glVertex3f(landpt[arraySize / 2].x, landpt[arraySize / 2].y, landpt[arraySize / 2].z); glVertex3f(landpt[arraySize / 8 * 5].x, landpt[arraySize / 8 * 5].y, landpt[arraySize / 8 * 5].z); glVertex3f(landpt[arraySize / 4 * 3].x, landpt[arraySize / 4 * 3].y, landpt[arraySize / 4 * 3].z); glVertex3f(landpt[arraySize / 8 * 7].x, landpt[arraySize / 8 * 7].y, landpt[arraySize / 8 * 7].z); glVertex3f(landpt[0].x, landpt[0].y, landpt[0].z); glEnd(); }