view example/regex_mas/ppe/Exec.cc @ 1739:dfeeb2f681db draft

fix BMsearch
author Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
date Tue, 12 Nov 2013 13:53:29 +0900
parents aff937b6a26a
children 5a294d8e5643
line wrap: on
line source

#include <stdio.h>
#include <string.h>
#include "Exec.h"
#include "Func.h"
#include "SchedTask.h"
#define max(a,b)((a)>(b)?a:b) 

/* これは必須 */
SchedDefineTask(Exec);

//ボイヤームーア法による文字列検索アルゴリズム
int BM_method(unsigned char *text,int *offset,int *text_length,
              unsigned char *pattern,unsigned long long *match_string)
{
    int skip[256];
    int text_len = (long int)text_length;
    int pattern_len = strlen((char *)pattern);
    int i = 0;
    int match_counter = 0;

    for (i = 0; i < 256; ++i){
        skip[i] = pattern_len;
    }

    for (i = 0; i < pattern_len-1 ; ++i){
        skip[(int)pattern[i]] = pattern_len - i - 1;
    }



    i = pattern_len - 1;

    while ( i < text_len){
        int j = pattern_len - 1;
        while (text[i] == pattern[j]){
            if (j == 0){
                match_counter++;
            }
            --i;
            --j;
        }
        i = i + max((int)skip[(int)text[i]],pattern_len - j);
    }
    return match_counter;
}

static int
run(SchedTask *s, void *rbuf, void *wbuf)
{
    unsigned char *i_data = (unsigned char *)rbuf;
    unsigned long long *o_data = (unsigned long long*)wbuf;
    int *offset = (int*)s->get_param(0);
    int *length = (int*)s->get_param(1);
    //unsigned char *search_word = (unsigned char*)s->get_input(2);
    unsigned char search_word[] = "doing";

    o_data[0] = BM_method(i_data,offset,length,search_word,o_data);

    //s->printf("in Exec.cc\n");

   
    return 0;
}