diff options
Diffstat (limited to 'src/libs')
-rw-r--r-- | src/libs/sqlite/sqlitebasestatement.cpp | 3 | ||||
-rw-r--r-- | src/libs/sqlite/sqlitebasestatement.h | 20 | ||||
-rw-r--r-- | src/libs/utils/set_algorithm.h | 59 |
3 files changed, 78 insertions, 4 deletions
diff --git a/src/libs/sqlite/sqlitebasestatement.cpp b/src/libs/sqlite/sqlitebasestatement.cpp index 91b417bea1..d9d677c395 100644 --- a/src/libs/sqlite/sqlitebasestatement.cpp +++ b/src/libs/sqlite/sqlitebasestatement.cpp @@ -274,6 +274,9 @@ void BaseStatement::bind(int index, ValueView value) void BaseStatement::prepare(Utils::SmallStringView sqlStatement) { + if (!m_database.isLocked()) + throw DatabaseIsNotLocked{}; + int resultCode; do { diff --git a/src/libs/sqlite/sqlitebasestatement.h b/src/libs/sqlite/sqlitebasestatement.h index 1178b97f3a..54d08260b7 100644 --- a/src/libs/sqlite/sqlitebasestatement.h +++ b/src/libs/sqlite/sqlitebasestatement.h @@ -49,6 +49,7 @@ public: BaseStatement(const BaseStatement &) = delete; BaseStatement &operator=(const BaseStatement &) = delete; + BaseStatement(BaseStatement &&) = default; bool next() const; void step() const; @@ -146,6 +147,7 @@ class StatementImplementation : public BaseStatement public: using BaseStatement::BaseStatement; + StatementImplementation(StatementImplementation &&) = default; void execute() { @@ -496,16 +498,30 @@ private: return createValue<ResultType>(std::make_integer_sequence<int, ResultCount>{}); } + template<typename Callable, typename... Arguments> + CallbackControl invokeCallable(Callable &&callable, Arguments &&...arguments) + { + if constexpr (std::is_void_v<std::invoke_result_t<Callable, Arguments...>>) { + std::invoke(std::forward<Callable>(callable), std::forward<Arguments>(arguments)...); + return CallbackControl::Continue; + } else { + return std::invoke(std::forward<Callable>(callable), + std::forward<Arguments>(arguments)...); + } + } + template<typename Callable, int... ColumnIndices> CallbackControl callCallable(Callable &&callable, std::integer_sequence<int, ColumnIndices...>) { - return std::invoke(callable, ValueGetter(*this, ColumnIndices)...); + return invokeCallable(std::forward<Callable>(callable), + ValueGetter(*this, ColumnIndices)...); } template<typename Callable> CallbackControl callCallable(Callable &&callable) { - return callCallable(callable, std::make_integer_sequence<int, ResultCount>{}); + return callCallable(std::forward<Callable>(callable), + std::make_integer_sequence<int, ResultCount>{}); } void setMaximumResultCount(std::size_t count) diff --git a/src/libs/utils/set_algorithm.h b/src/libs/utils/set_algorithm.h index a3e442f736..f6d3f73fed 100644 --- a/src/libs/utils/set_algorithm.h +++ b/src/libs/utils/set_algorithm.h @@ -60,8 +60,17 @@ bool set_intersection_compare( ++first1; } else { if (!comp(*first2, *first1)) { - if (call(*first1++, *first2)) - return true; + if constexpr (std::is_void_v<std::invoke_result_t<Callable, + decltype(*first1), + decltype(*first2)>>) { + call(*first1, *first2); + ++first1; + } else { + auto success = call(*first1, *first2); + ++first1; + if (success) + return true; + } } ++first2; } @@ -71,6 +80,52 @@ bool set_intersection_compare( } template<class InputIt1, class InputIt2, class Callable, class Compare> +bool set_greedy_intersection_compare( + InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Callable call, Compare comp) +{ + while (first1 != last1 && first2 != last2) { + if (comp(*first1, *first2)) { + ++first1; + } else { + if (!comp(*first2, *first1)) { + if constexpr (std::is_void_v<std::invoke_result_t<Callable, + decltype(*first1), + decltype(*first2)>>) { + call(*first1, *first2); + ++first1; + } else { + auto success = call(*first1, *first2); + ++first1; + if (success) + return true; + } + } else { + ++first2; + } + } + } + + return false; +} + +template<typename InputIt1, typename InputIt2, typename OutputIt> +constexpr OutputIt set_greedy_intersection( + InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt result) +{ + while (first1 != last1 && first2 != last2) + if (*first1 < *first2) + ++first1; + else if (*first2 < *first1) + ++first2; + else { + *result = *first1; + ++first1; + ++result; + } + return result; +} + +template<class InputIt1, class InputIt2, class Callable, class Compare> void set_greedy_difference( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Callable call, Compare comp) { |