• 刁肥宅数据结构课设“布隆过滤器的实现和应用”源代码(v1.0,永不上交)


           代码很简单,写了一些注释;加上注释看就很清楚了。项目代码和报告的GitHub地址:https://github.com/25thengineer/Data-structure-course-design-The-Implementations-and-Applications-of-The-Bloom-Filter-in-HFUT

           文件bloomfilter.cpp:

      1 #include "bloomfilter.h"
      2 
      3 // return a hash range from 0 to 79999
      4 int hash(const char* str, int index)
      5 {
      6     int hash = 1;
      7     int seed = 12345;
      8     int curr;
      9     switch(index)
     10     {
     11     case 0:{
     12         while(curr = int(*str++))
     13         {
     14             hash = 128 * hash + curr;
     15         }
     16         return abs(hash%80000);
     17     }
     18     case 1:{
     19         while(curr = int(*str++))
     20         {
     21             hash = (25536 * hash + curr)%80000;
     22         }
     23         return abs(hash);
     24     }
     25     case 2:{
     26         while(curr = int(*str++))
     27         {
     28             hash = (seed * hash + curr)%80000;
     29             seed *= 123;
     30         }
     31         return abs(hash);
     32     }
     33     case 3:{
     34         while(curr = int(*str++))
     35         {
     36             hash += curr*curr;
     37         }
     38         return abs(hash%80000);
     39     }
     40     case 4:{
     41         while(curr = int(*str++))
     42         {
     43             hash += abs(curr*curr*curr);
     44         }
     45         return abs(hash%80000);
     46     }
     47     case 5:{
     48         while(curr = int(*str++))
     49         {
     50             hash *= (hash + curr*seed)%80000;
     51         }
     52         return abs(hash%80000);
     53     }
     54     case 6:{
     55         while(curr = int(*str++))
     56         {
     57             seed = 345;
     58             hash = (seed * hash + curr)%80000;
     59             seed *= 345;
     60         }
     61         return abs(hash);
     62     }
     63     }
     64     return -1;
     65 }
     66 
     67 void initBitMap(unsigned char* bitMap)
     68 {
     69     for(int i = 0; i<10000;i++)
     70     {
     71         bitMap[i] = 0;
     72     }
     73 }
     74 
     75 bool isKeyExistInBitMap(unsigned char* bitMap, const char* str)
     76 {
     77     for(int i = 0; i<7; i++)
     78     {
     79         int code = hash(str, i);
     80         if(!((bitMap[code/8] >> code%8) % 2))
     81         {
     82             return false;
     83         }
     84     }
     85     return true;
     86 }
     87 
     88 void appendKey2BitMap(unsigned char* bitMap, const char* str)
     89 {
     90     if(isKeyExistInBitMap(bitMap, str))
     91     {
     92         return;
     93     }
     94     for(int i = 0; i<7; i++)
     95     {
     96         int code = hash(str, i);
     97         if(!((bitMap[code/8] >> code%8) % 2))
     98         {
     99             bitMap[code/8] += 1 << code%8;
    100         }
    101     }
    102 }

           文件bloomfilter.h:

     1 #ifndef BLOOMFILTER_H
     2 #define BLOOMFILTER_H
     3 
     4 #include <stdio.h>
     5 #include <math.h>
     6 
     7 int hash(const char* str, int index);
     8 void initBitMap(unsigned char* bitMap);
     9 bool isKeyExistInBitMap(unsigned char* bitMap, const char* str);
    10 void appendKey2BitMap(unsigned char* bitMap, const char* str);
    11 
    12 #endif // BLOOMFILTER_H

           文件main.cpp:

     1 #include "mainwindow.h"
     2 #include <QApplication>
     3 
     4 int main(int argc, char *argv[])
     5 {
     6     QApplication a(argc, argv);
     7     MainWindow w;
     8     /*w.show();*/
     9     QIcon icon("E:/code/Qt/build-TangTang-DFZ-Release/release/th.jpg");
    10     w.setWindowIcon(icon);
    11     w.setWindowTitle("Simple Code Editor");
    12     w.show();
    13 
    14     return a.exec();
    15 }

           文件mainwindow.cpp:

     1 #include "mainwindow.h"
     2 #include "ui_mainwindow.h"
     3 
     4 #include <QSyntaxHighlighter>
     5 #include <QPushButton>
     6 #include <QObject>
     7 #include <QTextEdit>
     8 #include "myhighlight.h"
     9 
    10 MainWindow::MainWindow(QWidget *parent) :
    11     QMainWindow(parent),
    12     ui(new Ui::MainWindow)
    13 {
    14     ui->setupUi(this);
    15     QFont font;
    16     font.setFamily("Consolas");
    17     font.setFixedPitch(true);
    18     font.setPointSize(20);
    19 
    20     editor = new QTextEdit;
    21     editor->setFont(font);
    22 
    23     highlighter = new myHighLight(editor->document());
    24 
    25     setCentralWidget(editor);
    26     setWindowTitle("Simple C++ Code Editor");
    27 
    28     connect(ui->actionappend_key, &QAction::triggered, this, &MainWindow::appendKey);
    29     connect(ui->actionDelete_key, &QAction::triggered, this, &MainWindow::deleteKey);
    30 }
    31 
    32 MainWindow::~MainWindow()
    33 {
    34     delete editor;
    35     delete ui;
    36 }
    37 
    38 void MainWindow::appendKey()
    39 {
    40     QString selectedText = editor->textCursor().selectedText();
    41     highlighter->appendKey(selectedText);
    42     highlighter->setDocument(editor->document());
    43 }
    44 
    45 void MainWindow::deleteKey()
    46 {
    47     QString selectedText = editor->textCursor().selectedText();
    48     highlighter->deleteKey(selectedText);
    49     highlighter->setDocument(editor->document());
    50 }

           文件mainwindow.h:

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 #include "myhighlight.h"
     6 #include <QTextEdit>
     7 #include <QtCore>
     8 
     9 namespace Ui {
    10 class MainWindow;
    11 }
    12 
    13 class MainWindow : public QMainWindow
    14 {
    15     Q_OBJECT
    16 
    17 public:
    18     explicit MainWindow(QWidget *parent = 0);
    19     ~MainWindow();
    20 
    21 private slots:
    22     // append selected text for ui
    23     void appendKey();
    24     void deleteKey();
    25 
    26 private:
    27     Ui::MainWindow *ui;
    28 
    29     QTextEdit *editor;
    30     myHighLight *highlighter;
    31 };
    32 
    33 #endif // MAINWINDOW_H

           文件mainwindow.ui自己拖一拖就行!

           文件myhighlight.cpp:

      1 #include "myhighlight.h"
      2 #include "bloomfilter.cpp"
      3 #include <QtGui>
      4 
      5 myHighLight::myHighLight(QTextDocument *parent)
      6     : QSyntaxHighlighter(parent)
      7 {
      8     // find each word
      9     rule = new QRegExp("(\b)(\w+)(\b)");
     10 
     11     format.setForeground(Qt::blue);
     12     format.setFontWeight(QFont::Bold);
     13 
     14     initBitMap(bitMap);
     15     QStringList keyWords = cppKeyword();
     16     for(QString key : keyWords)
     17     {
     18         appendKey(key);
     19     }
     20 }
     21 
     22 myHighLight::~myHighLight()
     23 {
     24     if(rule)
     25         delete rule;
     26 }
     27 
     28 QStringList myHighLight::cppKeyword()
     29 {
     30     return QStringList()<<"asm"<<"auto"<<"bool"<<"break"<<"case"<<"catch"<<"char"
     31                        <<"class"<<"const"<<"const_cast"<<"continue"<<"default"<<"delete"
     32                       <<"do"<<"double"<<"dynamic_cast"<<"else"<<"enum"<<"explicit"
     33                      <<"export"<<"extern"<<"false"<<"float"<<"for"<<"friend"<<"goto"
     34                     <<"if"<<"inline"<<"int"<<"long"<<"mutable"<<"namespace"<<"new"
     35                    <<"operator"<<"private"<<"protected"<<"public"<<"register"
     36                   <<"reinterpret_cast"<<"return"<<"short"<<"signed"<<"sizeof"<<"static"
     37                  <<"static_cast"<<"struct"<<"switch"<<"template"<<"this"<<"throw"
     38                 <<"true"<<"try"<<"typedef"<<"typeid"<<"typename"<<"union"<<"unsigned"
     39                <<"using"<<"virtual"<<"void"<<"volatile"<<"wchar_t"<<"while";
     40 }
     41 
     42 //QVector<int> myHighLight::Hash(QString word)
     43 //{
     44 //    std::hash<std::string> str_hash;
     45 //    QVector<int> res;
     46 //    if(word.size()%hashCount == 0)
     47 //    {
     48 //        word += " ";
     49 //    }
     50 //    QString key;
     51 //    while(key.size() < 100)
     52 //    {
     53 //        key += word;
     54 //    }
     55 
     56 //    for(int i = 0; i<hashCount; i++)
     57 //    {
     58 //        QString key0;
     59 //        for(int j = i;j<key.size(); j+=hashCount)
     60 //        {
     61 //            key0.append(key[j]);
     62 //        }
     63 //        res.append(abs(int(str_hash(key0.toStdString()))%(BloomFilter.size()*8-1)));
     64 //    }
     65 
     66 //    return res;
     67 //}
     68 
     69 void myHighLight::appendKey(QString key)
     70 {
     71     if(!rule->exactMatch(key))
     72     {
     73         qDebug()<<"KeyWord "<<key<<" is not a word";
     74         return;
     75     }
     76 
     77     if(whiteNameList.contains(key))
     78     {
     79         whiteNameList.removeOne(key);
     80     }
     81 
     82     appendKey2BitMap(bitMap, key.toStdString().data());
     83 //    if(isKeyExisted(key))
     84 //    {
     85 //        qDebug()<<"KeyWord "<<key<<" alredy existed!";
     86 //        return;
     87 //    }
     88 
     89 //    QVector<int> hash = Hash(key);
     90 //    for(auto code : hash)
     91 //    {
     92 //        if(!((BloomFilter[code/8] >> code%8) % 2))
     93 //        {
     94 //            BloomFilter[code/8] += 1 << code%8;
     95 //        }
     96 //    }
     97 }
     98 
     99 void myHighLight::deleteKey(QString key)
    100 {
    101     if(!rule->exactMatch(key))
    102     {
    103         qDebug()<<"KeyWord "<<key<<" is not a word";
    104         return;
    105     }
    106 
    107     if(whiteNameList.contains(key))
    108     {
    109         qDebug()<<"KeyWord "<<key<<" to be deleted has already been in whitelist";
    110         return;
    111     }
    112 
    113     if(!isKeyExisted(key))
    114     {
    115         qDebug()<<"KeyWord "<<key<<" to be deleted is not in the BloomFilter!";
    116         return;
    117     }
    118 
    119     whiteNameList.push_back(key);
    120     qDebug()<<whiteNameList;
    121 }
    122 
    123 bool myHighLight::isKeyExisted(QString key)
    124 {
    125     if(whiteNameList.contains(key))
    126     {
    127         return false;
    128     }
    129 
    130     return isKeyExistInBitMap(bitMap, key.toStdString().data());
    131 //    QVector<int> hash = Hash(key);
    132 //    for(auto code : hash)
    133 //    {
    134 //        if(!((BloomFilter[code/8] >> code%8) % 2))
    135 //        {
    136 //            return false;
    137 //        }
    138 //    }
    139 //    return true;
    140 }
    141 
    142 void myHighLight::highlightBlock(const QString &text)
    143 {
    144     int index = rule->indexIn(text);
    145     while(index >= 0)
    146     {
    147         int length = rule->matchedLength();
    148         if(isKeyExisted(text.mid(index, length)))
    149         {
    150             setFormat(index, length, format);
    151         }
    152         index = rule->indexIn(text, index + length);
    153     }
    154 }

           文件myhighlight.h:

     1 #ifndef MYHIGHLIGHT_H
     2 #define MYHIGHLIGHT_H
     3 
     4 #include <QSyntaxHighlighter>
     5 #include <QTextCharFormat>
     6 
     7 class QTextDocument;
     8 
     9 class myHighLight : public QSyntaxHighlighter
    10 {
    11     Q_OBJECT
    12 public:
    13     myHighLight(QTextDocument *parent = 0);
    14     ~myHighLight();
    15 
    16     static QStringList cppKeyword();
    17 
    18     // get a Hash Value Array from a word
    19     QVector<int> Hash(QString word);
    20     // append a keyword to the bloom filter
    21     void appendKey(QString key);
    22     // delete a keyword from the bloom filter
    23     void deleteKey(QString key);
    24     // judge if the word is existing in the bloom filter
    25     bool isKeyExisted(QString key);
    26 
    27 protected:
    28     // highlight the keyword block on updating document
    29     void highlightBlock(const QString &text);
    30 
    31 private:
    32     // regression expression for searching every words
    33     QRegExp* rule;
    34     // the format we plan to apply in keyword
    35     QTextCharFormat format;
    36 
    37     // the structure to store keywords. it is expected to store 8000 words in total.
    38     // More details in program report.
    39     unsigned char bitMap[10000];
    40 //    QVector<unsigned char> BloomFilter;
    41     // each keyword in the whitelist is considered not to be in the BloomFilter
    42     QVector<QString> whiteNameList;
    43 
    44 //    // the hash function count. 7 is best for 1% error possibility
    45 //    const int hashCount = 7;
    46 };
    47 
    48 #endif // MYHIGHLIGHT_H
  • 相关阅读:
    iOS开发官方文档汇总
    Hadoop安装配置手册
    访问.Net程序集、COM和WMI
    UML用例图教程详解
    JIRA的详细安装和破解
    [转]编程经典好书分类
    走向资深架构师的旅程
    12款响应式 Lightbox(灯箱)效果插件
    ASP.NET MVC应用程序的安全性介绍总括
    MapReduce篇之InputFormat
  • 原文地址:https://www.cnblogs.com/25th-engineer/p/9784588.html
Copyright © 2020-2023  润新知