示例#1
0
文件: mergesort.cpp 项目: yoos/c
void merge(char** left, char** right, uint64_t lSize, uint64_t rSize, uint64_t wordMaxLength)
{
	// Allocate temporary array to sort into.
	char* tmp[lSize+rSize];
	for (uint64_t i=0; i<lSize+rSize; i++) {
		tmp[i] = (char*) calloc(wordMaxLength+1, sizeof(char));
	}

	uint64_t ti = 0;   // tmp index
	uint64_t li = 0;   // Left index
	uint64_t ri = 0;   // Right index

	// Merge into temporary array.
	while (li < lSize || ri < rSize) {
		if (li < lSize && ri < rSize) {
			sprintf(tmp[ti++], (wordCompare(left[li], right[ri]) > 0) ? left[li++] : right[ri++]);
		}
		else if (li < lSize) {
			sprintf(tmp[ti++], left[li++]);
		}
		else {
			sprintf(tmp[ti++], right[ri++]);
		}
	}

	// Copy contents of temporary array into old memory location and free memory.
	for (uint64_t i=0; i<lSize+rSize; i++) {
		sprintf(left[i], tmp[i]);
		free(tmp[i]);
	}
}
ComResWord * ComResWords::binarySearch(ComResWord *val)
{
   Int32 lower = 0;
   size_t numEntries = sizeof(resWords_)/sizeof(ComResWord);

#pragma nowarn(1506)   // warning elimination 
   Int32 upper = numEntries -1;
#pragma warn(1506)  // warning elimination 
   short result = 0;
   while ( lower <= upper ) 
   {
    Int32 middle = (lower+upper) >> 1;
#pragma nowarn(1506)   // warning elimination 
    result = wordCompare(&(resWords_[middle]), val);
#pragma warn(1506)  // warning elimination 

    if ( result == 0 )
       return (ComResWord*)(&(resWords_[middle]));

    if ( result < 0 )
      lower = middle + 1;
    else
      upper = middle - 1;
    }
 return val; 
}