KWStatus SSShiftSort(void) { int i,j,k; lineCount = 0; int lineListSize = 1; if(!(lineList = malloc(sizeof(LineList)))) return KWMEMORYERROR; /* compute the size of lineList */ int numLines = LSNumLines(); for (i = 0; i < numLines; i++) { int numWords = LSNumWords(i); for (j = 0; j < numWords; j++) { /* exclude lines that start with a noise word */ if (!WTIsMember(LSGetWord(i,j))){ if(lineCount == lineListSize){ if(!(lineList = realloc(lineList, sizeof(lineList)*lineListSize*2))) return KWMEMORYERROR; lineListSize *= 2; } lineList[lineCount].lineNum = i; lineList[lineCount].shiftNum = j; lineCount++; } } } // sort the shifted lines qsort(lineList,lineCount,sizeof(LineList), (int (*)(const void *, const void *))lineCompare); return KWSUCCESS; }
KWStatus SSShiftSort(void) { clock_t cstart = clock(); clock_t cend = 0; int i,j,k; /* compute the size of lineList */ lineCount = 0; for (i = 0; i < LSNumLines(); i++) { for (j = 0; j < LSNumWords(i); j++) { /* exclude lines that start with a noise word */ if (!WTIsMember(LSGetWord(i,j))) lineCount++; } } /* allocate space for lineList */ lineList = calloc(lineCount, sizeof(LineList)); if (lineList == NULL) { lineCount = 0; return KWMEMORYERROR; } /* fill lineList */ k = 0; for (i = 0; i < LSNumLines(); i++) { for (j = 0; j < LSNumWords(i); j++) { /* exclude lines that start with a noise word */ if (!WTIsMember(LSGetWord(i,j))) { lineList[k].lineNum = i; lineList[k].shiftNum = j; k++; } } } /* sort the shifted lines */ qsort(lineList,lineCount,sizeof(LineList), (int (*)(const void *, const void *))lineCompare); cend = clock(); //printf ("%.3f Shift cpu sec\n", ((double)cend - (double)cstart)* 1.0e-6); return KWSUCCESS; }
static void runTest() { int i,j,status; int rangeErr[4]; const char* word; printf("check that LSAddWord properly signals KWRANGEERROR\n"); status = LSAddWord(""); if (status != KWRANGEERROR) { printf("Error in LSAddWord return value."); printf(" Actual: %d Expected: %d\n",status,KWRANGEERROR); errorCount++; } printf("load lineList into LineStorage\n"); for (i = 0; i < NUMLINES; i++) { printf("\tline %d\n",i); status = LSAddLine(); if (status != KWSUCCESS) { printf("Error in LSAddLine return value."); printf(" Actual: %d Expected: %d\n",status,KWSUCCESS); errorCount++; } for (j = 0; j < lineList[i].numWords; j++) { printf("\t\tword %d\n",j); status = LSAddWord(lineList[i].wordList[j]); if (status != KWSUCCESS) { printf("Error in LSAddWord return value."); printf(" Actual: %d Expected: %d\n", status,KWSUCCESS); errorCount++; } } } printf("check that the lines have been properly stored\n"); if (LSNumLines() != NUMLINES) { printf("Error in LSNumLines. Actual: %d Expected: %d\n", LSNumLines(),NUMLINES); errorCount++; } for (i = 0; i < NUMLINES; i++) { printf("\tline %d\n",i); if (LSNumWords(i) != lineList[i].numWords) { printf("Error in LSNumWord. Actual: %d Expected: %d\n", LSNumWords(i),lineList[i].numWords); errorCount++; } for (j = 0; j < lineList[i].numWords; j++) { printf("\t\tword %d\n",j); if (strcmp(LSGetWord(i,j),lineList[i].wordList[j])) { printf("Error in LSGetWords(%d,%d).",i,j); printf(" Actual: !%s! Expected: !%s!\n", LSGetWord(i,j),lineList[i].wordList[j]); errorCount++; } } } printf("check that line number range errors are properly signaled\n"); rangeErr[0] = -1000; rangeErr[1] = -1; rangeErr[2] = NUMLINES; rangeErr[3] = NUMLINES+1000; for (i = 0; i < 4; i++) { printf("\tline %d\n",rangeErr[i]); status = LSNumWords(rangeErr[i]); if (status != KWRANGEERROR) { printf("Error in LSNumWords return value."); printf(" Actual: %d Expected: %d\n", status,KWRANGEERROR); errorCount++; } word = LSGetWord(rangeErr[i],0); if (word != 0) { printf("Error in LSGetWord return value."); printf(" Actual: !%s! Expected: %d\n",word,0); errorCount++; } } printf("check that word number range errors are properly signaled\n"); rangeErr[0] = -1000; rangeErr[1] = -1; rangeErr[2] = lineList[0].numWords; rangeErr[3] = lineList[0].numWords+1000; for (i = 0; i < 4; i++) { printf("\tword %d\n",rangeErr[i]); word = LSGetWord(0,rangeErr[i]); if (word != 0) { printf("Error in LSGetWord return value."); printf(" Actual: !%s! Expected: %d\n",word,0); errorCount++; } } }