From a08666f7771b6b728ef26e7d547fd52318439f3b Mon Sep 17 00:00:00 2001 From: Maxim Pimenov Date: Fri, 29 Jun 2018 17:36:01 +0300 Subject: [PATCH] Review fixes. --- search/base/text_index.hpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/search/base/text_index.hpp b/search/base/text_index.hpp index a500b147e2..6f546610cf 100644 --- a/search/base/text_index.hpp +++ b/search/base/text_index.hpp @@ -101,16 +101,20 @@ template class TextIndexDictionary { public: - bool GetTokenId(Token const & token, uint32_t & id) const + bool GetTokenId(Token const & token, size_t & id) const { auto const it = std::lower_bound(m_tokens.cbegin(), m_tokens.cend(), token); if (it == m_tokens.cend() || *it != token) return false; - id = static_cast(std::distance(m_tokens.cbegin(), it)); + id = ::base::checked_cast(std::distance(m_tokens.cbegin(), it)); return true; } - void SetTokens(std::vector && tokens) { m_tokens = std::move(tokens); } + void SetTokens(std::vector && tokens) + { + ASSERT(std::is_sorted(tokens.begin(), tokens.end()), ()); + m_tokens = std::move(tokens); + } std::vector const & GetTokens() const { return m_tokens; } template @@ -145,7 +149,7 @@ public: } template - void Deserialize(Source & source, TextIndexHeader header) + void Deserialize(Source & source, TextIndexHeader const & header) { auto const startPos = source.Pos(); @@ -362,10 +366,10 @@ template class TextIndexReader { public: - TextIndexReader(FileReader const & fileReader) : m_fileReader(fileReader) + explicit TextIndexReader(FileReader const & fileReader) : m_fileReader(fileReader) { - ReaderSource headerSource(m_fileReader); TextIndexHeader header; + ReaderSource headerSource(m_fileReader); header.Deserialize(headerSource); uint64_t const dictStart = header.m_dictPositionsOffset; @@ -386,16 +390,13 @@ public: template void ForEachPosting(Token const & token, Fn && fn) const { - uint32_t tokenId = 0; + size_t tokenId = 0; if (!m_dictionary.GetTokenId(token, tokenId)) return; CHECK_LESS(tokenId + 1, m_postingsStarts.size(), ()); - uint64_t const allPostingsStart = m_header.m_postingsListsOffset; - uint64_t const tokenPostingsStart = allPostingsStart + m_postingsStarts[tokenId]; - uint64_t const tokenPostingsEnd = allPostingsStart + m_postingsStarts[tokenId + 1]; - ReaderSource source( - m_fileReader.SubReader(tokenPostingsStart, tokenPostingsEnd - tokenPostingsStart)); + ReaderSource source(m_fileReader.SubReader( + m_postingsStarts[tokenId], m_postingsStarts[tokenId + 1] - m_postingsStarts[tokenId])); uint32_t last = 0; while (source.Size() > 0) @@ -407,7 +408,6 @@ public: private: FileReader m_fileReader; - TextIndexHeader m_header; TextIndexDictionary m_dictionary; std::vector m_postingsStarts; };