【基础知识】使用Darts
2012-12-04 20:17
295 查看
Darts是double array的一个c++ template实现。经过企业级大规模数据实践检验,其效率远高于stl中的hash_set、hash_map等。
不过Darts是用c++ template实现的,初看起来,会被他的模版参数、以及函数参数等吓到,不知道怎么用。我这里写了个简单的小例子,来看一下它的使用。基本使用情景就是我有一个词表,将这个词表load到内存,转换成double array的形式,并来查询用户输入的字符串是否在当前词表当中。
贴代码。第一个函数,从文本文件中load词表,并据此构建双数组,再序列化到文件当中。
说三点:
模版默认参数就是为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,并给用户查询:
这里调用的是exactMatchSearch函数,表示完全匹配。还有commonPrefixSearch,匹配公共前缀。
总结一下。可以看到,如果要用darts,必须要将现有的内容通过build函数构建双数组结构。在函数接口中,除了build函数,没有其他的函数能够增、删、改双数组的内容了。这说明这个工具适合用来存储资源,并提供访问接口。在效率上,个人经过大规模数据实践检验,darts效率远高于hash_set。
darts详细情况请参考上一篇转载的博文《Darts: Double-ARray Trie System》。
不过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 vector vector<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 array char ** 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 array Darts::DoubleArray theDoubleAry; theDoubleAry.build (LexVec.size(), pStrAry, 0, 0); // 1.4 save it theDoubleAry.save ("LexDoubleAry"); // 1.5 clean up free (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 array Darts::DoubleArray theDoubleAry; if (-1 == theDoubleAry.open ("LexDoubleAry")) return; // 2.2 look it up do { 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; else cout << "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》。
相关文章推荐
- C#基础知识之if与switcth的使用
- [转]使用Aspose.Cells的基础知识整理
- memcached系列之1:memcached基础知识简介(为什么要使用memcached做缓存服务器)
- Java基础知识强化87:BigInteger类之BigInteger加减乘除法的使用
- JUnit基础知识和使用
- php基础知识的灵活使用
- Oracle基础知识-sqlplus使用
- Notification(一)——基础知识使用示例
- 使用Jquery解析Json基础知识
- Java 基础知识 mySQL使用 (二)
- 【Unity基础知识之五】Unity3d-在Unity项目中使用Mono DLL(动态链接库)
- MongoDB使用学习(一)-基础知识
- 使用Forms Authentication实现用户注册、登录 (一)基础知识
- Java基础知识强化之集合框架笔记09:Collection集合迭代器使用的问题探讨
- 【IOS基础知识】NSTimer定时器使用
- 使用matlab处理图像的基础知识
- 使用tf.contrib.learn记录和监控基础知识
- java基础知识5--集合类(Set,List,Map)和迭代器Iterator的使用
- Java基础知识强化之IO流笔记66:Properties的概述 和 使用(作为Map集合使用)