std::ostream& operator<<(std::ostream& os, Unit x){ os << "Unit(" << ind << iendl << x->name; os << iendl << "PARENTS"; printtable(os, x->parents, x->parentCount); os << iendl << "GLOBALS"; printtable(os, x->globals, x->globalCount); os << iendl << "CONSTRUCTORS"; printtable(os, x->constructors, x->constructorCount); os << iendl << "NATIVEFUNCS"; printtable(os, x->nativeFuncs, x->nativeFuncCount); os << iendl << "NATIVEPREDS"; printtable(os, x->nativePreds, x->nativePredCount); os << iendl << "DENOTATIONS"; printtable(os, x->denotations, x->denotationCount); os << iendl << "initSchemaIndex=" << x->initSchemaIndex; os << iendl << "SCHEMAS"; printtable(os, x->schemas, x->schemaCount); os << iendl << "CODE"; int step = 0; while (step < x->codeSize){ os << iendl; printNext(os, x->code, step); } os << unind << ")" << iendl; return os; }
void callTextSearch(uchar text[], uchar pattern[]) { int position , patn_len ; patn_len = strlen((char*)pattern); /* パターンの長さをセット */ #if BF printf("--- B F ---\n"); printf("%s\n",text); position = brute_force_search(text, pattern); printf("position=%d\n",position); #endif #if KMP printf("--- KMP ---\n"); init_next(pattern); printNext(pattern, patn_len); printf("%s\n",text); position = kmp_search(text, pattern); printf("position=%d\n",position); #endif #if BM printf("--- B M ---\n"); init_skip(pattern); printSkip(pattern, patn_len); printf("%s\n",text); position = bm_search(text, pattern); printf("position=%d\n",position); #endif }
int* buildNext ( char* P ) { //构造模式串P的next表(改进版本) size_t m = strlen ( P ), j = 0; //“主”串指针 int* N = new int[m]; //next表 int t = N[0] = -1; //模式串指针 while ( j < m - 1 ) if ( 0 > t || P[j] == P[t] ) { //匹配 N[j] = ( P[++j] != P[++t] ? t : N[t] ); //注意此句与未改进之前的区别 } else //失配 t = N[t]; /*DSA*/printString ( P ); printf ( "\n" ); /*DSA*/printNext ( N, 0, strlen ( P ) ); return N; }
int match ( char* P, char* T ) { //KMP算法 int* next = buildNext ( P ); //构造next表 int n = ( int ) strlen ( T ), i = 0; //文本串指针 int m = ( int ) strlen ( P ), j = 0; //模式串指针 while ( j < m && i < n ) //自左向右逐个比对字符 /*DSA*/{ /*DSA*/showProgress ( T, P, i - j, j ); /*DSA*/printNext ( next, i - j, strlen ( P ) ); /*DSA*/getchar(); printf ( "\n" ); if ( 0 > j || T[i] == P[j] ) //若匹配,或P已移出最左侧(两个判断的次序不可交换) { i ++; j ++; } //则转到下一字符 else //否则 j = next[j]; //模式串右移(注意:文本串不用回退) /*DSA*/} delete [] next; //释放next表 return i - j; }
void printThreadAt(std::ostream& os, Thread thread, Instr::Pointer pc){ int step = pc - thread->inten->schema->unit->code; os << thread << " at "; printNext(os, pc - step, step, false); }