SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <stdio.h> | |
| 2 | #include <string.h> | |
| 3 | #include <stdlib.h> | |
| 4 | #include <time.h> | |
| 5 | ||
| 6 | //#define _countof(a) (sizeof(a)/sizeof(*(a))) | |
| 7 | ||
| 8 | #define DoBenchmark | |
| 9 | ||
| 10 | #ifdef DoBenchmark | |
| 11 | #define _Benchmark while (abs(clock()-ttime)<CLOCKS_PER_SEC) | |
| 12 | #else | |
| 13 | #define _Benchmark | |
| 14 | #endif | |
| 15 | ||
| 16 | typedef struct {
| |
| 17 | int group; | |
| 18 | const char* rule; | |
| 19 | const char* ldraw_output; | |
| 20 | } Rule; | |
| 21 | ||
| 22 | #include "table.c" | |
| 23 | ||
| 24 | const Rule* ruleIdx[_countof(rules)]; | |
| 25 | ||
| 26 | //comparator function for qsort | |
| 27 | int CompareRules( const void* pA , const void* pB ) {
| |
| 28 | return strcmp( (*((const Rule**)pA))->rule , (*((const Rule**)pB))->rule ); | |
| 29 | } | |
| 30 | void InitRules() {
| |
| 31 | //set ptrs for each rules element at initial index | |
| 32 | for (int N=0 ; N<_countof(ruleIdx) ; N++ ) { ruleIdx[N] = &rules[N]; }
| |
| 33 | qsort( ruleIdx , _countof(ruleIdx) , sizeof(void*) , CompareRules ); | |
| 34 | printf("%i rules\n",_countof(ruleIdx));
| |
| 35 | } | |
| 36 | const Rule* FindRule( char* pzRule ) {
| |
| 37 | int iBegin = 0 , iEnd = _countof(ruleIdx); | |
| 38 | while (iEnd >= iBegin) {
| |
| 39 | //try item from middle | |
| 40 | int idx = (iBegin+iEnd)/2; | |
| 41 | const char* pzEntry = ruleIdx[idx]->rule; | |
| 42 | int iResu = strcmp( pzRule , pzEntry ); | |
| 43 | if (!iResu) { return ruleIdx[idx]; } //found
| |
| 44 | if (iBegin == iEnd) { return NULL; } //NOT found
| |
| 45 | //remove the wrong half of possibilities | |
| 46 | if (iResu>0) { iBegin= idx+1; } else { iEnd = idx-1; }
| |
| 47 | } | |
| 48 | return NULL; //NOT found | |
| 49 | } | |
| 50 | ||
| 51 | void NormalizeInput( char* pzInput ) {
| |
| 52 | char WasSpace=1, *pOut = pzInput, C; | |
| 53 | while ((C=*pzInput++)) {
| |
| 54 | switch (C) {
| |
| 55 | case ' ' : case '\t' : case '\r' : case '\n' : | |
| 56 | if (!WasSpace) { *pOut++ = ' '; }
| |
| 57 | WasSpace=1; break; | |
| 58 | default: | |
| 59 | *pOut++ = C; WasSpace=0; | |
| 60 | } | |
| 61 | } | |
| 62 | - | const Rule* pFound; |
| 62 | + | while (pOut[-1]==' ') { pOut--; }
|
| 63 | - | pFound = FindRule( userInput ); |
| 63 | + | *pOut = '\0'; |
| 64 | - | for (volatile int N=0 ; N<999 ; N++) { pFound = FindRule( userInput ); } //repeat 1000 times for benchmark
|
| 64 | + | |
| 65 | ||
| 66 | - | ttime = clock()-ttime; |
| 66 | + | |
| 67 | ||
| 68 | InitRules(); | |
| 69 | ||
| 70 | FILE *outputFile = fopen("output.txt", "a"); // Open the file in append mode
| |
| 71 | ||
| 72 | while (1) {
| |
| 73 | char userInput[100]; | |
| 74 | ||
| 75 | - | printf("took %.03fms\n",(ttime*1.0)/CLOCKS_PER_SEC);
|
| 75 | + | |
| 76 | fgets(userInput, 100, stdin); | |
| 77 | userInput[strcspn(userInput, "\n")] = '\0'; | |
| 78 | NormalizeInput( userInput ); | |
| 79 | printf("'%s'\n",userInput);
| |
| 80 | ||
| 81 | ||
| 82 | if (strcmp(userInput, "exit") == 0) {
| |
| 83 | break; // Exit the loop if the user types 'exit' | |
| 84 | } | |
| 85 | ||
| 86 | #ifdef DoBenchmark | |
| 87 | clock_t ttime = clock(); | |
| 88 | #endif | |
| 89 | long long iCount = 0; | |
| 90 | ||
| 91 | const Rule* pFound = NULL; | |
| 92 | #if 1 | |
| 93 | _Benchmark {
| |
| 94 | pFound = FindRule( userInput ); | |
| 95 | iCount++; | |
| 96 | } | |
| 97 | #else | |
| 98 | _Benchmark {
| |
| 99 | for (int N=0 ; N<_countof(rules) ; N++) {
| |
| 100 | if (!strcmp(userInput , rules[N].rule)) {
| |
| 101 | pFound = &rules[N]; break; | |
| 102 | } | |
| 103 | } | |
| 104 | iCount++; | |
| 105 | } | |
| 106 | #endif | |
| 107 | if (pFound) {
| |
| 108 | printf("%s\n", pFound->ldraw_output);
| |
| 109 | fprintf(outputFile, "%s\n", pFound->ldraw_output); | |
| 110 | } else {
| |
| 111 | printf("No match found.\n");
| |
| 112 | } | |
| 113 | ||
| 114 | #ifdef DoBenchmark | |
| 115 | printf("took %.05fms %lli/s\n",1000.0/iCount,iCount);
| |
| 116 | #endif | |
| 117 | ||
| 118 | } | |
| 119 | ||
| 120 | fclose(outputFile); // Close the file | |
| 121 | ||
| 122 | return 0; | |
| 123 | ||
| 124 | } | |
| 125 |