view regexParser/threadedSearch.cc @ 278:99d635926ef3

ceriumCreateAnyState implemented
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Sat, 30 Jan 2016 21:19:05 +0900
parents 7b4bcc7b5ae6
children b74e3b4b11d7
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>

#include "regexParser.h"
#include "threadedSearch.h"
#include "subsetConstruction.h"

void tSearch(TSValue tsv);

static
TSValue stateNothing(TSValue tsv) {
    return tsv;
}

static
TSValue stateSkip(TSValue tsv) {
    tsv.buff.matchBegin = tsv.buff.buffptr;
    return tsv;
}

static
TSValue stateMatch(TSValue tsv) {
    fwrite(tsv.buff.matchBegin,tsv.buff.buffptr-tsv.buff.matchBegin-1,1,stdout);
    puts("");
    tsv.current = tsv.tg->stateList->tState;
    tsv.buff.buffptr--;
    tsv = stateSkip(tsv);
    return tsv;
}

TStatePtr generateTState(StatePtr state, TransitionGeneratorPtr tg) {
    TStatePtr tState = NEW(TState);
    tState->state = state;
    state->tState = tState;
    int ccvSize = 0;
    CharClassWalkerPtr ccw = createCharClassWalker(state->cc);
    while (hasNext(ccw)) {
        getNext(ccw);
        ccvSize++;
    }
    tState->ccvSize = ccvSize;
    if (state->accept) {
        tState->stateSkip = tg->stateMatch;
        tState->stateContinue = tg->stateNothing;
    } else {
        tState->stateSkip = tg->stateSkip;
        tState->stateContinue = tg->stateNothing;
    }
    if (ccvSize == 0) {
        tState->ccv = NULL;
        return tState;
    } else tState->ccv = (ccv*)malloc(sizeof(ccv)*ccvSize);
    ccw = createCharClassWalker(state->cc);
    int i = 0;
    while (hasNext(ccw)) {
        CharClassPtr cc = getNext(ccw);
        unsigned long begin = cc->cond.range.begin;
        unsigned long end = cc->cond.range.end;
        struct ccv *ccv = &tState->ccv[i++];
        ccv->begin = begin;
        ccv->end = end;
        ccv->tState = NULL;
        ccv->state = cc->nextState;
        ccv->w = cc->cond.w;
    }
    free(ccw);
    return tState;
}

TStatePtr nextTState(BitVector bi,TransitionGeneratorPtr tg) {
    // create tSearch in next state.
    StatePtr state = tg->stateArray[bi.bitContainer];
    if (state == NULL) {
        // on the fly subset construction.
        state = createState(tg,bi);
        determinize(state,tg);
        tg->stateArray[bi.bitContainer] = state;
    }
    if (state->tState == NULL) {
        generateTState(state,tg);
    }
    return state->tState;
}

void tSearch(TSValue tsv) {
    next: while (tsv.buff.buffptr < tsv.buff.buffend) {
        unsigned char c = *tsv.buff.buffptr++;
        // printState(tsv.current->state);
        for (int i = 0; i < tsv.current->ccvSize; i++) {
            CCVPtr ccv = &tsv.current->ccv[i];
            if (c<ccv->begin) {
                tsv = tsv.current->stateSkip(tsv);
                goto next;
            } else if (c<=ccv->end) {
                // range matched.
                if (ccv->w.word) {
                    // match the word.
                    // if (not match) continue;
                }
                if (ccv->tState) {
                    tsv.current = ccv->tState;
                } else {
                    tsv.current = nextTState(ccv->state,tsv.tg);
                    ccv->tState = tsv.current;
                }
                // tsv = tsv.current->stateContinue(tsv);
                goto next;
            }
        }
        tsv = tsv.current->stateSkip(tsv);
    }
}

void threadedSearch(TransitionGeneratorPtr tg, Buffer buff) {
    TSValue tsv;
    tsv.buff = buff;
    tsv.tg = tg;
    tsv.result = NULL;
    tsv.tg->stateSkip = stateSkip;
    tsv.tg->stateMatch = stateMatch;
    tsv.tg->stateNothing = stateNothing;
    tsv.current = generateTState(tg->stateList,tg);
    tSearch(tsv);
}