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) bool Segment::cutDAG(const string& str, vector<string>& res)
@ -207,36 +207,20 @@ int main()
cerr<<"1"<<endl; cerr<<"1"<<endl;
return 1; return 1;
} }
getchar();
//segment.init("dicts/jieba.dict.utf8"); //segment.init("dicts/jieba.dict.utf8");
ifstream ifile("testtitle");
vector<string> res; vector<string> res;
string title; string line;
title = "我来到北京清华大学"; while(getline(ifile, line))
{
res.clear(); res.clear();
segment.cutDAG(title, res); segment.cutDAG(line, res);
PRINT_VECTOR(res); PRINT_VECTOR(res);
getchar(); getchar();
}
cout<<__FILE__<<__LINE__<<endl;
title = "特价camel骆驼 柔软舒适头层牛皮平底凉鞋女 休闲平跟妈妈鞋夏"; segment.dispose();
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();
return 0; return 0;
} }

View File

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

View File

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

View File

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