diff --git a/src/FullSegment.hpp b/src/FullSegment.hpp index 410399e..b582f0d 100644 --- a/src/FullSegment.hpp +++ b/src/FullSegment.hpp @@ -15,23 +15,47 @@ namespace CppJieba class FullSegment: public SegmentBase { private: - DictTrie _dictTrie; - + const DictTrie* _dictTrie; + bool _isBorrowed; public: - FullSegment(){_setInitFlag(false);}; - explicit FullSegment(const string& dictPath){_setInitFlag(init(dictPath));} - virtual ~FullSegment(){}; + FullSegment() + { + _dictTrie = NULL; + _isBorrowed = false; + } + explicit FullSegment(const string& dictPath) + { + _dictTrie = NULL; + init(dictPath); + } + explicit FullSegment(const DictTrie* dictTrie) + { + _dictTrie = NULL; + init(dictTrie); + } + virtual ~FullSegment() + { + if(_dictTrie && ! _isBorrowed) + { + delete _dictTrie; + } + + }; public: bool init(const string& dictPath) { - if(_getInitFlag()) - { - LogError("already inited before now."); - return false; - } - _dictTrie.init(dictPath.c_str()); - assert(_dictTrie); - return _setInitFlag(true); + assert(_dictTrie == NULL); + _dictTrie = new DictTrie(dictPath); + _isBorrowed = false; + return true; + } + bool init(const DictTrie* dictTrie) + { + assert(_dictTrie == NULL); + assert(dictTrie); + _dictTrie = dictTrie; + _isBorrowed = true; + return true; } public: @@ -40,7 +64,7 @@ namespace CppJieba public: bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector& res) const { - assert(_getInitFlag()); + assert(_dictTrie); if (begin >= end) { LogError("begin >= end"); @@ -61,7 +85,7 @@ namespace CppJieba for (Unicode::const_iterator uItr = begin; uItr != end; uItr++) { //find word start from uItr - if (_dictTrie.find(uItr, end, tRes, 0)) + if (_dictTrie->find(uItr, end, tRes, 0)) { for(DagType::const_iterator itr = tRes.begin(); itr != tRes.end(); itr++) //for (vector >::const_iterator itr = tRes.begin(); itr != tRes.end(); itr++) @@ -94,7 +118,7 @@ namespace CppJieba bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector& res) const { - assert(_getInitFlag()); + assert(_dictTrie); if (begin >= end) { LogError("begin >= end"); diff --git a/src/Limonp/InitOnOff.hpp b/src/Limonp/InitOnOff.hpp index 926daab..563ef6a 100644 --- a/src/Limonp/InitOnOff.hpp +++ b/src/Limonp/InitOnOff.hpp @@ -13,7 +13,7 @@ namespace Limonp bool _getInitFlag()const{return _isInited;}; bool _setInitFlag(bool flag){return _isInited = flag;}; public: - operator bool(){return _getInitFlag();}; + operator bool() const {return _getInitFlag();}; }; } diff --git a/src/MPSegment.hpp b/src/MPSegment.hpp index 01834ce..1b8cde7 100644 --- a/src/MPSegment.hpp +++ b/src/MPSegment.hpp @@ -114,6 +114,10 @@ namespace CppJieba return true; } + const DictTrie* getDictTrie() const + { + return &_dictTrie; + } private: void _calcDP(vector& SegmentChars) const diff --git a/src/MixSegment.hpp b/src/MixSegment.hpp index 9cea09b..f8a4369 100644 --- a/src/MixSegment.hpp +++ b/src/MixSegment.hpp @@ -122,6 +122,11 @@ namespace CppJieba } return true; } + + const DictTrie* getDictTrie() const + { + return _mpSeg.getDictTrie(); + } }; } diff --git a/src/QuerySegment.hpp b/src/QuerySegment.hpp index f20029e..8f284eb 100644 --- a/src/QuerySegment.hpp +++ b/src/QuerySegment.hpp @@ -42,7 +42,7 @@ namespace CppJieba LogError("_mixSeg init"); return false; } - if (!_fullSeg.init(dict)) + if (!_fullSeg.init(_mixSeg.getDictTrie())) { LogError("_fullSeg init"); return false; diff --git a/src/SegmentBase.hpp b/src/SegmentBase.hpp index bab4eb0..6b3ca93 100644 --- a/src/SegmentBase.hpp +++ b/src/SegmentBase.hpp @@ -42,7 +42,6 @@ namespace CppJieba virtual bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector& res) const = 0; virtual bool cut(const string& str, vector& res) const { - assert(_getInitFlag()); res.clear(); Unicode unicode;