From 41c18b845c5379a3c8f5ecdfeea7dc05fde7dcf4 Mon Sep 17 00:00:00 2001 From: Yuri Gorshenin Date: Wed, 8 Feb 2017 16:50:50 +0300 Subject: [PATCH] [base] Implemented SafeSmallSet. --- base/small_set.hpp | 53 ++++++++++++++++++++++++++++++++++++++++- search/query_params.hpp | 34 +------------------------- 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/base/small_set.hpp b/base/small_set.hpp index 35c7afe7a1..1e1995d38a 100644 --- a/base/small_set.hpp +++ b/base/small_set.hpp @@ -154,7 +154,7 @@ uint64_t constexpr SmallSet::kNumBlocks; template uint64_t constexpr SmallSet::kOne; -template +template std::string DebugPrint(SmallSet const & set) { std::ostringstream os; @@ -164,4 +164,55 @@ std::string DebugPrint(SmallSet const & set) os << "]"; return os.str(); } + +// This is a delegate for SmallSet<>, that checks the validity of +// argument in Insert(), Remove() and Contains() methods and does +// nothing when the argument is not valid. +template +class SafeSmallSet +{ +public: + using Set = SmallSet; + using Iterator = typename Set::Iterator; + + void Insert(uint64_t value) + { + if (IsValid(value)) + m_set.Insert(value); + } + + void Remove(uint64_t value) + { + if (IsValid(value)) + m_set.Remove(value); + } + + bool Contains(uint64_t value) const { return IsValid(value) && m_set.Contains(value); } + + uint64_t Size() const { return m_set.Size(); } + + void Clear() { m_set.Clear(); } + + Iterator begin() const { return m_set.begin(); } + Iterator cbegin() const { return m_set.cbegin(); } + + Iterator end() const { return m_set.end(); } + Iterator cend() const { return m_set.cend(); } + +private: + bool IsValid(uint64_t value) const { return value < UpperBound; } + + Set m_set; +}; + +template +std::string DebugPrint(SafeSmallSet const & set) +{ + std::ostringstream os; + os << "SafeSmallSet<" << UpperBound << "> [" << set.Size() << ": "; + for (auto const & v : set) + os << v << " "; + os << "]"; + return os.str(); +} } // namespace base diff --git a/search/query_params.hpp b/search/query_params.hpp index 480ebee23c..d489deb6bc 100644 --- a/search/query_params.hpp +++ b/search/query_params.hpp @@ -23,39 +23,7 @@ class QueryParams public: using String = strings::UniString; using TypeIndices = vector; - - class Langs - { - public: - using Set = base::SmallSet; - using Iterator = Set::Iterator; - - void Insert(int8_t lang) - { - if (IsValid(lang)) - m_set.Insert(lang); - } - - bool Contains(int8_t lang) const { return IsValid(lang) ? m_set.Contains(lang) : false; } - - void Clear() { m_set.Clear(); } - - Iterator begin() const { return m_set.begin(); } - Iterator cbegin() const { return m_set.cbegin(); } - - Iterator end() const { return m_set.end(); } - Iterator cend() const { return m_set.cend(); } - - private: - friend string DebugPrint(Langs const & langs) { return DebugPrint(langs.m_set); } - - bool IsValid(int8_t lang) const - { - return lang >= 0 && lang < StringUtf8Multilang::kMaxSupportedLanguages; - } - - Set m_set; - }; + using Langs = base::SafeSmallSet; struct Token {