From f9862af6f481080a2c0d0c49f926c4af590da465 Mon Sep 17 00:00:00 2001 From: Yury Melnichek Date: Tue, 27 Sep 2011 21:46:23 +0200 Subject: [PATCH] [search] Allow to specify string array in KeywordMatcher constructor. --- search/keyword_matcher.cpp | 50 +++++++++++++++++++++++++++++++------- search/keyword_matcher.hpp | 10 ++++++-- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/search/keyword_matcher.cpp b/search/keyword_matcher.cpp index 67e0ca78b3..9d8a487cc1 100644 --- a/search/keyword_matcher.cpp +++ b/search/keyword_matcher.cpp @@ -5,23 +5,53 @@ #include "../base/string_utils.hpp" #include "../std/algorithm.hpp" -search::KeywordMatcher::KeywordMatcher(strings::UniString const * const * pKeywords, - int keywordCount, - strings::UniString const * pPrefix) - : m_pKeywords(pKeywords), m_keywordCount(keywordCount), m_pPrefix(pPrefix) +namespace search { - ASSERT_LESS(m_keywordCount, int(MAX_TOKENS), ()); - m_keywordCount = min(m_keywordCount, int(MAX_TOKENS)); + +KeywordMatcher::KeywordMatcher(strings::UniString const * const * pKeywords, + size_t keywordCount, + strings::UniString const * pPrefix) + : m_pKeywords(pKeywords), m_keywordCount(keywordCount), m_pPrefix(pPrefix), m_bOwnKeywords(false) +{ + Initialize(); +} + +KeywordMatcher::KeywordMatcher(strings::UniString const * keywords, + size_t keywordCount, + strings::UniString const * pPrefix) + : m_keywordCount(keywordCount), m_pPrefix(pPrefix), m_bOwnKeywords(false) +{ + Initialize(); + if (m_keywordCount > 0) + { + strings::UniString const * * pKeywords = new strings::UniString const * [m_keywordCount]; + for (size_t i = 0; i < m_keywordCount; ++i) + pKeywords[i] = &keywords[i]; + m_bOwnKeywords = true; + m_pKeywords = pKeywords; + } +} + +void KeywordMatcher::Initialize() +{ + ASSERT_LESS(m_keywordCount, size_t(MAX_TOKENS), ()); + m_keywordCount = min(m_keywordCount, size_t(MAX_TOKENS)); if (m_pPrefix && m_pPrefix->empty()) m_pPrefix = NULL; } -uint32_t search::KeywordMatcher::Score(string const & name) const +KeywordMatcher::~KeywordMatcher() +{ + if (m_bOwnKeywords) + delete [] m_pKeywords; +} + +uint32_t KeywordMatcher::Score(string const & name) const { return Score(NormalizeAndSimplifyString(name)); } -uint32_t search::KeywordMatcher::Score(strings::UniString const & name) const +uint32_t KeywordMatcher::Score(strings::UniString const & name) const { buffer_vector tokens; SplitUniString(name, MakeBackInsertFunctor(tokens), Delimiters()); @@ -29,7 +59,7 @@ uint32_t search::KeywordMatcher::Score(strings::UniString const & name) const return Score(tokens.data(), static_cast(tokens.size())); } -uint32_t search::KeywordMatcher::Score(strings::UniString const * tokens, int tokenCount) const +uint32_t KeywordMatcher::Score(strings::UniString const * tokens, int tokenCount) const { ASSERT_LESS(tokenCount, int(MAX_TOKENS), ()); @@ -73,3 +103,5 @@ uint32_t search::KeywordMatcher::Score(strings::UniString const * tokens, int to return score; } + +} // namespace search diff --git a/search/keyword_matcher.hpp b/search/keyword_matcher.hpp index 3dcced4254..10e44b67d3 100644 --- a/search/keyword_matcher.hpp +++ b/search/keyword_matcher.hpp @@ -13,8 +13,11 @@ class KeywordMatcher public: enum { MAX_SCORE = MAX_TOKENS }; - KeywordMatcher(strings::UniString const * const * pKeywords, int keywordCount, + KeywordMatcher(strings::UniString const * const * pKeywords, size_t keywordCount, strings::UniString const * pPrefix); + KeywordMatcher(strings::UniString const * keywords, size_t keywordCount, + strings::UniString const * pPrefix); + ~KeywordMatcher(); // Returns penalty (which is less than MAX_SCORE) if name matched, or MAX_SCORE otherwise. @@ -23,9 +26,12 @@ public: uint32_t Score(strings::UniString const * tokens, int tokenCount) const; private: + void Initialize(); + strings::UniString const * const * m_pKeywords; - int m_keywordCount; + size_t m_keywordCount; strings::UniString const * m_pPrefix; + bool m_bOwnKeywords; }; } // namespace search