From dbf88adcb15830e52e4e234ad854db0383da6704 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Mon, 22 Aug 2011 01:01:01 +0000 Subject: [PATCH] dbms: development [#CONV-2944]. --- dbms/include/DB/Functions/FunctionsLogical.h | 368 +++++++++++++++++++ dbms/src/Interpreters/tests/expression.cpp | 35 +- 2 files changed, 393 insertions(+), 10 deletions(-) create mode 100644 dbms/include/DB/Functions/FunctionsLogical.h diff --git a/dbms/include/DB/Functions/FunctionsLogical.h b/dbms/include/DB/Functions/FunctionsLogical.h new file mode 100644 index 00000000000..a87f90c654c --- /dev/null +++ b/dbms/include/DB/Functions/FunctionsLogical.h @@ -0,0 +1,368 @@ +#pragma once + +#include + +#include +#include +#include + + +namespace DB +{ + +/** Функции - логические связки: and, or, not, xor. + * Принимают любые числовые типы, возвращают UInt8, содержащий 0 или 1. + */ + +template +struct AndImpl +{ + static void vector_vector(const std::vector & a, const std::vector & b, std::vector & c) + { + size_t size = a.size(); + for (size_t i = 0; i < size; ++i) + c[i] = a[i] && b[i]; + } + + static void vector_constant(const std::vector & a, B b, std::vector & c) + { + size_t size = a.size(); + for (size_t i = 0; i < size; ++i) + c[i] = a[i] && b; + } + + static void constant_vector(A a, const std::vector & b, std::vector & c) + { + size_t size = b.size(); + for (size_t i = 0; i < size; ++i) + c[i] = a && b[i]; + } + + static void constant_constant(A a, B b, UInt8 & c) + { + c = a && b; + } +}; + +template +struct OrImpl +{ + static void vector_vector(const std::vector & a, const std::vector & b, std::vector & c) + { + size_t size = a.size(); + for (size_t i = 0; i < size; ++i) + c[i] = a[i] || b[i]; + } + + static void vector_constant(const std::vector & a, B b, std::vector & c) + { + size_t size = a.size(); + for (size_t i = 0; i < size; ++i) + c[i] = a[i] || b; + } + + static void constant_vector(A a, const std::vector & b, std::vector & c) + { + size_t size = b.size(); + for (size_t i = 0; i < size; ++i) + c[i] = a || b[i]; + } + + static void constant_constant(A a, B b, UInt8 & c) + { + c = a || b; + } +}; + +template +struct XorImpl +{ + static void vector_vector(const std::vector & a, const std::vector & b, std::vector & c) + { + size_t size = a.size(); + for (size_t i = 0; i < size; ++i) + c[i] = (a[i] && !b[i]) || (!a[i] && b[i]); + } + + static void vector_constant(const std::vector & a, B b, std::vector & c) + { + size_t size = a.size(); + for (size_t i = 0; i < size; ++i) + c[i] = (a[i] && !b) || (!a[i] && b); + } + + static void constant_vector(A a, const std::vector & b, std::vector & c) + { + size_t size = b.size(); + for (size_t i = 0; i < size; ++i) + c[i] = (a && !b[i]) || (!a && b[i]); + } + + static void constant_constant(A a, B b, UInt8 & c) + { + c = (a && !b) || (!a && b); + } +}; + +template +struct NotImpl +{ + static void vector(const std::vector & a, std::vector & c) + { + size_t size = a.size(); + for (size_t i = 0; i < size; ++i) + c[i] = !a[i]; + } + + static void constant(A a, UInt8 & c) + { + c = !a; + } +}; + + +template