nrgrep函數分析(1)–搜尋流程簡析

來源:互聯網
上載者:User

NR-grep是由智利大學的Gonzalo Navarro開發的一款“快速、靈活的模式串匹配工具”,其優點在於Nrgrep幾乎全部基於BNDM演算法及其擴充,效能隨著搜尋問題複雜度的增加而平滑下降(Agrep劇烈下降)。而且支援精確搜尋和允許錯誤的近似搜尋(grep不支援近似搜尋);而且Nrgrep將效能平滑性看做是模式複雜度的一個函數,一旦預測到BNDM演算法搜尋的代價太大,他將更換為Shift-And演算法。

但是Nrgrep的一個缺點在於不支援多串匹配,我們的實驗便是想在Nrgrep的基礎上,設計出利用GPU效能提高的多字串匹配程式。

 

/* nrgrep程式共有33個檔案 */

 

首先讓我們來分析一下Nrgrep程式的大體執行流程。

 

一、/* get the options */ and /* some consistency checks */

    Nrgrep程式從Shell.c的main (int argc, char **argv)開始,第一步便是擷取使用者輸入的尾碼(如-i等尾碼參數),這裡調用了Linux中的特有標頭檔getopt.h中的getopt函數(ps:getopt.h為開發Linux帶參數程式提供特有的方便),然後根據不同的尾碼做出不同的switch-case處理(各尾碼作用將在以後分析)。

   

二、/* get the pattern */

    然後,開始對搜尋字串作分析(分析第一個字是否’^’,最後一個字是否’$’)和調用預先處理函數searchPreproc (patt)——在search.c內定義,其參數patt傳遞了一個pattern。該函數的作用為“Preprocesses pat and creates a searchData structure for searching”,為搜尋結構確定、初始化了搜尋類型(如:SIMPLE,EXTENDED,REGULAR),不同類型指向了不同的處理函數:

=== simplePreproc(pat,tree,pos);    esimplePreproc(pat,tree,pos,OptErrors); ===

=== extendedPreproc(pat,tree,pos); eextendedPreproc(pat,tree,pos,OptErrors); ===

=== regularPreproc(pat,tree,pos);   eregularPreproc(pat,tree,pos,OptErrors); ===

    接著再調用了recPreproc ()函數,該函數定義於record.c:“makes the preprocessing for record handling”。

 

三、/* search the files */

初始化工作做好後,便可以開始檔案搜尋(同時支援標準輸入stdin,調用fileno(stdin))。

主要調用的函數有:

bufSetFile (B,f)

 

if (OptRecNumber || !OptRecPositive)

    newm = recSearchRecFile (*files,B,sData);

    /* searches the file handled by B for P using R as record

      separator, but scans record by record. this is our way to

      handle OptRecNumber or !OptRecPositive */

 

else newm = recSearchFile (*files,B,sData);

    /* searches the file handled by B for P using R as record

       separator, reports matches as appropriate and returns number

       of matches */

 

其中函數搜尋調用的是searchScan (byte **beg, byte **end, searchData *P)

/* Searches from *beg to *end-1 for P. Returns if it could find

       it. In case of returning true, *beg and *end are set to limit

       the printable record containing the occurrence of P */

 

四、/* final report */

輸出最終結果

 

五、/* clean up */

釋放個資料結構的空間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.