view regexParser/cerium/CeriumMain.cc @ 293:948428caf616

NFA maximum match worked
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 02 Feb 2016 10:38:45 +0900
parents 868f01f1ba8e
children 63213964502a
line wrap: on
line source

#include <stdio.h>
#include <sys/time.h>
#include "TaskManager.h"
#include "SchedTask.h"
#include "Func.h"
#include "FileMapReduce.h"
#include "CeriumGrep.h"
#include "subsetConstruction.h"
#include "threadedSearch.h"
#include "CeriumMain.h"

extern void task_init();
void TMend(TaskManager *);
static double st_time;
static double ed_time;
const char* usr_help_str = "";

static double
getTime() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec + (double)tv.tv_usec*1e-6;
}

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

static
TSValue stateSkip(TSValue tsv) {
    tsv.current = tsv.tg->stateStart->tState;
    if (tsv.matchEnd) {
        ResultPtr r = NEW(Result);
        r->continued = false;
        r->begin = tsv.matchBegin;
        r->end = tsv.matchEnd;
        *tsv.blk->resultEnd = r;
        r->next = NULL;
        tsv.blk->resultEnd = &r->next;
        tsv.matchEnd=NULL;
    }
    return tsv;
}

static
TSValue stateMatch(TSValue tsv) {
    tsv.matchEnd = tsv.buff.buffptr;  // next char of the match
    return tsv;
}

ResultPtr resultPrint(ResultPtr r,const char* comment) {
    ResultPtr prev = NULL;
    // printf("%s\n",comment);
    for (;r;r = r->next) {
        if (r->continued) {
            prev = r;
            break;
        }
        fwrite(r->begin,r->end - r->begin,1,stdout);
        puts("");
    }
    return prev;
}

void
ceriumCreateAnyState(TransitionGeneratorPtr tg) {
    tg->stateSkip = stateSkip;
    tg->stateMatch = stateMatch;
    tg->stateNothing = stateNothing;
    createAnyState(tg);
    generateTState(tg->anyState,tg);
    // generateTState for startState. It is used in stateMatch.
    generateTState(tg->stateList,tg);
    tg->stateStart = NEW(State);
    *tg->stateStart = *tg->stateList;
    tg->stateStart->accept = false; // Start state never accept
    generateTState(tg->stateStart,tg);
}

int
TMmain(TaskManager *manager, int argc, char *argv[])
{
    char *filename = 0;
    st_time = getTime();
    Search s = grep(argc,argv,true);

    ceriumCreateAnyState(s.tg);

    FileMapReduce *fmp = new FileMapReduce(manager,TASK_EXEC,TASK_EXEC_DATA_PARALLEL,TASK_PRINT);
    filename = fmp->init(argc, argv);
    fmp->w->global = (void*)s.tg;
    if (filename < 0) {
        return -1;
    }
    fmp->division_out_size = sizeof(void*)*3; // *Result,*blockBegin,*blockEnd
    task_init();
    fmp->run_start(manager, filename);
    manager->set_TMend(TMend);
    return 0;
}

void
TMend(TaskManager *manager)
{
    ed_time = getTime();
    printf("Time: %0.6f\n",ed_time-st_time);
}

/* end */