【基础知识】使用Darts
来源:互联网 发布:淡雅女士香水知乎 编辑:程序博客网 时间:2024/06/10 09:17
Darts是double array的一个c++ template实现。经过企业级大规模数据实践检验,其效率远高于stl中的hash_set、hash_map等。
不过Darts是用c++ template实现的,初看起来,会被他的模版参数、以及函数参数等吓到,不知道怎么用。我这里写了个简单的小例子,来看一下它的使用。基本使用情景就是我有一个词表,将这个词表load到内存,转换成double array的形式,并来查询用户输入的字符串是否在当前词表当中。
贴代码。第一个函数,从文本文件中load词表,并据此构建双数组,再序列化到文件当中。
#include "darts.h"#include <iostream>#include <string>#include <vector>#include <fstream>#include <algorithm>using namespace std;void BuildDoubleAryFrmLex (void){// 1. load the lexicon and build double array// 1.1 load the lexicon and fill in a vectorvector<string> LexVec;ifstream in ("Lex.txt");if (!in){cerr << "Can not open the file of Lex.txt" << endl;return;}string sTmp;while (in >> sTmp)LexVec.push_back (sTmp);sort (LexVec.begin(), LexVec.end());// it's necessary for darts// 1.2 make LexVec as a c-style string array, and make the value arraychar ** pStrAry = (char **)malloc (LexVec.size() * sizeof(char *));vector<string>::iterator pIter = LexVec.begin();int i = 0;while (pIter != LexVec.end()){pStrAry[i] = (char*)pIter->c_str();pIter++;i++;}// 1.3 build the double arrayDarts::DoubleArray theDoubleAry;theDoubleAry.build (LexVec.size(), pStrAry, 0, 0);// 1.4 save ittheDoubleAry.save ("LexDoubleAry");// 1.5 clean upfree (pStrAry);}
说三点:
- 模版默认参数就是为c-style字符串准备的。所以用Darts::DoubleArray (注意加上“Darts”命名空间)来声明双数组,默认就是Darts::DoubleArray<char, unsigned char, int, unsigned int>。
- 在调用build函数构建双数组之前,所有字符串是要按照字符顺序排序的(在fill LexVec之后,调用sort函数),否则会出现异常
- 如果是想模拟hash_set的作用,build的后两个参数用“0”就行了;如果是要模拟hash_map,则,前面两个参数是描述key,后面两个是对应的val
第二个函数,load double array,并给用户查询:
void LookUpDoubleAry (void){// 2. load the double array and look up// 2.1 load the double arrayDarts::DoubleArray theDoubleAry;if (-1 == theDoubleAry.open ("LexDoubleAry"))return;// 2.2 look it updo {cout << "input a word (quit for terminate this): " << endl;string sWord;cin >> sWord;if ("quit" == sWord)break;else{int iVal = 0;theDoubleAry.exactMatchSearch (sWord.c_str(), iVal);if (-1 == iVal)cout << "not in the lexicon" << endl;elsecout << "bingo!" << endl;}} while (1);cout << "Bye" << endl;}int main (void){cout << "Now playing with Darts!" << endl;// BuildDoubleAryFrmLex ();LookUpDoubleAry();return 0;}
这里调用的是exactMatchSearch函数,表示完全匹配。还有commonPrefixSearch,匹配公共前缀。
总结一下。可以看到,如果要用darts,必须要将现有的内容通过build函数构建双数组结构。在函数接口中,除了build函数,没有其他的函数能够增、删、改双数组的内容了。这说明这个工具适合用来存储资源,并提供访问接口。在效率上,个人经过大规模数据实践检验,darts效率远高于hash_set。
darts详细情况请参考上一篇转载的博文《Darts: Double-ARray Trie System》。
- 【基础知识】使用Darts
- Darts
- 【基础知识】Darts: Double-ARray Trie System
- java codebase的Hadoop应用中使用Darts
- poj1959 Darts
- DARTS v.s. RTSAD
- POJ1959 Darts 暴力
- ZOJ3720 Magnet Darts
- poj 1959 Darts
- [TOJ 1732] Darts
- poj 1959 Darts
- poj 杂题 - 1959 Darts
- poj 1959 Darts
- POJ-1959 Darts
- 【CERC2012】【BZOJ4063】Darts
- poj 1959 Darts
- LA6262 Darts 浮点数处理
- HDU 4074 Darts 概率dp
- libc.so.6: not found
- 判断输入的日期是一年中第几天(调用函数)
- 虚拟机VMware3种网络模式(桥接、nat、Host-only)的工作原理
- android SIM卡状态
- Android控件拖动
- 【基础知识】使用Darts
- 电池电量分析 从上层到底层
- linux设置IP
- JAVA如何实现条件编译(从思路开始)
- 自定义el表达式方法
- atoi 和itoa用法
- 自动变色的小程序(点击可停止变色也可显示当前颜色的16进制数)
- 设计模式学习-----外观模式
- Hibernate关系映射——组件映射