fix dispose bug in trie.cpp

This commit is contained in:
gwdwyy 2013-07-21 10:18:43 +08:00
parent 25ab67a503
commit 196d2d563f
4 changed files with 35 additions and 58 deletions

View File

@ -35,9 +35,9 @@ namespace CppJieba
}
bool Segment::destroy()
bool Segment::dispose()
{
return _trie.destroy();
return _trie.dispose();
}
bool Segment::cutDAG(const string& str, vector<string>& res)
@ -207,36 +207,20 @@ int main()
cerr<<"1"<<endl;
return 1;
}
getchar();
//segment.init("dicts/jieba.dict.utf8");
ifstream ifile("testtitle");
vector<string> res;
string title;
title = "我来到北京清华大学";
res.clear();
segment.cutDAG(title, res);
PRINT_VECTOR(res);
getchar();
string line;
while(getline(ifile, line))
{
res.clear();
segment.cutDAG(line, res);
PRINT_VECTOR(res);
getchar();
}
cout<<__FILE__<<__LINE__<<endl;
title = "特价camel骆驼 柔软舒适头层牛皮平底凉鞋女 休闲平跟妈妈鞋夏";
res.clear();
segment.cutDAG(title, res);
PRINT_VECTOR(res);
getchar();
title = "包邮拉菲草18cm大檐进口草帽子超强遮阳防晒欧美日韩新款夏天 女";
res.clear();
segment.cutDAG(title, res);
PRINT_VECTOR(res);
getchar();
title = "2013新款19CM超大檐帽 遮阳草帽子 沙滩帽防晒大檐欧美新款夏天女";
res.clear();
segment.cutDAG(title, res);
PRINT_VECTOR(res);
getchar();
segment.destroy();
segment.dispose();
return 0;
}

View File

@ -24,7 +24,7 @@ namespace CppJieba
public:
bool init();
bool loadSegDict(const string& filePath);
bool destroy();
bool dispose();
public:
bool cutDAG(const string& chStr, vector<string>& res);
double getUtf8WordWeight(const string& word);

View File

@ -35,7 +35,7 @@ namespace CppJieba
Trie::~Trie()
{
destroy();
dispose();
}
bool Trie::setEncoding(const string& enc)
@ -53,14 +53,14 @@ namespace CppJieba
{
return _initFlag;
}
void Trie::_setInitFlag()
void Trie::_setInitFlag(bool on)
{
_initFlag = true;
_initFlag = on;
}
bool Trie::init()
{
if(!_getInitFlag())
if(_getInitFlag())
{
LogError("already initted!");
return false;
@ -78,7 +78,7 @@ namespace CppJieba
{
return false;
}
_setInitFlag();
_setInitFlag(true);
return true;
}
@ -155,29 +155,23 @@ namespace CppJieba
return true;
}
bool Trie::destroy()
bool Trie::dispose()
{
if(!_getInitFlag())
{
return false;
}
else
{
bool ret = _destroyNode(_root);
_root = NULL;
return ret;
}
_nodeInfoVec.clear();
}
void Trie::display()
{
for(uint i = 0; i < _nodeInfoVec.size(); i++)
bool ret = _deleteNode(_root);
if(!ret)
{
cout<<_nodeInfoVec[i].word<<","
<<_nodeInfoVec[i].count<<","
<<endl;
LogFatal("_deleteNode failed!");
return false;
}
_root = NULL;
_nodeInfoVec.clear();
_setInitFlag(false);
return ret;
}
const TrieNodeInfo* Trie::findPrefix(const string& str)
@ -304,12 +298,12 @@ namespace CppJieba
return _totalCount;
}
bool Trie::_destroyNode(TrieNode* node)
bool Trie::_deleteNode(TrieNode* node)
{
for(TrieNodeMap::iterator it = node->hmap.begin(); it != node->hmap.end(); it++)
{
TrieNode* next = it->second;
_destroyNode(next);
_deleteNode(next);
}
delete node;
@ -454,7 +448,7 @@ int main()
trie.loadDict("../dicts/segdict.utf8.v2.1");
cout<<trie.getMinWeight()<<endl;
cout<<trie.getTotalCount()<<endl;
trie.destroy();
trie.dispose();
return 0;
}
#endif

View File

@ -82,11 +82,10 @@ namespace CppJieba
bool init();
bool loadDict(const string& filePath);
bool setEncoding(const string& enc);
bool destroy();
void display();
bool dispose();
private:
void _setInitFlag();
void _setInitFlag(bool on);
bool _getInitFlag();
public:
@ -110,7 +109,7 @@ namespace CppJieba
private:
bool _buildTree(const string& filePath);
bool _countWeight();
bool _destroyNode(TrieNode* node);
bool _deleteNode(TrieNode* node);
const TrieNodeInfo* _findUniStr(const string& uniStr);
public: