int Minimum_Edit_Distance(char * A, int m, char * B, int n) { int metrix[10][10]; // init for (int i = 0; i < m; i++) { metrix[i][0] = i; } for (int j = 1; j < n; j++) { metrix[0][j] = j; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { int z = 1; if(A[i] == B[j]) z = 0; metrix[i][j] = Minimum( metrix[i-1][j] + 1, metrix[i][j-1] + 1, metrix[i-1][j-1] + z); printf("%d,%d:\n",i,j); printf("metrix[i-1][j] + 1 : %d,%d = %d\n",i-1,j,metrix[i-1][j] + 1); printf("metrix[i][j-1] + 1 : %d,%d = %d\n",i,j-1,metrix[i][j-1] + 1); printf("metrix[i-1][j-1] + z : %d,%d,%d = %d\n",i-1,j-1,z,metrix[i-1][j-1] + z); printf("Metrix[%d][%d] = %d\n",i,j,metrix[i][j]); printf("\n"); } } printf("result = %d\n",metrix[m-1][n-1]); return metrix[m-1][n-1]; }
int main (void) { FILE *test; char name[20]; int grade; Student S; Tree T; Initialize(&T,copyStudent,destroyStudent,compareStudents); printf("Initialize()\n"); printf("Size=%d, Height=%d, ",Size(&T),Height(&T)); if(Balanced(&T)) printf("Balanced=YES\n\n"); else printf("Balanced=NO\n\n"); test=fopen("test.txt","r"); while(fscanf(test,"%s %d",name,&grade)==2) { InitializeStudent(name,grade,&S); Insert(&T,&S); printf("Insert(%s,%d)\n",NameOfStudent(S),GradeOfStudent(S)); printf("Size=%d, Height=%d, ",Size(&T),Height(&T)); if(Balanced(&T)) printf("Balanced=YES\n\n"); else printf("Balanced=NO\n\n"); FreeStudent(&S); } fclose(test); Minimum(&T,&S); do { printf("%s\t%d%%\n",NameOfStudent(S),GradeOfStudent(S)); FreeStudent(&S); } while(Successor(&T,&S)); Destroy(&T); return EXIT_SUCCESS; }
rb_node_t *RBTree::Delete(rb_node_t *z) { rb_node_t *x, *y = z; rb_color_t orig_color = z->color; if (z->left == &sentry) { x = z->right; TransPlant(z, z->right); } else if (z->right == &sentry) { x = z->left; TransPlant(z, z->left); } else { y = Minimum(z->right); orig_color = y->color; x = y->right; if (p_of(y) == z) { p_of(x) = y; } else { TransPlant(y, y->right); y->right = z->right; p_of(y->right) = y; } TransPlant(z, y); y->left = z->left; y->left->p = y; y->color = z->color; } if (orig_color == BLACK) { DeleteFixup(x); } return y; }
void RGB_To_HSV( double r, double g, double b, double *h, double *s, double *v ) { /* * given: rgb, each in [0,1] * desired: h in [0,360), s and v in [0,1] except if s = 0 then h = undefined */ double max = Maximum( r, g, b ); double min = Minimum( r, g, b ); *v = max; /*This is the value v. */ /* Next calculate saturation, s. Saturation is 0 if red, green and blue are all 0 */ *s = (max != 0.0) ? ( (max-min)/max ) : 0.0; if (*s == 0.0) { *h = UNDEFINED; } else { /*Chromatic case: Saturation is not 0*/ double delta = max-min; /*Determine Hue*/ if (r == max) *h = (g-b)/delta; /* resulting color is between yellow and magenta */ else if (g == max) *h = 2.0+(b-r)/delta; /* resulting color is between cyan and yellow */ else if (b == max) *h = 4.0+(r-g)/delta; /* resulting color is between magenta and cyan */ *h *= 60.0; /*convert hue to degrees*/ if (*h < 0.0) *h += 360.0; /*make sure its not negative*/ } }
void HID_CtrlGetDescriptorIn(void * pVoid) { uint32_t u32Len; // S_DRVUSB_DEVICE *psDevice = &gsUsbDevice; // uint32_t u32EpId; DBG_PRINTF(" >>> 0x%08x %d size.\n", gpu8UsbBuf, gu32BytesInUsbBuf); if(gpu8UsbBuf) { if(gu32BytesInUsbBuf == 0) { /* Zero packet */ DrvUSB_DataIn(0, gpu8UsbBuf, 0); gpu8UsbBuf = 0; //u32Len = 10000; //while(u32Len--); //_DRVUSB_TRIG_EP(1,0x00); } else { u32Len = Minimum(gu32BytesInUsbBuf, HID_MAX_PACKET_SIZE_CTRL); DrvUSB_DataIn(0, gpu8UsbBuf, u32Len); gpu8UsbBuf += u32Len; gu32BytesInUsbBuf -= u32Len; if(gu32BytesInUsbBuf == 0) { if(u32Len < HID_MAX_PACKET_SIZE_CTRL) { /* This should be last IN packet due to it is less than HID_MAX_PACKET_SIZE_EP0 */ gpu8UsbBuf = 0; //u32Len = 10000; //while(u32Len--); //_DRVUSB_TRIG_EP(1,0x00); } else { if(!gIsOverRequest) { gpu8UsbBuf = 0; //u32Len = 10000; //while(u32Len--); //_DRVUSB_TRIG_EP(1,0x00); } } } } } else { /* The EP id 1 should always be used as control (OUT) endpoint */ _DRVUSB_TRIG_EP(1,0x00); } }
void RenderMesh(int width, int height, Triangle *tris) { int column, row, i; int numTris = tris[0].mesh->numTris; Hit hit; Vector2 pt, V0V1, V2V0; int top,bot,lft,rgt; //preallocate Vector3 a,b,c; float avgZ; for (i=0;i<numTris;++i) { top = tris[i].bbox.min.y; bot = Minimum(tris[i].bbox.max.y+1, height); lft = tris[i].bbox.min.x; rgt = Minimum(tris[i].bbox.max.x+1, width); //printf("t %i b %i l %i r %i\n", t,b,l,r); //Check if back-facing! a = tris[i].mesh->vertPoints[tris[i].vert[0]]; b = tris[i].mesh->vertPoints[tris[i].vert[1]]; c = tris[i].mesh->vertPoints[tris[i].vert[2]]; V0V1; V0V1.x = b.x-a.x; V0V1.y = b.y-a.y; V2V0; V2V0.x = a.x-c.x; V2V0.y = a.y-c.y; if (DetVec2(&V0V1, &V2V0) < 0.f) continue; for (row=top;row<bot;++row) { for (column=lft;column<rgt;++column) { avgZ = (a.z+b.z+c.z)/3.f; if (avgZ < DepthBuffer[row*width + column] && avgZ > 0.f) { pt.x = column; pt.y = row; if (DoesPointLieOnTri(&(tris[i]), &pt, &hit)) { DepthBuffer[row*width + column] = avgZ; Pixels[row*width*3 + column*3 + 0] = (char)hit.material.red; Pixels[row*width*3 + column*3 + 1] = (char)hit.material.green; Pixels[row*width*3 + column*3 + 2] = (char)hit.material.blue; //printf("Rendered %f %f %f\n", // hit.material.red, // hit.material.green, // hit.material.blue); } } } } } }
TNormFactory::TNormFactory() { registerClass(Minimum().className(), &(Minimum::constructor)); registerClass(AlgebraicProduct().className(), &(AlgebraicProduct::constructor)); registerClass(BoundedDifference().className(), &(BoundedDifference::constructor)); registerClass(DrasticProduct().className(), &(DrasticProduct::constructor)); registerClass(EinsteinProduct().className(), &(EinsteinProduct::constructor)); registerClass(HamacherProduct().className(), &(HamacherProduct::constructor)); }
SDL_Rect Intersection(const SDL_Rect& a, const SDL_Rect& b) { int x1 = Maximum(a.x, b.x); int y1 = Maximum(a.y, b.y); int x2 = Minimum(a.x + a.w, b.x + b.w); int y2 = Minimum(a.y + a.h, b.y + b.h); int width = x2 - x1; int height = y2 - y1; if(width > 0 && height > 0) { SDL_Rect intersect = {x1, y1, width, height}; return intersect; } else { SDL_Rect intersect = {0, 0, 0, 0}; return intersect; } }
void Slider::HandleVertical(int pos) { suic::Rect rcItem(0, 0, RenderSize().cx, RenderSize().cy); const suic::Size& thumbSize = _thumbBtn->GetDesiredSize(); if (pos <= rcItem.top ) { SetValue(Minimum()); } else if (pos >= rcItem.bottom - thumbSize.cy) { SetValue(Maximum()); } else { SetValue(Minimum() + (Maximum() - Minimum()) * (double)(pos) / (double)(rcItem.Height() - thumbSize.cy)); } }
void Slider::HandleHorizontal(int pos) { suic::Rect rcItem(0, 0, RenderSize().cx, RenderSize().cy); const suic::Size& thumbSize = _thumbBtn->GetDesiredSize(); if (pos >= rcItem.right - thumbSize.cx) { SetValue(Maximum()); } else if (pos <= rcItem.left) { SetValue(Minimum()); } else { SetValue(Minimum() + (Maximum() - Minimum()) * (double)(pos) / (double)(rcItem.Width() - thumbSize.cx)); } }
// Strings of size m and n are passed. // Construct the Table for X[0...m, m+1], Y[0...n, n+1] int EditDistanceDP(char X[], char Y[]) { // Cost of alignment int cost = 0; int leftCell, topCell, cornerCell; int m = strlen(X)+1; int n = strlen(Y)+1; // T[m][n] int *T = (int *)malloc(m * n * sizeof(int)); // Initialize table for(int i = 0; i < m; i++) for(int j = 0; j < n; j++) *(T + i * n + j) = SENTINEL; // Set up base cases // T[i][0] = i for(int i = 0; i < m; i++) *(T + i * n) = i; // T[0][j] = j for(int j = 0; j < n; j++) *(T + j) = j; // Build the T in top-down fashion for(int i = 1; i < m; i++) { for(int j = 1; j < n; j++) { // T[i][j-1] leftCell = *(T + i*n + j-1); leftCell += EDIT_COST; // deletion // T[i-1][j] topCell = *(T + (i-1)*n + j); topCell += EDIT_COST; // insertion // Top-left (corner) cell // T[i-1][j-1] cornerCell = *(T + (i-1)*n + (j-1) ); // edit[(i-1), (j-1)] = 0 if X[i] == Y[j], 1 otherwise cornerCell += (X[i-1] != Y[j-1]); // may be replace // Minimum cost of current cell // Fill in the next cell T[i][j] *(T + (i)*n + (j)) = Minimum(leftCell, topCell, cornerCell); } } // Cost is in the cell T[m][n] cost = *(T + m*n - 1); free(T); return cost; }
typename Map<K,V,KeyElementTraits, ValueElementTraits, ThreadingModel>::Iterator Map<K,V,KeyElementTraits, ValueElementTraits, ThreadingModel>::Begin() { Node* pNode = Minimum(m_pRoot); if (pNode == 0) return End(); else return Iterator(this,pNode); }
/** * Sends a data buffer asynchronously over the USB virtual COM port. * This is an internal function. * * @param buf Data buffer. * @param size Number of bytes to send. */ static void USB_VirtualCOM_SendAsync(const uint8_t *buf, uint32_t size) { USB_VirtualCOM_TxTransfer_t *transfer; uint32_t partialSize; if(size == 0) { return; } // Enter critical section __set_PRIMASK(1); partialSize = 0; if(USB_VirtualCOM_bulkInWaiting) { // Transfer first packet right away // If size is a multiple of USB_VCOM_BULK_IN_MAX_PKT_SIZE it will stay that way partialSize = Minimum(size, USB_VCOM_BULK_IN_MAX_PKT_SIZE); USB_VirtualCOM_SendBulkInPayload(buf, partialSize); buf += partialSize; size -= partialSize; } if(partialSize != 0 && partialSize < USB_VCOM_BULK_IN_MAX_PKT_SIZE) { // We already transferred the whole packet __set_PRIMASK(0); return; } // Allocate memory for transfer + buffer // If the buffer was exactly USB_VCOM_BULK_IN_MAX_PKT_SIZE bytes and // it has already been transferred, the transfer info still needs to be // allocated for the bulk IN handler to send the zero packet. transfer = (USB_VirtualCOM_TxTransfer_t *) malloc(sizeof(USB_VirtualCOM_TxTransfer_t) + size); if(transfer == NULL) { __set_PRIMASK(0); return; } // Fill in transfer data transfer->next = NULL; transfer->size = size; transfer->txSize = 0; if(size != 0) { memcpy(transfer->buffer, buf, size); } // Append transfer to queue if(USB_VirtualCOM_txQueue.head == NULL) { USB_VirtualCOM_txQueue.head = USB_VirtualCOM_txQueue.tail = transfer; } else { USB_VirtualCOM_txQueue.tail->next = transfer; USB_VirtualCOM_txQueue.tail = transfer; } // Exit critical section __set_PRIMASK(0); }
typename Set<K,ElementTraits>::Iterator Set<K,ElementTraits>::Begin() { Node* pNode = Minimum(m_pRoot); if (pNode == 0) return End(); else return Iterator(this,pNode); }
int UUIDDistance::LD(String& s, String& t) { int** d; // matrix int n; // length of s int m; // length of t int i; // iterates through s int j; // iterates through t char s_i; // ith character of s char t_j; // jth character of t int cost; // cost // Step 1 n = s.length(); m = t.length(); if (n == 0) { return m; } if (m == 0) { return n; } //d = new int[n + 1][m + 1]; d = new int*[n+1]; for(i=0; i < n+1; i++){ d[i] = new int[m+1]; } // Step 2 for (i = 0; i <= n; i++) { d[i][0] = i; } for (j = 0; j <= m; j++) { d[0][j] = j; } // Step 3 for (i = 1; i <= n; i++) { //s_i = s.charAt(i - 1); s_i = s.fast_rep()[i - 1]; // Step 4 for (j = 1; j <= m; j++) { //t_j = t.charAt(j - 1); t_j = t.fast_rep()[j - 1]; // Step 5 if (s_i == t_j) { cost = 0; } else { cost = 1; } // Step 6 d[i][j] = Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost); } } // Step 7 int ret = d[n][m]; for(i=0; i < n+1; i++){ delete d[i]; } delete d; return ret; }
char *DBGridIF::ValueFormat() const { switch (DataPTR->Type()) { case DBTypeGridDiscrete: return ((char *) "%s"); case DBTypeGridContinuous: return (DBMathFloatAutoFormat(fabs(Maximum()) > fabs(Minimum()) ? Maximum() : Minimum())); default: return ((char *) NULL); } }
TNormFactory::TNormFactory() : ConstructionFactory<TNorm*>("TNorm") { registerConstructor("", fl::null); registerConstructor(AlgebraicProduct().className(), &(AlgebraicProduct::constructor)); registerConstructor(BoundedDifference().className(), &(BoundedDifference::constructor)); registerConstructor(DrasticProduct().className(), &(DrasticProduct::constructor)); registerConstructor(EinsteinProduct().className(), &(EinsteinProduct::constructor)); registerConstructor(HamacherProduct().className(), &(HamacherProduct::constructor)); registerConstructor(Minimum().className(), &(Minimum::constructor)); registerConstructor(NilpotentMinimum().className(), &(NilpotentMinimum::constructor)); }
void Min_Max_Test() { int n = 102; int* arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = i; shuffle(arr, n, 100); Minimum(arr, n); Maximum(arr, n); Minimum_and_Maximum(arr, n); }
SDL_Rect Spank::Intersection(const SDL_Rect& boundA, const SDL_Rect& boundB) { int x1 = Maximum(boundA.x, boundB.x); int y1 = Maximum(boundA.y, boundB.y); int x2 = Minimum(boundA.x + boundA.w, boundB.x + boundB.w); int y2 = Minimum(boundA.y + boundA.h, boundB.y + boundB.h); int width = x2 - x1; int height = y2 - y1; if(width > 0 && height > 0) { SDL_Rect intersect = {x1, y1, width, height}; return intersect; } else { SDL_Rect intersect = {0, 0, 0, 0}; return intersect; } }
//recursive implementation int EditDistance_recursive( char *X, char *Y, int m, int n ) { if( m == 0 && n == 0 ) return 0; if( m == 0 ) return n; if( n == 0 ) return m; int left = EditDistance_recursive(X, Y, m-1, n) + 1; int right = EditDistance_recursive(X, Y, m, n-1) + 1; int corner = EditDistance_recursive(X, Y, m-1, n-1) + (X[m-1] != Y[n-1]); return Minimum(left, right, corner); }
void RunningStats::Print(FILE * pFile, const char * header) const { fprintf (pFile, "\n%s\n", header); fprintf (pFile, "NumDataValues: %ld\n", NumDataValues()); fprintf (pFile, "Mean: %f\n", Mean()); fprintf (pFile, "Variance: %f\n", Variance()); fprintf (pFile, "StandardDeviation: %f\n", StandardDeviation()); fprintf (pFile, "Skewness: %f\n", Skewness()); fprintf (pFile, "Kurtosis: %f\n", Kurtosis()); fprintf (pFile, "Maximum: %f\n", Maximum()); fprintf (pFile, "Minimum: %f\n", Minimum()); return; }
void ProgressBar::OnRender(suic::DrawingContext * drawing) { // 先绘制背景 suic::Rect elemrect(0, 0, RenderSize().cx, RenderSize().cy); suic::TriggerPtr trg(suic::UIRender::GetTriggerByStatus(this, GetStyle())); suic::UIRender::DrawBackground(drawing, trg, &elemrect); // // 绘制进度条状态 // suic::ImageBrushPtr bkgnd(trg->GetValue(_T("Thumb"))); if (bkgnd) { suic::Rect rcdraw(elemrect); // 水平 if (GetOrientation() == CoreFlags::Horizontal) { LONG iOff = (LONG)((GetValue() - Minimum()) * (double)(rcdraw.right - rcdraw.left) / (Maximum() - Minimum())); rcdraw.right = rcdraw.left + iOff; } else { LONG iOff = (LONG)((double)(rcdraw.bottom - rcdraw.top) * (GetValue() - Minimum()) / (Maximum() - Minimum())); rcdraw.top = rcdraw.bottom - iOff; } if (!rcdraw.Empty()) { bkgnd->Draw(drawing, &rcdraw); } } suic::UIRender::DrawText(drawing, GetText(), trg, &elemrect , GetHorizontalContentAlignment(), GetVerticalContentAlignment()); }
extern int Successor (Tree *T, void *I) { TreeNode * P; P = malloc(sizeof(TreeNode)); T->current = T->root; if (T->current->right != NULL) return Minimum(T, I); P->value = T->current->parent; while ((P->value != NULL) && T->current == P->right) { /*loops until NULL or the current is equal to the right*/ T->current = P->value; P->value = P->parent; } I=P; return 1; }
rb_node_t *BST::Successor(rb_node_t *z) { rb_node_t *p; if (z->right) { return Minimum(z->right); } p = p_of(z); while(p && z == p->right) { z = p; p = p_of(p); } return p; }
void Slider::OnKeyDown(suic::KeyEventArg& e) { if (GetOrientation() == CoreFlags::Horizontal) { if (e.IsLeftArrow()) { if (GetValue() > Minimum()) { SetValue((int)GetValue() - 1); } } else if (e.IsRightArrow()) { if (GetValue() < Maximum()) { SetValue((int)GetValue() + 1); } } } else { if (e.IsUpArrow()) { if (GetValue() > Minimum()) { SetValue((int)GetValue() - 1); } } else if (e.IsDownArrow()) { if (GetValue() < Maximum()) { SetValue((int)GetValue() + 1); } } } }
void HID_CtrlGetDescriptorIn(void * pVoid) { uint32_t u32Len; DBG_PRINTF(" >>> 0x%08x %d size.\n", gpu8UsbBuf, gu32BytesInUsbBuf); if (gpu8UsbBuf) { if (gu32BytesInUsbBuf == 0) { /* Zero packet */ DrvUSB_DataIn(0, gpu8UsbBuf, 0); gpu8UsbBuf = 0; } else { u32Len = Minimum(gu32BytesInUsbBuf, HID_MAX_PACKET_SIZE_EP0); DrvUSB_DataIn(0, gpu8UsbBuf, u32Len); gpu8UsbBuf += u32Len; gu32BytesInUsbBuf -= u32Len; if (gu32BytesInUsbBuf == 0) { if (u32Len < HID_MAX_PACKET_SIZE_EP0) { /* This should be last IN packet due to it is less than UAC_MAX_PACKET_SIZE_EP0 */ gpu8UsbBuf = 0; } else { if (!gIsOverRequest) { /* This should be the last IN packet because there is no more data to transfer and it is not over request transfer */ gpu8UsbBuf = 0; } } } } } else { /* The EP id 1 should always be used as control (OUT) endpoint */ _DRVUSB_TRIG_EP(1, 0x00); } }
BSTNode<T>* BinarySearchTree<T>::Successor(BSTNode<T> *pNode) { if(pNode->pRight != NULL) //If the node's right is not NULL, the successor is the minimum one in its right sub tree { return Minimum(pNode->pRight); } BSTNode<T>* pTempY = pNode->pParent; BSTNode<T>* pTempX = pNode; while((pTempY != NULL) && (pTempY->pRight == pTempX)) //If the node's right is NULL, the the lowest predecessor is the predecessor and its left child is also the node's predecessor { pTempX = pTempY; pTempY = pTempY->pParent; } return pTempY; }
// function call once for each thread by the host void Processor::multiThreadProcessing(unsigned int threadId, unsigned int nThreads, void *arg) { Processor *proc = (Processor *) arg; // slice the y range into the number of threads it has unsigned int dy = proc->window.y2 - proc->window.y1; unsigned int y1 = proc->window.y1 + threadId * dy/nThreads; unsigned int y2 = proc->window.y1 + Minimum((threadId + 1) * dy/nThreads, dy); OfxRectI win = proc->window; win.y1 = y1; win.y2 = y2; // and render that thread on each proc->doProcessing(win); }
int main() { char studentName[40]; int studentGrade; Student * curStudent = NULL; FILE * studentFile = fopen("test.txt","r"); Tree theTree; int first = 0; int shouldContinue = 1; /* initialize tree */ Initialize (&theTree, (©Student), (&destroyStudent), (&compareStudents)); printf("Initialize()\n"); getTreeInfo(&theTree); while (fscanf(studentFile,"%s %d",studentName,&studentGrade)==2) { curStudent = malloc(sizeof(Student)); InitializeStudent(studentName,studentGrade,curStudent); printf("Insert(%s,%d)\n", studentName,studentGrade); Insert(&theTree,curStudent); getTreeInfo(&theTree); } fclose(studentFile); while (shouldContinue == 1) { void * theStudent = malloc(sizeof(Student)); Student * printStudent; if (first == 0) { shouldContinue = Minimum(&theTree,theStudent); if (shouldContinue == 0) break; first++; } else { shouldContinue = Successor(&theTree,theStudent); if (shouldContinue == 0) break; } printStudent = (Student *)theStudent; printf("%s \t%d%%\n", NameOfStudent(*printStudent),GradeOfStudent(*printStudent)); free(theStudent); } Destroy(&theTree); return 0; }
SizeInt Viewer::calculateImageSize( ResizeBehaviour mode, float xratio, float yratio, const SizeInt &imageSize, const SizeInt &windowEdges ) { wxDisplay display(DisplayFromPointFallback(PositionScreen())); auto rtDesktop = wxToRect(display.GetClientArea()); // The image is larger than the desktop. The image is not supposed // to be downscaled so fill the screen. if ((mode == ResizeEnlargeOnly) && (xratio < 1.0) && (yratio < 1.0)) return rtDesktop.Dimensions(); // The image is smaller than the desktop. It must not be made // smaller needlessly so size the window after the image. else if ((mode == ResizeReduceOnly) && (xratio > 1.0) && (yratio > 1.0)) return SizeInt( std::max<int>(MinWindowWidth, imageSize.Width + windowEdges.Width), std::max<int>(MinWindowHeight,imageSize.Height + windowEdges.Height)); return Maximum(SizeInt(MinWindowWidth, MinWindowHeight), Minimum(RoundCast(imageSize * std::min(xratio, yratio)) + windowEdges, rtDesktop.Dimensions())); }