KMP算法实现bit搜索

工作需要,自己封装了一个比特搜索类,都是静态函数。用KMP算法实现。

这是头文件:

//  BitSearch.h

#ifndef __CBITSEARCH__H
#define  __CBITSEARCH__H

class  CBitSearch
{
public:
    
static int IndexOfKMP( const unsigned char* S, const unsigned char* T, int slen, int tlen, int start = 0 );
    
static int IndexOf( const unsigned char* S, const unsigned char* T, int slen, int tlen, int start = 0 );
    
static int GetCount( const unsigned char *S, const unsigned char* T, int slen, int tlen, int start = 0 );
private:
    
static const unsigned char _fillmasks[];
    
static void GetNext( const unsigned char* T, int len, int next[] );
    
static inline bool GetBit( const unsigned char* T, int pos )
    
{
        
return ( T[pos>>3& _fillmasks[pos%8] )?true:false;
    }

}
;

#endif

 

 

这是实现文件:

 

//  BitSearch.cpp

#include 
" BitSearch.h "


const  unsigned  char  CBitSearch::_fillmasks[]  =   0x800x400x200x100x080x040x020x01 } ;

void  CBitSearch::GetNext(  const  unsigned  char *  T,  int  len,  int  next[] )
{
    
int j = 0, k = -1;
    next[
0= -1;
    
while( j < len )
    
{
        
if( k == -1 || GetBit(T,j) == GetBit(T,k) )
        
{
            
++j; ++k;
            
if( GetBit(T,j) != GetBit(T,k) ) next[j] = k;
            
else next[j] = next[k];
        }

        
else k = next[k];
    }

}


//  Search the sub bit stream using KMP
//  Caller should ensure the input parameters are legal
int  CBitSearch::IndexOfKMP(  const  unsigned  char *  S,  const  unsigned  char *  T,  int  slen,  int  tlen,  int  start )
{
    
if( start+tlen > slen ) return -1;

    
int *next = new int[tlen+1];
    
if(!next) return -1;

    
// Get the next value
    GetNext(T, tlen, next);

    
int i = start, j = 0;
    
while( i < slen && j < tlen )
    
{
        
if(j == -1 || GetBit(S,i) == GetBit(T,j)) ++i; ++j; }
        
else j = next[j];
    }


    delete[] next;

    
if( j == tlen ) return i-j;
    
else return -1;
}


//  Caller should ensure the input parameters are legal
int  CBitSearch::IndexOf(  const  unsigned  char *  S,  const  unsigned  char *  T,  int  slen,  int  tlen,  int  start )
{
    
if( start+tlen > slen ) return -1;

    
int i = start, j = 0;
    
while( i < slen && j < tlen )
    
{
        
if(GetBit(S,i) == GetBit(T,j)) ++i; ++j; }
        
else { i = i - j + 1; j = 0; }
    }


    
if( j == tlen ) return i-j;
    
else return -1;
}


//  Get the times of sub bit stream appears in source bit stream 
int  CBitSearch::GetCount(  const  unsigned  char   * S,  const  unsigned  char *  T,  int  slen,  int  tlen,  int  start )
{
    
int count = 0;
    
int pos = 0;

    pos 
=  IndexOfKMP( S, T, slen, tlen, start );
    
while( pos >= 0 )
    
{
        pos 
= IndexOfKMP( S, T, slen, tlen, pos+tlen );
        
++count;
    }


    
return count;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值