#if defined IMPL_STL_NA_NA #define IMPL_STL #endif #if defined IMPL_BUHR94_NA_NA #define IMPL_BUHR94 #endif #if defined IMPL_STL #include #include #include using namespace std; #define IMPL_CXX #elif defined IMPL_CFA_HL_SHARE #define IMPL_CFA_HL #define IMPL_CFA #elif defined IMPL_CFA_LL_SHARE #define IMPL_CFA_LL #define IMPL_CFA #elif defined IMPL_CFA_HL_NOSHARE #define IMPL_CFA_HL #define CFA_NOSHARE #define IMPL_CFA #elif defined IMPL_CFA_LL_NOSHARE #define IMPL_CFA_LL #define CFA_NOSHARE #define IMPL_CFA #elif defined IMPL_BUHR94 #include #include #include "/u0/mlbrooks/usys1/sm/string/StringSharing/src/string.h" #define IMPL_CXX #else #error Bad IMPL_ #endif #if defined IMPL_CFA_HL #include #elif defined IMPL_CFA_LL #include #endif #if defined CFA_NOSHARE #include #define STRING_SHARING_CONTROL \ string_sharectx c = { NO_SHARING }; #else #define STRING_SHARING_CONTROL #endif #if defined IMPL_CFA #include extern "C" { void malloc_stats( void ); } #elif defined IMPL_CXX #include using std::min; #include #endif #include #include // atoi #include // strlen, only during setup #if defined IMPL_STL || defined IMPL_BUHR94 #define PRINT(s) std::cout << s << std::endl #elif defined IMPL_CFA_HL || defined IMPL_CFA_LL #define PRINT(s) sout | s; #else #error Unhandled print case #endif #if defined IMPL_CFA_LL #define STRING_T string_res #define ASSIGN_CHAR(str, idx, val) assignAt(str, idx, val) #else #define STRING_T string #define ASSIGN_CHAR(str, idx, val) str[idx] = val #endif #if defined IMPL_CFA #define LEN(str) size(str) #elif defined IMPL_STL #define LEN(str) str.length() #else #error need LEN definition for this IMPL_ #endif double meanLen(int N, char ** strings) { int totalLen = 0; for (int i = 0 ; i < N; i ++) { totalLen += strlen(strings[i]); } return (double)totalLen / (double)N; } volatile int checkthis = 0; #define MAYBE( op ) if (checkthis) { op; } // setup, not being timed, nor judged for aesthetics void makeSuperCorpus( STRING_T & target, int corpusLen, char ** corpus, int scSize, char scDelimiter ) { char delimiterStr[2] = { scDelimiter, '\0' }; for ( int i = 0; i < scSize; i++ ) { target += corpus[ i % corpusLen ]; target += delimiterStr; } } // the function at issue #if defined IMPL_CFA_HL void processThing( string & thing, char magicChar ) { MAYBE(PRINT(thing)); size_t foundPos; for (;;) { size_t size_thing = size(thing); foundPos = find(thing, magicChar); if( foundPos == size_thing ) break; string mcOccur = thing(foundPos, foundPos+1)`shareEdits; mcOccur = ""; } return thing; } #elif defined IMPL_CFA_LL void processThing( string_res & thing, char magicChar ) { MAYBE(PRINT(thing)); size_t foundPos; for (;;) { size_t size_thing = size(thing); foundPos = find(thing, magicChar); if( foundPos == size_thing ) break; string_res mcOccur = { thing, SHARE_EDITS, foundPos, foundPos+1 }; mcOccur = ""; } } #elif defined IMPL_STL void processThing( string & thing, char magicChar ) { MAYBE(PRINT(thing)); size_t foundPos; for (;;) { size_t size_thing = thing.length(); foundPos = thing.find(magicChar); if (foundPos == string::npos) break; thing.erase(foundPos, 1); } } #endif /* void runCorrectnessDemo(char** corpus, int corpusLen) { STRING_T item = "asdf"; PRINT( processThing( item, '-' ) ); item = "as-df"; PRINT( processThing( item, '-' ) ); item = "-asdf-"; PRINT( processThing( item, '-' ) ); item = "-"; PRINT( processThing( item, '-' ) ); for ( int i = 0; i < corpusLen; i ++ ) { item = corpus[i]; PRINT( processThing( item, '-' ) ); } STRING_T supercorpus; makeSuperCorpus( supercorpus, corpusLen, corpus, 30, ','); PRINT( supercorpus ); } */ int main( int argc, char ** argv ) { STRING_SHARING_CONTROL int corpuslen = 0; char ** corpus = (char**) 0; clock_t start, endTarget, end_actual; const char * usage_args = "SCsize MagicChar ExecTimeSecs Corpus..."; const int static_arg_posns = 4; int scSize = -1; char magicChar = '\0'; int execTimeSecs = -1; switch (min(argc, static_arg_posns)) { case 4: execTimeSecs = atoi(argv[3]); case 3: magicChar = argv[2][0]; case 2: scSize = atoi(argv[1]); } corpuslen = argc - static_arg_posns; corpus = argv + static_arg_posns; if (scSize < 1 || magicChar == '\0' || execTimeSecs < 1 || corpuslen < 1) { printf("usage: %s %s\n", argv[0], usage_args); printf("output:\nxxx,corpusItemCount,corpusMeanLenChars,invcationCountActual,execTimeActualSec\n"); exit(1); } double meanCorpusLen = meanLen(corpuslen, corpus); // runCorrectnessDemo(corpus, corpuslen); STRING_T supercorpus; makeSuperCorpus( supercorpus, corpuslen, corpus, scSize, ','); size_t sc_charlen = LEN(supercorpus); start = clock(); endTarget = start + CLOCKS_PER_SEC * execTimeSecs; volatile unsigned int t = 0; size_t lastChunkEnd = -1; for ( ; t % 10000 != 0 || clock() < endTarget ; t += 1 ) { #if defined OP_PNO lastChunkEnd++; if ( lastChunkEnd >= sc_charlen ) { lastChunkEnd = 0; } #if defined IMPL_CFA_HL size_t thisChunkEnd = findFrom( supercorpus, lastChunkEnd, ',' ); //if (thisChunkEnd == string::npos) throw 1; string chunkProcessed = supercorpus(lastChunkEnd, thisChunkEnd); processThing(chunkProcessed, '-'); #elif defined IMPL_CFA_LL size_t thisChunkEnd = findFrom( supercorpus, lastChunkEnd, ',' ); //if (thisChunkEnd == string::npos) throw 1; string_res chunkProcessed = { supercorpus, COPY_VALUE, lastChunkEnd, thisChunkEnd }; processThing( chunkProcessed, '-' ); #elif defined IMPL_STL size_t thisChunkEnd = supercorpus.find( ',', lastChunkEnd ); if (thisChunkEnd == string::npos) throw 1; string chunkProcessed = supercorpus.substr(lastChunkEnd, thisChunkEnd-lastChunkEnd); processThing(chunkProcessed, '-'); #else #error Bad IMPL_ #endif MAYBE( PRINT(chunkProcessed) ); lastChunkEnd = thisChunkEnd; #else #error Bad OP_ #endif } end_actual = clock(); double elapsed = ((double) (end_actual - start)) / CLOCKS_PER_SEC; printf("xxx,%d,%f,%d,%f\n", corpuslen, meanCorpusLen, t, elapsed); // malloc_stats(); return 0; }