//O(h) //if node (n) has left child, then predecessor of n is the right most child of the left sub tree // Else, predecessor of n is the ancestor vertex v such that n is the decendent of right child of v int InOrderPredecessorBSTIter(struct node* root, struct node* node) { if(node == NULL || root == NULL) return -1; if(node->left != NULL) { return maxVal(node->left); } struct node* pre = NULL; while(root != NULL) { if(root->data < node->data) { pre = root; root = root->right; } else if(root->data > node->data) { root = root->left; } else break; } if(pre) return pre->data; else return -1; }
int QwtCounter::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { _id = QWidget::qt_metacall(_c, _id, _a); if (_id < 0) return _id; if (_c == QMetaObject::InvokeMetaMethod) { switch (_id) { case 0: buttonReleased((*reinterpret_cast< double(*)>(_a[1]))); break; case 1: valueChanged((*reinterpret_cast< double(*)>(_a[1]))); break; case 2: btnReleased(); break; case 3: btnClicked(); break; case 4: textChanged(); break; } _id -= 5; } #ifndef QT_NO_PROPERTIES else if (_c == QMetaObject::ReadProperty) { void *_v = _a[0]; switch (_id) { case 0: *reinterpret_cast< int*>(_v) = numButtons(); break; case 1: *reinterpret_cast< double*>(_v) = step(); break; case 2: *reinterpret_cast< double*>(_v) = minVal(); break; case 3: *reinterpret_cast< double*>(_v) = maxVal(); break; case 4: *reinterpret_cast< int*>(_v) = stepButton1(); break; case 5: *reinterpret_cast< int*>(_v) = stepButton2(); break; case 6: *reinterpret_cast< int*>(_v) = stepButton3(); break; case 7: *reinterpret_cast< double*>(_v) = value(); break; case 8: *reinterpret_cast< bool*>(_v) = editable(); break; } _id -= 9; } else if (_c == QMetaObject::WriteProperty) { void *_v = _a[0]; switch (_id) { case 0: setNumButtons(*reinterpret_cast< int*>(_v)); break; case 1: setStep(*reinterpret_cast< double*>(_v)); break; case 2: setMinValue(*reinterpret_cast< double*>(_v)); break; case 3: setMaxValue(*reinterpret_cast< double*>(_v)); break; case 4: setStepButton1(*reinterpret_cast< int*>(_v)); break; case 5: setStepButton2(*reinterpret_cast< int*>(_v)); break; case 6: setStepButton3(*reinterpret_cast< int*>(_v)); break; case 7: setValue(*reinterpret_cast< double*>(_v)); break; case 8: setEditable(*reinterpret_cast< bool*>(_v)); break; } _id -= 9; } else if (_c == QMetaObject::ResetProperty) { _id -= 9; } else if (_c == QMetaObject::QueryPropertyDesignable) { _id -= 9; } else if (_c == QMetaObject::QueryPropertyScriptable) { _id -= 9; } else if (_c == QMetaObject::QueryPropertyStored) { _id -= 9; } else if (_c == QMetaObject::QueryPropertyEditable) { _id -= 9; } else if (_c == QMetaObject::QueryPropertyUser) { _id -= 9; } #endif // QT_NO_PROPERTIES return _id; }
static Mat getSegmMask(const Mat& scores) { const int rows = scores.size[2]; const int cols = scores.size[3]; const int numClasses = scores.size[1]; Mat maxCl = Mat::zeros(rows, cols, CV_8UC1); Mat maxVal(rows, cols, CV_32FC1, Scalar(0)); for (int ch = 0; ch < numClasses; ch++) { for (int row = 0; row < rows; row++) { const float *ptrScore = scores.ptr<float>(0, ch, row); uint8_t *ptrMaxCl = maxCl.ptr<uint8_t>(row); float *ptrMaxVal = maxVal.ptr<float>(row); for (int col = 0; col < cols; col++) { if (ptrScore[col] > ptrMaxVal[col]) { ptrMaxVal[col] = ptrScore[col]; ptrMaxCl[col] = (uchar)ch; } } } } return maxCl; }
int maxPoints(vector<Point>& points) { unordered_map<float, int> m; int size = points.size(); if (size == 0) return 0; int res = 0; for (int i=0; i<size; i++) { m.clear(); int same = 0; int sameY = 0; const Point& pointI = points[i]; for (int j=i+1; j<size; j++) { const Point& pointJ = points[j]; if (pointI.x == pointJ.x) { if (pointI.y == pointJ.y) { same ++; } else { // a special case, equal y, but x is not equal, it can not use getK to compute K sameY ++; } } else { m[getK(points[i], points[j])] ++; } } res = max(res, max(sameY, maxVal(m)) + same + 1); } return res; }
// make a report on the data collected so far and print it to a stream void report(FILE *file) const { fprintf(file, "Statistics on: %s Num samples = %u SUM=%f\n", _name, samples(), sum()); if (_samples > 0) fprintf(file, "MAX=%f MIN=%f Mean=%f StdDev=%f\n", maxVal(), minVal(), mean(), stddev()); }
// make a report on the data collected so far and print it to a string buffer // The buffer must be allocated by the caller (256 bytes should be enough) void report(char *str) const { int l = sprintf(str, "Statistics on: %s Num samples = %u SUM=%f\n", _name, samples(), sum()); if (_samples > 0) sprintf(str+l, "MAX=%f MIN=%f Mean=%f StdDev=%f\n", maxVal(), minVal(), mean(), stddev()); }
LogSequence2::LogSequence2(unsigned int numbits, size_t capacity) : numbits(numbits), numentries(0), IsMapped(false) { maxval = maxVal(numbits); size_t totalSize = numElementsFor(numbits, capacity); if(totalSize==0) data.reserve(1); data.resize(totalSize); array = &data[0]; arraysize = totalSize; }
int getMaxAreaRect(int* hist, int* st, int n, int l, int r) { if (l > r) return INT_MIN; if (l == r) return hist[l]; int m = RMQ(hist, st, n, l, r); return maxVal(getMaxAreaRect(hist, st, n, l, m- 1), getMaxAreaRect(hist, st, n, m + 1, r), (r-l+1)*hist[m]); }
long constructSTUtil2(long *arr,long ss,long se,long *st,long si){ if(ss==se){ st[si]=arr[ss]; return arr[ss]; } long mid=getMid(ss,se); long a=constructSTUtil2(arr,ss,mid,st,si*2+1), b=constructSTUtil2(arr,mid+1,se,st,si*2+2); st[si]=maxVal(a,b); return st[si]; }
ll query(ll start,ll end,ll PN,ll pr_start,ll pr_end) { if(start>pr_end || end<pr_start) return -INF; else if(pr_start>=start && pr_end<=end) return ST[PN]; else { ll mid=(pr_start+pr_end)/2; ll LS=query(start,end,PN*2,pr_start,mid); ll RS=query(start,end,PN*2+1,mid+1,pr_end); return maxVal(LS,RS); } }
int constructSTUtil_m(int arr[], int ss, int se, int *st, int si) { if (ss == se) { st[si] = arr[ss]; return arr[ss]; } int mid = getMid(ss, se); st[si] = maxVal(constructSTUtil_m(arr, ss, mid, st, si*2+1), constructSTUtil_m(arr, mid+1, se, st, si*2+2)); return st[si]; }
void buildST(ll PN,ll pr_start,ll pr_end) { if(pr_start==pr_end) ST[PN]=A[pr_start]; else { ll mid=(pr_start+pr_end)/2; buildST(PN*2,pr_start,mid); buildST(PN*2+1,mid+1,pr_end); ST[PN]=maxVal(ST[PN*2],ST[PN*2+1]); } }
int RMQUtil_m(int *st, int ss, int se, int qs, int qe, int index) { if (qs <= ss && qe >= se) return st[index]; if (se < qs || ss > qe) return INT_MAX; int mid = getMid(ss, se); return maxVal(RMQUtil_m(st, ss, mid, qs, qe, 2*index+1), RMQUtil_m(st, mid+1, se, qs, qe, 2*index+2)); }
int KnapsackMulti(int w[],int v[], int n, int capacity){ int i,j,temp1, k, temp2; if(capacity== 0) return 0; for(i=1;i<=n;i++){ for(j=0;j<=capacity;j++){ if(w[i]>j){ result[i][j]=result[i-1][j]; } else{ temp1 = 0; for(k=1; k<=i;k++){ temp2 = getValue(i-1, j-w[k]); temp1 = maxVal(temp1,temp2+v[k]); } result[i][j] = maxVal(getValue(i-1,j),temp1); } } } for(i=0;i<=n;i++){ for(j=0;j<=capacity;j++){ printf("%2d ",result[i][j]); } printf("\n"); } }
long RMQUtil2(long *st,long ss,long se,long qs,long qe,long index){ if(qs<=ss && qe>=se){ return st[index]; } if(se<qs || ss>qe){ return LONG_MIN; } long mid=getMid(ss,se); long a= RMQUtil2(st,ss,mid,qs,qe,2*index+1), b=RMQUtil2(st,mid+1,se,qs,qe,2*index+2); return maxVal(a,b); }
int main() { inputAndStore(); ll M,x,y,l,r,i; ll sum=0; scanf("%lld%lld%lld",&M,&x,&y); buildST(1,0,N-1); l=minVal(x,y); r=maxVal(x,y); sum=query(l,r,1,0,N-1); //printf("%lld\n",sum); for(i=2;i<=M;i++) { x=(x+7)%(N-1); y=(y+11)%N; l=minVal(x,y); r=maxVal(x,y); sum+=query(l,r,1,0,N-1); } printf("%lld\n",sum); return 0; }
void Maillage::geometry(const glm::vec3 ¢er, const char* obj, glm::vec3 &min, glm::vec3 &max) { glm::vec3 minVal(1E100, 1E100, 1E100), maxVal(-1E100, -1E100, -1E100); FILE* f; fopen_s(&f, obj, "rt"); //= fopen(obj, "r"); while (!feof(f)) { char line[255]; fgets(line, 255, f); if (line[0]=='v' && line[1]==' ') { glm::vec3 vec; sscanf_s(line, "v %f %f %f\n", &vec[0], &vec[2], &vec[1]); vec[2] = -vec[2]; glm::vec3 p = vec + center; //std::cout << p.x << " " << p.y << " " << p.z << std::endl; geom.push_back(p); maxVal[0] = std::max(maxVal[0], p[0]); maxVal[1] = std::max(maxVal[1], p[1]); maxVal[2] = std::max(maxVal[2], p[2]); minVal[0] = std::min(minVal[0], p[0]); minVal[1] = std::min(minVal[1], p[1]); minVal[2] = std::min(minVal[2], p[2]); min = glm::vec3(minVal[0], minVal[1], minVal[2]); max = glm::vec3(maxVal[0], maxVal[1], maxVal[2]); } if (line[0]=='v' && line[1]=='n') { glm::vec3 vec; sscanf_s(line, "vn %f %f %f\n", &vec[0], &vec[2], &vec[1]); vec[2] = -vec[2]; normales.push_back(vec); } if (line[0]=='f') { int i0, i1, i2; int j0,j1,j2; int k0,k1,k2; sscanf_s(line, "f %u//%u %u//%u %u//%u\n", &i0, &k0, &i1, &k1, &i2, &k2 ); topo.push_back(i0-1); topo.push_back(i1-1); topo.push_back(i2-1); normalIds.push_back(k0-1); normalIds.push_back(k1-1); normalIds.push_back(k2-1); } } /*boundingSphere.C = 0.5*(minVal+maxVal); boundingSphere.R = sqrt((maxVal-minVal).sqrNorm())*0.5;*/ fclose(f); }
void LogSequence2::load(std::istream & input) { CRC8 crch; CRC32 crcd; unsigned char buf[9]; // Read type uint8_t type; crch.readData(input, (unsigned char*)&type, sizeof(type)); if(type!=TYPE_SEQLOG) { //throw "Trying to read a LOGArray but data is not LogArray"; } // Read numbits crch.readData(input, (unsigned char*)&numbits, sizeof(numbits)); // Read numentries uint64_t numentries64 = csd::VByte::decode(input); unsigned int pos = csd::VByte::encode(buf, numentries64); crch.update(buf, pos); // Validate Checksum Header crc8_t filecrch = crc8_read(input); if(crch.getValue()!=filecrch) { throw "Checksum error while reading LogSequence2 header."; } // Update local variables and validate maxval = maxVal(numbits); numentries = (size_t) numentries64; if(numbits>sizeof(size_t)*8 || numentries64>std::numeric_limits<size_t>::max()) { throw "This data structure is too big for this machine"; } // Calculate data size, reserve buffer. size_t numbytes = numBytesFor(numbits, numentries); data.resize(numElementsFor(numbits, numentries)); arraysize = data.size(); array = &data[0]; // Read data crcd.readData(input, (unsigned char*)&array[0], numbytes); // Validate checksum data crc32_t filecrcd = crc32_read(input); if(crcd.getValue()!=filecrcd) { throw "Checksum error while reading LogSequence2 Data"; } IsMapped = false; }
void visualize(size_t samplingRate = 44100) const { float max = std::fabs(maxVal()); for (size_t i = 0; i < signal.size() / 2; ++i) { std::cout << binFrequency(i, samplingRate) << " Hz: "; float amp = std::fabs(signal[i].real()); int visualAmp = static_cast<int>(70 * (amp / max)); for (int k = 0; k < visualAmp; ++k) { std::cout << "#"; } std::cout << std::endl; } }
void moveRight(){ _goRight = true; if (turnedLeft){ turnedLeft = false; turnCounter = 0; printf("Expected to move %d\n", maxVal(speedingUp[turnCounter], slowingDown[slowCounter])); slowCounter = 5; } else{ if (_jump){ printf("Expected to move 3\n"); } else{ if (slowCounter != 0){ printf("Expected to move %d\n", maxVal(speedingUp[turnCounter], slowingDown[slowCounter])); } else{ printf("Expected to move %d\n", speedingUp[turnCounter]); } } slowCounter = 0; } turnCounter = turnCounter < sizeof(speedingUp) ? turnCounter + 1 : turnCounter; }
Vector3<float> FluidSolver::GetMaxValue() const { Vector3<float> maxVal(0.0,0.0,0.0); for (int i = 0; i < mVoxels.GetDimX(); i++) { for (int j = 0; j < mVoxels.GetDimY(); j++) { for (int k = 0; k < mVoxels.GetDimZ(); k++) { if (maxVal.Norm() < mVelocityField.GetValue(i,j,k).Norm()) maxVal = mVelocityField.GetValue(i,j,k); } } } return maxVal; }
size_t LogSequence2::load(const unsigned char *ptr, const unsigned char *ptrMax, ProgressListener *listener) { size_t count = 0; // Read type CHECKPTR(&ptr[count], ptrMax, 1); if(ptr[count]!=TYPE_SEQLOG) { throw "Trying to read a LOGArray but data is not LogArray"; } count++; // Read numbits CHECKPTR(&ptr[count], ptrMax, 1); numbits = ptr[count++]; // Read numentries uint64_t numentries64; count += csd::VByte::decode(&ptr[count], ptrMax, &numentries64); // Validate Checksum Header CRC8 crch; crch.update(&ptr[0], count); CHECKPTR(&ptr[count], ptrMax, 1); if(crch.getValue()!=ptr[count++]) throw "Checksum error while reading LogSequence2 header."; // Update local variables and validate maxval = maxVal(numbits); numentries = (size_t) numentries64; if(numbits>sizeof(size_t)*8 || numentries64>std::numeric_limits<size_t>::max()) { throw "This data structure is too big for this machine"; } // Setup array of data arraysize = numBytesFor(numbits, numentries); array = (size_t *) &ptr[count]; count+=arraysize; IsMapped = true; if(&ptr[count]>=ptrMax) throw "LogSequence2 tries to read beyond the end of the file"; CHECKPTR(&ptr[count], ptrMax, 4); count+=4; // CRC of data return count; }
int InOrderPredecessorParentPtr(struct node* node) { if(node == NULL) return -1; if(node->left) return maxVal(node->left); struct node* p = node->parent; while(p!= NULL && node == p->left) { node = p; p = node->parent; } if(p) return p->data; else return -1; }
/* * Returns the minimum value from a tree. */ double minVal(double alpha, double beta, struct State *state, int depth) { int x; struct State newState; /* If we've reached the depth limit then evaluate this node and return its * value. */ if (depth <= 0) { return evaluateBoard(state); } --depth; /* descend one level in the tree */ FindLegalMoves(state); /* Walk the move list and find the best one. */ for (x=0; x<state->numLegalMoves; ++x) { /* Check to see if this thread has been cancelled before moving on. */ pthread_testcancel(); /* Copy the current board state. */ memcpy(&newState, state, sizeof(struct State)); /* Perform a move on the copied state. */ PerformMove(newState.board, newState.movelist[x], MoveLength(newState.movelist[x])); /* Toggle the current player. */ newState.player = (newState.player == 1) ? 2 : 1; /* Perform a depth limited MiniMax search for the best move. * Uses Alpha-Beta pruning. */ beta = MIN(beta, maxVal(alpha, beta, &newState, depth)); if (beta <= alpha) { return alpha; } } return beta; }
double minVal(State *state, int depth) { int x; double minv=100000.0; if(depth--<=0) return evalBoard1(state); FindLegalMoves(state); for(x=0;x<state->numLegalMoves;x++) { State newState; memcpy(&newState, state, sizeof(State)); PerformMove(newState.board, newState.movelist[x],MoveLength(newState.movelist[x])); newState.player = (newState.player==1) ? 2 : 1; minv = MIN(maxVal(&newState, depth),minv); } return minv; }
void Experiment2D22::scaling() { //scaling the [min,max] to the range [a,b] //f(x) = (b-a)(x-min) // ------------ + a // max - min int rows = pData.size(); int cols = pData[0].size(); vector<float> minVal(cols,INT_MAX); vector<float> maxVal(cols,INT_MIN ); float a, b; a = 0; b = 5; for (int i = 0;i < rows;i++) { for (int j = 0;j < cols;j++) { if (pData[i][j] > maxVal[j]) { maxVal[j] = pData[i][j]; } if (pData[i][j] < minVal[j]) { minVal[j] = pData[i][j]; } } } for (int i = 0;i < rows;i++) { vector<float> temp; for (int j = 0;j < cols;j++) { float t; t = ((b - a) * (pData[i][j] - minVal[j]) / (maxVal[j] - minVal[j])) + a; temp.push_back(t); } scaledData.push_back(temp); } }
void LogSequence2::reduceBits() { size_t max = 0; for(size_t i=0;i<numentries;i++) { size_t value = get(i); max = value>max ? value : max; } unsigned int newbits = bits(max); if(newbits<numbits) { // Go through elements, read one and write one. // Since the number of bits is smaller they don't overlap. for(size_t i=0;i<numentries;i++) { size_t value = get_field(&array[0], numbits, i); set_field(&array[0], newbits, i, value); } numbits = newbits; maxval = maxVal(numbits); size_t totalSize = numElementsFor(numbits, numentries); data.resize(totalSize); // Warning: It does not deallocate memory :( arraysize = data.size(); array = &data[0]; } }
int main(int argc, char* argv[]) { MaxValueSimple maxVal(argc, argv); if (maxVal.LEN < 1) return EXIT_FAILURE; maxVal.initialize(); /* TODO logging Logger.logInfo(CLAZZ, "Device type: " + maxVal.TYPE); Logger.logInfo(CLAZZ, "Vector size: " + maxVal.LEN); */ /* Erzeugen der Vektoren */ int* values = maxVal.prepareData(maxVal.LEN); size_t max_pos = maxVal.setTestValues(values, maxVal.LEN); /* TODO Logging Logger.logDebug(CLAZZ, "max_pos: " + max_pos + "; values[max_pos]: " + values[max_pos]); */ std::cout << "max_pos: " << max_pos << "; values[max_pos]: " << values[max_pos] << std::endl; /* Implementierung waehlen */ int max = maxVal.maxValue(values, maxVal.LEN); maxVal.finalize(); if (max > MaxValueSimple::MAX_FAILURE) { for (size_t i = 0; i < 5; i++) std::cout << "values[" << i << "]: " << values[i] << std::endl; return EXIT_SUCCESS; } else { // TODO Logger.logError(CLAZZ, "Error, no result!"); return EXIT_FAILURE; } }
MeshBoxes readObj(const glm::vec3 ¢er, const char* obj) { QVector<glm::vec3> vertices; QVector<glm::vec3> normals; QVector<int> faces; QVector<int> normalIds; glm::vec3 minVal(1E100, 1E100, 1E100), maxVal(-1E100, -1E100, -1E100); FILE* f = fopen(obj, "r"); while (!feof(f)) { char line[255]; fgets(line, 255, f); if (line[0]=='v' && line[1]==' ') { glm::vec3 vec; sscanf(line, "v %f %f %f\n", &(vec.x), &(vec.z), &(vec.y)); vec.z = -vec.z; glm::vec3 p = vec*50.f + center; vertices.push_back(p); maxVal[0] = std::max(maxVal[0], p[0]); maxVal[1] = std::max(maxVal[1], p[1]); maxVal[2] = std::max(maxVal[2], p[2]); minVal[0] = std::min(minVal[0], p[0]); minVal[1] = std::min(minVal[1], p[1]); minVal[2] = std::min(minVal[2], p[2]); } if (line[0]=='v' && line[1]=='n') { glm::vec3 vec; sscanf(line, "vn %f %f %f\n", &(vec.x), &(vec.z), &(vec.y)); vec.z = -vec.z; normals.push_back(vec); } if (line[0]=='f') { int i0, i1, i2; int j0,j1,j2; int k0,k1,k2; sscanf(line, "f %u/%u/%u %u/%u/%u %u/%u/%u\n", &i0, &j0, &k0, &i1, &j1, &k1, &i2, &j2, &k2 ); faces.push_back(i0-1); faces.push_back(i1-1); faces.push_back(i2-1); normalIds.push_back(k0-1); normalIds.push_back(k1-1); normalIds.push_back(k2-1); } } fclose(f); std::cout << "Min : " << minVal.x << " " << minVal.y << " " << minVal.z << std::endl; std::cout << "Max : " << maxVal.x << " " << maxVal.y << " " << maxVal.z << std::endl; QVector<int> indicesTriangles(faces.size()/3); for (int i = 0; i < faces.size()/3; i++) { indicesTriangles[i] = i*3; } Box boundingBox{minVal, maxVal, indicesTriangles}; Mesh mesh{vertices, normals, faces, normalIds}; MeshBoxes meshBox{mesh, boundingBox}; divideMeshBox(meshBox); return meshBox; }
bool Ray::CheckIntersection(const BoundingBox &aabb, Vector3 &point) { Vector3 maxVal(-1, -1, -1); Vector3 bbMin(aabb.mMin), bbMax(aabb.mMax); bool rayInside = false; // false // X check if(mOrigin.x < bbMin.x) { point.x = bbMin.x; rayInside = false; if(mDirection.x != 0) maxVal.x = (bbMin.x - mOrigin.x) / mDirection.x; } else if(mOrigin.x > bbMax.x) { point.x = bbMax.x; rayInside = false; if(mDirection.x != 0) maxVal.x = (bbMax.x - mOrigin.x) / mDirection.x; } // Y check if(mOrigin.y < bbMin.y) { point.y = bbMin.y; rayInside = false; if(mDirection.y != 0) maxVal.y = (bbMin.y - mOrigin.y) / mDirection.y; } else if(mOrigin.y > bbMax.y) { point.y = bbMax.y; rayInside = false; if(mDirection.y != 0) maxVal.y = (bbMax.y - mOrigin.y) / mDirection.y; } // Z check if(mOrigin.z < bbMin.z) { point.z = bbMin.z; rayInside = false; if(mDirection.z != 0) maxVal.z = (bbMin.z - mOrigin.z) / mDirection.z; } else if(mOrigin.z > bbMax.z) { point.z = bbMax.z; rayInside = false; if(mDirection.z != 0) maxVal.z = (bbMax.z - mOrigin.z) / mDirection.z; } if(rayInside) { point = mOrigin; return true; } int index = 0; float temp[3]; temp[0] = maxVal.x; temp[1] = maxVal.y; temp[2] = maxVal.z; if(maxVal.y > temp[index]) index = 1; if(maxVal.z > temp[index]) index = 2; if(temp[index] < 0) return false; if(index != 0) { point.x = mOrigin.x + maxVal.x * mDirection.x; if(point.x < bbMin.x - 0.00001f || point.x < bbMax.x + 0.00001f) return false; } if(index != 1) { point.y = mOrigin.y + maxVal.y * mDirection.y; if(point.y < bbMin.y - 0.00001f || point.y < bbMax.y + 0.00001f) return false; } if(index != 2) { point.z = mOrigin.z + maxVal.z * mDirection.z; if(point.z < bbMin.z - 0.00001f || point.z < bbMax.z + 0.00001f) return false; } return true; }