刁肥宅資料結構課設“布隆過濾器的實踐與應用”原始碼(v1.0,永不上交)
程式碼很簡單,寫了一些註釋;加上註釋看就很清楚了。
檔案bloomfilter.cpp:
#include "bloomfilter.h" // return a hash range from 0 to 79999 int hash(const char* str, int index) { int hash = 1; int seed = 12345; int curr; switch(index) { case 0:{ while(curr = int(*str++)) { hash = 128 * hash + curr; } return abs(hash%80000); } case 1:{ while(curr = int(*str++)) { hash = (25536 * hash + curr)%80000; } return abs(hash); } case 2:{ while(curr = int(*str++)) { hash = (seed * hash + curr)%80000; seed *= 123; } return abs(hash); } case 3:{ while(curr = int(*str++)) { hash += curr*curr; } return abs(hash%80000); } case 4:{ while(curr = int(*str++)) { hash += abs(curr*curr*curr); } return abs(hash%80000); } case 5:{ while(curr = int(*str++)) { hash *= (hash + curr*seed)%80000; } return abs(hash%80000); } case 6:{ while(curr = int(*str++)) { seed = 345; hash = (seed * hash + curr)%80000; seed *= 345; } return abs(hash); } } return -1; } void initBitMap(unsigned char* bitMap) { for(int i = 0; i<10000;i++) { bitMap[i] = 0; } } bool isKeyExistInBitMap(unsigned char* bitMap, const char* str) { for(int i = 0; i<7; i++) { int code = hash(str, i); if(!((bitMap[code/8] >> code%8) % 2)) { return false; } } return true; } void appendKey2BitMap(unsigned char* bitMap, const char* str) { if(isKeyExistInBitMap(bitMap, str)) { return; } for(int i = 0; i<7; i++) { int code = hash(str, i); if(!((bitMap[code/8] >> code%8) % 2)) { bitMap[code/8] += 1 << code%8; } } }
檔案bloomfilter.h:
#ifndef BLOOMFILTER_H #define BLOOMFILTER_H #include <stdio.h> #include <math.h> int hash(const char* str, int index); void initBitMap(unsigned char* bitMap); bool isKeyExistInBitMap(unsigned char* bitMap, const char* str); void appendKey2BitMap(unsigned char* bitMap, const char* str); #endif // BLOOMFILTER_H
檔案main.cpp:
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; /*w.show();*/ QIcon icon("E:/code/Qt/build-TangTang-DFZ-Release/release/th.jpg"); w.setWindowIcon(icon); w.setWindowTitle("Simple Code Editor"); w.show(); return a.exec(); }
檔案mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSyntaxHighlighter>
#include <QPushButton>
#include <QObject>
#include <QTextEdit>
#include "myhighlight.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFont font;
font.setFamily("Consolas");
font.setFixedPitch(true);
font.setPointSize(20);
editor = new QTextEdit;
editor->setFont(font);
highlighter = new myHighLight(editor->document());
setCentralWidget(editor);
setWindowTitle("Simple C++ Code Editor");
connect(ui->actionappend_key, &QAction::triggered, this, &MainWindow::appendKey);
connect(ui->actionDelete_key, &QAction::triggered, this, &MainWindow::deleteKey);
}
MainWindow::~MainWindow()
{
delete editor;
delete ui;
}
void MainWindow::appendKey()
{
QString selectedText = editor->textCursor().selectedText();
highlighter->appendKey(selectedText);
highlighter->setDocument(editor->document());
}
void MainWindow::deleteKey()
{
QString selectedText = editor->textCursor().selectedText();
highlighter->deleteKey(selectedText);
highlighter->setDocument(editor->document());
}
檔案mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "myhighlight.h"
#include <QTextEdit>
#include <QtCore>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
// append selected text for ui
void appendKey();
void deleteKey();
private:
Ui::MainWindow *ui;
QTextEdit *editor;
myHighLight *highlighter;
};
#endif // MAINWINDOW_H
檔案mainwindow.ui自己拖一拖就行!
檔案myhighlight.cpp:
#include "myhighlight.h"
#include "bloomfilter.cpp"
#include <QtGui>
myHighLight::myHighLight(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
// find each word
rule = new QRegExp("(\\b)(\\w+)(\\b)");
format.setForeground(Qt::blue);
format.setFontWeight(QFont::Bold);
initBitMap(bitMap);
QStringList keyWords = cppKeyword();
for(QString key : keyWords)
{
appendKey(key);
}
}
myHighLight::~myHighLight()
{
if(rule)
delete rule;
}
QStringList myHighLight::cppKeyword()
{
return QStringList()<<"asm"<<"auto"<<"bool"<<"break"<<"case"<<"catch"<<"char"
<<"class"<<"const"<<"const_cast"<<"continue"<<"default"<<"delete"
<<"do"<<"double"<<"dynamic_cast"<<"else"<<"enum"<<"explicit"
<<"export"<<"extern"<<"false"<<"float"<<"for"<<"friend"<<"goto"
<<"if"<<"inline"<<"int"<<"long"<<"mutable"<<"namespace"<<"new"
<<"operator"<<"private"<<"protected"<<"public"<<"register"
<<"reinterpret_cast"<<"return"<<"short"<<"signed"<<"sizeof"<<"static"
<<"static_cast"<<"struct"<<"switch"<<"template"<<"this"<<"throw"
<<"true"<<"try"<<"typedef"<<"typeid"<<"typename"<<"union"<<"unsigned"
<<"using"<<"virtual"<<"void"<<"volatile"<<"wchar_t"<<"while";
}
//QVector<int> myHighLight::Hash(QString word)
//{
// std::hash<std::string> str_hash;
// QVector<int> res;
// if(word.size()%hashCount == 0)
// {
// word += " ";
// }
// QString key;
// while(key.size() < 100)
// {
// key += word;
// }
// for(int i = 0; i<hashCount; i++)
// {
// QString key0;
// for(int j = i;j<key.size(); j+=hashCount)
// {
// key0.append(key[j]);
// }
// res.append(abs(int(str_hash(key0.toStdString()))%(BloomFilter.size()*8-1)));
// }
// return res;
//}
void myHighLight::appendKey(QString key)
{
if(!rule->exactMatch(key))
{
qDebug()<<"KeyWord "<<key<<" is not a word";
return;
}
if(whiteNameList.contains(key))
{
whiteNameList.removeOne(key);
}
appendKey2BitMap(bitMap, key.toStdString().data());
// if(isKeyExisted(key))
// {
// qDebug()<<"KeyWord "<<key<<" alredy existed!";
// return;
// }
// QVector<int> hash = Hash(key);
// for(auto code : hash)
// {
// if(!((BloomFilter[code/8] >> code%8) % 2))
// {
// BloomFilter[code/8] += 1 << code%8;
// }
// }
}
void myHighLight::deleteKey(QString key)
{
if(!rule->exactMatch(key))
{
qDebug()<<"KeyWord "<<key<<" is not a word";
return;
}
if(whiteNameList.contains(key))
{
qDebug()<<"KeyWord "<<key<<" to be deleted has already been in whitelist";
return;
}
if(!isKeyExisted(key))
{
qDebug()<<"KeyWord "<<key<<" to be deleted is not in the BloomFilter!";
return;
}
whiteNameList.push_back(key);
qDebug()<<whiteNameList;
}
bool myHighLight::isKeyExisted(QString key)
{
if(whiteNameList.contains(key))
{
return false;
}
return isKeyExistInBitMap(bitMap, key.toStdString().data());
// QVector<int> hash = Hash(key);
// for(auto code : hash)
// {
// if(!((BloomFilter[code/8] >> code%8) % 2))
// {
// return false;
// }
// }
// return true;
}
void myHighLight::highlightBlock(const QString &text)
{
int index = rule->indexIn(text);
while(index >= 0)
{
int length = rule->matchedLength();
if(isKeyExisted(text.mid(index, length)))
{
setFormat(index, length, format);
}
index = rule->indexIn(text, index + length);
}
}
檔案myhighlight.h:
#ifndef MYHIGHLIGHT_H
#define MYHIGHLIGHT_H
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
class QTextDocument;
class myHighLight : public QSyntaxHighlighter
{
Q_OBJECT
public:
myHighLight(QTextDocument *parent = 0);
~myHighLight();
static QStringList cppKeyword();
// get a Hash Value Array from a word
QVector<int> Hash(QString word);
// append a keyword to the bloom filter
void appendKey(QString key);
// delete a keyword from the bloom filter
void deleteKey(QString key);
// judge if the word is existing in the bloom filter
bool isKeyExisted(QString key);
protected:
// highlight the keyword block on updating document
void highlightBlock(const QString &text);
private:
// regression expression for searching every words
QRegExp* rule;
// the format we plan to apply in keyword
QTextCharFormat format;
// the structure to store keywords. it is expected to store 8000 words in total.
// More details in program report.
unsigned char bitMap[10000];
// QVector<unsigned char> BloomFilter;
// each keyword in the whitelist is considered not to be in the BloomFilter
QVector<QString> whiteNameList;
// // the hash function count. 7 is best for 1% error possibility
// const int hashCount = 7;
};
#endif // MYHIGHLIGHT_H
相關推薦
刁肥宅資料結構課設“布隆過濾器的實踐與應用”原始碼(v1.0,永不上交)
程式碼很簡單,寫了一些註釋;加上註釋看就很清楚了。 檔案bloomfilter.cpp: #include "bloomfilter.h" // return a hash range from 0 to 79999 int hash(con
資料結構課設 詞頻統計
5-20 詞頻統計 (30分) 請編寫程式,對一段英文文字,統計其中所有不同單詞的個數,以及詞頻最大的前10%的單詞。 所謂“單詞”,是指由不超過80個單詞字元組成的連續字串,但長度超過15的單詞將只擷取保留前15個單詞字元。而合法的“單詞字元”為大小寫字母、數
資料結構課設 銀行排隊問題之單佇列多視窗服務
5-7 銀行排隊問題之單佇列多視窗服務 (25分) 假設銀行有KK個視窗提供服務,視窗前設一條黃線,所有顧客按到達時間在黃線後排成一條長龍。當有視窗空閒時,下一位顧客即去該視窗處理事務。當有
#C++#資料結構課設踩的坑
題目是超市選址問題,但要求實現圖形介面,搭了Qt,圖形介面還是比較好實現的,但是因為大一一年一直都在鹹魚和Ctrl+c,所以幾乎相當於第一次自己獨立寫一個小程式。基礎不牢,地動山搖,有的坑我現在也沒明白emmmmm。。。 return返回函式執行處
通訊錄——資料結構課設
幫同學寫了n多課設,覺得蠻簡單,沒怎麼在意,這又花20min幫人寫了一個,決定發在部落格上,以後有誰要類似的就直接給個連結, ;-) 機智的窩 任務要求: 題目描述:通訊錄的基本屬性包括編號、姓名、性別、住址、聯絡電話等。要求實現最基本的功能模組如
資料結構課設 列印學生選課清單 (Hash)
5-24 列印學生選課清單 (25分) 假設全校有最多40000名學生和最多2500門課程。現給出每門課的選課學生名單,要求輸出每個前來查詢的學生的選課清單。 輸入格式: 輸入的第一行是兩
資料結構課設--用B樹實現圖書管理系統
此文章是分享一下上學期資料結構課程的課程設計,我選擇的是以B樹為資料結構,開發一個圖書管理系統,B樹的優點在於查詢快,增刪結點相對於連結串列或者順序表效率更好,因此用來儲存大量圖書資訊更加合適。(開發環境為:vs2015) 如需要完整工程檔案、說明文件以及可執
【資料結構】二叉樹的建立與遍歷(遞迴)
該程式全是使用遞迴的操作 執行環境是:Dev-C++ #include <stdio.h> #include <stdlib.h> typedef struct node{ char data; struct node *lchild,*rchild; }bi
Python 資料結構與演算法——列表(連結串列,linked list)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
資料結構 筆記:遞迴的思想與應用
遞迴是一種數學上分而自治的思想 -將原問題分解為規模較小的問題進行處理 ·分解後的問題與原問題的型別完全相同,單規模較小 ·通過小規模問題的解,能夠輕易求得原問題的解 -問題的分解是有限的(遞迴不能無限進行) ·當邊界條件不滿足時,分解問題(遞迴繼續進行) ·當邊界條件不滿足
資料結構之線性表演算法的構建與應用
//資料結構值順序線性表的實現與應用 //c++程式碼進行實現 //sqlist.h程式碼 //線性表的初始化 #def
資料結構-排序-快速排序之固定位置選取基準法(遞迴/非遞迴)
快速排序: 資料量小的時候,使用插入排序;聚焦相同基準元素法 固定位置選取排序:資料越有序,複雜度越高 隨機選取基準排序 三分取中法 思想:均勻的分割待排序序列 :選取一個基準par,定義兩個指標 low high 一般基準選取low下標的值;如果
資料結構之---C語言實現最短路徑之Dijkstra(迪傑斯特拉)演算法
此處共有兩段程式碼: 一、 這段程式碼比較全面,其中參考了github上的相關原始碼。可以說功能強大。 //Dijkstra(迪傑斯特拉演算法) #include <stdio.h> #include <stdlib.h> #include <
資料結構| |二叉樹的三種遍歷方式(遞迴&&非遞迴)
首先來寫一下遞迴的! 對於遞迴要將大問題轉化為小問題,並且要有一個結束的位置。 比如:要前序遍歷一個二叉樹,那就是先訪問根節點,然後在訪問根節點的左子樹,在訪問根節點的右子樹,而左子樹與右子樹,又可以變成訪問該節點和該結點的左子樹和右子樹。這就變成了一個遞迴
Bloom filter(布隆過濾器)概念與原理
概念 int 復雜 gravity water pac 基數 AS class https://en.wikipedia.org/wiki/Bloom_filter 寫在前面 在大數據與雲計算發展的時代,我們經常會碰到這樣的問題。我們是否能高效的判斷一個用
布隆過濾器簡述及應用
一、布隆過濾器 1、維基百科 布隆過濾器(Bloom Filter)是1970年由布隆提出的。 實際上是一個很長的二進位制向量和一系列隨機對映函式。布隆過濾器可以用於檢索一個元素是否在一個集合中。 優點是不需要儲存 key,節省空間,空間效率和查詢時間都遠遠超過一般的演算法,缺點是有一定的
刁肥宅詳解中綴表示式求值問題:C++實現順序/鏈棧解決
1. 表示式的種類 如何將表示式翻譯成能夠正確求值的指令序列,是語言處理程式要解決的基本問題,作為棧的應用事例,下面介紹表示式的求值過程。 任何一個表示式都是由
資料結構--雜湊擴充套件 ( 布隆過濾器 )
布隆過濾器(Bloom Filter)是1970年由布隆提出的。它實際上是一個很長的二進位制向量和一系列隨機對映函式。布隆過濾器可以用於檢索一個元素是否在一個集合中。它的優點是空間效率和查詢時間都遠遠超過一般的演算法,缺點是有一定的誤識別率和刪除困難。 基本概念 : 如果想
資料結構課上筆記2
今天繼續說明了一些基本概念,講解了時間空間複雜度。 (對於概念的掌握也很最重要) 元素之間的關係在計算機中有兩種表示方法:順序映像和非順序映像,由此得到兩種不同的 儲存結構:順序儲存結構和鏈式儲存結構。 順序:根據元素在儲存器中的相對位置表示關係 鏈式:藉助指標
資料結構課上筆記15
圖的儲存 多重連結串列:完全模擬圖的樣子,每個節點內的指標都指向該指向的節點。 節點結構內指標數為度 缺點:浪費空間、不容易操作 陣列表示法(鄰接矩陣表示法) 可用兩個陣列儲存。其中一個 一維陣列儲存資料元素(頂點)的資訊,另一個二維