Initial PSTL commit
authorJF Bastien <jfbastien@apple.com>
Wed, 19 Dec 2018 17:45:32 +0000 (17:45 +0000)
committerJF Bastien <jfbastien@apple.com>
Wed, 19 Dec 2018 17:45:32 +0000 (17:45 +0000)
The initial commit of the Parallel STL upstream (under LLVM umbrella) based on
Parallel STL 20181204 open source release, which is available by
https://github.com/intel/parallelstl

Author: Mikhail Dvorskiy <mikhail.dvorskiy@intel.com>

Differential Revision: https://reviews.llvm.org/D55889

llvm-svn: 349653

101 files changed:
pstl/.arcconfig [new file with mode: 0644]
pstl/.clang-format [new file with mode: 0644]
pstl/CMakeLists.txt [new file with mode: 0644]
pstl/CREDITS.txt [new file with mode: 0644]
pstl/LICENSE.txt [new file with mode: 0644]
pstl/ParallelSTLConfig.cmake.in [new file with mode: 0644]
pstl/README.md [new file with mode: 0644]
pstl/build/Makefile [new file with mode: 0644]
pstl/build/Makefile.common [new file with mode: 0644]
pstl/build/Makefile.pstl [new file with mode: 0644]
pstl/build/android.inc [new file with mode: 0644]
pstl/build/android.linux.launcher.sh [new file with mode: 0644]
pstl/build/clang.inc [new file with mode: 0644]
pstl/build/gcc.inc [new file with mode: 0644]
pstl/build/icc.inc [new file with mode: 0644]
pstl/build/icl.inc [new file with mode: 0644]
pstl/build/jni/Android.mk [new file with mode: 0644]
pstl/build/jni/Application.mk [new file with mode: 0644]
pstl/build/macos.inc [new file with mode: 0644]
pstl/build/mingw.inc [new file with mode: 0644]
pstl/build/unix.inc [new file with mode: 0644]
pstl/build/windows.inc [new file with mode: 0644]
pstl/cmake/FindTBB.cmake [new file with mode: 0644]
pstl/include/pstl/algorithm [new file with mode: 0644]
pstl/include/pstl/execution [new file with mode: 0644]
pstl/include/pstl/internal/algorithm_impl.h [new file with mode: 0644]
pstl/include/pstl/internal/execution_defs.h [new file with mode: 0644]
pstl/include/pstl/internal/execution_impl.h [new file with mode: 0644]
pstl/include/pstl/internal/glue_algorithm_defs.h [new file with mode: 0644]
pstl/include/pstl/internal/glue_algorithm_impl.h [new file with mode: 0644]
pstl/include/pstl/internal/glue_execution_defs.h [new file with mode: 0644]
pstl/include/pstl/internal/glue_memory_defs.h [new file with mode: 0644]
pstl/include/pstl/internal/glue_memory_impl.h [new file with mode: 0644]
pstl/include/pstl/internal/glue_numeric_defs.h [new file with mode: 0644]
pstl/include/pstl/internal/glue_numeric_impl.h [new file with mode: 0644]
pstl/include/pstl/internal/memory_impl.h [new file with mode: 0644]
pstl/include/pstl/internal/numeric_impl.h [new file with mode: 0644]
pstl/include/pstl/internal/parallel_backend.h [new file with mode: 0644]
pstl/include/pstl/internal/parallel_backend_tbb.h [new file with mode: 0644]
pstl/include/pstl/internal/parallel_backend_utils.h [new file with mode: 0644]
pstl/include/pstl/internal/parallel_impl.h [new file with mode: 0644]
pstl/include/pstl/internal/pstl_config.h [new file with mode: 0644]
pstl/include/pstl/internal/unseq_backend_simd.h [new file with mode: 0644]
pstl/include/pstl/internal/utils.h [new file with mode: 0644]
pstl/include/pstl/memory [new file with mode: 0644]
pstl/include/pstl/numeric [new file with mode: 0644]
pstl/test/pstl_test_config.h [new file with mode: 0644]
pstl/test/test_adjacent_difference.cpp [new file with mode: 0644]
pstl/test/test_adjacent_find.cpp [new file with mode: 0644]
pstl/test/test_all_of.cpp [new file with mode: 0644]
pstl/test/test_any_of.cpp [new file with mode: 0644]
pstl/test/test_copy_if.cpp [new file with mode: 0644]
pstl/test/test_copy_move.cpp [new file with mode: 0644]
pstl/test/test_count.cpp [new file with mode: 0644]
pstl/test/test_equal.cpp [new file with mode: 0644]
pstl/test/test_fill.cpp [new file with mode: 0644]
pstl/test/test_find.cpp [new file with mode: 0644]
pstl/test/test_find_end.cpp [new file with mode: 0644]
pstl/test/test_find_first_of.cpp [new file with mode: 0644]
pstl/test/test_find_if.cpp [new file with mode: 0644]
pstl/test/test_for_each.cpp [new file with mode: 0644]
pstl/test/test_generate.cpp [new file with mode: 0644]
pstl/test/test_includes.cpp [new file with mode: 0644]
pstl/test/test_inplace_merge.cpp [new file with mode: 0644]
pstl/test/test_is_heap.cpp [new file with mode: 0644]
pstl/test/test_is_partitioned.cpp [new file with mode: 0644]
pstl/test/test_is_sorted.cpp [new file with mode: 0644]
pstl/test/test_lexicographical_compare.cpp [new file with mode: 0644]
pstl/test/test_merge.cpp [new file with mode: 0644]
pstl/test/test_minmax_element.cpp [new file with mode: 0644]
pstl/test/test_mismatch.cpp [new file with mode: 0644]
pstl/test/test_none_of.cpp [new file with mode: 0644]
pstl/test/test_nth_element.cpp [new file with mode: 0644]
pstl/test/test_partial_sort.cpp [new file with mode: 0644]
pstl/test/test_partial_sort_copy.cpp [new file with mode: 0644]
pstl/test/test_partition.cpp [new file with mode: 0644]
pstl/test/test_partition_copy.cpp [new file with mode: 0644]
pstl/test/test_reduce.cpp [new file with mode: 0644]
pstl/test/test_remove.cpp [new file with mode: 0644]
pstl/test/test_remove_copy.cpp [new file with mode: 0644]
pstl/test/test_replace.cpp [new file with mode: 0644]
pstl/test/test_replace_copy.cpp [new file with mode: 0644]
pstl/test/test_reverse.cpp [new file with mode: 0644]
pstl/test/test_reverse_copy.cpp [new file with mode: 0644]
pstl/test/test_rotate.cpp [new file with mode: 0644]
pstl/test/test_rotate_copy.cpp [new file with mode: 0644]
pstl/test/test_scan.cpp [new file with mode: 0644]
pstl/test/test_search_n.cpp [new file with mode: 0644]
pstl/test/test_set.cpp [new file with mode: 0644]
pstl/test/test_sort.cpp [new file with mode: 0644]
pstl/test/test_swap_ranges.cpp [new file with mode: 0644]
pstl/test/test_transform_binary.cpp [new file with mode: 0644]
pstl/test/test_transform_reduce.cpp [new file with mode: 0644]
pstl/test/test_transform_scan.cpp [new file with mode: 0644]
pstl/test/test_transform_unary.cpp [new file with mode: 0644]
pstl/test/test_uninitialized_construct.cpp [new file with mode: 0644]
pstl/test/test_uninitialized_copy_move.cpp [new file with mode: 0644]
pstl/test/test_uninitialized_fill_destroy.cpp [new file with mode: 0644]
pstl/test/test_unique.cpp [new file with mode: 0644]
pstl/test/test_unique_copy_equal.cpp [new file with mode: 0644]
pstl/test/utils.h [new file with mode: 0644]

diff --git a/pstl/.arcconfig b/pstl/.arcconfig
new file mode 100644 (file)
index 0000000..ced528f
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "repository.callsign" : "PSTL",             
+  "conduit_uri" : "https://reviews.llvm.org/"
+}
diff --git a/pstl/.clang-format b/pstl/.clang-format
new file mode 100644 (file)
index 0000000..e658579
--- /dev/null
@@ -0,0 +1,17 @@
+BasedOnStyle: LLVM
+
+Language: Cpp
+Standard: Cpp11
+
+IndentWidth: 4
+ColumnLimit: 120
+
+AlwaysBreakTemplateDeclarations: true
+AlwaysBreakAfterReturnType: All
+PointerAlignment: Left
+AllowShortIfStatementsOnASingleLine: false
+BreakBeforeBraces: Allman
+
+# Disable formatting options which may break tests.
+SortIncludes: false
+ReflowComments: false
diff --git a/pstl/CMakeLists.txt b/pstl/CMakeLists.txt
new file mode 100644 (file)
index 0000000..98836e4
--- /dev/null
@@ -0,0 +1,71 @@
+#===-- CMakeLists.txt ----------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+cmake_minimum_required(VERSION 3.1)
+
+set(PARALLELSTL_VERSION_FILE "include/pstl/internal/pstl_config.h")
+file(STRINGS "${PARALLELSTL_VERSION_FILE}" PARALLELSTL_VERSION_SOURCE REGEX "#define PSTL_VERSION .*$")
+string(REGEX MATCH "#define PSTL_VERSION (.*)$" PARALLELSTL_VERSION_SOURCE "${PARALLELSTL_VERSION_SOURCE}")
+math(EXPR VERSION_MAJOR "${PARALLELSTL_VERSION_SOURCE} / 100")
+math(EXPR VERSION_MINOR "${PARALLELSTL_VERSION_SOURCE} % 100")
+
+project(ParallelSTL VERSION ${VERSION_MAJOR}.${VERSION_MINOR} LANGUAGES CXX)
+
+option(PARALLELSTL_USE_PARALLEL_POLICIES "Enable parallel policies" ON)
+set(PARALLELSTL_BACKEND "tbb" CACHE STRING "Threading backend; defaults to TBB")
+
+include(CMakePackageConfigHelpers)
+
+if (NOT TBB_DIR)
+    get_filename_component(PSTL_DIR_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
+    string(REPLACE pstl tbb TBB_DIR_NAME ${PSTL_DIR_NAME})
+    if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake")
+        get_filename_component(TBB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake" ABSOLUTE)
+    endif()
+endif()
+
+set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
+
+add_library(ParallelSTL INTERFACE)
+add_library(pstl::ParallelSTL ALIAS ParallelSTL)
+
+if (PARALLELSTL_USE_PARALLEL_POLICIES)
+    if (PARALLELSTL_BACKEND STREQUAL "tbb")
+        find_package(TBB 2018 REQUIRED tbb OPTIONAL_COMPONENTS tbbmalloc)
+        message(STATUS "Parallel STL uses TBB ${TBB_VERSION} (interface version: ${TBB_INTERFACE_VERSION})")
+        target_link_libraries(ParallelSTL INTERFACE TBB::tbb)
+    else()
+        if (TARGET ${PARALLELSTL_BACKEND})
+            target_link_libraries(ParallelSTL INTERFACE ${PARALLELSTL_BACKEND})
+        else()
+            find_package(${PARALLELSTL_BACKEND} REQUIRED)
+            target_link_libraries(ParallelSTL INTERFACE ${${PARALLELSTL_BACKEND}_IMPORTED_TARGETS})
+        endif()
+    endif()
+else()
+    target_add_definitions(ParallelSTL INTERFACE PSTL_USE_PARALLEL_POLICIES=0)
+endif()
+
+target_include_directories(ParallelSTL
+    INTERFACE
+    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
+    $<INSTALL_INTERFACE:include>)
+
+write_basic_package_version_file(
+    ${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfigVersion.cmake
+    VERSION ${PROJECT_VERSION}
+    COMPATIBILITY AnyNewerVersion)
+
+configure_file(
+    ParallelSTLConfig.cmake.in
+    ${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfig.cmake
+    @ONLY)
+
+export(TARGETS ParallelSTL NAMESPACE pstl:: FILE ParallelSTLTargets.cmake)
+export(PACKAGE ParallelSTL)
diff --git a/pstl/CREDITS.txt b/pstl/CREDITS.txt
new file mode 100644 (file)
index 0000000..7855b68
--- /dev/null
@@ -0,0 +1,17 @@
+This file is a partial list of people who have contributed to the LLVM/pstl\r
+(Parallel STL) project.  If you have contributed a patch or made some other\r
+contribution to LLVM/pstl, please submit a patch to this file to add yourself,\r
+and it will be done!\r
+\r
+The list is sorted by surname and formatted to allow easy grepping and\r
+beautification by scripts.  The fields are: name (N), email (E), web-address \r
+(W), PGP key ID and fingerprint (P), description (D), and snail-mail address\r
+(S).\r
+\r
+N: Intel Corporation\r
+W: http://www.intel.com\r
+D: Created the initial implementation.\r
+\r
+N: Thomas Rodgers\r
+E: trodgers@redhat.com\r
+D: Identifier name transformation for inclusion in a Standard C++ library.\r
diff --git a/pstl/LICENSE.txt b/pstl/LICENSE.txt
new file mode 100644 (file)
index 0000000..14efdfa
--- /dev/null
@@ -0,0 +1,70 @@
+==============================================================================\r
+\r
+The software contained in this directory tree is dual licensed under both the\r
+University of Illinois "BSD-Like" license and the MIT license.  As a user of\r
+this code you may choose to use it under either license.  As a contributor,\r
+you agree to allow your code to be used under both.\r
+\r
+Full text of the relevant licenses is included below.\r
+\r
+==============================================================================\r
+\r
+University of Illinois/NCSA\r
+Open Source License\r
+\r
+Copyright (c) 2017-2018 by the contributors listed in CREDITS.TXT\r
+\r
+All rights reserved.\r
+\r
+Developed by:\r
+    Threading Runtimes Team\r
+    Intel Corporation\r
+    http://www.intel.com\r
+\r
+Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+this software and associated documentation files (the "Software"), to deal with\r
+the Software without restriction, including without limitation the rights to\r
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\r
+of the Software, and to permit persons to whom the Software is furnished to do\r
+so, subject to the following conditions:\r
+\r
+    * Redistributions of source code must retain the above copyright notice,\r
+      this list of conditions and the following disclaimers.\r
+\r
+    * Redistributions in binary form must reproduce the above copyright notice,\r
+      this list of conditions and the following disclaimers in the\r
+      documentation and/or other materials provided with the distribution.\r
+\r
+    * Neither the names of Intel Corporation Threading Runtimes Team nor the \r
+      names of its contributors may be used to endorse or promote products \r
+      derived from this Software without specific prior written permission.\r
+\r
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE\r
+CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE\r
+SOFTWARE.\r
+\r
+==============================================================================\r
+\r
+Copyright (c) 2017-2018 by the contributors listed in CREDITS.TXT\r
+\r
+Permission is hereby granted, free of charge, to any person obtaining a copy\r
+of this software and associated documentation files (the "Software"), to deal\r
+in the Software without restriction, including without limitation the rights\r
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+copies of the Software, and to permit persons to whom the Software is\r
+furnished to do so, subject to the following conditions:\r
+\r
+The above copyright notice and this permission notice shall be included in\r
+all copies or substantial portions of the Software.\r
+\r
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+THE SOFTWARE.\r
diff --git a/pstl/ParallelSTLConfig.cmake.in b/pstl/ParallelSTLConfig.cmake.in
new file mode 100644 (file)
index 0000000..3e6b6c4
--- /dev/null
@@ -0,0 +1,18 @@
+#===-- ParallelSTLConfig.cmake.in ----------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+include(CMakeFindDependencyMacro)
+
+set(PARALLELSTL_BACKEND "@PARALLELSTL_BACKEND@")
+
+if(PARALLELSTL_BACKEND STREQUAL "tbb")
+    find_dependency(TBB 2018 REQUIRED tbb)
+endif()
+
+include("${CMAKE_CURRENT_LIST_DIR}/ParallelSTLTargets.cmake")
diff --git a/pstl/README.md b/pstl/README.md
new file mode 100644 (file)
index 0000000..c360c35
--- /dev/null
@@ -0,0 +1,40 @@
+# Parallel STL 
+
+Parallel STL is an implementation of the C++ standard library algorithms with support for execution policies,
+as specified in ISO/IEC 14882:2017 standard, commonly called C++17. The implementation also supports the unsequenced 
+execution policy specified in Parallelism TS version 2 and proposed for the next version of the C++ standard in the 
+C++ working group paper P1001R1.
+Parallel STL offers efficient support for both parallel and vectorized execution of algorithms. For sequential 
+execution, it relies on an available implementation of the C++ standard library.
+
+## Prerequisites
+To use Parallel STL, you must have the following software installed:
+* C++ compiler with:
+  * Support for C++11
+  * Support for OpenMP* 4.0 SIMD constructs
+* Threading Building Blocks (TBB) which is available to download in the GitHub [repository](https://github.com/01org/tbb/)
+
+## Known Issues or limitations
+    unseq and par_unseq policies only have effect with compilers that
+        support '#pragma omp simd' or '#pragma simd'.
+    Parallel and vector execution is only supported for the algorithms
+        if random access iterators are provided, while for other iterator
+        types the execution will remain serial.
+    The following algorithms do not allow efficient SIMD execution:
+       includes, inplace_merge, merge, nth_element, partial_sort,
+        partial_sort_copy, set_difference, set_intersection,
+        set_symmetric_difference, set_union, sort, stable_partition,
+        stable_sort, unique.
+    The initial value type for exclusive_scan, inclusive_scan,
+        transform_exclusive_scan, transform_inclusive_scan shall satisfy
+        the DefaultConstructible requirements. A default constructed-instance
+        of the initial value type shall be the identity element for binary_op.
+    For max_element, min_element, minmax_element, partial_sort,
+        partial_sort_copy, sort, stable_sort the dereferenced value type of
+        the provided iterators shall be DefaultConstructible.
+    For remove, remove_if, unique the dereferenced value type of the provided
+        iterators shall be MoveConstructible.
+    The following algorithms require additional O(n) memory space for parallel
+        execution: copy_if, inplace_merge, partial_sort, partial_sort_copy,
+        partition_copy, remove, remove_if, rotate, sort, stable_sort, unique,
+        unique_copy.
diff --git a/pstl/build/Makefile b/pstl/build/Makefile
new file mode 100644 (file)
index 0000000..efe249c
--- /dev/null
@@ -0,0 +1,102 @@
+#===-- Makefile ----------------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+#------------------------------------------------------------------------------
+# Define rules for making the Parallel STL library.
+#------------------------------------------------------------------------------
+
+
+proj_root ?= $(CURDIR)/..
+
+include $(proj_root)/build/Makefile.common
+CPLUS := $(compiler) $(COMPILER_NOLOGO_KEY) $(USE_SHARED_CPPRUNTIME_KEY)
+
+.SECONDARY:
+.PHONY: clean clean_all clean_pstl bench
+
+VPATH = $(proj_root)/test
+
+CPLUS_FLAGS += -I$(proj_root)
+
+PSTL_MAKEFILE = $(proj_root)/build/Makefile.pstl
+BENCH_MAKEFILE = $(proj_root)/build/Makefile.bench
+
+
+test_hdr = $(wildcard $(proj_root)/test/*.h)
+test_src = $(wildcard $(proj_root)/test/test_*.cpp)
+test_bin = $(notdir $(test_src:.cpp=.exe))
+
+
+all: $(test_bin)
+
+test_%.offload.exe: test_%.offload$(OBJ_SFX) exception_list.offload$(OBJ_SFX)
+       $(CPLUS) $(CPLUS_FLAGS) $^ $(FKEY)o$@ $(LDFLAGS)
+
+$(PSTL_LIB_NAME):
+       $(MAKE) -f $(PSTL_MAKEFILE) backend=$(backend) cfg=$(cfg)
+
+test_%.exe: test_%$(OBJ_SFX) $(PSTL_LIB_NAME)
+       $(LD) $< $(LD_OUT_KEY)$@ $(LDFLAGS) $(DYN_LDFLAGS) $(PSTL_LIB_LINK)
+
+test_%: test_%.exe
+       $(run_cmd) $(RUN_CMD)test_$*.exe
+
+test_%$(OBJ_SFX): test_%.cpp $(test_hdr) $(proj_root)/build/Makefile
+       $(CPLUS) $(CPLUS_FLAGS) -c $< $(FKEY)o$@
+
+# This definition intentionally consists of two blank lines
+define eol
+
+
+endef
+
+test: $(test_bin)
+       $(foreach test, $(test_bin), $(run_cmd) $(RUN_CMD)$(test) $(args) $(eol))
+
+%.s: %.cpp $(proj_root)/build/Makefile
+       $(CPLUS) $(CPLUS_FLAGS) -S $< $(FKEY)o$@
+
+%.E: %.cpp
+       $(CPLUS) $(CPLUS_FLAGS) -E $< >$@
+
+TEMPLATE_FILES=$(wildcard $(proj_root)/bench/*.*tmpl)
+BENCH_COMMON_FILES=$(wildcard $(proj_root)/bench/*.h) $(wildcard $(proj_root)/bench/*.cpp)
+
+$(BENCH_MAKEFILE): $(proj_root)/bench/algorithm.json $(proj_root)/bench/gen.py  $(TEMPLATE_FILES)
+       $(PYTHON) $(proj_root)/bench/gen.py $(proj_root)/bench/algorithm.json
+
+bench : $(BENCH_MAKEFILE) $(BENCH_COMMON_FILES) $(PSTL_LIB_NAME)
+       @echo     TEMPLATE_FILES=$(TEMPLATE_FILES)
+       @echo     proj_root=$(proj_root)
+       ls -la      $(proj_root)/bench/gen.py
+       $(MAKE) -f $(BENCH_MAKEFILE)
+
+clean_bench:
+       $(DEL_CMD) $(BENCH_MAKEFILE)
+       $(DEL_CMD) batch.py
+       $(DEL_CMD) $(proj_root)/build/bench/*.*
+
+clean:
+       $(DEL_CMD) *$(OBJ_SFX) *.exe *.E *.s *.asm *.d *.pdb *.pdb *.suo *.ilk
+
+clean_pstl:
+       $(MAKE) -f $(PSTL_MAKEFILE) clean
+
+clean_all: clean clean_pstl clean_bench
+
+info:
+       @echo OS = $(os_name)
+       @echo proj_root = "$(proj_root)"
+       @echo $(CURDIR)
+       @echo VPATH = $(VPATH)
+       @echo LIBRARY_PATH = $(LIBRARY_PATH)
+       @echo backend = $(backend)
+       @echo compiler = $(compiler)
+
+-include *.d
diff --git a/pstl/build/Makefile.common b/pstl/build/Makefile.common
new file mode 100644 (file)
index 0000000..0c603da
--- /dev/null
@@ -0,0 +1,113 @@
+#===-- Makefile.common ---------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+#------------------------------------------------------------------------------
+# Define common parts for Parallel STL
+#------------------------------------------------------------------------------
+
+.SUFFIXES:
+
+goals = $(or $(MAKECMDGOALS),all)
+ifneq (, $(filter-out clean clean_all,$(goals)))
+    ifeq (, $(filter $(backend), tbb))
+        $(info Threading backend was not specified; using TBB)
+        backend=tbb
+    endif
+endif
+
+ifndef os_name
+  # Windows sets environment variable OS; for other systems, ask uname
+  ifeq ($(OS),)
+    OS:=$(shell uname)
+    ifeq ($(OS),)
+      $(error "Cannot detect operating system")
+    endif
+    os_name=$(OS)
+  endif
+
+  ifeq ($(OS), Windows_NT)
+    os_name=windows
+  endif
+  ifeq ($(OS), Linux)
+    os_name=linux
+  endif
+  ifeq ($(OS), Darwin)
+    os_name=macos
+  endif
+endif # !os_name
+
+cfg ?= release
+stdver ?= c++11
+
+override INCLUDES += -I$(proj_root)/include -I$(proj_root)/test
+
+TEST_MACRO += -D__PSTL_TEST_SUCCESSFUL_KEYWORD=1
+
+ifeq ($(backend), tbb)
+    BACKEND_MACRO += -D__PSTL_PAR_BACKEND_TBB
+endif
+
+target ?= $(os_name)
+#OS specific keys
+ifeq ($(target),windows)
+    ifneq (, $(filter $(compiler), gcc g++))
+        include $(proj_root)/build/mingw.inc
+    else
+        include $(proj_root)/build/windows.inc
+    endif
+else
+    include $(proj_root)/build/unix.inc
+    ifneq (,$(wildcard $(proj_root)/build/$(target).inc))
+        include $(proj_root)/build/$(target).inc
+        $(info included additional file $(proj_root)/build/$(target).inc)
+    endif
+endif
+
+# compiler specific keys
+ifneq (, $(filter $(compiler), gcc g++))
+    include $(proj_root)/build/gcc.inc
+endif
+
+ifneq (, $(filter $(compiler), clang clang++))
+    include $(proj_root)/build/clang.inc
+endif
+
+ifneq (, $(filter $(compiler), icc icpc icx))
+    include $(proj_root)/build/icc.inc
+endif
+
+ifneq (, $(filter $(compiler), icl))
+    include $(proj_root)/build/icl.inc
+endif
+
+
+OPTIMIZATION_ENABLED_FLAGS += $(XHOST_FLAG)
+OPTIMIZATION_DISABLED_FLAGS += $(XHOST_FLAG)
+
+
+ifeq ($(cfg), debug)
+    TBB_LIB_NAME = tbb_debug
+    BACKEND_MACRO += -DTBB_USE_DEBUG=1
+    DEBUG_MACRO += -DPSTL_USE_DEBUG
+    OPTIMIZATION_KEYS = $(OPTIMIZATION_DISABLED_FLAGS)
+else
+    OPTIMIZATION_KEYS = $(OPTIMIZATION_ENABLED_FLAGS)
+endif
+
+DYN_LDFLAGS += $(PSTL_ARCH)
+
+CPLUS_FLAGS += $(TEST_MACRO)
+CPLUS_FLAGS += $(INCLUDES)
+CPLUS_FLAGS += $(BACKEND_MACRO)
+CPLUS_FLAGS += $(DEBUG_MACRO)
+CPLUS_FLAGS += $(CXXFLAGS)
+CPLUS_FLAGS += $(OPTIMIZATION_KEYS)
+
+CPLUS_FLAGS += $(DISABLED_WARNINGS)
+CPLUS_FLAGS += $(PSTL_ARCH)
diff --git a/pstl/build/Makefile.pstl b/pstl/build/Makefile.pstl
new file mode 100644 (file)
index 0000000..784047a
--- /dev/null
@@ -0,0 +1,47 @@
+#===-- Makefile.pstl -----------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+#------------------------------------------------------------------------------
+# Define rules for making the Parallel STL library.
+#------------------------------------------------------------------------------
+
+proj_root ?= $(CURDIR)/..
+
+include $(proj_root)/build/Makefile.common
+
+.PHONY: clean
+
+VPATH = $(proj_root)/src
+
+lib_src = $(wildcard $(proj_root)/src/*.cpp)
+lib_obj = $(notdir $(lib_src:.cpp=$(OBJ_SFX)))
+
+all: pstl
+
+pstl: $(PSTL_LIB_NAME)
+
+%$(OBJ_SFX): %.cpp $(proj_root)/build/Makefile.pstl
+       $(CPLUS) $(CPLUS_FLAGS) -c $< $(FKEY)o$@
+
+%.s: %.cpp $(proj_root)/build/Makefile
+       $(CPLUS) $(CPLUS_FLAGS) -S $< $(FKEY)o$@
+
+%.E: %.cpp
+       $(CPLUS) $(CPLUS_FLAGS) -E $< >$@
+
+clean:
+       $(DEL_CMD) *$(OBJ_SFX) *.lib *.dll *.so *.exp *$(PSTL_LIB_NAME)*
+
+info:
+       @echo OS = $(os_name)
+       @echo proj_root = "$(proj_root)"
+       @echo $(CURDIR)
+       @echo VPATH=$(VPATH)
+
+-include *.d
diff --git a/pstl/build/android.inc b/pstl/build/android.inc
new file mode 100644 (file)
index 0000000..ab8e86b
--- /dev/null
@@ -0,0 +1,48 @@
+#===-- android.inc -------------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+PSTL_ARCH=
+PIE_FLAGS = -pie -fPIE -fPIC
+SDL_FLAGS = -fstack-protector -Wformat -Wformat-security
+CPLUS_FLAGS += $(TARGET_CFLAGS) $(PIE_FLAGS) $(SDL_FLAGS)
+
+# Paths to the NDK prebuilt tools and libraries
+ifeq (,$(findstring $(ndk_version), $(foreach v, 7 8 9 10 11 12 13 14 15,r$(v) r$(v)b r$(v)c r$(v)d r$(v)e)))
+    ifeq (clang,$(compiler))
+        # Since Android* NDK r16 another sysroot and isystem paths have to be specified
+        CPLUS_FLAGS += --sysroot=$(NDK_ROOT)/sysroot -isystem $(NDK_ROOT)/sysroot/usr/include/$(TRIPLE)
+        # Android* version flag required since r16
+        CPLUS_FLAGS += -D__ANDROID_API__=$(API_LEVEL)
+    else
+        CPLUS_FLAGS += --sysroot=$(SYSROOT)
+    endif
+else
+    CPLUS_FLAGS += --sysroot=$(SYSROOT)
+endif
+
+LDFLAGS += --sysroot=$(SYSROOT) $(TARGET_CFLAGS)
+PSTL_LIB_LINK +=  -lc++abi -L$(CPLUS_LIB_PATH) -lc++_shared
+
+ifeq (arm,$(arch))
+    PSTL_LIB_LINK += -lunwind
+endif
+
+# TARGET_CXX cames from NDK
+override CPLUS:=$(TARGET_CXX) $(USE_SHARED_CPPRUNTIME_KEY)
+LD = $(CPLUS) $(TARGET_CFLAGS) $(PIE_FLAGS) $(SDL_FLAGS)
+
+run_cmd ?= -sh $(proj_root)/build/android.linux.launcher.sh $(largs)
+
+# TBB_LIBRARIES := $(foreach dir,$(LIBRARY_PATH),$(wildcard $(dir)/libtbb*so))
+TBB_LIBRARIES := $(foreach dir,$(LIBRARY_PATH),$(wildcard $(dir)/*))
+LIB_STL_ANDROID += $(TBB_LIBRARIES)
+
+$(warning LIB_STL_ANDROID=$(LIB_STL_ANDROID))
+$(warning TBB_LIBRARIES=$(TBB_LIBRARIES))
+$(warning LIBRARY_PATH=$(LIBRARY_PATH))
diff --git a/pstl/build/android.linux.launcher.sh b/pstl/build/android.linux.launcher.sh
new file mode 100644 (file)
index 0000000..d6cf7db
--- /dev/null
@@ -0,0 +1,149 @@
+#!/bin/sh
+#===-- android.linux.launcher.sh -----------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+#
+#
+
+# Usage:
+# android.linux.launcher.sh [-v] [-q] [-s] [-r <repeats>] [-u] [-l <library>] <executable> <arg1> <arg2> <argN>
+#         where: -v enables verbose output
+#         where: -q enables quiet mode
+#         where: -s runs the test in stress mode (until non-zero exit code or ctrl-c pressed)
+#         where: -r <repeats> specifies number of times to repeat execution
+#         where: -u is ignored on Android
+#         where: -l <library> specifies the library name to be assigned to LD_PRELOAD
+#
+# Libs and executable necessary for testing should be present in the current directory before running.
+# ANDROID_SERIAL must be set to the connected Android target device name for file transfer and test runs.
+# ANDROID_TEST_DIRECTORY may be set to the directory used for testing on the Android target device; otherwise,
+#                        the default directory used is "/data/local/tmp/$(basename $PWD)".
+# Note: Do not remove the redirections to '/dev/null' in the script, otherwise the nightly test system will fail.
+
+do_cleanup() #
+{ #
+    adb pull $targetdir/events.txt events.txt > /dev/null 2>&1 #
+    # Remove target directory on the device
+    adb shell "rm -r ${targetdir}; mkdir -p ${targetdir}" > /dev/null 2>&1 #
+} #
+do_trap_cleanup() #
+{ #
+    do_cleanup #
+    exit -1 #
+} #
+while getopts  "qvsr:ul:" flag #
+do case $flag in #
+    s )  # Stress testing mode
+         echo Doing stress testing. Press Ctrl-C to terminate
+         run_env='stressed() { while $*; do :; done; }; ' #
+         run_prefix="stressed $run_prefix" ;; #
+    r )  # Repeats test n times
+         run_env="repeated() { for i in $(seq -s ' ' 1 $OPTARG) ; do echo \$i of $OPTARG:; \$*; done; }; " #
+         run_prefix="repeated $run_prefix" ;; #
+    l )  # Additional library
+         ldpreload="$OPTARG " ;; #
+    u )  # Stack limit
+         ;; #
+    q )  # Quiet mode, removes 'done' but prepends any other output by test name
+         OUTPUT='2>&1 | sed -e "s/done//;/^[[:space:]]*$/d;s!^!$exename: !"' ;; #
+    v )  # Verbose mode
+         SUPPRESS='' #
+         verbose=1 ;; #
+esac done #
+shift `expr $OPTIND - 1` #
+[ -z "$OUTPUT" ] && OUTPUT='| sed -e "s/\\r$//"' #
+[ $verbose ] || SUPPRESS='>/dev/null' #
+# Collect the executable name
+exename=$(basename $1) #
+shift #
+
+# Prepare the target directory on the device
+currentdir=$(basename $PWD) #
+targetdir=${ANDROID_TEST_DIRECTORY:-/data/local/tmp/$currentdir} #
+do_cleanup #
+trap do_trap_cleanup INT  # if someone hits control-c, cleanup the device
+
+# Collect the list of files to transfer to the target device, starting with executable itself.
+fnamelist="$exename" #
+# Add the C++ standard library from the NDK, which is required for all tests on Android.
+if [ ! -z "${LIB_STL_ANDROID}" ]; then #
+    fnamelist="$fnamelist ${LIB_STL_ANDROID}" #
+else #
+    fnamelist="$fnamelist libc++_shared.so" #
+fi #
+
+# Find the TBB libraries and add them to the list.
+
+OLD_SEP=$IFS
+IFS=':'
+for dir in $LD_LIBRARY_PATH; do #
+    found="`ls $dir/lib*.so 2>/dev/null` "||: #
+    fnamelist+="$fnamelist $found"
+done #
+IFS=$OLD_SEP
+
+files="$(ls libtbb* 2> /dev/null)" #
+[ -z "$files" ] || fnamelist="$fnamelist $files" #
+
+# Add any libraries built for specific tests.
+exeroot=${exename%\.*} #
+files="$(ls ${exeroot}*.so ${exeroot}*.so.* 2> /dev/null)" #
+[ -z "$files" ] || fnamelist="$fnamelist $files" #
+
+# Transfer collected executable and library files to the target device.
+transfers_ok=1 #
+for fullname in $fnamelist; do { #
+    if [ -r $fullname ]; then { #
+        # Transfer the executable and libraries to top-level target directory
+        if [ "$OS" = 'Windows_NT' ]; then #
+            fullname=`cygpath -m "$fullname"` #
+        fi #
+        [ $verbose ] && echo -n "Pushing $fullname: " #
+        eval "adb push $fullname ${targetdir}/$(basename $fullname) $SUPPRESS 2>&1" #
+    }; else { #
+        echo "Error: required file ${currentdir}/${fullname} for test $exename not available for transfer." #
+        transfers_ok=0 #
+    }; fi #
+}; done #
+if [ "${transfers_ok}" = "0" ]; then { #
+    do_cleanup #
+    exit -1 #
+}; fi #
+# Transfer input files used by example codes by scanning the executable argument list.
+for fullname in "$@"; do { #
+    if [ -r $fullname ]; then { #
+        directory=$(dirname $fullname) #
+        filename=$(basename $fullname) #
+        # strip leading "." from fullname if present
+        if [ "$directory" = "\." ]; then { #
+            directory="" #
+            fullname=$filename #
+        }; fi #
+        # Create the target directory to hold input file if necessary
+        if [ ! -z $directory ]; then { #
+            eval "adb shell 'mkdir $directory' $SUPPRESS 2>&1" #
+        }; fi #
+        # Transfer the input file to corresponding directory on target device
+        [ $verbose ] && echo -n "Pushing $fullname: " #
+        eval "adb push $fullname ${targetdir}/$fullname $SUPPRESS 2>&1" #
+    }; fi #
+}; done #
+
+# Set LD_PRELOAD if necessary
+[ -z "$ldpreload" ] || run_prefix="LD_PRELOAD='$ldpreload' $run_prefix" #
+[ $verbose ] && echo Running $run_prefix ./$exename $* #
+run_env="$run_env cd $targetdir; export LD_LIBRARY_PATH=." #
+[ -z "$VIRTUAL_MACHINE" ] || run_env="$run_env; export VIRTUAL_MACHINE=$VIRTUAL_MACHINE" #
+# The return_code file is the best way found to return the status of the test execution when using adb shell.
+eval 'adb shell "$run_env; $run_prefix ./$exename $* || echo -n \$? >error_code"' "${OUTPUT}" #
+# Capture the return code string and remove the trailing \r from the return_code file contents
+err=`adb shell "cat $targetdir/error_code 2>/dev/null"` #
+[ -z $err ] || echo $exename: exited with error $err #
+do_cleanup #
+# Return the exit code of the test.
+exit $err #
diff --git a/pstl/build/clang.inc b/pstl/build/clang.inc
new file mode 100644 (file)
index 0000000..6baf2be
--- /dev/null
@@ -0,0 +1,22 @@
+#===-- clang.inc ---------------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+override compiler:=clang++
+
+ifneq ($(target),android)
+    PSTL_ARCH += $(KEY)march=native
+endif
+
+XHOST_FLAG = -fno-vectorize
+CPLUS_FLAGS += $(FQKEY)std=$(stdver)
+# XHOST_FLAG = $(KEY)mavx2 -fno-vectorize
+# XHOST_FLAG = $(KEY)mavx512f -fno-vectorize
+# DYN_LDFLAGS += $(LINK_KEY)c++
+# CPLUS_FLAGS += -stdlib=libc++
+# CPLUS_FLAGS += -fopenmp-simd //it will be supported in he future version
diff --git a/pstl/build/gcc.inc b/pstl/build/gcc.inc
new file mode 100644 (file)
index 0000000..e60923d
--- /dev/null
@@ -0,0 +1,28 @@
+#===-- gcc.inc -----------------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+override compiler:=g++
+XHOST_FLAG = $(KEY)march=native -fno-tree-vectorize
+#    XHOST_FLAG = $(KEY)mavx2 -fno-tree-vectorize
+#    XHOST_FLAG = $(KEY)mavx512f -fno-tree-vectorize
+ DYN_LDFLAGS += $(LINK_KEY)stdc++
+# GCC 4.8.5 and early doesn't  support -fopenmp-simd; GCC 4.9 supports OpenMP 4.0 for C/C++
+ifneq (, $(shell gcc -dumpversion | egrep  "^4\.9\.[0-9]"))
+    CPLUS_FLAGS += -fopenmp-simd
+endif
+ifneq (, $(shell gcc -dumpversion | egrep  "^[5-9]\.[0-9]\.[0-9]"))
+    CPLUS_FLAGS += -fopenmp-simd
+#   CPLUS_FLAGS += -fdump-rtl-loop2 #use this option to enable optimization report
+endif
+
+CPLUS_FLAGS += $(FQKEY)std=$(stdver)
+
+ifeq ($(os_name),windows)
+DISABLED_WARNINGS = $(KEY)Wno-attributes #disable MinGW warnings about extended alignment
+endif
diff --git a/pstl/build/icc.inc b/pstl/build/icc.inc
new file mode 100644 (file)
index 0000000..5d8d530
--- /dev/null
@@ -0,0 +1,24 @@
+#===-- icc.inc -----------------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+vecreport ?= 0 #may be set to [0..7], see https://software.intel.com/en-us/node/522949 for details
+
+XHOST_FLAG = $(KEY)xHOST -no-vec
+CPLUS_FLAGS += $(QKEY)opt-assume-safe-padding
+
+# XHOST_FLAG = $(KEY)xCORE-AVX2 -no-vec
+# XHOST_FLAG = $(KEY)xSSE4.1 -no-vec
+# XHOST_FLAG = $(KEY)xMIC-AVX512 -no-vec
+
+CPLUS_FLAGS += $(QKEY)openmp-simd
+CPLUS_FLAGS += $(FQKEY)MMD
+CPLUS_FLAGS += $(FQKEY)std=$(stdver)
+CPLUS_FLAGS +=  $(QKEY)opt-report=$(vecreport) $(QKEY)opt-report-phase vec
+
+OPTIMIZATION_DISABLED_FLAGS += $(KEY)debug inline-debug-info
diff --git a/pstl/build/icl.inc b/pstl/build/icl.inc
new file mode 100644 (file)
index 0000000..ba2415c
--- /dev/null
@@ -0,0 +1,23 @@
+#===-- icl.inc -----------------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+vecreport ?= 0 #may be set to [0..7], see https://software.intel.com/en-us/node/522949 for details
+
+XHOST_FLAG = $(QKEY)xHOST
+XHOST_FLAG = $(QKEY)vec-
+# XHOST_FLAG = $(QKEY)xCORE-AVX2
+# XHOST_FLAG = $(QKEY)xSSE4.1
+# XHOST_FLAG = $(QKEY)xMIC-AVX512
+CPLUS_FLAGS += $(QKEY)opt-assume-safe-padding
+CPLUS_FLAGS += $(QKEY)openmp-simd
+CPLUS_FLAGS += $(FQKEY)MMD
+CPLUS_FLAGS += $(FQKEY)std=$(stdver)
+CPLUS_FLAGS += $(QKEY)opt-report:$(vecreport) $(QKEY)opt-report-phase:vec $(QKEY)opt-report-phase:loop
+
+DISABLED_WARNINGS = $(QKEY)diag-disable:2586 #use comma-separated values to specify multiple entries
diff --git a/pstl/build/jni/Android.mk b/pstl/build/jni/Android.mk
new file mode 100644 (file)
index 0000000..329f029
--- /dev/null
@@ -0,0 +1,54 @@
+#===-- Android.mk --------------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+export proj_root?=$(NDK_PROJECT_PATH)/..
+
+ifeq (armeabi-v7a,$(APP_ABI))
+       export SYSROOT:=$(NDK_ROOT)/platforms/$(APP_PLATFORM)/arch-arm
+else ifeq (arm64-v8a,$(APP_ABI))
+       export SYSROOT:=$(NDK_ROOT)/platforms/$(APP_PLATFORM)/arch-arm64
+else
+       export SYSROOT:=$(NDK_ROOT)/platforms/$(APP_PLATFORM)/arch-$(APP_ABI)
+endif
+
+ifeq (windows,$(os_name))
+       export CPATH_SEPARATOR :=;
+else
+       export CPATH_SEPARATOR :=:
+endif
+
+export ANDROID_NDK_ROOT:=$(NDK_ROOT)
+export ndk_version:=$(lastword $(subst -, ,$(ANDROID_NDK_ROOT)))
+ndk_version:= $(firstword $(subst /, ,$(ndk_version)))
+ndk_version:= $(firstword $(subst \, ,$(ndk_version)))
+
+ifeq (clang,$(compiler))
+       # "TBB_RTL :=llvm-libc++/libcxx" should be used for ndk_version r13 r13b r14.
+       TBB_RTL :=llvm-libc++
+       TBB_RTL_LIB :=llvm-libc++
+       TBB_RTL_FILE :=libc++_shared.so
+else
+       TBB_RTL :=gnu-libstdc++/$(NDK_TOOLCHAIN_VERSION)
+       TBB_RTL_LIB :=$(TBB_RTL)
+       TBB_RTL_FILE :=libgnustl_shared.so
+endif
+
+export CPATH := $(INCLUDE)$(CPATH_SEPARATOR)$(SYSROOT)/usr/include$(CPATH_SEPARATOR)$(NDK_ROOT)/sources/cxx-stl/$(TBB_RTL)/include$(CPATH_SEPARATOR)$(NDK_ROOT)/sources/cxx-stl/$(TBB_RTL)/libs/$(APP_ABI)/include$(CPATH_SEPARATOR)$(NDK_ROOT)/sources/android/support/include
+
+LIB_STL_ANDROID_DIR := $(NDK_ROOT)/sources/cxx-stl/$(TBB_RTL_LIB)/libs/$(APP_ABI)
+#LIB_STL_ANDROID is required to be set up for copying Android specific library to a device next to test
+export LIB_STL_ANDROID := $(LIB_STL_ANDROID_DIR)/$(TBB_RTL_FILE)
+export CPLUS_LIB_PATH := $(SYSROOT)/usr/lib -L$(LIB_STL_ANDROID_DIR)
+export target_os_version:=$(APP_PLATFORM)
+export tbb_tool_prefix:=$(TOOLCHAIN_PREFIX)
+export TARGET_CXX
+export TARGET_CC
+export TARGET_CFLAGS
+
+include $(proj_root)/build/Makefile
diff --git a/pstl/build/jni/Application.mk b/pstl/build/jni/Application.mk
new file mode 100644 (file)
index 0000000..5e23211
--- /dev/null
@@ -0,0 +1,61 @@
+#===-- Application.mk ----------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+
+ifndef os_name
+  # Windows sets environment variable OS; for other systems, ask uname
+  ifeq ($(OS),)
+    OS:=$(shell uname)
+    ifeq ($(OS),)
+      $(error "Cannot detect operating system")
+    endif
+    export os_name=$(OS)
+  endif
+
+  ifeq ($(OS), Windows_NT)
+    export os_name=windows
+  endif
+  ifeq ($(OS), Linux)
+    export os_name=linux
+  endif
+  ifeq ($(OS), Darwin)
+    export os_name=macos
+  endif
+endif
+
+export compiler?=clang
+export arch?=ia32
+export target?=android
+
+ifeq (ia32,$(arch))
+    APP_ABI:=x86
+    export TRIPLE:=i686-linux-android
+else ifeq (intel64,$(arch))
+    APP_ABI:=x86_64
+    export TRIPLE:=x86_64-linux-android
+else ifeq (arm,$(arch))
+    APP_ABI:=armeabi-v7a
+    export TRIPLE:=arm-linux-androideabi
+else ifeq (arm64,$(arch))
+    APP_ABI:=arm64-v8a
+    export TRIPLE:=aarch64-linux-android
+else
+    APP_ABI:=$(arch)
+endif
+
+api_version?=21
+export API_LEVEL:=$(api_version)
+APP_PLATFORM:=android-$(api_version)
+
+ifeq (clang,$(compiler))
+    NDK_TOOLCHAIN_VERSION:=clang
+    APP_STL:=c++_shared
+else
+    NDK_TOOLCHAIN_VERSION:=4.9
+endif
diff --git a/pstl/build/macos.inc b/pstl/build/macos.inc
new file mode 100644 (file)
index 0000000..ef5cfc4
--- /dev/null
@@ -0,0 +1,10 @@
+#===-- macos.inc ---------------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+DYN_LDFLAGS += -Wl,-rpath,$(TBBROOT)/lib
diff --git a/pstl/build/mingw.inc b/pstl/build/mingw.inc
new file mode 100644 (file)
index 0000000..a4a50f7
--- /dev/null
@@ -0,0 +1,50 @@
+#===-- mingw.inc ---------------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+PYTHON = python
+KEY = -
+QKEY = $(KEY)q
+FKEY = $(KEY)
+FQKEY = $(KEY)
+MACHINE_KEY = $(KEY)m
+OBJ_SFX = .o
+DEL_CMD = del /F
+RUN_CMD = 
+COMMAND_SEPARATOR = &
+compiler ?= gcc
+COMPILER_NOLOGO_KEY =
+OPTIMIZATION_DISABLED_FLAGS = $(KEY)Og $(KEY)g
+OPTIMIZATION_ENABLED_FLAGS +=  $(KEY)O2
+TBB_LIB_NAME = tbb
+CPLUS = $(compiler)
+LD = $(CPLUS)
+
+USE_SHARED_CPPRUNTIME_KEY =
+LINK_KEY = $(KEY)l
+
+LD_OUT_KEY = $(KEY)o
+DYN_LDFLAGS += -L. -L$(proj_root)/build
+
+ifneq ($(PSTL_LIB_NAME), )
+    PSTL_LIB_LINK += $(LINK_KEY)$(PSTL_LIB_NAME)$(PSTL_LIB_EXT)
+endif
+
+ifeq ($(backend),tbb)
+    DYN_LDFLAGS += $(LINK_KEY)$(TBB_LIB_NAME)
+endif
+
+ifeq ($(arch),intel64)
+    PSTL_ARCH = $(MACHINE_KEY)64
+else ifeq ($(arch),ia32)
+    PSTL_ARCH = $(MACHINE_KEY)32
+else ifeq ($(arch),)
+    $(info arch=native by default)
+else
+    PSTL_ARCH = $(MACHINE_KEY)$(arch)
+endif
diff --git a/pstl/build/unix.inc b/pstl/build/unix.inc
new file mode 100644 (file)
index 0000000..595fde3
--- /dev/null
@@ -0,0 +1,51 @@
+#===-- unix.inc ----------------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+PYTHON = python
+KEY = -
+QKEY = $(KEY)q
+FKEY = $(KEY)
+FQKEY = $(KEY)
+MACHINE_KEY = $(KEY)m
+OBJ_SFX = .o
+DEL_CMD = rm $(KEY)f
+RUN_CMD = ./
+COMMAND_SEPARATOR = ;
+compiler ?= icc
+COMPILER_NOLOGO_KEY =
+OPTIMIZATION_DISABLED_FLAGS = $(KEY)O0 $(KEY)g
+OPTIMIZATION_ENABLED_FLAGS +=  $(KEY)O2
+TBB_LIB_NAME = tbb
+CPLUS = $(compiler)
+LD = $(CPLUS)
+
+USE_SHARED_CPPRUNTIME_KEY =
+LINK_KEY = $(KEY)l
+
+LD_OUT_KEY = $(KEY)o
+DYN_LDFLAGS += -L. -L$(proj_root)/build
+
+ifneq ($(PSTL_LIB_NAME), )
+    PSTL_LIB_LINK += $(LINK_KEY)$(PSTL_LIB_NAME)$(PSTL_LIB_EXT)
+endif
+
+ifeq ($(backend), tbb)
+    DYN_LDFLAGS += $(LINK_KEY)$(TBB_LIB_NAME)
+endif
+
+
+ifeq ($(arch),intel64)
+    PSTL_ARCH = $(MACHINE_KEY)64
+else ifeq ($(arch),ia32)
+    PSTL_ARCH = $(MACHINE_KEY)32
+else ifeq ($(arch),)
+    $(info arch=native by default)
+else
+    PSTL_ARCH = $(MACHINE_KEY)$(arch)
+endif
diff --git a/pstl/build/windows.inc b/pstl/build/windows.inc
new file mode 100644 (file)
index 0000000..9a152c6
--- /dev/null
@@ -0,0 +1,54 @@
+#===-- windows.inc -------------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+PYTHON = python
+compiler ?= icl
+LD = LINK
+KEY = /
+LINK_KEY =
+QKEY = $(KEY)Q
+FKEY = $(KEY)F
+FQKEY = $(QKEY)
+MACHINE_KEY = $(QKEY)
+OBJ_SFX = .obj
+DEL_CMD = del $(KEY)F
+RUN_CMD =
+COMMAND_SEPARATOR = &
+COMPILER_NOLOGO_KEY = $(KEY)nologo
+OPTIMIZATION_DISABLED_FLAGS = $(KEY)Od $(KEY)Zi $(KEY)DEBUG $(KEY)Fd"$*.pdb"
+OPTIMIZATION_ENABLED_FLAGS = $(KEY)O2 $(KEY)DNDEBUG
+LD_OUT_KEY = $(KEY)OUT:
+
+ifneq ($(PSTL_LIB_NAME), )
+    PSTL_LIB_EXT = .lib
+    PSTL_LIB_LINK += $(LINK_KEY)$(PSTL_LIB_NAME)$(PSTL_LIB_EXT)
+endif
+# Do not update LDFLAGS with corresponding TBB_LIB_NAME here, because of
+# implicit linkage capability of TBB library
+
+ifeq ($(cfg),debug)
+    LINK_KEY += $(KEY)debug
+    USE_SHARED_CPPRUNTIME_KEY += $(KEY)MDd $(KEY)EHsc
+    BACKEND_MACRO += -DTBB_USE_DEBUG=1
+else
+    USE_SHARED_CPPRUNTIME_KEY += $(KEY)MD $(KEY)EHsc
+endif
+
+ifneq (, $(filter $(compiler), cl icl))
+    CPLUS_FLAGS += $(KEY)bigobj
+endif
+
+
+DYN_LDFLAGS += $(LINK_KEY)
+
+ifneq (,$(filter uwp,$(target_app) $(target_ui)))
+    CPLUS_FLAGS += /ZW:nostdlib /D "_UNICODE" /D "UNICODE" /D "WINAPI_FAMILY=WINAPI_FAMILY_APP"
+    _WIN32_WINNT = 0x0A00
+    DYN_LDFLAGS += /NODEFAULTLIB:"kernel32.lib" OneCore.lib
+endif
diff --git a/pstl/cmake/FindTBB.cmake b/pstl/cmake/FindTBB.cmake
new file mode 100644 (file)
index 0000000..654ee41
--- /dev/null
@@ -0,0 +1,78 @@
+#===-- FindTBB.cmake -----------------------------------------------------===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is dual licensed under the MIT and the University of Illinois Open
+# Source Licenses. See LICENSE.TXT for details.
+#
+#===----------------------------------------------------------------------===##
+
+include(FindPackageHandleStandardArgs)
+
+# Firstly search for TBB in config mode (i.e. search for TBBConfig.cmake).
+find_package(TBB QUIET CONFIG)
+if (TBB_FOUND)
+    find_package_handle_standard_args(TBB
+                                      REQUIRED_VARS TBB_IMPORTED_TARGETS
+                                      HANDLE_COMPONENTS
+                                      VERSION_VAR TBB_VERSION
+                                      CONFIG_MODE)
+    return()
+endif()
+
+if (NOT TBB_FIND_COMPONENTS)
+    set(TBB_FIND_COMPONENTS tbb tbbmalloc)
+    foreach (_tbb_component ${TBB_FIND_COMPONENTS})
+        set(TBB_FIND_REQUIRED_${_tbb_component} 1)
+    endforeach()
+endif()
+
+find_path(_tbb_include_dir tbb/tbb.h)
+if (_tbb_include_dir)
+    file(READ "${_tbb_include_dir}/tbb/tbb_stddef.h" _tbb_stddef LIMIT 2048)
+    string(REGEX REPLACE ".*#define TBB_VERSION_MAJOR ([0-9]+).*" "\\1" _tbb_ver_major "${_tbb_stddef}")
+    string(REGEX REPLACE ".*#define TBB_VERSION_MINOR ([0-9]+).*" "\\1" _tbb_ver_minor "${_tbb_stddef}")
+    string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" TBB_INTERFACE_VERSION "${_tbb_stddef}")
+
+    set(TBB_VERSION "${_tbb_ver_major}.${_tbb_ver_minor}")
+
+    unset(_tbb_stddef)
+    unset(_tbb_ver_major)
+    unset(_tbb_ver_minor)
+
+    foreach (_tbb_component ${TBB_FIND_COMPONENTS})
+        find_library(_tbb_release_lib ${_tbb_component})
+        if (_tbb_release_lib)
+            set(TBB_${_tbb_component}_FOUND 1)
+
+            add_library(TBB::${_tbb_component} SHARED IMPORTED)
+            list(APPEND TBB_IMPORTED_TARGETS TBB::${_tbb_component})
+
+            set(_tbb_lib_suffix)
+            if (UNIX AND NOT APPLE)
+                set(_tbb_lib_suffix ".2")
+            endif()
+
+            set_target_properties(TBB::${_tbb_component} PROPERTIES
+                                  IMPORTED_CONFIGURATIONS       "RELEASE"
+                                  IMPORTED_LOCATION_RELEASE     "${_tbb_release_lib}${_tbb_lib_suffix}"
+                                  INTERFACE_INCLUDE_DIRECTORIES "${_tbb_include_dir}")
+
+            find_library(_tbb_debug_lib ${_tbb_component}_debug)
+            if (_tbb_debug_lib)
+                set_target_properties(TBB::${_tbb_component} PROPERTIES
+                                      IMPORTED_CONFIGURATIONS "RELEASE;DEBUG"
+                                      IMPORTED_LOCATION_DEBUG "${_tbb_debug_lib}${_tbb_lib_suffix}")
+            endif()
+            unset(_tbb_debug_lib CACHE)
+            unset(_tbb_lib_suffix)
+        endif()
+        unset(_tbb_release_lib CACHE)
+    endforeach()
+endif()
+unset(_tbb_include_dir CACHE)
+
+find_package_handle_standard_args(TBB
+                                  REQUIRED_VARS TBB_IMPORTED_TARGETS
+                                  HANDLE_COMPONENTS
+                                  VERSION_VAR TBB_VERSION)
diff --git a/pstl/include/pstl/algorithm b/pstl/include/pstl/algorithm
new file mode 100644 (file)
index 0000000..57d83b0
--- /dev/null
@@ -0,0 +1,25 @@
+// -*- C++ -*-
+//===-- algorithm ---------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_algorithm
+#define __PSTL_algorithm
+
+#include "internal/pstl_config.h"
+
+#if __PSTL_EXECUTION_POLICIES_DEFINED
+// If <execution> has already been included, pull in implementations
+#include "internal/glue_algorithm_impl.h"
+#else
+// Otherwise just pull in forward declarations
+#include "internal/glue_algorithm_defs.h"
+#define __PSTL_ALGORITHM_FORWARD_DECLARED 1
+#endif
+
+#endif /* __PSTL_algorithm */
diff --git a/pstl/include/pstl/execution b/pstl/include/pstl/execution
new file mode 100644 (file)
index 0000000..d476b74
--- /dev/null
@@ -0,0 +1,49 @@
+// -*- C++ -*-
+//===-- execution ---------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_execution
+#define __PSTL_execution
+
+#include "internal/pstl_config.h"
+#include "internal/execution_defs.h"
+
+#define __PSTL_EXECUTION_POLICIES_DEFINED 1
+
+#if __PSTL_ALGORITHM_FORWARD_DECLARED
+#include "internal/glue_algorithm_impl.h"
+#endif
+
+#if __PSTL_MEMORY_FORWARD_DECLARED
+#include "internal/glue_memory_impl.h"
+#endif
+
+#if __PSTL_NUMERIC_FORWARD_DECLARED
+#include "internal/glue_numeric_impl.h"
+#endif
+
+#if __PSTL_CPP17_EXECUTION_POLICIES_PRESENT
+__PSTL_PRAGMA_MESSAGE_POLICIES("The <Parallel STL> execution policies are defined in the namespace __pstl::execution")
+#else
+#include "internal/glue_execution_defs.h"
+__PSTL_PRAGMA_MESSAGE_POLICIES(
+    "The <Parallel STL> execution policies are injected into the standard namespace std::execution")
+#endif
+
+//TODO: __pstl::execution namespace is injected into the pstl::execution namespace when the implementation is not a part of
+// standard C++ library
+namespace pstl
+{
+namespace execution
+{
+using namespace __pstl::execution;
+}
+}
+
+#endif /* __PSTL_execution */
diff --git a/pstl/include/pstl/internal/algorithm_impl.h b/pstl/include/pstl/internal/algorithm_impl.h
new file mode 100644 (file)
index 0000000..a3b2a2c
--- /dev/null
@@ -0,0 +1,3557 @@
+// -*- C++ -*-
+//===-- algorithm_impl.h --------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_algorithm_impl_H
+#define __PSTL_algorithm_impl_H
+
+#include <iterator>
+#include <type_traits>
+#include <utility>
+#include <functional>
+#include <algorithm>
+
+#include "execution_impl.h"
+#include "memory_impl.h"
+#include "unseq_backend_simd.h"
+
+#if __PSTL_USE_PAR_POLICIES
+#include "parallel_backend.h"
+#endif
+#include "parallel_impl.h"
+
+namespace __pstl
+{
+namespace internal
+{
+
+//------------------------------------------------------------------------
+// any_of
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class _Pred>
+bool
+brick_any_of(const _ForwardIterator __first, const _ForwardIterator __last, _Pred __pred,
+             /*__is_vector=*/std::false_type) noexcept
+{
+    return std::any_of(__first, __last, __pred);
+};
+
+template <class _ForwardIterator, class _Pred>
+bool
+brick_any_of(const _ForwardIterator __first, const _ForwardIterator __last, _Pred __pred,
+             /*__is_vector=*/std::true_type) noexcept
+{
+    return unseq_backend::simd_or(__first, __last - __first, __pred);
+};
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Pred, class _IsVector>
+bool
+pattern_any_of(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Pred __pred,
+               _IsVector __is_vector, /*parallel=*/std::false_type) noexcept
+{
+    return internal::brick_any_of(__first, __last, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Pred, class _IsVector>
+bool
+pattern_any_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Pred __pred,
+               _IsVector __is_vector, /*parallel=*/std::true_type)
+{
+    return internal::except_handler([&]() {
+        return internal::parallel_or(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                     [__pred, __is_vector](_ForwardIterator __i, _ForwardIterator __j) {
+                                         return internal::brick_any_of(__i, __j, __pred, __is_vector);
+                                     });
+    });
+}
+
+// [alg.foreach]
+// for_each_n with no policy
+
+template <class _ForwardIterator, class _Size, class _Function>
+_ForwardIterator
+for_each_n_it_serial(_ForwardIterator __first, _Size __n, _Function __f)
+{
+    for (; __n > 0; ++__first, --__n)
+        __f(__first);
+    return __first;
+}
+
+//------------------------------------------------------------------------
+// walk1 (pseudo)
+//
+// walk1 evaluates f(x) for each dereferenced value x drawn from [first,last)
+//------------------------------------------------------------------------
+template <class _ForwardIterator, class _Function>
+void
+brick_walk1(_ForwardIterator __first, _ForwardIterator __last, _Function __f, /*vector=*/std::false_type) noexcept
+{
+    std::for_each(__first, __last, __f);
+}
+
+template <class _RandomAccessIterator, class _Function>
+void
+brick_walk1(_RandomAccessIterator __first, _RandomAccessIterator __last, _Function __f,
+            /*vector=*/std::true_type) noexcept
+{
+    unseq_backend::simd_walk_1(__first, __last - __first, __f);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Function, class _IsVector>
+void
+pattern_walk1(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Function __f,
+              _IsVector __is_vector,
+              /*parallel=*/std::false_type) noexcept
+{
+    internal::brick_walk1(__first, __last, __f, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Function, class _IsVector>
+void
+pattern_walk1(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Function __f,
+              _IsVector __is_vector,
+              /*parallel=*/std::true_type)
+{
+    internal::except_handler([&]() {
+        par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                  [__f, __is_vector](_ForwardIterator __i, _ForwardIterator __j) {
+                                      internal::brick_walk1(__i, __j, __f, __is_vector);
+                                  });
+    });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Brick>
+void
+pattern_walk_brick(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Brick __brick,
+                   /*parallel=*/std::false_type) noexcept
+{
+    __brick(__first, __last);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Brick>
+void
+pattern_walk_brick(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Brick __brick,
+                   /*parallel=*/std::true_type)
+{
+    internal::except_handler([&]() {
+        par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                  [__brick](_ForwardIterator __i, _ForwardIterator __j) { __brick(__i, __j); });
+    });
+}
+
+//------------------------------------------------------------------------
+// walk1_n
+//------------------------------------------------------------------------
+template <class _ForwardIterator, class _Size, class _Function>
+_ForwardIterator
+brick_walk1_n(_ForwardIterator __first, _Size __n, _Function __f, /*_IsVectorTag=*/std::false_type)
+{
+    return for_each_n_it_serial(__first, __n, [&__f](_ForwardIterator __it) { __f(*__it); }); // calling serial version
+}
+
+template <class _RandomAccessIterator, class _DifferenceType, class _Function>
+_RandomAccessIterator
+brick_walk1_n(_RandomAccessIterator __first, _DifferenceType __n, _Function __f,
+              /*vectorTag=*/std::true_type) noexcept
+{
+    return unseq_backend::simd_walk_1(__first, __n, __f);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Function, class _IsVector>
+_ForwardIterator
+pattern_walk1_n(_ExecutionPolicy&&, _ForwardIterator __first, _Size __n, _Function __f, _IsVector __is_vector,
+                /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_walk1_n(__first, __n, __f, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Function, class _IsVector>
+_RandomAccessIterator
+pattern_walk1_n(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _Size __n, _Function __f,
+                _IsVector __is_vector,
+                /*is_parallel=*/std::true_type)
+{
+    internal::pattern_walk1(std::forward<_ExecutionPolicy>(__exec), __first, __first + __n, __f, __is_vector,
+                            std::true_type());
+    return __first + __n;
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Brick>
+_ForwardIterator
+pattern_walk_brick_n(_ExecutionPolicy&&, _ForwardIterator __first, _Size __n, _Brick __brick,
+                     /*is_parallel=*/std::false_type) noexcept
+{
+    return __brick(__first, __n);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Brick>
+_RandomAccessIterator
+pattern_walk_brick_n(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _Size __n, _Brick __brick,
+                     /*is_parallel=*/std::true_type)
+{
+    return internal::except_handler([&]() {
+        par_backend::parallel_for(
+            std::forward<_ExecutionPolicy>(__exec), __first, __first + __n,
+            [__brick](_RandomAccessIterator __i, _RandomAccessIterator __j) { __brick(__i, __j - __i); });
+        return __first + __n;
+    });
+}
+
+//------------------------------------------------------------------------
+// walk2 (pseudo)
+//
+// walk2 evaluates f(x,y) for deferenced values (x,y) drawn from [first1,last1) and [first2,...)
+//------------------------------------------------------------------------
+template <class _ForwardIterator1, class _ForwardIterator2, class _Function>
+_ForwardIterator2
+brick_walk2(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _Function __f,
+            /*vector=*/std::false_type) noexcept
+{
+    for (; __first1 != __last1; ++__first1, ++__first2)
+        __f(*__first1, *__first2);
+    return __first2;
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _Function>
+_ForwardIterator2
+brick_walk2(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _Function __f,
+            /*vector=*/std::true_type) noexcept
+{
+    return unseq_backend::simd_walk_2(__first1, __last1 - __first1, __first2, __f);
+}
+
+template <class _ForwardIterator1, class _Size, class _ForwardIterator2, class _Function>
+_ForwardIterator2
+brick_walk2_n(_ForwardIterator1 __first1, _Size __n, _ForwardIterator2 __first2, _Function __f,
+              /*vector=*/std::false_type) noexcept
+{
+    for (; __n > 0; --__n, ++__first1, ++__first2)
+        __f(*__first1, *__first2);
+    return __first2;
+}
+
+template <class _ForwardIterator1, class _Size, class _ForwardIterator2, class _Function>
+_ForwardIterator2
+brick_walk2_n(_ForwardIterator1 __first1, _Size __n, _ForwardIterator2 __first2, _Function __f,
+              /*vector=*/std::true_type) noexcept
+{
+    return unseq_backend::simd_walk_2(__first1, __n, __first2, __f);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Function, class _IsVector>
+_ForwardIterator2
+pattern_walk2(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+              _Function __f, _IsVector __is_vector, /*parallel=*/std::false_type) noexcept
+{
+    return internal::brick_walk2(__first1, __last1, __first2, __f, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Function, class _IsVector>
+_ForwardIterator2
+pattern_walk2(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+              _ForwardIterator2 __first2, _Function __f, _IsVector __is_vector, /*parallel=*/std::true_type)
+{
+    return internal::except_handler([&]() {
+        par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __first1, __last1,
+                                  [__f, __first1, __first2, __is_vector](_ForwardIterator1 __i, _ForwardIterator1 __j) {
+                                      internal::brick_walk2(__i, __j, __first2 + (__i - __first1), __f, __is_vector);
+                                  });
+        return __first2 + (__last1 - __first1);
+    });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _Size, class _ForwardIterator2, class _Function,
+          class _IsVector>
+_ForwardIterator2
+pattern_walk2_n(_ExecutionPolicy&&, _ForwardIterator1 __first1, _Size n, _ForwardIterator2 __first2, _Function f,
+                _IsVector is_vector, /*parallel=*/std::false_type) noexcept
+{
+    return internal::brick_walk2_n(__first1, n, __first2, f, is_vector);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator1, class _Size, class _RandomAccessIterator2,
+          class _Function, class _IsVector>
+_RandomAccessIterator2
+pattern_walk2_n(_ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, _Size n, _RandomAccessIterator2 __first2,
+                _Function f, _IsVector is_vector, /*parallel=*/std::true_type)
+{
+    return internal::pattern_walk2(std::forward<_ExecutionPolicy>(__exec), __first1, __first1 + n, __first2, f,
+                                   is_vector, std::true_type());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Brick>
+_ForwardIterator2
+pattern_walk2_brick(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                    _ForwardIterator2 __first2, _Brick __brick, /*parallel=*/std::false_type) noexcept
+{
+    return __brick(__first1, __last1, __first2);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, class _Brick>
+_RandomAccessIterator2
+pattern_walk2_brick(_ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
+                    _RandomAccessIterator2 __first2, _Brick __brick, /*parallel=*/std::true_type)
+{
+    return except_handler([&]() {
+        par_backend::parallel_for(
+            std::forward<_ExecutionPolicy>(__exec), __first1, __last1,
+            [__first1, __first2, __brick](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) {
+                __brick(__i, __j, __first2 + (__i - __first1));
+            });
+        return __first2 + (__last1 - __first1);
+    });
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator1, class _Size, class _RandomAccessIterator2, class _Brick>
+_RandomAccessIterator2
+pattern_walk2_brick_n(_ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, _Size __n,
+                      _RandomAccessIterator2 __first2, _Brick __brick, /*parallel=*/std::true_type)
+{
+    return except_handler([&]() {
+        par_backend::parallel_for(
+            std::forward<_ExecutionPolicy>(__exec), __first1, __first1 + __n,
+            [__first1, __first2, __brick](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) {
+                __brick(__i, __j - __i, __first2 + (__i - __first1));
+            });
+        return __first2 + __n;
+    });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _Size, class _ForwardIterator2, class _Brick>
+_ForwardIterator2
+pattern_walk2_brick_n(_ExecutionPolicy&&, _ForwardIterator1 __first1, _Size __n, _ForwardIterator2 __first2,
+                      _Brick __brick, /*parallel=*/std::false_type) noexcept
+{
+    return __brick(__first1, __n, __first2);
+}
+
+//------------------------------------------------------------------------
+// walk3 (pseudo)
+//
+// walk3 evaluates f(x,y,z) for (x,y,z) drawn from [first1,last1), [first2,...), [first3,...)
+//------------------------------------------------------------------------
+template <class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator3, class _Function>
+_ForwardIterator3
+brick_walk3(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+            _ForwardIterator3 __first3, _Function __f, /*vector=*/std::false_type) noexcept
+{
+    for (; __first1 != __last1; ++__first1, ++__first2, ++__first3)
+        __f(*__first1, *__first2, *__first3);
+    return __first3;
+}
+
+template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _RandomAccessIterator3, class _Function>
+_RandomAccessIterator3
+brick_walk3(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2,
+            _RandomAccessIterator3 __first3, _Function __f, /*vector=*/std::true_type) noexcept
+{
+    return unseq_backend::simd_walk_3(__first1, __last1 - __first1, __first2, __first3, __f);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator3,
+          class _Function, class _IsVector>
+_ForwardIterator3
+pattern_walk3(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+              _ForwardIterator3 __first3, _Function __f, _IsVector __is_vector, /*parallel=*/std::false_type) noexcept
+{
+    return internal::brick_walk3(__first1, __last1, __first2, __first3, __f, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2,
+          class _RandomAccessIterator3, class _Function, class _IsVector>
+_RandomAccessIterator3
+pattern_walk3(_ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
+              _RandomAccessIterator2 __first2, _RandomAccessIterator3 __first3, _Function __f, _IsVector __is_vector,
+              /*parallel=*/std::true_type)
+{
+    return internal::except_handler([&]() {
+        par_backend::parallel_for(
+            std::forward<_ExecutionPolicy>(__exec), __first1, __last1,
+            [__f, __first1, __first2, __first3, __is_vector](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) {
+                internal::brick_walk3(__i, __j, __first2 + (__i - __first1), __first3 + (__i - __first1), __f,
+                                      __is_vector);
+            });
+        return __first3 + (__last1 - __first1);
+    });
+}
+
+//------------------------------------------------------------------------
+// equal
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+bool
+brick_equal(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _BinaryPredicate __p,
+            /* IsVector = */ std::false_type) noexcept
+{
+    return std::equal(__first1, __last1, __first2, __p);
+}
+
+template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate>
+bool
+brick_equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2,
+            _BinaryPredicate __p, /* is_vector = */ std::true_type) noexcept
+{
+    return unseq_backend::simd_first(__first1, __last1 - __first1, __first2, not_pred<_BinaryPredicate>(__p)).first ==
+           __last1;
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate,
+          class _IsVector>
+bool
+pattern_equal(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+              _BinaryPredicate __p, _IsVector __is_vector, /* is_parallel = */ std::false_type) noexcept
+{
+    return internal::brick_equal(__first1, __last1, __first2, __p, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate,
+          class _IsVector>
+bool
+pattern_equal(_ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
+              _RandomAccessIterator2 __first2, _BinaryPredicate __p, _IsVector __is_vector,
+              /*is_parallel=*/std::true_type)
+{
+    return internal::except_handler([&]() {
+        return !internal::parallel_or(
+            std::forward<_ExecutionPolicy>(__exec), __first1, __last1,
+            [__first1, __first2, __p, __is_vector](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) {
+                return !brick_equal(__i, __j, __first2 + (__i - __first1), __p, __is_vector);
+            });
+    });
+}
+
+//------------------------------------------------------------------------
+// find_if
+//------------------------------------------------------------------------
+template <class _ForwardIterator, class _Predicate>
+_ForwardIterator
+brick_find_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
+              /*is_vector=*/std::false_type) noexcept
+{
+    return std::find_if(__first, __last, __pred);
+}
+
+template <class _RandomAccessIterator, class _Predicate>
+_RandomAccessIterator
+brick_find_if(_RandomAccessIterator __first, _RandomAccessIterator __last, _Predicate __pred,
+              /*is_vector=*/std::true_type) noexcept
+{
+    typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _SizeType;
+    return unseq_backend::simd_first(
+        __first, _SizeType(0), __last - __first,
+        [&__pred](_RandomAccessIterator __it, _SizeType __i) { return __pred(__it[__i]); });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate, class _IsVector>
+_ForwardIterator
+pattern_find_if(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
+                _IsVector __is_vector,
+                /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_find_if(__first, __last, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate, class _IsVector>
+_ForwardIterator
+pattern_find_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
+                _IsVector __is_vector,
+                /*is_parallel=*/std::true_type)
+{
+    return internal::except_handler([&]() {
+        return internal::parallel_find(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                       [__pred, __is_vector](_ForwardIterator __i, _ForwardIterator __j) {
+                                           return internal::brick_find_if(__i, __j, __pred, __is_vector);
+                                       },
+                                       std::less<typename std::iterator_traits<_ForwardIterator>::difference_type>(),
+                                       /*is_first=*/true);
+    });
+}
+
+//------------------------------------------------------------------------
+// find_end
+//------------------------------------------------------------------------
+
+// find the first occurrence of the subsequence [s_first, s_last)
+//   or the  last occurrence of the subsequence in the range [first, last)
+// b_first determines what occurrence we want to find (first or last)
+template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate, class _IsVector>
+_RandomAccessIterator1
+find_subrange(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator1 __global_last,
+              _RandomAccessIterator2 __s_first, _RandomAccessIterator2 __s_last, _BinaryPredicate __pred,
+              bool __b_first, _IsVector __is_vector) noexcept
+{
+    typedef typename std::iterator_traits<_RandomAccessIterator2>::value_type _ValueType;
+    auto __n2 = __s_last - __s_first;
+    if (__n2 < 1)
+    {
+        return __b_first ? __first : __last;
+    }
+
+    auto __n1 = __global_last - __first;
+    if (__n1 < __n2)
+    {
+        return __last;
+    }
+
+    auto __cur = __last;
+    while (__first != __last && (__global_last - __first >= __n2))
+    {
+        // find position of *s_first in [first, last) (it can be start of subsequence)
+        __first = internal::brick_find_if(
+            __first, __last, internal::equal_value_by_pred<_ValueType, _BinaryPredicate>(*__s_first, __pred),
+            __is_vector);
+
+        // if position that was found previously is the start of subsequence
+        // then we can exit the loop (b_first == true) or keep the position
+        // (b_first == false)
+        if (__first != __last && (__global_last - __first >= __n2) &&
+            internal::brick_equal(__s_first + 1, __s_last, __first + 1, __pred, __is_vector))
+        {
+            if (__b_first)
+            {
+                return __first;
+            }
+            else
+            {
+                __cur = __first;
+            }
+        }
+        else if (__first == __last)
+        {
+            break;
+        }
+        else
+        {
+        }
+
+        // in case of b_first == false we try to find new start position
+        // for the next subsequence
+        ++__first;
+    }
+    return __cur;
+}
+
+template <class _RandomAccessIterator, class _Size, class _Tp, class _BinaryPredicate, class _IsVector>
+_RandomAccessIterator
+find_subrange(_RandomAccessIterator __first, _RandomAccessIterator __last, _RandomAccessIterator __global_last,
+              _Size __count, const _Tp& __value, _BinaryPredicate __pred, _IsVector __is_vector) noexcept
+{
+    if (__global_last - __first < __count || __count < 1)
+    {
+        return __last; // According to the standard last shall be returned when count < 1
+    }
+
+    auto __n = __global_last - __first;
+    auto __unary_pred = internal::equal_value_by_pred<_Tp, _BinaryPredicate>(__value, __pred);
+    while (__first != __last && (__global_last - __first >= __count))
+    {
+        __first = brick_find_if(__first, __last, __unary_pred, __is_vector);
+
+        // check that all of elements in [first+1, first+count) equal to value
+        if (__first != __last && (__global_last - __first >= __count) &&
+            !internal::brick_any_of(__first + 1, __first + __count,
+                                    internal::not_pred<decltype(__unary_pred)>(__unary_pred), __is_vector))
+        {
+            return __first;
+        }
+        else if (__first == __last)
+        {
+            break;
+        }
+        else
+        {
+            ++__first;
+        }
+    }
+    return __last;
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+_ForwardIterator1
+brick_find_end(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+               _ForwardIterator2 __s_last, _BinaryPredicate __pred, /*__is_vector=*/std::false_type) noexcept
+{
+    return std::find_end(__first, __last, __s_first, __s_last, __pred);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+_ForwardIterator1
+brick_find_end(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+               _ForwardIterator2 __s_last, _BinaryPredicate __pred, /*__is_vector=*/std::true_type) noexcept
+{
+    return internal::find_subrange(__first, __last, __last, __s_first, __s_last, __pred, false, std::true_type());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate,
+          class _IsVector>
+_ForwardIterator1
+pattern_find_end(_ExecutionPolicy&&, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+                 _ForwardIterator2 __s_last, _BinaryPredicate __pred, _IsVector __is_vector,
+                 /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_find_end(__first, __last, __s_first, __s_last, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate,
+          class _IsVector>
+_ForwardIterator1
+pattern_find_end(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                 _ForwardIterator2 __s_first, _ForwardIterator2 __s_last, _BinaryPredicate __pred,
+                 _IsVector __is_vector, /*is_parallel=*/std::true_type) noexcept
+{
+    if (__last - __first == __s_last - __s_first)
+    {
+        const bool __res = internal::pattern_equal(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first,
+                                                   __pred, __is_vector, std::true_type());
+        return __res ? __first : __last;
+    }
+    else
+    {
+        return except_handler([&]() {
+            return internal::parallel_find(
+                std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                [__first, __last, __s_first, __s_last, __pred, __is_vector](_ForwardIterator1 __i,
+                                                                            _ForwardIterator1 __j) {
+                    return internal::find_subrange(__i, __j, __last, __s_first, __s_last, __pred, false, __is_vector);
+                },
+                std::greater<typename std::iterator_traits<_ForwardIterator1>::difference_type>(), /*is_first=*/false);
+        });
+    }
+}
+
+//------------------------------------------------------------------------
+// find_first_of
+//------------------------------------------------------------------------
+template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+_ForwardIterator1
+brick_find_first_of(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+                    _ForwardIterator2 __s_last, _BinaryPredicate __pred, /*__is_vector=*/std::false_type) noexcept
+{
+    return std::find_first_of(__first, __last, __s_first, __s_last, __pred);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+_ForwardIterator1
+brick_find_first_of(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+                    _ForwardIterator2 __s_last, _BinaryPredicate __pred, /*__is_vector=*/std::true_type) noexcept
+{
+    return unseq_backend::simd_find_first_of(__first, __last, __s_first, __s_last, __pred);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate,
+          class _IsVector>
+_ForwardIterator1
+pattern_find_first_of(_ExecutionPolicy&&, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                      _ForwardIterator2 __s_first, _ForwardIterator2 __s_last, _BinaryPredicate __pred,
+                      _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_find_first_of(__first, __last, __s_first, __s_last, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate,
+          class _IsVector>
+_ForwardIterator1
+pattern_find_first_of(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                      _ForwardIterator2 __s_first, _ForwardIterator2 __s_last, _BinaryPredicate __pred,
+                      _IsVector __is_vector, /*is_parallel=*/std::true_type) noexcept
+{
+    return except_handler([&]() {
+        return internal::parallel_find(
+            std::forward<_ExecutionPolicy>(__exec), __first, __last,
+            [__s_first, __s_last, __pred, __is_vector](_ForwardIterator1 __i, _ForwardIterator1 __j) {
+                return internal::brick_find_first_of(__i, __j, __s_first, __s_last, __pred, __is_vector);
+            },
+            std::less<typename std::iterator_traits<_ForwardIterator1>::difference_type>(), /*is_first=*/true);
+    });
+}
+
+//------------------------------------------------------------------------
+// search
+//------------------------------------------------------------------------
+template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+_ForwardIterator1
+brick_search(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+             _ForwardIterator2 __s_last, _BinaryPredicate __pred, /*vector=*/std::false_type) noexcept
+{
+    return std::search(__first, __last, __s_first, __s_last, __pred);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+_ForwardIterator1
+brick_search(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+             _ForwardIterator2 __s_last, _BinaryPredicate __pred, /*vector=*/std::true_type) noexcept
+{
+    return internal::find_subrange(__first, __last, __last, __s_first, __s_last, __pred, true, std::true_type());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate,
+          class _IsVector>
+_ForwardIterator1
+pattern_search(_ExecutionPolicy&&, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+               _ForwardIterator2 __s_last, _BinaryPredicate __pred, _IsVector __is_vector,
+               /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_search(__first, __last, __s_first, __s_last, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate,
+          class _IsVector>
+_ForwardIterator1
+pattern_search(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __s_first, _ForwardIterator2 __s_last, _BinaryPredicate __pred, _IsVector __is_vector,
+               /*is_parallel=*/std::true_type) noexcept
+{
+    if (__last - __first == __s_last - __s_first)
+    {
+        const bool __res = internal::pattern_equal(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first,
+                                                   __pred, __is_vector, std::true_type());
+        return __res ? __first : __last;
+    }
+    else
+    {
+        return except_handler([&]() {
+            return internal::parallel_find(
+                std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                [__last, __s_first, __s_last, __pred, __is_vector](_ForwardIterator1 __i, _ForwardIterator1 __j) {
+                    return internal::find_subrange(__i, __j, __last, __s_first, __s_last, __pred, true, __is_vector);
+                },
+                std::less<typename std::iterator_traits<_ForwardIterator1>::difference_type>(), /*is_first=*/true);
+        });
+    }
+}
+
+//------------------------------------------------------------------------
+// search_n
+//------------------------------------------------------------------------
+template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
+_ForwardIterator
+brick_search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value,
+               _BinaryPredicate __pred, /*vector=*/std::false_type) noexcept
+{
+    return std::search_n(__first, __last, __count, __value, __pred);
+}
+
+template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
+_ForwardIterator
+brick_search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value,
+               _BinaryPredicate __pred, /*vector=*/std::true_type) noexcept
+{
+    return internal::find_subrange(__first, __last, __last, __count, __value, __pred, std::true_type());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate,
+          class _IsVector>
+_ForwardIterator
+pattern_search_n(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Size __count,
+                 const _Tp& __value, _BinaryPredicate __pred, _IsVector __is_vector,
+                 /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_search_n(__first, __last, __count, __value, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Tp, class _BinaryPredicate,
+          class _IsVector>
+_RandomAccessIterator
+pattern_search_n(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Size __count,
+                 const _Tp& __value, _BinaryPredicate __pred, _IsVector __is_vector,
+                 /*is_parallel=*/std::true_type) noexcept
+{
+    if (__last - __first == __count)
+    {
+        const bool __result =
+            !internal::pattern_any_of(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                      [&__value, &__pred](const _Tp& __val) { return !__pred(__val, __value); },
+                                      __is_vector, /*is_parallel*/ std::true_type());
+        return __result ? __first : __last;
+    }
+    else
+    {
+        return except_handler([&__exec, __first, __last, __count, &__value, __pred, __is_vector]() {
+            return internal::parallel_find(
+                std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                [__last, __count, &__value, __pred, __is_vector](_RandomAccessIterator __i, _RandomAccessIterator __j) {
+                    return internal::find_subrange(__i, __j, __last, __count, __value, __pred, __is_vector);
+                },
+                std::less<typename std::iterator_traits<_RandomAccessIterator>::difference_type>(), /*is_first=*/true);
+        });
+    }
+}
+
+//------------------------------------------------------------------------
+// copy_n
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class _Size, class _OutputIterator>
+_OutputIterator
+brick_copy_n(_ForwardIterator __first, _Size __n, _OutputIterator __result, /*vector=*/std::false_type) noexcept
+{
+    return std::copy_n(__first, __n, __result);
+}
+
+template <class _ForwardIterator, class _Size, class _OutputIterator>
+_OutputIterator
+brick_copy_n(_ForwardIterator __first, _Size __n, _OutputIterator __result, /*vector=*/std::true_type) noexcept
+{
+    return unseq_backend::simd_assign(__first, __n, __result,
+                                      [](_ForwardIterator __first, _OutputIterator __result) { *__result = *__first; });
+}
+
+//------------------------------------------------------------------------
+// copy
+//------------------------------------------------------------------------
+template <class _ForwardIterator, class _OutputIterator>
+_OutputIterator
+brick_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+           /*vector=*/std::false_type) noexcept
+{
+    return std::copy(__first, __last, __result);
+}
+
+template <class _RandomAccessIterator, class _OutputIterator>
+_OutputIterator
+brick_copy(_RandomAccessIterator __first, _RandomAccessIterator __last, _OutputIterator __result,
+           /*vector=*/std::true_type) noexcept
+{
+    return unseq_backend::simd_assign(
+        __first, __last - __first, __result,
+        [](_RandomAccessIterator __first, _OutputIterator __result) { *__result = *__first; });
+}
+
+//------------------------------------------------------------------------
+// move
+//------------------------------------------------------------------------
+template <class _ForwardIterator, class _OutputIterator>
+_OutputIterator
+brick_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+           /*vector=*/std::false_type) noexcept
+{
+    return std::move(__first, __last, __result);
+}
+
+template <class _RandomAccessIterator, class _OutputIterator>
+_OutputIterator
+brick_move(_RandomAccessIterator __first, _RandomAccessIterator __last, _OutputIterator __result,
+           /*vector=*/std::true_type) noexcept
+{
+    return unseq_backend::simd_assign(
+        __first, __last - __first, __result,
+        [](_RandomAccessIterator __first, _OutputIterator __result) { *__result = std::move(*__first); });
+}
+
+//------------------------------------------------------------------------
+// swap_ranges
+//------------------------------------------------------------------------
+template <class _ForwardIterator, class _OutputIterator>
+_OutputIterator
+brick_swap_ranges(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+                  /*vector=*/std::false_type) noexcept
+{
+    return std::swap_ranges(__first, __last, __result);
+}
+
+template <class _ForwardIterator, class _OutputIterator>
+_OutputIterator
+brick_swap_ranges(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+                  /*vector=*/std::true_type) noexcept
+{
+    using std::iter_swap;
+    return unseq_backend::simd_assign(__first, __last - __first, __result,
+                                      iter_swap<_ForwardIterator, _OutputIterator>);
+}
+
+//------------------------------------------------------------------------
+// copy_if
+//------------------------------------------------------------------------
+template <class _ForwardIterator, class _OutputIterator, class _UnaryPredicate>
+_OutputIterator
+brick_copy_if(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _UnaryPredicate __pred,
+              /*vector=*/std::false_type) noexcept
+{
+    return std::copy_if(__first, __last, __result, __pred);
+}
+
+template <class _ForwardIterator, class _OutputIterator, class _UnaryPredicate>
+_OutputIterator
+brick_copy_if(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _UnaryPredicate __pred,
+              /*vector=*/std::true_type) noexcept
+{
+#if (__PSTL_MONOTONIC_PRESENT)
+    return unseq_backend::simd_copy_if(__first, __last - __first, __result, __pred);
+#else
+    return std::copy_if(__first, __last, __result, __pred);
+#endif
+}
+
+// TODO: Try to use transform_reduce for combining brick_copy_if_phase1 on IsVector.
+template <class _DifferenceType, class _ForwardIterator, class _UnaryPredicate>
+std::pair<_DifferenceType, _DifferenceType>
+brick_calc_mask_1(_ForwardIterator __first, _ForwardIterator __last, bool* __restrict __mask, _UnaryPredicate __pred,
+                  /*vector=*/std::false_type) noexcept
+{
+    auto __count_true = _DifferenceType(0);
+    auto __size = __last - __first;
+
+    static_assert(internal::is_random_access_iterator<_ForwardIterator>::value,
+                  "Pattern-brick error. Should be a random access iterator.");
+
+    for (; __first != __last; ++__first, ++__mask)
+    {
+        *__mask = __pred(*__first);
+        if (*__mask)
+        {
+            ++__count_true;
+        }
+    }
+    return std::make_pair(__count_true, __size - __count_true);
+}
+
+template <class _DifferenceType, class _RandomAccessIterator, class _UnaryPredicate>
+std::pair<_DifferenceType, _DifferenceType>
+brick_calc_mask_1(_RandomAccessIterator __first, _RandomAccessIterator __last, bool* __mask, _UnaryPredicate __pred,
+                  /*vector=*/std::true_type) noexcept
+{
+    auto __result = unseq_backend::simd_calc_mask_1(__first, __last - __first, __mask, __pred);
+    return std::make_pair(__result, (__last - __first) - __result);
+}
+
+template <class _ForwardIterator, class _OutputIterator, class _Assigner>
+void
+brick_copy_by_mask(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, bool* __mask,
+                   _Assigner __assigner, /*vector=*/std::false_type) noexcept
+{
+    for (; __first != __last; ++__first, ++__mask)
+    {
+        if (*__mask)
+        {
+            __assigner(__first, __result);
+            ++__result;
+        }
+    }
+}
+
+template <class _ForwardIterator, class _OutputIterator, class _Assigner>
+void
+brick_copy_by_mask(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, bool* __restrict __mask,
+                   _Assigner __assigner, /*vector=*/std::true_type) noexcept
+{
+#if (__PSTL_MONOTONIC_PRESENT)
+    unseq_backend::simd_copy_by_mask(__first, __last - __first, __result, __mask, __assigner);
+#else
+    internal::brick_copy_by_mask(__first, __last, __result, __mask, __assigner, std::false_type());
+#endif
+}
+
+template <class _ForwardIterator, class _OutputIterator1, class _OutputIterator2>
+void
+brick_partition_by_mask(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator1 __out_true,
+                        _OutputIterator2 __out_false, bool* __mask, /*vector=*/std::false_type) noexcept
+{
+    for (; __first != __last; ++__first, ++__mask)
+    {
+        if (*__mask)
+        {
+            *__out_true = *__first;
+            ++__out_true;
+        }
+        else
+        {
+            *__out_false = *__first;
+            ++__out_false;
+        }
+    }
+}
+
+template <class _RandomAccessIterator, class _OutputIterator1, class _OutputIterator2>
+void
+brick_partition_by_mask(_RandomAccessIterator __first, _RandomAccessIterator __last, _OutputIterator1 __out_true,
+                        _OutputIterator2 __out_false, bool* __mask, /*vector=*/std::true_type) noexcept
+{
+#if (__PSTL_MONOTONIC_PRESENT)
+    unseq_backend::simd_partition_by_mask(__first, __last - __first, __out_true, __out_false, __mask);
+#else
+    internal::brick_partition_by_mask(__first, __last, __out_true, __out_false, __mask, std::false_type());
+#endif
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _UnaryPredicate, class _IsVector>
+_OutputIterator
+pattern_copy_if(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+                _UnaryPredicate __pred, _IsVector __is_vector, /*parallel=*/std::false_type) noexcept
+{
+    return internal::brick_copy_if(__first, __last, __result, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, class _UnaryPredicate,
+          class _IsVector>
+_OutputIterator
+pattern_copy_if(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last,
+                _OutputIterator __result, _UnaryPredicate __pred, _IsVector __is_vector, /*parallel=*/std::true_type)
+{
+    typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType;
+    const _DifferenceType __n = __last - __first;
+    if (_DifferenceType(1) < __n)
+    {
+        par_backend::buffer<bool> __mask_buf(__n);
+        return except_handler([&__exec, __n, __first, __last, __result, __is_vector, __pred, &__mask_buf]() {
+            bool* __mask = __mask_buf.get();
+            _DifferenceType __m{};
+            par_backend::parallel_strict_scan(
+                std::forward<_ExecutionPolicy>(__exec), __n, _DifferenceType(0),
+                [=](_DifferenceType __i, _DifferenceType __len) { // Reduce
+                    return brick_calc_mask_1<_DifferenceType>(__first + __i, __first + (__i + __len), __mask + __i,
+                                                              __pred, __is_vector)
+                        .first;
+                },
+                std::plus<_DifferenceType>(),                                                // Combine
+                [=](_DifferenceType __i, _DifferenceType __len, _DifferenceType __initial) { // Scan
+                    brick_copy_by_mask(__first + __i, __first + (__i + __len), __result + __initial, __mask + __i,
+                                       [](_RandomAccessIterator __x, _OutputIterator __z) { *__z = *__x; },
+                                       __is_vector);
+                },
+                [&__m](_DifferenceType __total) { __m = __total; });
+            return __result + __m;
+        });
+    }
+    // trivial sequence - use serial algorithm
+    return brick_copy_if(__first, __last, __result, __pred, __is_vector);
+}
+
+//------------------------------------------------------------------------
+// count
+//------------------------------------------------------------------------
+template <class _ForwardIterator, class _Predicate>
+typename std::iterator_traits<_ForwardIterator>::difference_type
+brick_count(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
+            /* is_vector = */ std::true_type) noexcept
+{
+    return unseq_backend::simd_count(__first, __last - __first, __pred);
+}
+
+template <class _ForwardIterator, class _Predicate>
+typename std::iterator_traits<_ForwardIterator>::difference_type
+brick_count(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
+            /* is_vector = */ std::false_type) noexcept
+{
+    return std::count_if(__first, __last, __pred);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate, class _IsVector>
+typename std::iterator_traits<_ForwardIterator>::difference_type
+pattern_count(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
+              /* is_parallel */ std::false_type, _IsVector __is_vector) noexcept
+{
+    return brick_count(__first, __last, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate, class _IsVector>
+typename std::iterator_traits<_ForwardIterator>::difference_type
+pattern_count(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
+              /* is_parallel */ std::true_type, _IsVector __is_vector)
+{
+    typedef typename std::iterator_traits<_ForwardIterator>::difference_type _SizeType;
+    return except_handler([&]() {
+        return par_backend::parallel_reduce(
+            std::forward<_ExecutionPolicy>(__exec), __first, __last, _SizeType(0),
+            [__pred, __is_vector](_ForwardIterator __begin, _ForwardIterator __end, _SizeType __value) -> _SizeType {
+                return __value + brick_count(__begin, __end, __pred, __is_vector);
+            },
+            std::plus<_SizeType>());
+    });
+}
+
+//------------------------------------------------------------------------
+// unique
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class _BinaryPredicate>
+_ForwardIterator
+brick_unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred,
+             /*is_vector=*/std::false_type) noexcept
+{
+    return std::unique(__first, __last, __pred);
+}
+
+template <class _ForwardIterator, class _BinaryPredicate>
+_ForwardIterator
+brick_unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred,
+             /*is_vector=*/std::true_type) noexcept
+{
+    __PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial");
+    return std::unique(__first, __last, __pred);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate, class _IsVector>
+_ForwardIterator
+pattern_unique(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred,
+               _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_unique(__first, __last, __pred, __is_vector);
+}
+
+// That function is shared between two algorithms - remove_if (pattern_remove_if) and unique (pattern unique). But a mask calculation is different.
+// So, a caller passes _CalcMask brick into remove_elements.
+template <class _ExecutionPolicy, class _ForwardIterator, class _CalcMask, class _IsVector>
+_ForwardIterator
+remove_elements(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _CalcMask __calc_mask,
+                _IsVector __is_vector)
+{
+    typedef typename std::iterator_traits<_ForwardIterator>::difference_type _DifferenceType;
+    typedef typename std::iterator_traits<_ForwardIterator>::value_type _Tp;
+    _DifferenceType __n = __last - __first;
+    par_backend::buffer<bool> __mask_buf(__n);
+    // 1. find a first iterator that should be removed
+    return except_handler([&]() {
+        bool* __mask = __mask_buf.get();
+        _DifferenceType __min = par_backend::parallel_reduce(
+            std::forward<_ExecutionPolicy>(__exec), _DifferenceType(0), __n, __n,
+            [__first, __mask, &__calc_mask, __is_vector](_DifferenceType __i, _DifferenceType __j,
+                                                         _DifferenceType __local_min) -> _DifferenceType {
+                // Create mask
+                __calc_mask(__mask + __i, __mask + __j, __first + __i);
+
+                // if minimum was found in a previous range we shouldn't do anymore
+                if (__local_min < __i)
+                {
+                    return __local_min;
+                }
+                // find first iterator that should be removed
+                bool* __result =
+                    brick_find_if(__mask + __i, __mask + __j, [](bool __val) { return !__val; }, __is_vector);
+                if (__result - __mask == __j)
+                {
+                    return __local_min;
+                }
+                return std::min(__local_min, _DifferenceType(__result - __mask));
+            },
+            [](_DifferenceType __local_min1, _DifferenceType __local_min2) -> _DifferenceType {
+                return std::min(__local_min1, __local_min2);
+            });
+
+        // No elements to remove - exit
+        if (__min == __n)
+        {
+            return __last;
+        }
+        __n -= __min;
+        __first += __min;
+
+        par_backend::buffer<_Tp> __buf(__n);
+        _Tp* __result = __buf.get();
+        __mask += __min;
+        _DifferenceType __m{};
+        // 2. Elements that doesn't satisfy pred are moved to result
+        par_backend::parallel_strict_scan(
+            std::forward<_ExecutionPolicy>(__exec), __n, _DifferenceType(0),
+            [__mask, __is_vector](_DifferenceType __i, _DifferenceType __len) {
+                return brick_count(__mask + __i, __mask + __i + __len, [](bool __val) { return __val; }, __is_vector);
+            },
+            std::plus<_DifferenceType>(),
+            [=](_DifferenceType __i, _DifferenceType __len, _DifferenceType __initial) {
+                brick_copy_by_mask(__first + __i, __first + __i + __len, __result + __initial, __mask + __i,
+                                   [](_ForwardIterator __x, _Tp* __z) {
+                                       invoke_if_else(std::is_trivial<_Tp>(), [&]() { *__z = std::move(*__x); },
+                                                      [&]() { ::new (std::addressof(*__z)) _Tp(std::move(*__x)); });
+                                   },
+                                   __is_vector);
+            },
+            [&__m](_DifferenceType __total) { __m = __total; });
+
+        // 3. Elements from result are moved to [first, last)
+        par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __result, __result + __m,
+                                  [__result, __first, __is_vector](_Tp* __i, _Tp* __j) {
+                                      brick_move(__i, __j, __first + (__i - __result), __is_vector);
+                                  });
+        return __first + __m;
+    });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate, class _IsVector>
+_ForwardIterator
+pattern_unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred,
+               _IsVector __is_vector, /*is_parallel=*/std::true_type) noexcept
+{
+    typedef typename std::iterator_traits<_ForwardIterator>::reference _ReferenceType;
+
+    if (__first == __last)
+    {
+        return __last;
+    }
+    if (__first + 1 == __last || __first + 2 == __last)
+    {
+        // Trivial sequence - use serial algorithm
+        return brick_unique(__first, __last, __pred, __is_vector);
+    }
+    return remove_elements(
+        std::forward<_ExecutionPolicy>(__exec), ++__first, __last,
+        [&__pred, __is_vector](bool* __b, bool* __e, _ForwardIterator __it) {
+            brick_walk3(__b, __e, __it - 1, __it,
+                        [&__pred](bool& __x, _ReferenceType __y, _ReferenceType __z) { __x = !__pred(__y, __z); },
+                        __is_vector);
+        },
+        __is_vector);
+}
+
+//------------------------------------------------------------------------
+// unique_copy
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class OutputIterator, class _BinaryPredicate>
+OutputIterator
+brick_unique_copy(_ForwardIterator __first, _ForwardIterator __last, OutputIterator __result, _BinaryPredicate __pred,
+                  /*vector=*/std::false_type) noexcept
+{
+    return std::unique_copy(__first, __last, __result, __pred);
+}
+
+template <class _RandomAccessIterator, class OutputIterator, class _BinaryPredicate>
+OutputIterator
+brick_unique_copy(_RandomAccessIterator __first, _RandomAccessIterator __last, OutputIterator __result,
+                  _BinaryPredicate __pred, /*vector=*/std::true_type) noexcept
+{
+#if (__PSTL_MONOTONIC_PRESENT)
+    return unseq_backend::simd_unique_copy(__first, __last - __first, __result, __pred);
+#else
+    return std::unique_copy(__first, __last, __result, __pred);
+#endif
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _BinaryPredicate,
+          class _IsVector>
+_OutputIterator
+pattern_unique_copy(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+                    _BinaryPredicate __pred, _IsVector __is_vector, /*parallel=*/std::false_type) noexcept
+{
+    return internal::brick_unique_copy(__first, __last, __result, __pred, __is_vector);
+}
+
+template <class _DifferenceType, class _RandomAccessIterator, class _BinaryPredicate>
+_DifferenceType
+brick_calc_mask_2(_RandomAccessIterator __first, _RandomAccessIterator __last, bool* __restrict __mask,
+                  _BinaryPredicate __pred, /*vector=*/std::false_type) noexcept
+{
+    _DifferenceType __count = 0;
+    for (; __first != __last; ++__first, ++__mask)
+    {
+        *__mask = !__pred(*__first, *(__first - 1));
+        __count += *__mask;
+    }
+    return __count;
+}
+
+template <class _DifferenceType, class _RandomAccessIterator, class _BinaryPredicate>
+_DifferenceType
+brick_calc_mask_2(_RandomAccessIterator __first, _RandomAccessIterator __last, bool* __restrict __mask,
+                  _BinaryPredicate __pred, /*vector=*/std::true_type) noexcept
+{
+    return unseq_backend::simd_calc_mask_2(__first, __last - __first, __mask, __pred);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, class _BinaryPredicate,
+          class _IsVector>
+_OutputIterator
+pattern_unique_copy(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last,
+                    _OutputIterator __result, _BinaryPredicate __pred, _IsVector __is_vector,
+                    /*parallel=*/std::true_type)
+{
+    typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType;
+    const _DifferenceType __n = __last - __first;
+    if (_DifferenceType(2) < __n)
+    {
+        par_backend::buffer<bool> __mask_buf(__n);
+        if (_DifferenceType(2) < __n)
+        {
+            return internal::except_handler([&__exec, __n, __first, __result, __pred, __is_vector, &__mask_buf]() {
+                bool* __mask = __mask_buf.get();
+                _DifferenceType __m{};
+                par_backend::parallel_strict_scan(
+                    std::forward<_ExecutionPolicy>(__exec), __n, _DifferenceType(0),
+                    [=](_DifferenceType __i, _DifferenceType __len) -> _DifferenceType { // Reduce
+                        _DifferenceType __extra = 0;
+                        if (__i == 0)
+                        {
+                            // Special boundary case
+                            __mask[__i] = true;
+                            if (--__len == 0)
+                                return 1;
+                            ++__i;
+                            ++__extra;
+                        }
+                        return brick_calc_mask_2<_DifferenceType>(__first + __i, __first + (__i + __len), __mask + __i,
+                                                                  __pred, __is_vector) +
+                               __extra;
+                    },
+                    std::plus<_DifferenceType>(),                                                // Combine
+                    [=](_DifferenceType __i, _DifferenceType __len, _DifferenceType __initial) { // Scan
+                        // Phase 2 is same as for pattern_copy_if
+                        internal::brick_copy_by_mask(
+                            __first + __i, __first + (__i + __len), __result + __initial, __mask + __i,
+                            [](_RandomAccessIterator __x, _OutputIterator __z) { *__z = *__x; }, __is_vector);
+                    },
+                    [&__m](_DifferenceType __total) { __m = __total; });
+                return __result + __m;
+            });
+        }
+    }
+    // trivial sequence - use serial algorithm
+    return brick_unique_copy(__first, __last, __result, __pred, __is_vector);
+}
+
+//------------------------------------------------------------------------
+// reverse
+//------------------------------------------------------------------------
+template <class _BidirectionalIterator>
+void
+brick_reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, /*__is_vector=*/std::false_type) noexcept
+{
+    std::reverse(__first, __last);
+}
+
+template <class _BidirectionalIterator>
+void
+brick_reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, /*__is_vector=*/std::true_type) noexcept
+{
+    typedef typename std::iterator_traits<_BidirectionalIterator>::reference _ReferenceType;
+
+    const auto __n = (__last - __first) / 2;
+    unseq_backend::simd_walk_2(__first, __n, std::reverse_iterator<_BidirectionalIterator>(__last),
+                               [](_ReferenceType __x, _ReferenceType __y) {
+                                   using std::swap;
+                                   swap(__x, __y);
+                               });
+}
+
+// this brick is called in parallel version, so we can use iterator arithmetic
+template <class _BidirectionalIterator>
+void
+brick_reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, _BidirectionalIterator __d_last,
+              /*is_vector=*/std::false_type) noexcept
+{
+    for (--__d_last; __first != __last; ++__first, --__d_last)
+    {
+        using std::iter_swap;
+        iter_swap(__first, __d_last);
+    }
+}
+
+// this brick is called in parallel version, so we can use iterator arithmetic
+template <class _BidirectionalIterator>
+void
+brick_reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, _BidirectionalIterator __d_last,
+              /*is_vector=*/std::true_type) noexcept
+{
+    typedef typename std::iterator_traits<_BidirectionalIterator>::reference _ReferenceType;
+
+    unseq_backend::simd_walk_2(__first, __last - __first, std::reverse_iterator<_BidirectionalIterator>(__d_last),
+                               [](_ReferenceType __x, _ReferenceType __y) {
+                                   using std::swap;
+                                   swap(__x, __y);
+                               });
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _IsVector>
+void
+pattern_reverse(_ExecutionPolicy&&, _BidirectionalIterator __first, _BidirectionalIterator __last, _IsVector _is_vector,
+                /*is_parallel=*/std::false_type) noexcept
+{
+    brick_reverse(__first, __last, _is_vector);
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _IsVector>
+void
+pattern_reverse(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last,
+                _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+    par_backend::parallel_for(
+        std::forward<_ExecutionPolicy>(__exec), __first, __first + (__last - __first) / 2,
+        [__is_vector, __first, __last](_BidirectionalIterator __inner_first, _BidirectionalIterator __inner_last) {
+            brick_reverse(__inner_first, __inner_last, __last - (__inner_first - __first), __is_vector);
+        });
+}
+
+//------------------------------------------------------------------------
+// reverse_copy
+//------------------------------------------------------------------------
+
+template <class _BidirectionalIterator, class _OutputIterator>
+_OutputIterator
+brick_reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __d_first,
+                   /*is_vector=*/std::false_type) noexcept
+{
+    return std::reverse_copy(__first, __last, __d_first);
+}
+
+template <class _BidirectionalIterator, class _OutputIterator>
+_OutputIterator
+brick_reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __d_first,
+                   /*is_vector=*/std::true_type) noexcept
+{
+    typedef typename std::iterator_traits<_BidirectionalIterator>::reference _ReferenceType1;
+    typedef typename std::iterator_traits<_OutputIterator>::reference _ReferenceType2;
+
+    return unseq_backend::simd_walk_2(std::reverse_iterator<_BidirectionalIterator>(__last), __last - __first,
+                                      __d_first, [](_ReferenceType1 __x, _ReferenceType2 __y) { __y = __x; });
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _OutputIterator, class _IsVector>
+_OutputIterator
+pattern_reverse_copy(_ExecutionPolicy&&, _BidirectionalIterator __first, _BidirectionalIterator __last,
+                     _OutputIterator __d_first, _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_reverse_copy(__first, __last, __d_first, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _OutputIterator, class _IsVector>
+_OutputIterator
+pattern_reverse_copy(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last,
+                     _OutputIterator __d_first, _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+    auto __len = __last - __first;
+    par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                              [__is_vector, __first, __len, __d_first](_BidirectionalIterator __inner_first,
+                                                                       _BidirectionalIterator __inner_last) {
+                                  brick_reverse_copy(__inner_first, __inner_last,
+                                                     __d_first + (__len - (__inner_last - __first)), __is_vector);
+                              });
+    return __d_first + __len;
+}
+
+//------------------------------------------------------------------------
+// rotate
+//------------------------------------------------------------------------
+template <class _ForwardIterator>
+_ForwardIterator
+brick_rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
+             /*is_vector=*/std::false_type) noexcept
+{
+#if __PSTL_CPP11_STD_ROTATE_BROKEN
+    std::rotate(__first, __middle, __last);
+    return std::next(__first, std::distance(__middle, __last));
+#else
+    return std::rotate(__first, __middle, __last);
+#endif
+}
+
+template <class _ForwardIterator>
+_ForwardIterator
+brick_rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
+             /*is_vector=*/std::true_type) noexcept
+{
+    auto __n = __last - __first;
+    auto __m = __middle - __first;
+    const _ForwardIterator __ret = __first + (__last - __middle);
+
+    bool __is_left = (__m <= __n / 2);
+    if (!__is_left)
+        __m = __n - __m;
+
+    while (__n > 1 && __m > 0)
+    {
+        using std::iter_swap;
+        const auto __m_2 = __m * 2;
+        if (__is_left)
+        {
+            for (; __last - __first >= __m_2; __first += __m)
+            {
+                unseq_backend::simd_assign(__first, __m, __first + __m, iter_swap<_ForwardIterator, _ForwardIterator>);
+            }
+        }
+        else
+        {
+            for (; __last - __first >= __m_2; __last -= __m)
+            {
+                unseq_backend::simd_assign(__last - __m, __m, __last - __m_2,
+                                           iter_swap<_ForwardIterator, _ForwardIterator>);
+            }
+        }
+        __is_left = !__is_left;
+        __m = __n % __m;
+        __n = __last - __first;
+    }
+
+    return __ret;
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _IsVector>
+_ForwardIterator
+pattern_rotate(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
+               _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_rotate(__first, __middle, __last, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _IsVector>
+_ForwardIterator
+pattern_rotate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
+               _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+    typedef typename std::iterator_traits<_ForwardIterator>::value_type _Tp;
+    auto __n = __last - __first;
+    auto __m = __middle - __first;
+    if (__m <= __n / 2)
+    {
+        par_backend::buffer<_Tp> __buf(__n - __m);
+        return except_handler([&__exec, __n, __m, __first, __middle, __last, __is_vector, &__buf]() {
+            _Tp* __result = __buf.get();
+            par_backend::parallel_for(
+                std::forward<_ExecutionPolicy>(__exec), __middle, __last,
+                [__first, __last, __middle, __result, __is_vector](_ForwardIterator __b, _ForwardIterator __e) {
+                    brick_uninitialized_move(__b, __e, __result + (__b - __middle), __is_vector);
+                });
+
+            par_backend::parallel_for(
+                std::forward<_ExecutionPolicy>(__exec), __first, __middle,
+                [__first, __last, __middle, __result, __is_vector](_ForwardIterator __b, _ForwardIterator __e) {
+                    brick_move(__b, __e, __b + (__last - __middle), __is_vector);
+                });
+
+            par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __result, __result + (__n - __m),
+                                      [__first, __result, __is_vector](_Tp* __b, _Tp* __e) {
+                                          brick_move(__b, __e, __first + (__b - __result), __is_vector);
+                                      });
+
+            return __first + (__last - __middle);
+        });
+    }
+    else
+    {
+        par_backend::buffer<_Tp> __buf(__m);
+        return except_handler([&__exec, __n, __m, __first, __middle, __last, __is_vector, &__buf]() {
+            _Tp* __result = __buf.get();
+            par_backend::parallel_for(
+                std::forward<_ExecutionPolicy>(__exec), __first, __middle,
+                [__m, __first, __last, __middle, __result, __is_vector](_ForwardIterator __b, _ForwardIterator __e) {
+                    brick_uninitialized_move(__b, __e, __result + (__b - __first), __is_vector);
+                });
+
+            par_backend::parallel_for(
+                std::forward<_ExecutionPolicy>(__exec), __middle, __last,
+                [__first, __last, __middle, __result, __is_vector](_ForwardIterator __b, _ForwardIterator __e) {
+                    brick_move(__b, __e, __first + (__b - __middle), __is_vector);
+                });
+
+            par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __result, __result + __m,
+                                      [__n, __m, __first, __result, __is_vector](_Tp* __b, _Tp* __e) {
+                                          brick_move(__b, __e, __first + ((__n - __m) + (__b - __result)), __is_vector);
+                                      });
+
+            return __first + (__last - __middle);
+        });
+    }
+}
+
+//------------------------------------------------------------------------
+// rotate_copy
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class _OutputIterator>
+_OutputIterator
+brick_rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
+                  _OutputIterator __result, /*__is_vector=*/std::false_type) noexcept
+{
+    return std::rotate_copy(__first, __middle, __last, __result);
+}
+
+template <class _ForwardIterator, class _OutputIterator>
+_OutputIterator
+brick_rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
+                  _OutputIterator __result, /*__is_vector=*/std::true_type) noexcept
+{
+    _OutputIterator __res = brick_copy(__middle, __last, __result, std::true_type());
+    return brick_copy(__first, __middle, __res, std::true_type());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _IsVector>
+_OutputIterator
+pattern_rotate_copy(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
+                    _OutputIterator __result, _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_rotate_copy(__first, __middle, __last, __result, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _IsVector>
+_OutputIterator
+pattern_rotate_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __middle,
+                    _ForwardIterator __last, _OutputIterator __result, _IsVector __is_vector,
+                    /*is_parallel=*/std::true_type)
+{
+    par_backend::parallel_for(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last,
+        [__first, __last, __middle, __result, __is_vector](_ForwardIterator __b, _ForwardIterator __e) {
+            if (__b > __middle)
+            {
+                brick_copy(__b, __e, __result + (__b - __middle), __is_vector);
+            }
+            else
+            {
+                _OutputIterator __new_result = __result + ((__last - __middle) + (__b - __first));
+                if (__e < __middle)
+                {
+                    brick_copy(__b, __e, __new_result, __is_vector);
+                }
+                else
+                {
+                    brick_copy(__b, __middle, __new_result, __is_vector);
+                    brick_copy(__middle, __e, __result, __is_vector);
+                }
+            }
+        });
+    return __result + (__last - __first);
+}
+
+//------------------------------------------------------------------------
+// is_partitioned
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class _UnaryPredicate>
+bool
+brick_is_partitioned(_ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+                     /*is_vector=*/std::false_type) noexcept
+{
+    return std::is_partitioned(__first, __last, __pred);
+}
+
+template <class _ForwardIterator, class _UnaryPredicate>
+bool
+brick_is_partitioned(_ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+                     /*is_vector=*/std::true_type) noexcept
+{
+    typedef typename std::iterator_traits<_ForwardIterator>::difference_type _SizeType;
+    if (__first == __last)
+    {
+        return true;
+    }
+    else
+    {
+        _ForwardIterator __result =
+            unseq_backend::simd_first(__first, _SizeType(0), __last - __first,
+                                      [&__pred](_ForwardIterator __it, _SizeType __i) { return !__pred(__it[__i]); });
+        if (__result == __last)
+        {
+            return true;
+        }
+        else
+        {
+            ++__result;
+            return !unseq_backend::simd_or(__result, __last - __result, __pred);
+        }
+    }
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate, class _IsVector>
+bool
+pattern_is_partitioned(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+                       _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_is_partitioned(__first, __last, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate, class _IsVector>
+bool
+pattern_is_partitioned(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last,
+                       _UnaryPredicate __pred, _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+    if (__first == __last)
+    {
+        return true;
+    }
+    else
+    {
+        return except_handler([&]() {
+            typedef typename std::iterator_traits<_ForwardIterator>::difference_type _SizeType;
+
+            // State of current range:
+            // broken     - current range is not partitioned by pred
+            // all_true   - all elements in current range satisfy pred
+            // all_false  - all elements in current range don't satisfy pred
+            // true_false - elements satisfy pred are placed before elements that don't satisfy pred
+            enum _ReduceType
+            {
+                __not_init = -1,
+                __broken,
+                __all_true,
+                __all_false,
+                __true_false
+            };
+            _ReduceType __init = __not_init;
+
+            // Array with states that we'll have when state from the left branch is merged with state from the right branch.
+            // State is calculated by formula: new_state = table[left_state * 4 + right_state]
+            _ReduceType __table[] = {__broken,     __broken,     __broken,     __broken, __broken,    __all_true,
+                                     __true_false, __true_false, __broken,     __broken, __all_false, __broken,
+                                     __broken,     __broken,     __true_false, __broken};
+
+            __init = par_backend::parallel_reduce(
+                std::forward<_ExecutionPolicy>(__exec), __first, __last, __init,
+                [__first, &__pred, &__table, __is_vector](_ForwardIterator __i, _ForwardIterator __j,
+                                                          _ReduceType __value) -> _ReduceType {
+                    if (__value == __broken)
+                    {
+                        return __broken;
+                    }
+                    _ReduceType __res = __not_init;
+                    // if first element satisfy pred
+                    if (__pred(*__i))
+                    {
+                        // find first element that don't satisfy pred
+                        _ForwardIterator __x =
+                            brick_find_if(__i + 1, __j, not_pred<_UnaryPredicate>(__pred), __is_vector);
+                        if (__x != __j)
+                        {
+                            // find first element after "x" that satisfy pred
+                            _ForwardIterator __y = brick_find_if(__x + 1, __j, __pred, __is_vector);
+                            // if it was found then range isn't partitioned by pred
+                            if (__y != __j)
+                            {
+                                return __broken;
+                            }
+                            else
+                            {
+                                __res = __true_false;
+                            }
+                        }
+                        else
+                        {
+                            __res = __all_true;
+                        }
+                    }
+                    else
+                    { // if first element doesn't satisfy pred
+                        // then we should find the first element that satisfy pred.
+                        // If we found it then range isn't partitioned by pred
+                        if (brick_find_if(__i + 1, __j, __pred, __is_vector) != __j)
+                        {
+                            return __broken;
+                        }
+                        else
+                        {
+                            __res = __all_false;
+                        }
+                    }
+                    // if we have value from left range then we should calculate the result
+                    return (__value == -1) ? __res : __table[__value * 4 + __res];
+                },
+
+                [&__table](_ReduceType __val1, _ReduceType __val2) -> _ReduceType {
+                    if (__val1 == __broken || __val2 == __broken)
+                    {
+                        return __broken;
+                    }
+                    // calculate the result for new big range
+                    return __table[__val1 * 4 + __val2];
+                });
+            return __init != __broken;
+        });
+    }
+}
+
+//------------------------------------------------------------------------
+// partition
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class _UnaryPredicate>
+_ForwardIterator
+brick_partition(_ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+                /*is_vector=*/std::false_type) noexcept
+{
+    return std::partition(__first, __last, __pred);
+}
+
+template <class _ForwardIterator, class _UnaryPredicate>
+_ForwardIterator
+brick_partition(_ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+                /*is_vector=*/std::true_type) noexcept
+{
+    __PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial");
+    return std::partition(__first, __last, __pred);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate, class _IsVector>
+_ForwardIterator
+pattern_partition(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+                  _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_partition(__first, __last, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate, class _IsVector>
+_ForwardIterator
+pattern_partition(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+                  _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+
+    // partitioned range: elements before pivot satisfy pred (true part),
+    //                    elements after pivot don't satisfy pred (false part)
+    struct _PartitionRange
+    {
+        _ForwardIterator __begin;
+        _ForwardIterator __pivot;
+        _ForwardIterator __end;
+    };
+
+    return except_handler([&]() {
+        _PartitionRange __init{__last, __last, __last};
+
+        // lambda for merging two partitioned ranges to one partitioned range
+        auto __reductor = [&__exec, __first, __is_vector](_PartitionRange __val1,
+                                                          _PartitionRange __val2) -> _PartitionRange {
+            auto __size1 = __val1.__end - __val1.__pivot;
+            auto __size2 = __val2.__pivot - __val2.__begin;
+            auto __new_begin = __val2.__begin - (__val1.__end - __val1.__begin);
+
+            // if all elements in left range satisfy pred then we can move new pivot to pivot of right range
+            if (__val1.__end == __val1.__pivot)
+            {
+                return {__new_begin, __val2.__pivot, __val2.__end};
+            }
+            // if true part of right range greater than false part of left range
+            // then we should swap the false part of left range and last part of true part of right range
+            else if (__size2 > __size1)
+            {
+                par_backend::parallel_for(
+                    std::forward<_ExecutionPolicy>(__exec), __val1.__pivot, __val1.__pivot + __size1,
+                    [__val1, __val2, __size1, __is_vector](_ForwardIterator __i, _ForwardIterator __j) {
+                        brick_swap_ranges(__i, __j, (__val2.__pivot - __size1) + (__i - __val1.__pivot), __is_vector);
+                    });
+                return {__new_begin, __val2.__pivot - __size1, __val2.__end};
+            }
+            // else we should swap the first part of false part of left range and true part of right range
+            else
+            {
+                par_backend::parallel_for(
+                    std::forward<_ExecutionPolicy>(__exec), __val1.__pivot, __val1.__pivot + __size2,
+                    [__val1, __val2, __is_vector](_ForwardIterator __i, _ForwardIterator __j) {
+                        brick_swap_ranges(__i, __j, __val2.__begin + (__i - __val1.__pivot), __is_vector);
+                    });
+                return {__new_begin, __val1.__pivot + __size2, __val2.__end};
+            }
+        };
+
+        _PartitionRange __result = par_backend::parallel_reduce(
+            std::forward<_ExecutionPolicy>(__exec), __first, __last, __init,
+            [__first, __pred, __is_vector, __reductor](_ForwardIterator __i, _ForwardIterator __j,
+                                                       _PartitionRange __value) -> _PartitionRange {
+                //1. serial partition
+                _ForwardIterator __pivot = brick_partition(__i, __j, __pred, __is_vector);
+
+                // 2. merging of two ranges (left and right respectively)
+                return __reductor(__value, {__i, __pivot, __j});
+            },
+            __reductor);
+        return __result.__pivot;
+    });
+}
+
+//------------------------------------------------------------------------
+// stable_partition
+//------------------------------------------------------------------------
+
+template <class _BidirectionalIterator, class _UnaryPredicate>
+_BidirectionalIterator
+brick_stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _UnaryPredicate __pred,
+                       /*__is_vector=*/std::false_type) noexcept
+{
+    return std::stable_partition(__first, __last, __pred);
+}
+
+template <class _BidirectionalIterator, class _UnaryPredicate>
+_BidirectionalIterator
+brick_stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _UnaryPredicate __pred,
+                       /*__is_vector=*/std::true_type) noexcept
+{
+    __PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial");
+    return std::stable_partition(__first, __last, __pred);
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _UnaryPredicate, class _IsVector>
+_BidirectionalIterator
+pattern_stable_partition(_ExecutionPolicy&&, _BidirectionalIterator __first, _BidirectionalIterator __last,
+                         _UnaryPredicate __pred, _IsVector __is_vector, /*is_parallelization=*/std::false_type) noexcept
+{
+    return internal::brick_stable_partition(__first, __last, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _UnaryPredicate, class _IsVector>
+_BidirectionalIterator
+pattern_stable_partition(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last,
+                         _UnaryPredicate __pred, _IsVector __is_vector, /*is_parallelization=*/std::true_type) noexcept
+{
+    // partitioned range: elements before pivot satisfy pred (true part),
+    //                    elements after pivot don't satisfy pred (false part)
+    struct _PartitionRange
+    {
+        _BidirectionalIterator __begin;
+        _BidirectionalIterator __pivot;
+        _BidirectionalIterator __end;
+    };
+    typedef typename std::iterator_traits<_BidirectionalIterator>::value_type T;
+
+    return except_handler([&]() {
+        _PartitionRange __init{__last, __last, __last};
+
+        // lambda for merging two partitioned ranges to one partitioned range
+        auto __reductor = [__first, __is_vector, __pred](_PartitionRange __val1,
+                                                         _PartitionRange __val2) -> _PartitionRange {
+            auto __size1 = __val1.__end - __val1.__pivot;
+            auto __new_begin = __val2.__begin - (__val1.__end - __val1.__begin);
+
+            // if all elements in left range satisfy pred then we can move new pivot to pivot of right range
+            if (__val1.__end == __val1.__pivot)
+            {
+                return {__new_begin, __val2.__pivot, __val2.__end};
+            }
+            // if true part of right range greater than false part of left range
+            // then we should swap the false part of left range and last part of true part of right range
+            else
+            {
+                brick_rotate(__val1.__pivot, __val2.__begin, __val2.__pivot, __is_vector);
+                return {__new_begin, __val2.__pivot - __size1, __val2.__end};
+            }
+        };
+
+        _PartitionRange __result = par_backend::parallel_reduce(
+            std::forward<_ExecutionPolicy>(__exec), __first, __last, __init,
+            [__first, &__pred, __is_vector, __reductor](_BidirectionalIterator __i, _BidirectionalIterator __j,
+                                                        _PartitionRange __value) -> _PartitionRange {
+                //1. serial stable_partition
+                _BidirectionalIterator __pivot = brick_stable_partition(__i, __j, __pred, __is_vector);
+
+                // 2. merging of two ranges (left and right respectively)
+                return __reductor(__value, {__i, __pivot, __j});
+            },
+            __reductor);
+        return __result.__pivot;
+    });
+}
+
+//------------------------------------------------------------------------
+// partition_copy
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class _OutputIterator1, class _OutputIterator2, class _UnaryPredicate>
+std::pair<_OutputIterator1, _OutputIterator2>
+brick_partition_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator1 __out_true,
+                     _OutputIterator2 __out_false, _UnaryPredicate __pred, /*is_vector=*/std::false_type) noexcept
+{
+    return std::partition_copy(__first, __last, __out_true, __out_false, __pred);
+}
+
+template <class _ForwardIterator, class _OutputIterator1, class _OutputIterator2, class _UnaryPredicate>
+std::pair<_OutputIterator1, _OutputIterator2>
+brick_partition_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator1 __out_true,
+                     _OutputIterator2 __out_false, _UnaryPredicate __pred, /*is_vector=*/std::true_type) noexcept
+{
+#if (__PSTL_MONOTONIC_PRESENT)
+    return unseq_backend::simd_partition_copy(__first, __last - __first, __out_true, __out_false, __pred);
+#else
+    return std::partition_copy(__first, __last, __out_true, __out_false, __pred);
+#endif
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator1, class _OutputIterator2,
+          class _UnaryPredicate, class _IsVector>
+std::pair<_OutputIterator1, _OutputIterator2>
+pattern_partition_copy(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last,
+                       _OutputIterator1 __out_true, _OutputIterator2 __out_false, _UnaryPredicate __pred,
+                       _IsVector __is_vector, /*is_parallelization=*/std::false_type) noexcept
+{
+    return internal::brick_partition_copy(__first, __last, __out_true, __out_false, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator1, class _OutputIterator2,
+          class _UnaryPredicate, class _IsVector>
+std::pair<_OutputIterator1, _OutputIterator2>
+pattern_partition_copy(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last,
+                       _OutputIterator1 __out_true, _OutputIterator2 __out_false, _UnaryPredicate __pred,
+                       _IsVector __is_vector, /*is_parallelization=*/std::true_type)
+{
+    typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType;
+    typedef std::pair<_DifferenceType, _DifferenceType> _ReturnType;
+    const _DifferenceType __n = __last - __first;
+    if (_DifferenceType(1) < __n)
+    {
+        par_backend::buffer<bool> __mask_buf(__n);
+        return internal::except_handler(
+            [&__exec, __n, __first, __last, __out_true, __out_false, __is_vector, __pred, &__mask_buf]() {
+                bool* __mask = __mask_buf.get();
+                _ReturnType __m{};
+                par_backend::parallel_strict_scan(
+                    std::forward<_ExecutionPolicy>(__exec), __n, std::make_pair(_DifferenceType(0), _DifferenceType(0)),
+                    [=](_DifferenceType __i, _DifferenceType __len) { // Reduce
+                        return internal::brick_calc_mask_1<_DifferenceType>(__first + __i, __first + (__i + __len),
+                                                                            __mask + __i, __pred, __is_vector);
+                    },
+                    [](const _ReturnType& __x, const _ReturnType& __y) -> _ReturnType {
+                        return std::make_pair(__x.first + __y.first, __x.second + __y.second);
+                    },                                                                       // Combine
+                    [=](_DifferenceType __i, _DifferenceType __len, _ReturnType __initial) { // Scan
+                        internal::brick_partition_by_mask(__first + __i, __first + (__i + __len),
+                                                          __out_true + __initial.first, __out_false + __initial.second,
+                                                          __mask + __i, __is_vector);
+                    },
+                    [&__m](_ReturnType __total) { __m = __total; });
+                return std::make_pair(__out_true + __m.first, __out_false + __m.second);
+            });
+    }
+    // trivial sequence - use serial algorithm
+    return internal::brick_partition_copy(__first, __last, __out_true, __out_false, __pred, __is_vector);
+}
+
+//------------------------------------------------------------------------
+// sort
+//------------------------------------------------------------------------
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsVector,
+          class _IsMoveConstructible>
+void
+pattern_sort(_ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
+             _IsVector /*is_vector*/, /*is_parallel=*/std::false_type, _IsMoveConstructible) noexcept
+{
+    std::sort(__first, __last, __comp);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsVector>
+void
+pattern_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
+             _IsVector /*is_vector*/, /*is_parallel=*/std::true_type, /*is_move_constructible=*/std::true_type)
+{
+    except_handler([&]() {
+        par_backend::parallel_stable_sort(std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp,
+                                          [](_RandomAccessIterator __first, _RandomAccessIterator __last,
+                                             _Compare __comp) { std::sort(__first, __last, __comp); },
+                                          __last - __first);
+    });
+}
+
+//------------------------------------------------------------------------
+// stable_sort
+//------------------------------------------------------------------------
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsVector>
+void
+pattern_stable_sort(_ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
+                    _IsVector /*is_vector*/, /*is_parallel=*/std::false_type) noexcept
+{
+    std::stable_sort(__first, __last, __comp);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsVector>
+void
+pattern_stable_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last,
+                    _Compare __comp, _IsVector /*is_vector*/, /*is_parallel=*/std::true_type)
+{
+    internal::except_handler([&]() {
+        par_backend::parallel_stable_sort(std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp,
+                                          [](_RandomAccessIterator __first, _RandomAccessIterator __last,
+                                             _Compare __comp) { std::stable_sort(__first, __last, __comp); });
+    });
+}
+
+//------------------------------------------------------------------------
+// partial_sort
+//------------------------------------------------------------------------
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsVector>
+void
+pattern_partial_sort(_ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __middle,
+                     _RandomAccessIterator __last, _Compare __comp, _IsVector, /*is_parallel=*/std::false_type) noexcept
+{
+    std::partial_sort(__first, __middle, __last, __comp);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsVector>
+void
+pattern_partial_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __middle,
+                     _RandomAccessIterator __last, _Compare __comp, _IsVector, /*is_parallel=*/std::true_type)
+{
+    const auto __n = __middle - __first;
+    except_handler([&]() {
+        par_backend::parallel_stable_sort(
+            std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp,
+            [__n](_RandomAccessIterator __begin, _RandomAccessIterator __end, _Compare __comp) {
+                if (__n < __end - __begin)
+                    std::partial_sort(__begin, __begin + __n, __end, __comp);
+                else
+                    std::sort(__begin, __end, __comp);
+            },
+            __n);
+    });
+}
+
+//------------------------------------------------------------------------
+// partial_sort_copy
+//------------------------------------------------------------------------
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _RandomAccessIterator, class _Compare, class _IsVector>
+_RandomAccessIterator
+pattern_partial_sort_copy(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last,
+                          _RandomAccessIterator __d_first, _RandomAccessIterator __d_last, _Compare __comp, _IsVector,
+                          /*is_parallel=*/std::false_type) noexcept
+{
+    return std::partial_sort_copy(__first, __last, __d_first, __d_last, __comp);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _RandomAccessIterator, class _Compare, class _IsVector>
+_RandomAccessIterator
+pattern_partial_sort_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last,
+                          _RandomAccessIterator __d_first, _RandomAccessIterator __d_last, _Compare __comp,
+                          _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+    if (__last == __first || __d_last == __d_first)
+    {
+        return __d_first;
+    }
+    auto __n1 = __last - __first;
+    auto __n2 = __d_last - __d_first;
+    return except_handler([&]() {
+        if (__n2 >= __n1)
+        {
+            par_backend::parallel_stable_sort(
+                std::forward<_ExecutionPolicy>(__exec), __d_first, __d_first + __n1, __comp,
+                [__n1, __first, __d_first, __is_vector](_RandomAccessIterator __i, _RandomAccessIterator __j,
+                                                        _Compare __comp) {
+                    _ForwardIterator __i1 = __first + (__i - __d_first);
+                    _ForwardIterator __j1 = __first + (__j - __d_first);
+
+                // 1. Copy elements from input to output
+#if !__PSTL_ICC_18_OMP_SIMD_BROKEN
+                    brick_copy(__i1, __j1, __i, __is_vector);
+#else
+                    std::copy(__i1, __j1, __i);
+#endif
+                    // 2. Sort elements in output sequence
+                    std::sort(__i, __j, __comp);
+                },
+                __n1);
+            return __d_first + __n1;
+        }
+        else
+        {
+            typedef typename std::iterator_traits<_ForwardIterator>::value_type _T1;
+            typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _T2;
+            par_backend::buffer<_T1> __buf(__n1);
+            _T1* __r = __buf.get();
+
+            par_backend::parallel_stable_sort(std::forward<_ExecutionPolicy>(__exec), __r, __r + __n1, __comp,
+                                              [__n2, __first, __r](_T1* __i, _T1* __j, _Compare __comp) {
+                                                  _ForwardIterator __it = __first + (__i - __r);
+
+                                                  // 1. Copy elements from input to raw memory
+                                                  for (_T1* __k = __i; __k != __j; ++__k, ++__it)
+                                                  {
+                                                      ::new (__k) _T2(*__it);
+                                                  }
+
+                                                  // 2. Sort elements in temporary buffer
+                                                  if (__n2 < __j - __i)
+                                                      std::partial_sort(__i, __i + __n2, __j, __comp);
+                                                  else
+                                                      std::sort(__i, __j, __comp);
+                                              },
+                                              __n2);
+
+            // 3. Move elements from temporary buffer to output
+            par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __r, __r + __n2,
+                                      [__r, __d_first, __is_vector](_T1* __i, _T1* __j) {
+                                          brick_move(__i, __j, __d_first + (__i - __r), __is_vector);
+                                      });
+            return __d_first + __n2;
+        }
+    });
+}
+
+//------------------------------------------------------------------------
+// adjacent_find
+//------------------------------------------------------------------------
+template <class _ForwardIterator, class _BinaryPredicate>
+_ForwardIterator
+brick_adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred,
+                    /* IsVector = */ std::true_type, bool __or_semantic) noexcept
+{
+    return unseq_backend::simd_adjacent_find(__first, __last, __pred, __or_semantic);
+}
+
+template <class _ForwardIterator, class _BinaryPredicate>
+_ForwardIterator
+brick_adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred,
+                    /* IsVector = */ std::false_type, bool __or_semantic) noexcept
+{
+    return std::adjacent_find(__first, __last, __pred);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate, class _IsVector>
+_ForwardIterator
+pattern_adjacent_find(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred,
+                      /* is_parallel */ std::false_type, _IsVector __is_vector, bool __or_semantic) noexcept
+{
+    return internal::brick_adjacent_find(__first, __last, __pred, __is_vector, __or_semantic);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _BinaryPredicate, class _IsVector>
+_RandomAccessIterator
+pattern_adjacent_find(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last,
+                      _BinaryPredicate __pred, /* is_parallel */ std::true_type, _IsVector __is_vector,
+                      bool __or_semantic)
+{
+    if (__last - __first < 2)
+        return __last;
+
+    return internal::except_handler([&]() {
+        return par_backend::parallel_reduce(
+            std::forward<_ExecutionPolicy>(__exec), __first, __last, __last,
+            [__last, __pred, __is_vector, __or_semantic](_RandomAccessIterator __begin, _RandomAccessIterator __end,
+                                                         _RandomAccessIterator __value) -> _RandomAccessIterator {
+                // TODO: investigate performance benefits from the use of shared variable for the result,
+                // checking (compare_and_swap idiom) its __value at __first.
+                if (__or_semantic && __value < __last)
+                { //found
+                    par_backend::cancel_execution();
+                    return __value;
+                }
+
+                if (__value > __begin)
+                {
+                    // modify __end to check the predicate on the boundary __values;
+                    // TODO: to use a custom range with boundaries overlapping
+                    // TODO: investigate what if we remove "if" below and run algorithm on range [__first, __last-1)
+                    // then check the pair [__last-1, __last)
+                    if (__end != __last)
+                        ++__end;
+
+                    //correct the global result iterator if the "brick" returns a local "__last"
+                    const _RandomAccessIterator __res =
+                        internal::brick_adjacent_find(__begin, __end, __pred, __is_vector, __or_semantic);
+                    if (__res < __end)
+                        __value = __res;
+                }
+                return __value;
+            },
+            [](_RandomAccessIterator __x, _RandomAccessIterator __y) -> _RandomAccessIterator {
+                return __x < __y ? __x : __y;
+            } //reduce a __value
+        );
+    });
+}
+
+//------------------------------------------------------------------------
+// nth_element
+//------------------------------------------------------------------------
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsVector>
+void
+pattern_nth_element(_ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __nth,
+                    _RandomAccessIterator __last, _Compare __comp, _IsVector, /*is_parallel=*/std::false_type) noexcept
+{
+    std::nth_element(__first, __nth, __last, __comp);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsVector>
+void
+pattern_nth_element(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __nth,
+                    _RandomAccessIterator __last, _Compare __comp, _IsVector __is_vector,
+                    /*is_parallel=*/std::true_type) noexcept
+{
+    if (__first == __last || __nth == __last)
+    {
+        return;
+    }
+
+    using std::iter_swap;
+    typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _Tp;
+    _RandomAccessIterator __x;
+    do
+    {
+        __x = pattern_partition(std::forward<_ExecutionPolicy>(__exec), __first + 1, __last,
+                                [&__comp, __first](const _Tp& __x) { return __comp(__x, *__first); }, __is_vector,
+                                /*is_parallel=*/std::true_type());
+        --__x;
+        if (__x != __first)
+        {
+            iter_swap(__first, __x);
+        }
+        // if x > nth then our new range for partition is [first, x)
+        if (__x - __nth > 0)
+        {
+            __last = __x;
+        }
+        // if x < nth then our new range for partition is [x, last)
+        else if (__x - __nth < 0)
+        {
+            // if *x == *nth then we can start new partition with x+1
+            if (!__comp(*__nth, *__x) && !__comp(*__x, *__nth))
+            {
+                ++__x;
+            }
+            else
+            {
+                iter_swap(__nth, __x);
+            }
+            __first = __x;
+        }
+    } while (__x != __nth);
+}
+
+//------------------------------------------------------------------------
+// fill, fill_n
+//------------------------------------------------------------------------
+template <class _ForwardIterator, class _Tp>
+void
+brick_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value,
+           /* __is_vector = */ std::true_type) noexcept
+{
+    unseq_backend::simd_fill_n(__first, __last - __first, __value);
+}
+
+template <class _ForwardIterator, class _Tp>
+void
+brick_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value,
+           /* __is_vector = */ std::false_type) noexcept
+{
+    std::fill(__first, __last, __value);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _IsVector>
+void
+pattern_fill(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value,
+             /*is_parallel=*/std::false_type, _IsVector __is_vector) noexcept
+{
+    internal::brick_fill(__first, __last, __value, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _IsVector>
+_ForwardIterator
+pattern_fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value,
+             /*is_parallel=*/std::true_type, _IsVector __is_vector)
+{
+    return except_handler([&__exec, __first, __last, &__value, __is_vector]() {
+        par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                  [&__value, __is_vector](_ForwardIterator __begin, _ForwardIterator __end) {
+                                      internal::brick_fill(__begin, __end, __value, __is_vector);
+                                  });
+        return __last;
+    });
+}
+
+template <class _OutputIterator, class _Size, class _Tp>
+_OutputIterator
+brick_fill_n(_OutputIterator __first, _Size __count, const _Tp& __value, /* __is_vector = */ std::true_type) noexcept
+{
+    return unseq_backend::simd_fill_n(__first, __count, __value);
+}
+
+template <class _OutputIterator, class _Size, class _Tp>
+_OutputIterator
+brick_fill_n(_OutputIterator __first, _Size __count, const _Tp& __value, /* __is_vector = */ std::false_type) noexcept
+{
+    return std::fill_n(__first, __count, __value);
+}
+
+template <class _ExecutionPolicy, class _OutputIterator, class _Size, class _Tp, class _IsVector>
+_OutputIterator
+pattern_fill_n(_ExecutionPolicy&&, _OutputIterator __first, _Size __count, const _Tp& __value,
+               /*is_parallel=*/std::false_type, _IsVector __is_vector) noexcept
+{
+    return internal::brick_fill_n(__first, __count, __value, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _OutputIterator, class _Size, class _Tp, class _IsVector>
+_OutputIterator
+pattern_fill_n(_ExecutionPolicy&& __exec, _OutputIterator __first, _Size __count, const _Tp& __value,
+               /*is_parallel=*/std::true_type, _IsVector __is_vector)
+{
+    return internal::pattern_fill(std::forward<_ExecutionPolicy>(__exec), __first, __first + __count, __value,
+                                  std::true_type(), __is_vector);
+}
+
+//------------------------------------------------------------------------
+// generate, generate_n
+//------------------------------------------------------------------------
+template <class _RandomAccessIterator, class _Generator>
+void
+brick_generate(_RandomAccessIterator __first, _RandomAccessIterator __last, _Generator __g,
+               /* is_vector = */ std::true_type) noexcept
+{
+    unseq_backend::simd_generate_n(__first, __last - __first, __g);
+}
+
+template <class _ForwardIterator, class _Generator>
+void
+brick_generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __g,
+               /* is_vector = */ std::false_type) noexcept
+{
+    std::generate(__first, __last, __g);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Generator, class _IsVector>
+void
+pattern_generate(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Generator __g,
+                 /*is_parallel=*/std::false_type, _IsVector __is_vector) noexcept
+{
+    internal::brick_generate(__first, __last, __g, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Generator, class _IsVector>
+_ForwardIterator
+pattern_generate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Generator __g,
+                 /*is_parallel=*/std::true_type, _IsVector __is_vector)
+{
+    return internal::except_handler([&]() {
+        par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                  [__g, __is_vector](_ForwardIterator __begin, _ForwardIterator __end) {
+                                      internal::brick_generate(__begin, __end, __g, __is_vector);
+                                  });
+        return __last;
+    });
+}
+
+template <class OutputIterator, class Size, class _Generator>
+OutputIterator
+brick_generate_n(OutputIterator __first, Size __count, _Generator __g, /* is_vector = */ std::true_type) noexcept
+{
+    return unseq_backend::simd_generate_n(__first, __count, __g);
+}
+
+template <class OutputIterator, class Size, class _Generator>
+OutputIterator
+brick_generate_n(OutputIterator __first, Size __count, _Generator __g, /* is_vector = */ std::false_type) noexcept
+{
+    return std::generate_n(__first, __count, __g);
+}
+
+template <class _ExecutionPolicy, class _OutputIterator, class _Size, class _Generator, class _IsVector>
+_OutputIterator
+pattern_generate_n(_ExecutionPolicy&&, _OutputIterator __first, _Size __count, _Generator __g,
+                   /*is_parallel=*/std::false_type, _IsVector __is_vector) noexcept
+{
+    return internal::brick_generate_n(__first, __count, __g, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _OutputIterator, class _Size, class _Generator, class _IsVector>
+_OutputIterator
+pattern_generate_n(_ExecutionPolicy&& __exec, _OutputIterator __first, _Size __count, _Generator __g,
+                   /*is_parallel=*/std::true_type, _IsVector __is_vector)
+{
+    static_assert(internal::is_random_access_iterator<_OutputIterator>::value,
+                  "Pattern-brick error. Should be a random access iterator.");
+    return internal::pattern_generate(std::forward<_ExecutionPolicy>(__exec), __first, __first + __count, __g,
+                                      std::true_type(), __is_vector);
+}
+
+//------------------------------------------------------------------------
+// remove
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class _UnaryPredicate>
+_ForwardIterator
+brick_remove_if(_ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+                /* __is_vector = */ std::false_type) noexcept
+{
+    return std::remove_if(__first, __last, __pred);
+}
+
+template <class _RandomAccessIterator, class _UnaryPredicate>
+_RandomAccessIterator
+brick_remove_if(_RandomAccessIterator __first, _RandomAccessIterator __last, _UnaryPredicate __pred,
+                /* __is_vector = */ std::true_type) noexcept
+{
+#if __PSTL_MONOTONIC_PRESENT
+    return unseq_backend::simd_remove_if(__first, __last - __first, __pred);
+#else
+    return std::remove_if(__first, __last, __pred);
+#endif
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate, class _IsVector>
+_ForwardIterator
+pattern_remove_if(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+                  _IsVector __is_vector, /*is_parallel*/ std::false_type) noexcept
+{
+    return internal::brick_remove_if(__first, __last, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate, class _IsVector>
+_ForwardIterator
+pattern_remove_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+                  _IsVector __is_vector, /*is_parallel*/ std::true_type) noexcept
+{
+    typedef typename std::iterator_traits<_ForwardIterator>::reference _ReferenceType;
+
+    if (__first == __last || __first + 1 == __last)
+    {
+        // Trivial sequence - use serial algorithm
+        return brick_remove_if(__first, __last, __pred, __is_vector);
+    }
+
+    return remove_elements(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last,
+        [&__pred, __is_vector](bool* __b, bool* __e, _ForwardIterator __it) {
+            brick_walk2(__b, __e, __it, [&__pred](bool& __x, _ReferenceType __y) { __x = !__pred(__y); }, __is_vector);
+        },
+        __is_vector);
+}
+
+//------------------------------------------------------------------------
+// merge
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare>
+_OutputIterator
+brick_merge(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+            _ForwardIterator2 __last2, _OutputIterator __d_first, _Compare __comp,
+            /* __is_vector = */ std::false_type) noexcept
+{
+    return std::merge(__first1, __last1, __first2, __last2, __d_first, __comp);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare>
+_OutputIterator
+brick_merge(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+            _ForwardIterator2 __last2, _OutputIterator __d_first, _Compare __comp,
+            /* __is_vector = */ std::true_type) noexcept
+{
+    __PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial");
+    return std::merge(__first1, __last1, __first2, __last2, __d_first, __comp);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator,
+          class _Compare, class _IsVector>
+_OutputIterator
+pattern_merge(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+              _ForwardIterator2 __last2, _OutputIterator __d_first, _Compare __comp, _IsVector __is_vector,
+              /* is_parallel = */ std::false_type) noexcept
+{
+    return internal::brick_merge(__first1, __last1, __first2, __last2, __d_first, __comp, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, class _OutputIterator,
+          class _Compare, class _IsVector>
+_OutputIterator
+pattern_merge(_ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
+              _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _OutputIterator __d_first,
+              _Compare __comp, _IsVector __is_vector, /* is_parallel = */ std::true_type)
+{
+    par_backend::parallel_merge(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __d_first, __comp,
+        [__is_vector](_RandomAccessIterator1 __f1, _RandomAccessIterator1 __l1, _RandomAccessIterator2 __f2,
+                      _RandomAccessIterator2 __l2, _OutputIterator __f3,
+                      _Compare __comp) { return brick_merge(__f1, __l1, __f2, __l2, __f3, __comp, __is_vector); });
+    return __d_first + (__last1 - __first1) + (__last2 - __first2);
+}
+
+//------------------------------------------------------------------------
+// inplace_merge
+//------------------------------------------------------------------------
+template <class _BidirectionalIterator, class _Compare>
+void
+brick_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
+                    _Compare __comp, /* __is_vector = */ std::false_type) noexcept
+{
+    std::inplace_merge(__first, __middle, __last, __comp);
+}
+
+template <class _BidirectionalIterator, class _Compare>
+void
+brick_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
+                    _Compare __comp, /* __is_vector = */ std::true_type) noexcept
+{
+    __PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial")
+    std::inplace_merge(__first, __middle, __last, __comp);
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _Compare, class _IsVector>
+void
+pattern_inplace_merge(_ExecutionPolicy&&, _BidirectionalIterator __first, _BidirectionalIterator __middle,
+                      _BidirectionalIterator __last, _Compare __comp, _IsVector __is_vector,
+                      /* is_parallel = */ std::false_type) noexcept
+{
+    internal::brick_inplace_merge(__first, __middle, __last, __comp, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _Compare, class _IsVector>
+void
+pattern_inplace_merge(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __middle,
+                      _BidirectionalIterator __last, _Compare __comp, _IsVector __is_vector,
+                      /*is_parallel=*/std::true_type)
+{
+    if (__first == __last || __first == __middle || __middle == __last)
+    {
+        return;
+    }
+    typedef typename std::iterator_traits<_BidirectionalIterator>::value_type _Tp;
+    auto __n = __last - __first;
+    par_backend::buffer<_Tp> __buf(__n);
+    _Tp* __r = __buf.get();
+    except_handler([&]() {
+        auto __move_values = [](_BidirectionalIterator __x, _Tp* __z) {
+            invoke_if_else(std::is_trivial<_Tp>(), [&]() { *__z = std::move(*__x); },
+                           [&]() { ::new (std::addressof(*__z)) _Tp(std::move(*__x)); });
+        };
+
+        auto __move_sequences = [](_BidirectionalIterator __first1, _BidirectionalIterator __last1, _Tp* __first2) {
+            return brick_uninitialized_move(__first1, __last1, __first2, _IsVector());
+        };
+
+        par_backend::parallel_merge(
+            std::forward<_ExecutionPolicy>(__exec), __first, __middle, __middle, __last, __r, __comp,
+            [__n, __comp, __move_values, __move_sequences](_BidirectionalIterator __f1, _BidirectionalIterator __l1,
+                                                           _BidirectionalIterator __f2, _BidirectionalIterator __l2,
+                                                           _Tp* __f3, _Compare __comp) {
+                auto __func = par_backend::serial_move_merge<decltype(__move_values), decltype(__move_sequences)>(
+                    __n, __move_values, __move_sequences);
+                __func(__f1, __l1, __f2, __l2, __f3, __comp);
+                return __f3 + (__l1 - __f1) + (__l2 - __f2);
+            });
+        par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __r, __r + __n,
+                                  [__r, __first, __is_vector](_Tp* __i, _Tp* __j) {
+                                      brick_move(__i, __j, __first + (__i - __r), __is_vector);
+                                  });
+    });
+}
+
+//------------------------------------------------------------------------
+// includes
+//------------------------------------------------------------------------
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare, class _IsVector>
+bool
+pattern_includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _Compare __comp, _IsVector,
+                 /*is_parallel=*/std::false_type) noexcept
+{
+    return std::includes(__first1, __last1, __first2, __last2, __comp);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare, class _IsVector>
+bool
+pattern_includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _Compare __comp, _IsVector __is_vector,
+                 /*is_parallel=*/std::true_type)
+{
+    if (__first2 >= __last2)
+        return true;
+
+    if (__first1 >= __last1 || __comp(*__first2, *__first1) || __comp(*(__last1 - 1), *(__last2 - 1)))
+        return false;
+
+    __first1 = std::lower_bound(__first1, __last1, *__first2, __comp);
+    if (__first1 == __last1)
+        return false;
+
+    if (__last2 - __first2 == 1)
+        return !__comp(*__first1, *__first2) && !__comp(*__first2, *__first1);
+
+    return except_handler([&]() {
+        return !internal::parallel_or(
+            std::forward<_ExecutionPolicy>(__exec), __first2, __last2,
+            [__first1, __last1, __first2, __last2, &__comp](_ForwardIterator2 __i, _ForwardIterator2 __j) {
+                assert(__j > __i);
+                //assert(__j - __i > 1);
+
+                //1. moving boundaries to "consume" subsequence of equal elements
+                auto __is_equal = [&__comp](_ForwardIterator2 __a, _ForwardIterator2 __b) -> bool {
+                    return !__comp(*__a, *__b) && !__comp(*__b, *__a);
+                };
+
+                //1.1 left bound, case "aaa[aaaxyz...]" - searching "x"
+                if (__i > __first2 && __is_equal(__i, __i - 1))
+                {
+                    //whole subrange continues to content equal elements - return "no op"
+                    if (__is_equal(__i, __j - 1))
+                        return false;
+
+                    __i = std::upper_bound(__i, __last2, *__i, __comp);
+                }
+
+                //1.2 right bound, case "[...aaa]aaaxyz" - searching "x"
+                if (__j < __last2 && __is_equal(__j - 1, __j))
+                    __j = std::upper_bound(__j, __last2, *__j, __comp);
+
+                //2. testing is __a subsequence of the second range included into the first range
+                auto __b = std::lower_bound(__first1, __last1, *__i, __comp);
+
+                assert(!__comp(*(__last1 - 1), *__b));
+                assert(!__comp(*(__j - 1), *__i));
+                return !std::includes(__b, __last1, __i, __j, __comp);
+            });
+    });
+}
+
+constexpr auto __set_algo_cut_off = 1000;
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator,
+          class _Compare, class _IsVector, class _SizeFunction, class _SetOP>
+_OutputIterator
+parallel_set_op(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                _SizeFunction __size_func, _SetOP __set_op, _IsVector __is_vector)
+{
+    typedef typename std::iterator_traits<_ForwardIterator1>::difference_type _DifferenceType;
+    typedef typename std::iterator_traits<_OutputIterator>::value_type _T;
+
+    struct _SetRange
+    {
+        _DifferenceType __pos, __len, __buf_pos;
+        bool
+        empty() const
+        {
+            return __len == 0;
+        }
+    };
+
+    const _DifferenceType __n1 = __last1 - __first1;
+    const _DifferenceType __n2 = __last2 - __first2;
+
+    par_backend::buffer<_T> __buf(__size_func(__n1, __n2));
+
+    return except_handler([&__exec, __n1, __first1, __last1, __first2, __last2, __result, __is_vector, __comp,
+                           __size_func, __set_op, &__buf]() {
+        auto __buffer = __buf.get();
+        _DifferenceType __m{};
+        auto __scan = [=](_DifferenceType, _DifferenceType, const _SetRange& __s) { // Scan
+            if (!__s.empty())
+                brick_move(__buffer + __s.__buf_pos, __buffer + (__s.__buf_pos + __s.__len), __result + __s.__pos,
+                           __is_vector);
+        };
+        par_backend::parallel_strict_scan(
+            std::forward<_ExecutionPolicy>(__exec), __n1, _SetRange{0, 0, 0}, //-1, 0},
+            [=](_DifferenceType __i, _DifferenceType __len) {                 // Reduce
+                //[__b; __e) - a subrange of the first sequence, to reduce
+                _ForwardIterator1 __b = __first1 + __i, __e = __first1 + (__i + __len);
+
+                //try searching for the first element which not equal to *__b
+                if (__b != __first1)
+                    __b = std::upper_bound(__b, __last1, *__b, __comp);
+
+                //try searching for the first element which not equal to *__e
+                if (__e != __last1)
+                    __e = std::upper_bound(__e, __last1, *__e, __comp);
+
+                //check is [__b; __e) empty
+                if (__e - __b < 1)
+                {
+                    _ForwardIterator2 __bb = __last2;
+                    if (__b != __last1)
+                        __bb = std::lower_bound(__first2, __last2, *__b, __comp);
+
+                    const _DifferenceType __buf_pos = __size_func((__b - __first1), (__bb - __first2));
+                    return _SetRange{0, 0, __buf_pos};
+                }
+
+                //try searching for "corresponding" subrange [__bb; __ee) in the second sequence
+                _ForwardIterator2 __bb = __first2;
+                if (__b != __first1)
+                    __bb = std::lower_bound(__first2, __last2, *__b, __comp);
+
+                _ForwardIterator2 __ee = __last2;
+                if (__e != __last1)
+                    __ee = std::lower_bound(__bb, __last2, *__e, __comp);
+
+                const _DifferenceType __buf_pos = __size_func((__b - __first1), (__bb - __first2));
+                auto __buffer_b = __buffer + __buf_pos;
+                auto __res = __set_op(__b, __e, __bb, __ee, __buffer_b, __comp);
+
+                return _SetRange{0, __res - __buffer_b, __buf_pos};
+            },
+            [](const _SetRange& __a, const _SetRange& __b) { // Combine
+                if (__b.__buf_pos > __a.__buf_pos || ((__b.__buf_pos == __a.__buf_pos) && !__b.empty()))
+                    return _SetRange{__a.__pos + __a.__len + __b.__pos, __b.__len, __b.__buf_pos};
+                return _SetRange{__b.__pos + __b.__len + __a.__pos, __a.__len, __a.__buf_pos};
+            },
+            __scan,                                     // Scan
+            [&__m, &__scan](const _SetRange& __total) { // Apex
+                //final scan
+                __scan(0, 0, __total);
+                __m = __total.__pos + __total.__len;
+            });
+        return __result + __m;
+    });
+}
+
+//a shared parallel pattern for 'pattern_set_union' and 'pattern_set_symmetric_difference'
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator,
+          class _Compare, class _SetUnionOp, class _IsVector>
+_OutputIterator
+parallel_set_union_op(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                      _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                      _SetUnionOp __set_union_op, _IsVector __is_vector)
+{
+    typedef typename std::iterator_traits<_OutputIterator>::value_type _T;
+    typedef typename std::iterator_traits<_ForwardIterator1>::difference_type _DifferenceType;
+
+    const auto __n1 = __last1 - __first1;
+    const auto __n2 = __last2 - __first2;
+
+    auto copy_range1 = [__is_vector](_ForwardIterator1 __begin, _ForwardIterator1 __end, _OutputIterator __res) {
+        return brick_copy(__begin, __end, __res, __is_vector);
+    };
+    auto copy_range2 = [__is_vector](_ForwardIterator2 __begin, _ForwardIterator2 __end, _OutputIterator __res) {
+        return brick_copy(__begin, __end, __res, __is_vector);
+    };
+
+    // {1} {}: parallel copying just first sequence
+    if (__n2 == 0)
+        return pattern_walk2_brick(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __result, copy_range1,
+                                   std::true_type());
+
+    // {} {2}: parallel copying justmake  second sequence
+    if (__n1 == 0)
+        return pattern_walk2_brick(std::forward<_ExecutionPolicy>(__exec), __first2, __last2, __result, copy_range2,
+                                   std::true_type());
+
+    // testing  whether the sequences are intersected
+    _ForwardIterator1 __left_bound_seq_1 = lower_bound(__first1, __last1, *__first2, __comp);
+
+    if (__left_bound_seq_1 == __last1)
+    {
+        //{1} < {2}: seq2 is wholly greater than seq1, so, do parallel copying seq1 and seq2
+        par_backend::parallel_invoke(std::forward<_ExecutionPolicy>(__exec),
+                                     [=] {
+                                         pattern_walk2_brick(std::forward<_ExecutionPolicy>(__exec), __first1, __last1,
+                                                             __result, copy_range1, std::true_type());
+                                     },
+                                     [=] {
+                                         pattern_walk2_brick(std::forward<_ExecutionPolicy>(__exec), __first2, __last2,
+                                                             __result + __n1, copy_range2, std::true_type());
+                                     });
+        return __result + __n1 + __n2;
+    }
+
+    // testing  whether the sequences are intersected
+    _ForwardIterator2 __left_bound_seq_2 = lower_bound(__first2, __last2, *__first1, __comp);
+
+    if (__left_bound_seq_2 == __last2)
+    {
+        //{2} < {1}: seq2 is wholly greater than seq1, so, do parallel copying seq1 and seq2
+        par_backend::parallel_invoke(std::forward<_ExecutionPolicy>(__exec),
+                                     [=] {
+                                         pattern_walk2_brick(std::forward<_ExecutionPolicy>(__exec), __first2, __last2,
+                                                             __result, copy_range2, std::true_type());
+                                     },
+                                     [=] {
+                                         pattern_walk2_brick(std::forward<_ExecutionPolicy>(__exec), __first1, __last1,
+                                                             __result + __n2, copy_range1, std::true_type());
+                                     });
+        return __result + __n1 + __n2;
+    }
+
+    const auto __m1 = __left_bound_seq_1 - __first1;
+    if (__m1 > __set_algo_cut_off)
+    {
+        auto __res_or = __result;
+        __result += __m1; //we know proper offset due to [first1; left_bound_seq_1) < [first2; last2)
+        par_backend::parallel_invoke(
+            std::forward<_ExecutionPolicy>(__exec),
+            //do parallel copying of [first1; left_bound_seq_1)
+            [=] {
+                pattern_walk2_brick(std::forward<_ExecutionPolicy>(__exec), __first1, __left_bound_seq_1, __res_or,
+                                    copy_range1, std::true_type());
+            },
+            [=, &__result] {
+                __result = parallel_set_op(std::forward<_ExecutionPolicy>(__exec), __left_bound_seq_1, __last1,
+                                           __first2, __last2, __result, __comp,
+                                           [](_DifferenceType __n, _DifferenceType __m) { return __n + __m; },
+                                           __set_union_op, __is_vector);
+            });
+        return __result;
+    }
+
+    const auto __m2 = __left_bound_seq_2 - __first2;
+    assert(__m1 == 0 || __m2 == 0);
+    if (__m2 > __set_algo_cut_off)
+    {
+        auto __res_or = __result;
+        __result += __m2; //we know proper offset due to [first2; left_bound_seq_2) < [first1; last1)
+        par_backend::parallel_invoke(
+            std::forward<_ExecutionPolicy>(__exec),
+            //do parallel copying of [first2; left_bound_seq_2)
+            [=] {
+                pattern_walk2_brick(std::forward<_ExecutionPolicy>(__exec), __first2, __left_bound_seq_2, __res_or,
+                                    copy_range2, std::true_type());
+            },
+            [=, &__result] {
+                __result = parallel_set_op(std::forward<_ExecutionPolicy>(__exec), __first1, __last1,
+                                           __left_bound_seq_2, __last2, __result, __comp,
+                                           [](_DifferenceType __n, _DifferenceType __m) { return __n + __m; },
+                                           __set_union_op, __is_vector);
+            });
+        return __result;
+    }
+
+    return parallel_set_op(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result,
+                           __comp, [](_DifferenceType __n, _DifferenceType __m) { return __n + __m; }, __set_union_op,
+                           __is_vector);
+}
+
+//------------------------------------------------------------------------
+// set_union
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare>
+_OutputIterator
+brick_set_union(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                /*__is_vector=*/std::false_type) noexcept
+{
+    return std::set_union(__first1, __last1, __first2, __last2, __result, __comp);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare>
+_OutputIterator
+brick_set_union(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                /*__is_vector=*/std::true_type) noexcept
+{
+    __PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial");
+    return std::set_union(__first1, __last1, __first2, __last2, __result, __comp);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator,
+          class _Compare, class _IsVector>
+_OutputIterator
+pattern_set_union(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                  _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp, _IsVector __is_vector,
+                  /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_set_union(__first1, __last1, __first2, __last2, __result, __comp, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator,
+          class _Compare, class _IsVector>
+_OutputIterator
+pattern_set_union(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                  _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                  _IsVector __is_vector, /*__is_parallel=*/std::true_type)
+{
+
+    const auto __n1 = __last1 - __first1;
+    const auto __n2 = __last2 - __first2;
+
+    // use serial algorithm
+    if (__n1 + __n2 <= __set_algo_cut_off)
+        return std::set_union(__first1, __last1, __first2, __last2, __result, __comp);
+
+    typedef typename std::iterator_traits<_OutputIterator>::value_type _T;
+    return parallel_set_union_op(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, __comp,
+        [__comp](_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                 _ForwardIterator2 __last2, _T* __result,
+                 _Compare __comp) { return std::set_union(__first1, __last1, __first2, __last2, __result, __comp); },
+        __is_vector);
+}
+
+//------------------------------------------------------------------------
+// set_intersection
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare>
+_OutputIterator
+brick_set_intersection(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                       _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                       /*__is_vector=*/std::false_type) noexcept
+{
+    return std::set_intersection(__first1, __last1, __first2, __last2, __result, __comp);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare>
+_OutputIterator
+brick_set_intersection(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                       _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                       /*__is_vector=*/std::true_type) noexcept
+{
+    __PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial");
+    return std::set_intersection(__first1, __last1, __first2, __last2, __result, __comp);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator,
+          class _Compare, class _IsVector>
+_OutputIterator
+pattern_set_intersection(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result,
+                         _Compare __comp, _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_set_intersection(__first1, __last1, __first2, __last2, __result, __comp, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator,
+          class _Compare, class _IsVector>
+_OutputIterator
+pattern_set_intersection(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result,
+                         _Compare __comp, _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+    typedef typename std::iterator_traits<_OutputIterator>::value_type _T;
+    typedef typename std::iterator_traits<_ForwardIterator1>::difference_type _DifferenceType;
+
+    const auto __n1 = __last1 - __first1;
+    const auto __n2 = __last2 - __first2;
+
+    // intersection is empty
+    if (__n1 == 0 || __n2 == 0)
+        return __result;
+
+    // testing  whether the sequences are intersected
+    _ForwardIterator1 __left_bound_seq_1 = lower_bound(__first1, __last1, *__first2, __comp);
+    //{1} < {2}: seq 2 is wholly greater than seq 1, so, the intersection is empty
+    if (__left_bound_seq_1 == __last1)
+        return __result;
+
+    // testing  whether the sequences are intersected
+    _ForwardIterator2 __left_bound_seq_2 = lower_bound(__first2, __last2, *__first1, __comp);
+    //{2} < {1}: seq 1 is wholly greater than seq 2, so, the intersection is empty
+    if (__left_bound_seq_2 == __last2)
+        return __result;
+
+    const auto __m1 = __last1 - __left_bound_seq_1 + __n2;
+    if (__m1 > __set_algo_cut_off)
+    {
+        //we know proper offset due to [first1; left_bound_seq_1) < [first2; last2)
+        return parallel_set_op(
+            std::forward<_ExecutionPolicy>(__exec), __left_bound_seq_1, __last1, __first2, __last2, __result, __comp,
+            [](_DifferenceType __n, _DifferenceType __m) { return std::min(__n, __m); },
+            [__comp](_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                     _ForwardIterator2 __last2, _T* __result, _Compare __comp) {
+                return std::set_intersection(__first1, __last1, __first2, __last2, __result, __comp);
+            },
+            __is_vector);
+    }
+
+    const auto __m2 = __last2 - __left_bound_seq_2 + __n1;
+    if (__m2 > __set_algo_cut_off)
+    {
+        //we know proper offset due to [first2; left_bound_seq_2) < [first1; last1)
+        __result = parallel_set_op(
+            std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __left_bound_seq_2, __last2, __result, __comp,
+            [](_DifferenceType __n, _DifferenceType __m) { return std::min(__n, __m); },
+            [__comp](_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                     _ForwardIterator2 __last2, _T* __result, _Compare __comp) {
+                return std::set_intersection(__first2, __last2, __first1, __last1, __result, __comp);
+            },
+            __is_vector);
+        return __result;
+    }
+
+    // [left_bound_seq_1; last1) and [left_bound_seq_2; last2) - use serial algorithm
+    return std::set_intersection(__left_bound_seq_1, __last1, __left_bound_seq_2, __last2, __result, __comp);
+}
+
+//------------------------------------------------------------------------
+// set_difference
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare>
+_OutputIterator
+brick_set_difference(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                     _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                     /*__is_vector=*/std::false_type) noexcept
+{
+    return std::set_difference(__first1, __last1, __first2, __last2, __result, __comp);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare>
+_OutputIterator
+brick_set_difference(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                     _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                     /*__is_vector=*/std::true_type) noexcept
+{
+    __PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial");
+    return std::set_difference(__first1, __last1, __first2, __last2, __result, __comp);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator,
+          class _Compare, class _IsVector>
+_OutputIterator
+pattern_set_difference(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                       _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                       _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_set_difference(__first1, __last1, __first2, __last2, __result, __comp, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator,
+          class _Compare, class _IsVector>
+_OutputIterator
+pattern_set_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                       _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                       _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+    typedef typename std::iterator_traits<_OutputIterator>::value_type _T;
+    typedef typename std::iterator_traits<_ForwardIterator1>::difference_type _DifferenceType;
+
+    const auto __n1 = __last1 - __first1;
+    const auto __n2 = __last2 - __first2;
+
+    // {} \ {2}: the difference is empty
+    if (__n1 == 0)
+        return __result;
+
+    // {1} \ {}: parallel copying just first sequence
+    if (__n2 == 0)
+        return pattern_walk2_brick(
+            std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __result,
+            [__is_vector](_ForwardIterator1 __begin, _ForwardIterator1 __end, _OutputIterator __res) {
+                return brick_copy(__begin, __end, __res, __is_vector);
+            },
+            std::true_type());
+
+    // testing  whether the sequences are intersected
+    _ForwardIterator1 __left_bound_seq_1 = lower_bound(__first1, __last1, *__first2, __comp);
+    //{1} < {2}: seq 2 is wholly greater than seq 1, so, parallel copying just first sequence
+    if (__left_bound_seq_1 == __last1)
+        return pattern_walk2_brick(
+            std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __result,
+            [__is_vector](_ForwardIterator1 __begin, _ForwardIterator1 __end, _OutputIterator __res) {
+                return brick_copy(__begin, __end, __res, __is_vector);
+            },
+            std::true_type());
+
+    // testing  whether the sequences are intersected
+    _ForwardIterator2 __left_bound_seq_2 = lower_bound(__first2, __last2, *__first1, __comp);
+    //{2} < {1}: seq 1 is wholly greater than seq 2, so, parallel copying just first sequence
+    if (__left_bound_seq_2 == __last2)
+        return pattern_walk2_brick(
+            std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __result,
+            [__is_vector](_ForwardIterator1 __begin, _ForwardIterator1 __end, _OutputIterator __res) {
+                return brick_copy(__begin, __end, __res, __is_vector);
+            },
+            std::true_type());
+
+    if (__n1 + __n2 > __set_algo_cut_off)
+        return parallel_set_op(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result,
+                               __comp, [](_DifferenceType __n, _DifferenceType __m) { return __n; },
+                               [__comp](_ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                                        _ForwardIterator2 __first2, _ForwardIterator2 __last2, _T* __result,
+                                        _Compare __comp) {
+                                   return std::set_difference(__first1, __last1, __first2, __last2, __result, __comp);
+                               },
+                               __is_vector);
+
+    // use serial algorithm
+    return std::set_difference(__first1, __last1, __first2, __last2, __result, __comp);
+}
+
+//------------------------------------------------------------------------
+// set_symmetric_difference
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare>
+_OutputIterator
+brick_set_symmetric_difference(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                               _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                               /*__is_vector=*/std::false_type) noexcept
+{
+    return std::set_symmetric_difference(__first1, __last1, __first2, __last2, __result, __comp);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare>
+_OutputIterator
+brick_set_symmetric_difference(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                               _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp,
+                               /*__is_vector=*/std::true_type) noexcept
+{
+    __PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial");
+    return std::set_symmetric_difference(__first1, __last1, __first2, __last2, __result, __comp);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator,
+          class _Compare, class _IsVector>
+_OutputIterator
+pattern_set_symmetric_difference(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                                 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result,
+                                 _Compare __comp, _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_set_symmetric_difference(__first1, __last1, __first2, __last2, __result, __comp,
+                                                    __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator,
+          class _Compare, class _IsVector>
+_OutputIterator
+pattern_set_symmetric_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                                 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result,
+                                 _Compare __comp, _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+
+    const auto __n1 = __last1 - __first1;
+    const auto __n2 = __last2 - __first2;
+
+    // use serial algorithm
+    if (__n1 + __n2 <= __set_algo_cut_off)
+        return std::set_symmetric_difference(__first1, __last1, __first2, __last2, __result, __comp);
+
+    typedef typename std::iterator_traits<_OutputIterator>::value_type _T;
+    return parallel_set_union_op(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, __comp,
+        [__comp](_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                 _ForwardIterator2 __last2, _T* __result, _Compare __comp) {
+            return std::set_symmetric_difference(__first1, __last1, __first2, __last2, __result, __comp);
+        },
+        __is_vector);
+}
+
+//------------------------------------------------------------------------
+// is_heap_until
+//------------------------------------------------------------------------
+
+template <class _RandomAccessIterator, class _Compare>
+_RandomAccessIterator
+brick_is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
+                    /* __is_vector = */ std::false_type) noexcept
+{
+    return std::is_heap_until(__first, __last, __comp);
+}
+
+template <class _RandomAccessIterator, class _Compare>
+_RandomAccessIterator
+brick_is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
+                    /* __is_vector = */ std::true_type) noexcept
+{
+    if (__last - __first < 2)
+        return __last;
+    typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _SizeType;
+    return unseq_backend::simd_first(
+        __first, _SizeType(0), __last - __first,
+        [&__comp](_RandomAccessIterator __it, _SizeType __i) { return __comp(__it[(__i - 1) / 2], __it[__i]); });
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsVector>
+_RandomAccessIterator
+pattern_is_heap_until(_ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
+                      _IsVector __is_vector, /* is_parallel = */ std::false_type) noexcept
+{
+    return internal::brick_is_heap_until(__first, __last, __comp, __is_vector);
+}
+
+template <class _RandomAccessIterator, class _DifferenceType, class _Compare>
+_RandomAccessIterator
+is_heap_until_local(_RandomAccessIterator __first, _DifferenceType __begin, _DifferenceType __end, _Compare __comp,
+                    /* __is_vector = */ std::false_type) noexcept
+{
+    _DifferenceType __i = __begin;
+    for (; __i < __end; ++__i)
+    {
+        if (__comp(__first[(__i - 1) / 2], __first[__i]))
+        {
+            break;
+        }
+    }
+    return __first + __i;
+}
+
+template <class _RandomAccessIterator, class _DifferenceType, class _Compare>
+_RandomAccessIterator
+is_heap_until_local(_RandomAccessIterator __first, _DifferenceType __begin, _DifferenceType __end, _Compare __comp,
+                    /* __is_vector = */ std::true_type) noexcept
+{
+    return unseq_backend::simd_first(
+        __first, __begin, __end,
+        [&__comp](_RandomAccessIterator __it, _DifferenceType __i) { return __comp(__it[(__i - 1) / 2], __it[__i]); });
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsVector>
+_RandomAccessIterator
+pattern_is_heap_until(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last,
+                      _Compare __comp, _IsVector __is_vector, /* is_parallel = */ std::true_type) noexcept
+{
+    if (__last - __first < 2)
+        return __last;
+
+    return internal::except_handler([&]() {
+        return internal::parallel_find(
+            std::forward<_ExecutionPolicy>(__exec), __first, __last,
+            [__first, __last, __comp, __is_vector](_RandomAccessIterator __i, _RandomAccessIterator __j) {
+                return internal::is_heap_until_local(__first, __i - __first, __j - __first, __comp, __is_vector);
+            },
+            std::less<typename std::iterator_traits<_RandomAccessIterator>::difference_type>(), /*is_first=*/true);
+    });
+}
+
+//------------------------------------------------------------------------
+// min_element
+//------------------------------------------------------------------------
+
+template <typename _ForwardIterator, typename _Compare>
+_ForwardIterator
+brick_min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp,
+                  /* __is_vector = */ std::false_type) noexcept
+{
+    return std::min_element(__first, __last, __comp);
+}
+
+template <typename _ForwardIterator, typename _Compare>
+_ForwardIterator
+brick_min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp,
+                  /* __is_vector = */ std::true_type) noexcept
+{
+#if __PSTL_UDR_PRESENT
+    return unseq_backend::simd_min_element(__first, __last - __first, __comp);
+#else
+    return std::min_element(__first, __last, __comp);
+#endif
+}
+
+template <typename _ExecutionPolicy, typename _ForwardIterator, typename _Compare, typename _IsVector>
+_ForwardIterator
+pattern_min_element(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp,
+                    _IsVector __is_vector, /* is_parallel = */ std::false_type) noexcept
+{
+    return internal::brick_min_element(__first, __last, __comp, __is_vector);
+}
+
+template <typename _ExecutionPolicy, typename _RandomAccessIterator, typename _Compare, typename _IsVector>
+_RandomAccessIterator
+pattern_min_element(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last,
+                    _Compare __comp, _IsVector __is_vector, /* is_parallel = */ std::true_type)
+{
+    if (__first == __last)
+        return __last;
+
+    return internal::except_handler([&]() {
+        return par_backend::parallel_reduce(
+            std::forward<_ExecutionPolicy>(__exec), __first + 1, __last, __first,
+            [=](_RandomAccessIterator __begin, _RandomAccessIterator __end,
+                _RandomAccessIterator __init) -> _RandomAccessIterator {
+                const _RandomAccessIterator subresult = brick_min_element(__begin, __end, __comp, __is_vector);
+                return internal::cmp_iterators_by_values(__init, subresult, __comp);
+            },
+            [=](_RandomAccessIterator __it1, _RandomAccessIterator __it2) -> _RandomAccessIterator {
+                return internal::cmp_iterators_by_values(__it1, __it2, __comp);
+            });
+    });
+}
+
+//------------------------------------------------------------------------
+// minmax_element
+//------------------------------------------------------------------------
+
+template <typename _ForwardIterator, typename _Compare>
+std::pair<_ForwardIterator, _ForwardIterator>
+brick_minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp,
+                     /* __is_vector = */ std::false_type) noexcept
+{
+    return std::minmax_element(__first, __last, __comp);
+}
+
+template <typename _ForwardIterator, typename _Compare>
+std::pair<_ForwardIterator, _ForwardIterator>
+brick_minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp,
+                     /* __is_vector = */ std::true_type) noexcept
+{
+#if __PSTL_UDR_PRESENT
+    return unseq_backend::simd_minmax_element(__first, __last - __first, __comp);
+#else
+    return std::minmax_element(__first, __last, __comp);
+#endif
+}
+
+template <typename _ExecutionPolicy, typename _ForwardIterator, typename _Compare, typename _IsVector>
+std::pair<_ForwardIterator, _ForwardIterator>
+pattern_minmax_element(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp,
+                       _IsVector __is_vector, /* is_parallel = */ std::false_type) noexcept
+{
+    return internal::brick_minmax_element(__first, __last, __comp, __is_vector);
+}
+
+template <typename _ExecutionPolicy, typename _ForwardIterator, typename _Compare, typename _IsVector>
+std::pair<_ForwardIterator, _ForwardIterator>
+pattern_minmax_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp,
+                       _IsVector __is_vector, /* is_parallel = */ std::true_type)
+{
+    if (__first == __last)
+        return std::make_pair(__first, __first);
+
+    return internal::except_handler([&]() {
+        typedef std::pair<_ForwardIterator, _ForwardIterator> _Result;
+
+        return par_backend::parallel_reduce(
+            std::forward<_ExecutionPolicy>(__exec), __first + 1, __last, std::make_pair(__first, __first),
+            [=](_ForwardIterator __begin, _ForwardIterator __end, _Result __init) -> _Result {
+                const _Result __subresult = brick_minmax_element(__begin, __end, __comp, __is_vector);
+                return std::make_pair(internal::cmp_iterators_by_values(__subresult.first, __init.first, __comp),
+                                      internal::cmp_iterators_by_values(__init.second, __subresult.second,
+                                                                        internal::not_pred<_Compare>(__comp)));
+            },
+            [=](_Result __p1, _Result __p2) -> _Result {
+                return std::make_pair(
+                    internal::cmp_iterators_by_values(__p1.first, __p2.first, __comp),
+                    internal::cmp_iterators_by_values(__p2.second, __p1.second, internal::not_pred<_Compare>(__comp)));
+            });
+    });
+}
+
+//------------------------------------------------------------------------
+// mismatch
+//------------------------------------------------------------------------
+template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+std::pair<_ForwardIterator1, _ForwardIterator2>
+mismatch_serial(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                _ForwardIterator2 __last2, _BinaryPredicate __pred)
+{
+#if __PSTL_CPP14_2RANGE_MISMATCH_EQUAL_PRESENT
+    return std::mismatch(__first1, __last1, __first2, __last2, __pred);
+#else
+    for (; __first1 != __last1 && __first2 != __last2 && __pred(*__first1, *__first2); ++__first1, ++__first2)
+    {
+    }
+    return std::make_pair(__first1, __first2);
+#endif
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _Predicate>
+std::pair<_ForwardIterator1, _ForwardIterator2>
+brick_mismatch(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+               _ForwardIterator2 __last2, _Predicate __pred, /* __is_vector = */ std::false_type) noexcept
+{
+    return internal::mismatch_serial(__first1, __last1, __first2, __last2, __pred);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _Predicate>
+std::pair<_ForwardIterator1, _ForwardIterator2>
+brick_mismatch(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+               _ForwardIterator2 __last2, _Predicate __pred, /* __is_vector = */ std::true_type) noexcept
+{
+    auto __n = std::min(__last1 - __first1, __last2 - __first2);
+    return unseq_backend::simd_first(__first1, __n, __first2, not_pred<_Predicate>(__pred));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate, class _IsVector>
+std::pair<_ForwardIterator1, _ForwardIterator2>
+pattern_mismatch(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                 _ForwardIterator2 __last2, _Predicate __pred, _IsVector __is_vector,
+                 /* is_parallel = */ std::false_type) noexcept
+{
+    return internal::brick_mismatch(__first1, __last1, __first2, __last2, __pred, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, class _Predicate,
+          class _IsVector>
+std::pair<_RandomAccessIterator1, _RandomAccessIterator2>
+pattern_mismatch(_ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
+                 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _Predicate __pred,
+                 _IsVector __is_vector, /* is_parallel = */ std::true_type) noexcept
+{
+    return internal::except_handler([&]() {
+        auto __n = std::min(__last1 - __first1, __last2 - __first2);
+        auto __result = internal::parallel_find(
+            std::forward<_ExecutionPolicy>(__exec), __first1, __first1 + __n,
+            [__first1, __first2, __pred, __is_vector](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) {
+                return internal::brick_mismatch(__i, __j, __first2 + (__i - __first1), __first2 + (__j - __first1),
+                                                __pred, __is_vector)
+                    .first;
+            },
+            std::less<typename std::iterator_traits<_RandomAccessIterator1>::difference_type>(), /*is_first=*/true);
+        return std::make_pair(__result, __first2 + (__result - __first1));
+    });
+}
+
+//------------------------------------------------------------------------
+// lexicographical_compare
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _Compare>
+bool
+brick_lexicographical_compare(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                              _ForwardIterator2 __last2, _Compare __comp, /* __is_vector = */ std::false_type) noexcept
+{
+    return std::lexicographical_compare(__first1, __last1, __first2, __last2, __comp);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _Compare>
+bool
+brick_lexicographical_compare(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+                              _ForwardIterator2 __last2, _Compare __comp, /* __is_vector = */ std::true_type) noexcept
+{
+    if (__first2 == __last2)
+    { // if second sequence is empty
+        return false;
+    }
+    else if (__first1 == __last1)
+    { // if first sequence is empty
+        return true;
+    }
+    else
+    {
+        typedef typename std::iterator_traits<_ForwardIterator1>::reference ref_type1;
+        typedef typename std::iterator_traits<_ForwardIterator2>::reference ref_type2;
+        --__last1;
+        --__last2;
+        auto __n = std::min(__last1 - __first1, __last2 - __first2);
+        std::pair<_ForwardIterator1, _ForwardIterator2> __result = unseq_backend::simd_first(
+            __first1, __n, __first2, [__comp](const ref_type1 __x, const ref_type2 __y) mutable {
+                return __comp(__x, __y) || __comp(__y, __x);
+            });
+
+        if (__result.first == __last1 && __result.second != __last2)
+        { // if first sequence shorter than second
+            return !__comp(*__result.second, *__result.first);
+        }
+        else
+        { // if second sequence shorter than first or both have the same number of elements
+            return __comp(*__result.first, *__result.second);
+        }
+    }
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare, class _IsVector>
+bool
+pattern_lexicographical_compare(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                                _ForwardIterator2 __first2, _ForwardIterator2 __last2, _Compare __comp,
+                                _IsVector __is_vector, /* is_parallel = */ std::false_type) noexcept
+{
+    return internal::brick_lexicographical_compare(__first1, __last1, __first2, __last2, __comp, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare, class _IsVector>
+bool
+pattern_lexicographical_compare(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                                _ForwardIterator2 __first2, _ForwardIterator2 __last2, _Compare __comp,
+                                _IsVector __is_vector, /* is_parallel = */ std::true_type) noexcept
+{
+    if (__first2 == __last2)
+    { // if second sequence is empty
+        return false;
+    }
+    else if (__first1 == __last1)
+    { // if first sequence is empty
+        return true;
+    }
+    else
+    {
+        typedef typename std::iterator_traits<_ForwardIterator1>::reference _RefType1;
+        typedef typename std::iterator_traits<_ForwardIterator2>::reference _RefType2;
+        --__last1;
+        --__last2;
+        auto __n = std::min(__last1 - __first1, __last2 - __first2);
+        auto __result = internal::parallel_find(
+            std::forward<_ExecutionPolicy>(__exec), __first1, __first1 + __n,
+            [__first1, __first2, &__comp, __is_vector](_ForwardIterator1 __i, _ForwardIterator1 __j) {
+                return brick_mismatch(__i, __j, __first2 + (__i - __first1), __first2 + (__j - __first1),
+                                      [&__comp](const _RefType1 __x, const _RefType2 __y) {
+                                          return !__comp(__x, __y) && !__comp(__y, __x);
+                                      },
+                                      __is_vector)
+                    .first;
+            },
+            std::less<typename std::iterator_traits<_ForwardIterator1>::difference_type>(), /*is_first=*/true);
+
+        if (__result == __last1 && __first2 + (__result - __first1) != __last2)
+        { // if first sequence shorter than second
+            return !__comp(*(__first2 + (__result - __first1)), *__result);
+        }
+        else
+        { // if second sequence shorter than first or both have the same number of elements
+            return __comp(*__result, *(__first2 + (__result - __first1)));
+        }
+    }
+}
+
+} // namespace internal
+} // namespace __pstl
+
+#endif /* __PSTL_algorithm_impl_H */
diff --git a/pstl/include/pstl/internal/execution_defs.h b/pstl/include/pstl/internal/execution_defs.h
new file mode 100644 (file)
index 0000000..d5fd652
--- /dev/null
@@ -0,0 +1,163 @@
+// -*- C++ -*-
+//===-- execution_defs.h --------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_execution_policy_defs_H
+#define __PSTL_execution_policy_defs_H
+
+#include <type_traits>
+
+namespace __pstl
+{
+namespace execution
+{
+inline namespace v1
+{
+
+// 2.4, Sequential execution policy
+class sequenced_policy
+{
+  public:
+    // For internal use only
+    static constexpr std::false_type
+    __allow_unsequenced()
+    {
+        return std::false_type{};
+    }
+    static constexpr std::false_type
+    __allow_vector()
+    {
+        return std::false_type{};
+    }
+    static constexpr std::false_type
+    __allow_parallel()
+    {
+        return std::false_type{};
+    }
+};
+
+#if __PSTL_USE_PAR_POLICIES
+// 2.5, Parallel execution policy
+class parallel_policy
+{
+  public:
+    // For internal use only
+    static constexpr std::false_type
+    __allow_unsequenced()
+    {
+        return std::false_type{};
+    }
+    static constexpr std::false_type
+    __allow_vector()
+    {
+        return std::false_type{};
+    }
+    static constexpr std::true_type
+    __allow_parallel()
+    {
+        return std::true_type{};
+    }
+};
+
+// 2.6, Parallel+Vector execution policy
+class parallel_unsequenced_policy
+{
+  public:
+    // For internal use only
+    static constexpr std::true_type
+    __allow_unsequenced()
+    {
+        return std::true_type{};
+    }
+    static constexpr std::true_type
+    __allow_vector()
+    {
+        return std::true_type{};
+    }
+    static constexpr std::true_type
+    __allow_parallel()
+    {
+        return std::true_type{};
+    }
+};
+#endif
+
+class unsequenced_policy
+{
+  public:
+    // For internal use only
+    static constexpr std::true_type
+    __allow_unsequenced()
+    {
+        return std::true_type{};
+    }
+    static constexpr std::true_type
+    __allow_vector()
+    {
+        return std::true_type{};
+    }
+    static constexpr std::false_type
+    __allow_parallel()
+    {
+        return std::false_type{};
+    }
+};
+
+// 2.8, Execution policy objects
+constexpr sequenced_policy seq{};
+#if __PSTL_USE_PAR_POLICIES
+constexpr parallel_policy par{};
+constexpr parallel_unsequenced_policy par_unseq{};
+#endif
+constexpr unsequenced_policy unseq{};
+
+// 2.3, Execution policy type trait
+template <class T>
+struct is_execution_policy : std::false_type
+{
+};
+
+template <>
+struct is_execution_policy<sequenced_policy> : std::true_type
+{
+};
+#if __PSTL_USE_PAR_POLICIES
+template <>
+struct is_execution_policy<parallel_policy> : std::true_type
+{
+};
+template <>
+struct is_execution_policy<parallel_unsequenced_policy> : std::true_type
+{
+};
+#endif
+template <>
+struct is_execution_policy<unsequenced_policy> : std::true_type
+{
+};
+
+#if __PSTL_CPP14_VARIABLE_TEMPLATES_PRESENT
+template <class T>
+constexpr bool is_execution_policy_v = is_execution_policy<T>::value;
+#endif
+
+} // namespace v1
+} // namespace execution
+
+namespace internal
+{
+template <class ExecPolicy, class T>
+using enable_if_execution_policy =
+    typename std::enable_if<__pstl::execution::is_execution_policy<typename std::decay<ExecPolicy>::type>::value,
+                            T>::type;
+} // namespace internal
+
+} // namespace __pstl
+
+#endif /* __PSTL_execution_policy_defs_H */
diff --git a/pstl/include/pstl/internal/execution_impl.h b/pstl/include/pstl/internal/execution_impl.h
new file mode 100644 (file)
index 0000000..02925bf
--- /dev/null
@@ -0,0 +1,156 @@
+// -*- C++ -*-
+//===-- execution_impl.h --------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_execution_impl_H
+#define __PSTL_execution_impl_H
+
+#include <iterator>
+#include <type_traits>
+
+#include "execution_defs.h"
+
+namespace __pstl
+{
+namespace internal
+{
+
+using namespace __pstl::execution;
+
+/* predicate */
+
+template <typename _Tp>
+std::false_type lazy_and(_Tp, std::false_type)
+{
+    return std::false_type{};
+};
+
+template <typename _Tp>
+inline _Tp
+lazy_and(_Tp __a, std::true_type)
+{
+    return __a;
+}
+
+template <typename _Tp>
+std::true_type lazy_or(_Tp, std::true_type)
+{
+    return std::true_type{};
+};
+
+template <typename _Tp>
+inline _Tp
+lazy_or(_Tp __a, std::false_type)
+{
+    return __a;
+}
+
+/* iterator */
+template <typename _IteratorType, typename... _OtherIteratorTypes>
+struct is_random_access_iterator
+{
+    static constexpr bool value =
+        is_random_access_iterator<_IteratorType>::value && is_random_access_iterator<_OtherIteratorTypes...>::value;
+    typedef std::integral_constant<bool, value> type;
+};
+
+template <typename _IteratorType>
+struct is_random_access_iterator<_IteratorType>
+    : std::is_same<typename std::iterator_traits<_IteratorType>::iterator_category, std::random_access_iterator_tag>
+{
+};
+
+/* policy */
+template <typename Policy>
+struct policy_traits
+{
+};
+
+template <>
+struct policy_traits<sequenced_policy>
+{
+    typedef std::false_type allow_parallel;
+    typedef std::false_type allow_unsequenced;
+    typedef std::false_type allow_vector;
+};
+
+template <>
+struct policy_traits<unsequenced_policy>
+{
+    typedef std::false_type allow_parallel;
+    typedef std::true_type allow_unsequenced;
+    typedef std::true_type allow_vector;
+};
+
+#if __PSTL_USE_PAR_POLICIES
+template <>
+struct policy_traits<parallel_policy>
+{
+    typedef std::true_type allow_parallel;
+    typedef std::false_type allow_unsequenced;
+    typedef std::false_type allow_vector;
+};
+
+template <>
+struct policy_traits<parallel_unsequenced_policy>
+{
+    typedef std::true_type allow_parallel;
+    typedef std::true_type allow_unsequenced;
+    typedef std::true_type allow_vector;
+};
+#endif
+
+template <typename _ExecutionPolicy>
+using collector_t = typename policy_traits<typename std::decay<_ExecutionPolicy>::type>::collector_type;
+
+template <typename _ExecutionPolicy>
+using allow_vector = typename internal::policy_traits<typename std::decay<_ExecutionPolicy>::type>::allow_vector;
+
+template <typename _ExecutionPolicy>
+using allow_unsequenced =
+    typename internal::policy_traits<typename std::decay<_ExecutionPolicy>::type>::allow_unsequenced;
+
+template <typename _ExecutionPolicy>
+using allow_parallel = typename internal::policy_traits<typename std::decay<_ExecutionPolicy>::type>::allow_parallel;
+
+template <typename _ExecutionPolicy, typename... _IteratorTypes>
+auto
+is_vectorization_preferred(_ExecutionPolicy&& __exec)
+    -> decltype(lazy_and(__exec.__allow_vector(), typename is_random_access_iterator<_IteratorTypes...>::type()))
+{
+    return internal::lazy_and(__exec.__allow_vector(), typename is_random_access_iterator<_IteratorTypes...>::type());
+}
+
+template <typename _ExecutionPolicy, typename... _IteratorTypes>
+auto
+is_parallelization_preferred(_ExecutionPolicy&& __exec)
+    -> decltype(lazy_and(__exec.__allow_parallel(), typename is_random_access_iterator<_IteratorTypes...>::type()))
+{
+    return internal::lazy_and(__exec.__allow_parallel(), typename is_random_access_iterator<_IteratorTypes...>::type());
+}
+
+template <typename policy, typename... _IteratorTypes>
+struct prefer_unsequenced_tag
+{
+    static constexpr bool value =
+        allow_unsequenced<policy>::value && is_random_access_iterator<_IteratorTypes...>::value;
+    typedef std::integral_constant<bool, value> type;
+};
+
+template <typename policy, typename... _IteratorTypes>
+struct prefer_parallel_tag
+{
+    static constexpr bool value = allow_parallel<policy>::value && is_random_access_iterator<_IteratorTypes...>::value;
+    typedef std::integral_constant<bool, value> type;
+};
+
+} // namespace internal
+} // namespace __pstl
+
+#endif /* __PSTL_execution_impl_H */
diff --git a/pstl/include/pstl/internal/glue_algorithm_defs.h b/pstl/include/pstl/internal/glue_algorithm_defs.h
new file mode 100644 (file)
index 0000000..73d302b
--- /dev/null
@@ -0,0 +1,552 @@
+// -*- C++ -*-
+//===-- glue_algorithm_defs.h ---------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_glue_algorithm_defs_H
+#define __PSTL_glue_algorithm_defs_H
+
+#include <functional>
+
+#include "execution_defs.h"
+
+namespace std
+{
+
+// [alg.any_of]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+any_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred);
+
+// [alg.all_of]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+all_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred);
+
+// [alg.none_of]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+none_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred);
+
+// [alg.foreach]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Function>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+for_each(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Function __f);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Function>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+for_each_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n, _Function __f);
+
+// [alg.find]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+find_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+find_if_not(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
+
+// [alg.find.end]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+find_end(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+         _ForwardIterator2 __s_last, _BinaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+find_end(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+         _ForwardIterator2 __s_last);
+
+// [alg.find_first_of]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+find_first_of(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+              _ForwardIterator2 __s_first, _ForwardIterator2 __s_last, _BinaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+find_first_of(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+              _ForwardIterator2 __s_first, _ForwardIterator2 __s_last);
+
+// [alg.adjacent_find]
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+adjacent_find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+adjacent_find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred);
+
+// [alg.count]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy,
+                                           typename iterator_traits<_ForwardIterator>::difference_type>
+count(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy,
+                                           typename iterator_traits<_ForwardIterator>::difference_type>
+count_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred);
+
+// [alg.search]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+search(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+       _ForwardIterator2 __s_last, _BinaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+search(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+       _ForwardIterator2 __s_last);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+search_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Size __count,
+         const _Tp& __value, _BinaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+search_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Size __count,
+         const _Tp& __value);
+
+// [alg.copy]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _Size, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+copy_n(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _Size __n, _ForwardIterator2 __result);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 result,
+        _Predicate __pred);
+
+// [alg.swap]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+swap_ranges(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+            _ForwardIterator2 __first2);
+
+// [alg.transform]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryOperation>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+transform(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result,
+          _UnaryOperation __op);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _BinaryOperation>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+transform(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+          _ForwardIterator __result, _BinaryOperation __op);
+
+// [alg.replace]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate, class _Tp>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+replace_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+           const _Tp& __new_value);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+replace(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value,
+        const _Tp& __new_value);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryPredicate, class _Tp>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+replace_copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                _ForwardIterator2 __result, _UnaryPredicate __pred, const _Tp& __new_value);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+replace_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result,
+             const _Tp& __old_value, const _Tp& __new_value);
+
+// [alg.fill]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __count, const _Tp& __value);
+
+// [alg.generate]
+template <class _ExecutionPolicy, class _ForwardIterator, class _Generator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+generate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Generator __g);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Generator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+generate_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size count, _Generator __g);
+
+// [alg.remove]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+remove_copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result, _Predicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+remove_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result,
+            const _Tp& __value);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+remove_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+remove(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
+
+// [alg.unique]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+unique_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result,
+            _BinaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+unique_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result);
+
+// [alg.reverse]
+
+template <class _ExecutionPolicy, class _BidirectionalIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+reverse(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last);
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+reverse_copy(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last,
+             _ForwardIterator __d_first);
+
+// [alg.rotate]
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+rotate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+rotate_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __middle, _ForwardIterator1 __last,
+            _ForwardIterator2 __result);
+
+// [alg.partitions]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+is_partitioned(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+partition(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _UnaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _BidirectionalIterator>
+stable_partition(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last,
+                 _UnaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _ForwardIterator1, class _ForwardIterator2,
+          class _UnaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>>
+partition_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last,
+               _ForwardIterator1 __out_true, _ForwardIterator2 __out_false, _UnaryPredicate __pred);
+
+// [alg.sort]
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last);
+
+// [stable.sort]
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+stable_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+stable_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last);
+
+// [mismatch]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>>
+mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+         _ForwardIterator2 __last2, _BinaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>>
+mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+         _BinaryPredicate __pred);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>>
+mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+         _ForwardIterator2 __last2);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>>
+mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2);
+
+// [alg.equal]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+      _BinaryPredicate __p);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+      _ForwardIterator2 __last2, _BinaryPredicate __p);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+      _ForwardIterator2 __last2);
+
+// [alg.move]
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first);
+
+// [partial.sort]
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+partial_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __middle,
+             _RandomAccessIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+partial_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __middle,
+             _RandomAccessIterator __last);
+
+// [partial.sort.copy]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _RandomAccessIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator>
+partial_sort_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last,
+                  _RandomAccessIterator __d_first, _RandomAccessIterator __d_last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _RandomAccessIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator>
+partial_sort_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last,
+                  _RandomAccessIterator __d_first, _RandomAccessIterator __d_last);
+
+// [is.sorted]
+template <class _ExecutionPolicy, class _ForwardIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+is_sorted_until(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+is_sorted_until(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
+
+// [alg.nth.element]
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+nth_element(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __nth,
+            _RandomAccessIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+nth_element(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __nth,
+            _RandomAccessIterator __last);
+
+// [alg.merge]
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+      _ForwardIterator2 __last2, _ForwardIterator __d_first, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+      _ForwardIterator2 __last2, _ForwardIterator __d_first);
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+inplace_merge(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __middle,
+              _BidirectionalIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _BidirectionalIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+inplace_merge(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __middle,
+              _BidirectionalIterator __last);
+
+// [includes]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+         _ForwardIterator2 __last2, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+         _ForwardIterator2 __last2);
+
+// [set.union]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_union(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+          _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_union(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+          _ForwardIterator2 __last2, _ForwardIterator __result);
+
+// [set.intersection]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_intersection(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_intersection(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result);
+
+// [set.difference]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+               _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+               _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result);
+
+// [set.symmetric.difference]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_symmetric_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator result,
+                         _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_symmetric_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result);
+
+// [is.heap]
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator>
+is_heap_until(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator>
+is_heap_until(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last);
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+is_heap(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+is_heap(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last);
+
+// [alg.min.max]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+min_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+min_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+max_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+max_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator, _ForwardIterator>>
+minmax_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator, _ForwardIterator>>
+minmax_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
+
+// [alg.lex.comparison]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+lexicographical_compare(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                        _ForwardIterator2 __first2, _ForwardIterator2 __last2, _Compare __comp);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+lexicographical_compare(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                        _ForwardIterator2 __first2, _ForwardIterator2 __last2);
+
+} // namespace std
+#endif /* __PSTL_glue_algorithm_defs_H */
diff --git a/pstl/include/pstl/internal/glue_algorithm_impl.h b/pstl/include/pstl/internal/glue_algorithm_impl.h
new file mode 100644 (file)
index 0000000..fb5032f
--- /dev/null
@@ -0,0 +1,1173 @@
+// -*- C++ -*-
+//===-- glue_algorithm_impl.h ---------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_glue_algorithm_impl_H
+#define __PSTL_glue_algorithm_impl_H
+
+#include <functional>
+
+#include "execution_defs.h"
+#include "utils.h"
+#include "algorithm_impl.h"
+#include "numeric_impl.h" /* count and count_if use pattern_transform_reduce */
+
+namespace std
+{
+
+// [alg.any_of]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+any_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_any_of(std::forward<_ExecutionPolicy>(__exec), __first, __last, __pred,
+                                    internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+                                    internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+// [alg.all_of]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Pred>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+all_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Pred __pred)
+{
+    return !std::any_of(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                        __pstl::internal::not_pred<_Pred>(__pred));
+}
+
+// [alg.none_of]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+none_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
+{
+    return !std::any_of(std::forward<_ExecutionPolicy>(__exec), __first, __last, __pred);
+}
+
+// [alg.foreach]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Function>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+for_each(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Function __f)
+{
+    using namespace __pstl;
+    internal::pattern_walk1(std::forward<_ExecutionPolicy>(__exec), __first, __last, __f,
+                            internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+                            internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Function>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+for_each_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n, _Function __f)
+{
+    using namespace __pstl;
+    return internal::pattern_walk1_n(
+        std::forward<_ExecutionPolicy>(__exec), __first, __n, __f,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+// [alg.find]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+find_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_find_if(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+find_if_not(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
+{
+    return std::find_if(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                        __pstl::internal::not_pred<_Predicate>(__pred));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
+{
+    return std::find_if(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                        __pstl::internal::equal_value<_Tp>(__value));
+}
+
+// [alg.find.end]
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+find_end(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+         _ForwardIterator2 __s_last, _BinaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_find_end(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+find_end(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+         _ForwardIterator2 __s_last)
+{
+    return std::find_end(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last,
+                         __pstl::internal::pstl_equal());
+}
+
+// [alg.find_first_of]
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+find_first_of(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+              _ForwardIterator2 __s_first, _ForwardIterator2 __s_last, _BinaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_find_first_of(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+find_first_of(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+              _ForwardIterator2 __s_first, _ForwardIterator2 __s_last)
+{
+    return std::find_first_of(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last,
+                              __pstl::internal::pstl_equal());
+}
+
+// [alg.adjacent_find]
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+adjacent_find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    using namespace __pstl;
+    return internal::pattern_adjacent_find(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, std::equal_to<_ValueType>(),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec), /*first_semantic*/ false);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+adjacent_find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_adjacent_find(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __pred,
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec), /*first_semantic*/ false);
+}
+
+// [alg.count]
+
+// Implementation note: count and count_if call the pattern directly instead of calling std::transform_reduce
+// so that we do not have to include <numeric>.
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy,
+                                           typename iterator_traits<_ForwardIterator>::difference_type>
+count(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    using namespace __pstl;
+    return internal::pattern_count(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                   [&__value](const _ValueType& __x) { return __value == __x; },
+                                   internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+                                   internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy,
+                                           typename iterator_traits<_ForwardIterator>::difference_type>
+count_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_count(std::forward<_ExecutionPolicy>(__exec), __first, __last, __pred,
+                                   internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+                                   internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+// [alg.search]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+search(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+       _ForwardIterator2 __s_last, _BinaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_search(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1>
+search(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
+       _ForwardIterator2 __s_last)
+{
+    return std::search(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last,
+                       __pstl::internal::pstl_equal());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+search_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Size __count,
+         const _Tp& __value, _BinaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_search_n(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __count, __value, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+search_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Size __count,
+         const _Tp& __value)
+{
+    return std::search_n(std::forward<_ExecutionPolicy>(__exec), __first, __last, __count, __value,
+                         std::equal_to<typename iterator_traits<_ForwardIterator>::value_type>());
+}
+
+// [alg.copy]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result)
+{
+    using namespace __pstl;
+    const auto __is_vector =
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec);
+
+    return internal::pattern_walk2_brick(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
+        [__is_vector](_ForwardIterator1 __begin, _ForwardIterator1 __end, _ForwardIterator2 __res) {
+            return internal::brick_copy(__begin, __end, __res, __is_vector);
+        },
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _Size, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+copy_n(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _Size __n, _ForwardIterator2 __result)
+{
+    using namespace __pstl;
+    const auto __is_vector =
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec);
+
+    return internal::pattern_walk2_brick_n(
+        std::forward<_ExecutionPolicy>(__exec), __first, __n, __result,
+        [__is_vector](_ForwardIterator1 __begin, _Size __sz, _ForwardIterator2 __res) {
+            return internal::brick_copy_n(__begin, __sz, __res, __is_vector);
+        },
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result,
+        _Predicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_copy_if(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+// [alg.swap]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+swap_ranges(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+            _ForwardIterator2 __first2)
+{
+    using namespace __pstl;
+    typedef typename iterator_traits<_ForwardIterator1>::reference _ReferenceType1;
+    typedef typename iterator_traits<_ForwardIterator2>::reference _ReferenceType2;
+    return internal::pattern_walk2(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2,
+        [](_ReferenceType1 __x, _ReferenceType2 __y) {
+            using std::swap;
+            swap(__x, __y);
+        },
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+// [alg.transform]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+transform(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result,
+          _UnaryOperation __op)
+{
+    typedef typename iterator_traits<_ForwardIterator1>::reference _InputType;
+    typedef typename iterator_traits<_ForwardIterator2>::reference _OutputType;
+    using namespace __pstl;
+    return internal::pattern_walk2(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
+        [__op](_InputType __x, _OutputType __y) mutable { __y = __op(__x); },
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _BinaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+transform(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+          _ForwardIterator __result, _BinaryOperation __op)
+{
+    typedef typename iterator_traits<_ForwardIterator1>::reference _Input1Type;
+    typedef typename iterator_traits<_ForwardIterator2>::reference _Input2Type;
+    typedef typename iterator_traits<_ForwardIterator>::reference _OutputType;
+    using namespace __pstl;
+    return internal::pattern_walk3(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __result,
+        [__op](_Input1Type x, _Input2Type y, _OutputType z) mutable { z = __op(x, y); },
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2, _ForwardIterator>(
+            __exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2,
+                                               _ForwardIterator>(__exec));
+}
+
+// [alg.replace]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+replace_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred,
+           const _Tp& __new_value)
+{
+    using namespace __pstl;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ElementType;
+    internal::pattern_walk1(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                            [&__pred, &__new_value](_ElementType __elem) {
+                                if (__pred(__elem))
+                                {
+                                    __elem = __new_value;
+                                }
+                            },
+                            internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+                            internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+replace(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value,
+        const _Tp& __new_value)
+{
+    std::replace_if(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                    __pstl::internal::equal_value<_Tp>(__old_value), __new_value);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryPredicate, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+replace_copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                _ForwardIterator2 __result, _UnaryPredicate __pred, const _Tp& __new_value)
+{
+    typedef typename iterator_traits<_ForwardIterator1>::reference _InputType;
+    typedef typename iterator_traits<_ForwardIterator2>::reference _OutputType;
+    using namespace __pstl;
+    return internal::pattern_walk2(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
+        [__pred, &__new_value](_InputType __x, _OutputType __y) mutable { __y = __pred(__x) ? __new_value : __x; },
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+replace_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result,
+             const _Tp& __old_value, const _Tp& __new_value)
+{
+    return std::replace_copy_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
+                                __pstl::internal::equal_value<_Tp>(__old_value), __new_value);
+}
+
+// [alg.fill]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
+{
+    using namespace __pstl;
+    internal::pattern_fill(std::forward<_ExecutionPolicy>(__exec), __first, __last, __value,
+                           internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+                           internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __count, const _Tp& __value)
+{
+    if (__count <= 0)
+        return __first;
+
+    using namespace __pstl;
+    return internal::pattern_fill_n(std::forward<_ExecutionPolicy>(__exec), __first, __count, __value,
+                                    internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+                                    internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+// [alg.generate]
+template <class _ExecutionPolicy, class _ForwardIterator, class _Generator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+generate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Generator __g)
+{
+    using namespace __pstl;
+    internal::pattern_generate(std::forward<_ExecutionPolicy>(__exec), __first, __last, __g,
+                               internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+                               internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Generator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+generate_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __count, _Generator __g)
+{
+    if (__count <= 0)
+        return __first;
+
+    using namespace __pstl;
+    return internal::pattern_generate_n(
+        std::forward<_ExecutionPolicy>(__exec), __first, __count, __g,
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+// [alg.remove]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+remove_copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result, _Predicate __pred)
+{
+    return std::copy_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
+                        __pstl::internal::not_pred<_Predicate>(__pred));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+remove_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result,
+            const _Tp& __value)
+{
+    return std::copy_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
+                        __pstl::internal::not_equal_value<_Tp>(__value));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+remove_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_remove_if(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+remove(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
+{
+    return std::remove_if(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                          __pstl::internal::equal_value<_Tp>(__value));
+}
+
+// [alg.unique]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_unique(std::forward<_ExecutionPolicy>(__exec), __first, __last, __pred,
+                                    internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+                                    internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
+{
+    return std::unique(std::forward<_ExecutionPolicy>(__exec), __first, __last, __pstl::internal::pstl_equal());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+unique_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result,
+            _BinaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_unique_copy(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+unique_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result)
+{
+    return std::unique_copy(__exec, __first, __last, __result, __pstl::internal::pstl_equal());
+}
+
+// [alg.reverse]
+
+template <class _ExecutionPolicy, class _BidirectionalIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+reverse(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last)
+{
+    using namespace __pstl;
+    internal::pattern_reverse(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                              internal::is_vectorization_preferred<_ExecutionPolicy, _BidirectionalIterator>(__exec),
+                              internal::is_parallelization_preferred<_ExecutionPolicy, _BidirectionalIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+reverse_copy(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last,
+             _ForwardIterator __d_first)
+{
+    using namespace __pstl;
+    return internal::pattern_reverse_copy(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __d_first,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _BidirectionalIterator, _ForwardIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _BidirectionalIterator, _ForwardIterator>(__exec));
+}
+
+// [alg.rotate]
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+rotate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
+{
+    using namespace __pstl;
+    return internal::pattern_rotate(std::forward<_ExecutionPolicy>(__exec), __first, __middle, __last,
+                                    internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+                                    internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+rotate_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __middle, _ForwardIterator1 __last,
+            _ForwardIterator2 __result)
+{
+    using namespace __pstl;
+    return internal::pattern_rotate_copy(
+        std::forward<_ExecutionPolicy>(__exec), __first, __middle, __last, __result,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+// [alg.partitions]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+is_partitioned(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_is_partitioned(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+partition(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_partition(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _UnaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _BidirectionalIterator>
+stable_partition(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last,
+                 _UnaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_stable_partition(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _BidirectionalIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _BidirectionalIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _ForwardIterator1, class _ForwardIterator2,
+          class _UnaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>>
+partition_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last,
+               _ForwardIterator1 __out_true, _ForwardIterator2 __out_false, _UnaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_partition_copy(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __out_true, __out_false, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator, _ForwardIterator1, _ForwardIterator2>(
+            __exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator, _ForwardIterator1,
+                                               _ForwardIterator2>(__exec));
+}
+
+// [alg.sort]
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
+{
+    typedef typename iterator_traits<_RandomAccessIterator>::value_type _InputType;
+    using namespace __pstl;
+    return internal::pattern_sort(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _RandomAccessIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _RandomAccessIterator>(__exec),
+        typename std::is_move_constructible<_InputType>::type());
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last)
+{
+    typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _InputType;
+    std::sort(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>());
+}
+
+// [stable.sort]
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+stable_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_stable_sort(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _RandomAccessIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _RandomAccessIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+stable_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last)
+{
+    typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _InputType;
+    std::stable_sort(__exec, __first, __last, std::less<_InputType>());
+}
+
+// [mismatch]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>>
+mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+         _ForwardIterator2 __last2, _BinaryPredicate __pred)
+{
+    using namespace __pstl;
+    return internal::pattern_mismatch(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __pred,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>>
+mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+         _BinaryPredicate __pred)
+{
+    return std::mismatch(__exec, __first1, __last1, __first2, std::next(__first2, std::distance(__first1, __last1)),
+                         __pred);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>>
+mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+         _ForwardIterator2 __last2)
+{
+    return std::mismatch(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2,
+                         __pstl::internal::pstl_equal());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>>
+mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
+{
+    //TODO: to get rid of "distance"
+    return std::mismatch(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2,
+                         std::next(__first2, std::distance(__first1, __last1)));
+}
+
+// [alg.equal]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+      _BinaryPredicate __p)
+{
+    using namespace __pstl;
+    return internal::pattern_equal(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __p,
+                                   internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1>(__exec),
+                                   internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
+{
+    return std::equal(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2,
+                      __pstl::internal::pstl_equal());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+      _ForwardIterator2 __last2, _BinaryPredicate __p)
+{
+    //TODO: to get rid of "distance"
+    if (std::distance(__first1, __last1) == std::distance(__first2, __last2))
+        return std::equal(__first1, __last1, __first2, __p);
+    else
+        return false;
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+      _ForwardIterator2 __last2)
+{
+    return equal(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __pstl::internal::pstl_equal());
+}
+
+// [alg.move]
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first)
+{
+    using namespace __pstl;
+    const auto __is_vector =
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec);
+
+    return internal::pattern_walk2_brick(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __d_first,
+        [__is_vector](_ForwardIterator1 __begin, _ForwardIterator1 __end, _ForwardIterator2 __res) {
+            return internal::brick_move(__begin, __end, __res, __is_vector);
+        },
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+// [partial.sort]
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+partial_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __middle,
+             _RandomAccessIterator __last, _Compare __comp)
+{
+    using namespace __pstl;
+    internal::pattern_partial_sort(
+        std::forward<_ExecutionPolicy>(__exec), __first, __middle, __last, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _RandomAccessIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _RandomAccessIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+partial_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __middle,
+             _RandomAccessIterator __last)
+{
+    typedef typename iterator_traits<_RandomAccessIterator>::value_type _InputType;
+    std::partial_sort(__exec, __first, __middle, __last, std::less<_InputType>());
+}
+
+// [partial.sort.copy]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _RandomAccessIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator>
+partial_sort_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last,
+                  _RandomAccessIterator __d_first, _RandomAccessIterator __d_last, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_partial_sort_copy(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __d_first, __d_last, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator, _RandomAccessIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator, _RandomAccessIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _RandomAccessIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator>
+partial_sort_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last,
+                  _RandomAccessIterator __d_first, _RandomAccessIterator __d_last)
+{
+    return std::partial_sort_copy(std::forward<_ExecutionPolicy>(__exec), __first, __last, __d_first, __d_last,
+                                  __pstl::internal::pstl_less());
+}
+
+// [is.sorted]
+template <class _ExecutionPolicy, class _ForwardIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+is_sorted_until(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
+{
+    using namespace __pstl;
+    const _ForwardIterator __res = internal::pattern_adjacent_find(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __pstl::internal::reorder_pred<_Compare>(__comp),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec), /*first_semantic*/ false);
+    return __res == __last ? __last : std::next(__res);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+is_sorted_until(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
+{
+    typedef typename std::iterator_traits<_ForwardIterator>::value_type _InputType;
+    return is_sorted_until(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_adjacent_find(
+               std::forward<_ExecutionPolicy>(__exec), __first, __last, internal::reorder_pred<_Compare>(__comp),
+               internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+               internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+               /*or_semantic*/ true) == __last;
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
+{
+    typedef typename std::iterator_traits<_ForwardIterator>::value_type _InputType;
+    return std::is_sorted(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>());
+}
+
+// [alg.merge]
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+      _ForwardIterator2 __last2, _ForwardIterator __d_first, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_merge(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __d_first, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2, _ForwardIterator>(
+            __exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2,
+                                               _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+      _ForwardIterator2 __last2, _ForwardIterator __d_first)
+{
+    return std::merge(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __d_first,
+                      __pstl::internal::pstl_less());
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+inplace_merge(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __middle,
+              _BidirectionalIterator __last, _Compare __comp)
+{
+    using namespace __pstl;
+    internal::pattern_inplace_merge(
+        std::forward<_ExecutionPolicy>(__exec), __first, __middle, __last, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _BidirectionalIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _BidirectionalIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _BidirectionalIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+inplace_merge(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __middle,
+              _BidirectionalIterator __last)
+{
+    typedef typename std::iterator_traits<_BidirectionalIterator>::value_type _InputType;
+    std::inplace_merge(__exec, __first, __middle, __last, std::less<_InputType>());
+}
+
+// [includes]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+         _ForwardIterator2 __last2, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_includes(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+         _ForwardIterator2 __last2)
+{
+    return std::includes(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2,
+                         __pstl::internal::pstl_less());
+}
+
+// [set.union]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_union(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+          _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_set_union(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2, _ForwardIterator>(
+            __exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2,
+                                               _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_union(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
+          _ForwardIterator2 __last2, _ForwardIterator __result)
+{
+    return std::set_union(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result,
+                          __pstl::internal::pstl_less());
+}
+
+// [set.intersection]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_intersection(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_set_intersection(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2, _ForwardIterator>(
+            __exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2,
+                                               _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_intersection(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result)
+{
+    return std::set_intersection(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result,
+                                 __pstl::internal::pstl_less());
+}
+
+// [set.difference]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+               _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_set_difference(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2, _ForwardIterator>(
+            __exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2,
+                                               _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+               _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result)
+{
+    return std::set_difference(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result,
+                               __pstl::internal::pstl_less());
+}
+
+// [set.symmetric.difference]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator,
+          class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_symmetric_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result,
+                         _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_set_symmetric_difference(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2, _ForwardIterator>(
+            __exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2,
+                                               _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+set_symmetric_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result)
+{
+    return std::set_symmetric_difference(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2,
+                                         __result, __pstl::internal::pstl_less());
+}
+
+// [is.heap]
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator>
+is_heap_until(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_is_heap_until(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _RandomAccessIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _RandomAccessIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator>
+is_heap_until(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last)
+{
+    typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _InputType;
+    return std::is_heap_until(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>());
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+is_heap(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
+{
+    return std::is_heap_until(std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp) == __last;
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+is_heap(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last)
+{
+    typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _InputType;
+    return std::is_heap(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>());
+}
+
+// [alg.min.max]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+min_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_min_element(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+min_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
+{
+    typedef typename std::iterator_traits<_ForwardIterator>::value_type _InputType;
+    return std::min_element(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+max_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
+{
+    return min_element(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                       __pstl::internal::reorder_pred<_Compare>(__comp));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+max_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
+{
+    typedef typename std::iterator_traits<_ForwardIterator>::value_type _InputType;
+    return std::min_element(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                            __pstl::internal::reorder_pred<std::less<_InputType>>(std::less<_InputType>()));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator, _ForwardIterator>>
+minmax_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_minmax_element(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator, _ForwardIterator>>
+minmax_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    return std::minmax_element(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_ValueType>());
+}
+
+// [alg.nth.element]
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+nth_element(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __nth,
+            _RandomAccessIterator __last, _Compare __comp)
+{
+    using namespace __pstl;
+    internal::pattern_nth_element(
+        std::forward<_ExecutionPolicy>(__exec), __first, __nth, __last, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _RandomAccessIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _RandomAccessIterator>(__exec));
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+nth_element(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __nth,
+            _RandomAccessIterator __last)
+{
+    typedef typename iterator_traits<_RandomAccessIterator>::value_type _InputType;
+    std::nth_element(std::forward<_ExecutionPolicy>(__exec), __first, __nth, __last, std::less<_InputType>());
+}
+
+// [alg.lex.comparison]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+lexicographical_compare(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                        _ForwardIterator2 __first2, _ForwardIterator2 __last2, _Compare __comp)
+{
+    using namespace __pstl;
+    return internal::pattern_lexicographical_compare(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __comp,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, bool>
+lexicographical_compare(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                        _ForwardIterator2 __first2, _ForwardIterator2 __last2)
+{
+    return std::lexicographical_compare(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2,
+                                        __pstl::internal::pstl_less());
+}
+
+} // namespace std
+
+#endif /* __PSTL_glue_algorithm_impl_H */
diff --git a/pstl/include/pstl/internal/glue_execution_defs.h b/pstl/include/pstl/internal/glue_execution_defs.h
new file mode 100644 (file)
index 0000000..2ab883f
--- /dev/null
@@ -0,0 +1,53 @@
+// -*- C++ -*-
+//===-- glue_execution_defs.h ---------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_glue_execution_defs_H
+#define __PSTL_glue_execution_defs_H
+
+#include <type_traits>
+
+#include "execution_defs.h"
+
+namespace std
+{
+// Type trait
+using __pstl::execution::is_execution_policy;
+#if __PSTL_CPP14_VARIABLE_TEMPLATES_PRESENT
+#if __INTEL_COMPILER
+template <class T>
+constexpr bool is_execution_policy_v = is_execution_policy<T>::value;
+#else
+using __pstl::execution::is_execution_policy_v;
+#endif
+#endif
+
+namespace execution
+{
+// Standard C++ policy classes
+using __pstl::execution::sequenced_policy;
+#if __PSTL_USE_PAR_POLICIES
+using __pstl::execution::parallel_policy;
+using __pstl::execution::parallel_unsequenced_policy;
+#endif
+// Standard predefined policy instances
+using __pstl::execution::seq;
+#if __PSTL_USE_PAR_POLICIES
+using __pstl::execution::par;
+using __pstl::execution::par_unseq;
+#endif
+// Implementation-defined names
+// Unsequenced policy is not yet standard, but for consistency
+// we include it into namespace std::execution as well
+using __pstl::execution::unseq;
+using __pstl::execution::unsequenced_policy;
+} // namespace execution
+} // namespace std
+
+#endif /* __PSTL_glue_execution_defs_H */
diff --git a/pstl/include/pstl/internal/glue_memory_defs.h b/pstl/include/pstl/internal/glue_memory_defs.h
new file mode 100644 (file)
index 0000000..4bea6f6
--- /dev/null
@@ -0,0 +1,80 @@
+// -*- C++ -*-
+//===-- glue_memory_defs.h ------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_glue_memory_defs_H
+#define __PSTL_glue_memory_defs_H
+
+#include "execution_defs.h"
+
+namespace std
+{
+
+// [uninitialized.copy]
+
+template <class _ExecutionPolicy, class _InputIterator, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_copy(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result);
+
+template <class _ExecutionPolicy, class _InputIterator, class _Size, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_copy_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result);
+
+// [uninitialized.move]
+
+template <class _ExecutionPolicy, class _InputIterator, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_move(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result);
+
+template <class _ExecutionPolicy, class _InputIterator, class _Size, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_move_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result);
+
+// [uninitialized.fill]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+uninitialized_fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n, const _Tp& __value);
+
+// [specialized.destroy]
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+destroy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+destroy_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n);
+
+// [uninitialized.construct.default]
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+uninitialized_default_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_default_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n);
+
+// [uninitialized.construct.value]
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+uninitialized_value_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_value_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n);
+
+} //  namespace std
+#endif /* __PSTL_glue_memory_defs_H */
diff --git a/pstl/include/pstl/internal/glue_memory_impl.h b/pstl/include/pstl/internal/glue_memory_impl.h
new file mode 100644 (file)
index 0000000..3cefa31
--- /dev/null
@@ -0,0 +1,357 @@
+// -*- C++ -*-
+//===-- glue_memory_impl.h ------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_glue_memory_impl_H
+#define __PSTL_glue_memory_impl_H
+
+#include "utils.h"
+#include "algorithm_impl.h"
+
+namespace std
+{
+
+// [uninitialized.copy]
+
+template <class _ExecutionPolicy, class _InputIterator, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_copy(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result)
+{
+    typedef typename iterator_traits<_InputIterator>::value_type _ValueType1;
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2;
+    typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2;
+    using namespace __pstl;
+
+    const auto __is_parallel =
+        internal::is_parallelization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
+    const auto __is_vector =
+        internal::is_vectorization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
+
+    return internal::invoke_if_else(
+        std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (),
+        [&]() {
+            return internal::pattern_walk2_brick(
+                std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
+                [__is_vector](_InputIterator __begin, _InputIterator __end, _ForwardIterator __res) {
+                    return internal::brick_copy(__begin, __end, __res, __is_vector);
+                },
+                __is_parallel);
+        },
+        [&]() {
+            return internal::pattern_walk2(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
+                                           [](_ReferenceType1 __val1, _ReferenceType2 __val2) {
+                                               ::new (std::addressof(__val2)) _ValueType2(__val1);
+                                           },
+                                           __is_vector, __is_parallel);
+        });
+}
+
+template <class _ExecutionPolicy, class _InputIterator, class _Size, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_copy_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result)
+{
+    typedef typename iterator_traits<_InputIterator>::value_type _ValueType1;
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2;
+    typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2;
+    using namespace __pstl;
+
+    const auto __is_parallel =
+        internal::is_parallelization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
+    const auto __is_vector =
+        internal::is_vectorization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
+
+    return internal::invoke_if_else(
+        std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (),
+        [&]() {
+            return internal::pattern_walk2_brick_n(
+                std::forward<_ExecutionPolicy>(__exec), __first, __n, __result,
+                [__is_vector](_InputIterator __begin, _Size __sz, _ForwardIterator __res) {
+                    return internal::brick_copy_n(__begin, __sz, __res, __is_vector);
+                },
+                __is_parallel);
+        },
+        [&]() {
+            return internal::pattern_walk2_n(std::forward<_ExecutionPolicy>(__exec), __first, __n, __result,
+                                             [](_ReferenceType1 __val1, _ReferenceType2 __val2) {
+                                                 ::new (std::addressof(__val2)) _ValueType2(__val1);
+                                             },
+                                             __is_vector, __is_parallel);
+        });
+}
+
+// [uninitialized.move]
+
+template <class _ExecutionPolicy, class _InputIterator, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_move(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result)
+{
+    typedef typename iterator_traits<_InputIterator>::value_type _ValueType1;
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2;
+    typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2;
+    using namespace __pstl;
+
+    const auto __is_parallel =
+        internal::is_parallelization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
+    const auto __is_vector =
+        internal::is_vectorization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
+
+    return internal::invoke_if_else(
+        std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (),
+        [&]() {
+            return internal::pattern_walk2_brick(
+                std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
+                [__is_vector](_InputIterator __begin, _InputIterator __end, _ForwardIterator __res) {
+                    return internal::brick_copy(__begin, __end, __res, __is_vector);
+                },
+                __is_parallel);
+        },
+        [&]() {
+            return internal::pattern_walk2(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
+                                           [](_ReferenceType1 __val1, _ReferenceType2 __val2) {
+                                               ::new (std::addressof(__val2)) _ValueType2(std::move(__val1));
+                                           },
+                                           __is_vector, __is_parallel);
+        });
+}
+
+template <class _ExecutionPolicy, class _InputIterator, class _Size, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_move_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result)
+{
+    typedef typename iterator_traits<_InputIterator>::value_type _ValueType1;
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2;
+    typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2;
+    using namespace __pstl;
+
+    const auto __is_parallel =
+        internal::is_parallelization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
+    const auto __is_vector =
+        internal::is_vectorization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
+
+    return internal::invoke_if_else(
+        std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (),
+        [&]() {
+            return internal::pattern_walk2_brick_n(
+                std::forward<_ExecutionPolicy>(__exec), __first, __n, __result,
+                [__is_vector](_InputIterator __begin, _Size __sz, _ForwardIterator __res) {
+                    return internal::brick_copy_n(__begin, __sz, __res, __is_vector);
+                },
+                __is_parallel);
+        },
+        [&]() {
+            return internal::pattern_walk2_n(std::forward<_ExecutionPolicy>(__exec), __first, __n, __result,
+                                             [](_ReferenceType1 __val1, _ReferenceType2 __val2) {
+                                                 ::new (std::addressof(__val2)) _ValueType2(std::move(__val1));
+                                             },
+                                             __is_vector, __is_parallel);
+        });
+}
+
+// [uninitialized.fill]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+uninitialized_fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
+    using namespace __pstl;
+
+    const auto __is_parallel = internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+    const auto __is_vector = internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+
+    internal::invoke_if_else(
+        std::is_arithmetic<_ValueType>(),
+        [&]() {
+            internal::pattern_walk_brick(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                         [&__value, &__is_vector](_ForwardIterator __begin, _ForwardIterator __end) {
+                                             internal::brick_fill(__begin, __end, _ValueType(__value), __is_vector);
+                                         },
+                                         __is_parallel);
+        },
+        [&]() {
+            internal::pattern_walk1(
+                std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                [&__value](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType(__value); }, __is_vector,
+                __is_parallel);
+        });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n, const _Tp& __value)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
+    using namespace __pstl;
+
+    const auto __is_parallel = internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+    const auto __is_vector = internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+
+    return internal::invoke_if_else(
+        std::is_arithmetic<_ValueType>(),
+        [&]() {
+            return internal::pattern_walk_brick_n(std::forward<_ExecutionPolicy>(__exec), __first, __n,
+                                                  [&__value, &__is_vector](_ForwardIterator __begin, _Size __count) {
+                                                      return internal::brick_fill_n(__begin, __count,
+                                                                                    _ValueType(__value), __is_vector);
+                                                  },
+                                                  __is_parallel);
+        },
+        [&]() {
+            return internal::pattern_walk1_n(
+                std::forward<_ExecutionPolicy>(__exec), __first, __n,
+                [&__value](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType(__value); }, __is_vector,
+                __is_parallel);
+        });
+}
+
+// [specialized.destroy]
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+destroy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
+    using namespace __pstl;
+
+    const auto __is_parallel = internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+    const auto __is_vector = internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+
+    internal::invoke_if_not(std::is_trivially_destructible<_ValueType>(), [&]() {
+        internal::pattern_walk1(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                [](_ReferenceType __val) { __val.~_ValueType(); }, __is_vector, __is_parallel);
+    });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+destroy_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
+    using namespace __pstl;
+
+    const auto __is_parallel = internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+    const auto __is_vector = internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+
+    return internal::invoke_if_else(
+        std::is_trivially_destructible<_ValueType>(), [&]() { return std::next(__first, __n); },
+        [&]() {
+            return internal::pattern_walk1_n(std::forward<_ExecutionPolicy>(__exec), __first, __n,
+                                             [](_ReferenceType __val) { __val.~_ValueType(); }, __is_vector,
+                                             __is_parallel);
+        });
+}
+
+// [uninitialized.construct.default]
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+uninitialized_default_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
+    using namespace __pstl;
+
+    const auto __is_parallel = internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+    const auto __is_vector = internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+
+    internal::invoke_if_not(std::is_trivial<_ValueType>(), [&]() {
+        internal::pattern_walk1(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                [](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType; }, __is_vector,
+                                __is_parallel);
+    });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_default_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
+    using namespace __pstl;
+
+    const auto __is_parallel = internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+    const auto __is_vector = internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+
+    return internal::invoke_if_else(std::is_trivial<_ValueType>(), [&]() { return std::next(__first, __n); },
+                                    [&]() {
+                                        return internal::pattern_walk1_n(
+                                            std::forward<_ExecutionPolicy>(__exec), __first, __n,
+                                            [](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType; },
+                                            __is_vector, __is_parallel);
+                                    });
+}
+
+// [uninitialized.construct.value]
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, void>
+uninitialized_value_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
+    using namespace __pstl;
+
+    const auto __is_parallel = internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+    const auto __is_vector = internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+
+    internal::invoke_if_else(
+        std::is_trivial<_ValueType>(),
+        [&]() {
+            internal::pattern_walk_brick(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                         [__is_vector](_ForwardIterator __begin, _ForwardIterator __end) {
+                                             internal::brick_fill(__begin, __end, _ValueType(), __is_vector);
+                                         },
+                                         __is_parallel);
+        },
+        [&]() {
+            internal::pattern_walk1(std::forward<_ExecutionPolicy>(__exec), __first, __last,
+                                    [](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType(); },
+                                    __is_vector, __is_parallel);
+        });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Size>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
+uninitialized_value_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
+    using namespace __pstl;
+
+    const auto __is_parallel = internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+    const auto __is_vector = internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
+
+    return internal::invoke_if_else(
+        std::is_trivial<_ValueType>(),
+        [&]() {
+            return internal::pattern_walk_brick_n(std::forward<_ExecutionPolicy>(__exec), __first, __n,
+                                                  [__is_vector](_ForwardIterator __begin, _Size __count) {
+                                                      return internal::brick_fill_n(__begin, __count, _ValueType(),
+                                                                                    __is_vector);
+                                                  },
+                                                  __is_parallel);
+        },
+        [&]() {
+            return internal::pattern_walk1_n(std::forward<_ExecutionPolicy>(__exec), __first, __n,
+                                             [](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType(); },
+                                             __is_vector, __is_parallel);
+        });
+}
+
+} // namespace std
+
+#endif /* __PSTL_glue_memory_imple_H */
diff --git a/pstl/include/pstl/internal/glue_numeric_defs.h b/pstl/include/pstl/internal/glue_numeric_defs.h
new file mode 100644 (file)
index 0000000..5e26aa2
--- /dev/null
@@ -0,0 +1,116 @@
+// -*- C++ -*-
+//===-- glue_numeric_defs.h -----------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_glue_numeric_defs_H
+#define __PSTL_glue_numeric_defs_H
+
+#include "execution_defs.h"
+
+namespace std
+{
+// [reduce]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _Tp>
+reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init,
+       _BinaryOperation __binary_op);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _Tp>
+reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init);
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, typename iterator_traits<_ForwardIterator>::value_type>
+reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _Tp>
+transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                 _ForwardIterator2 __first2, _Tp __init);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1,
+          class _BinaryOperation2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _Tp>
+transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                 _ForwardIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1,
+                 _BinaryOperation2 __binary_op2);
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _Tp>
+transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init,
+                 _BinaryOperation __binary_op, _UnaryOperation __unary_op);
+
+// [exclusive.scan]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result, _Tp __init);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation>
+_ForwardIterator2
+exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result, _Tp __init, _BinaryOperation __binary_op);
+
+// [inclusive.scan]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result, _BinaryOperation __binary_op);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result, _BinaryOperation __binary_op, _Tp __init);
+
+// [transform.exclusive.scan]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation,
+          class _UnaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+transform_exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                         _ForwardIterator2 __result, _Tp __init, _BinaryOperation __binary_op,
+                         _UnaryOperation __unary_op);
+
+// [transform.inclusive.scan]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation,
+          class _UnaryOperation, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+transform_inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                         _ForwardIterator2 __result, _BinaryOperation __binary_op, _UnaryOperation __unary_op,
+                         _Tp __init);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryOperation,
+          class _BinaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+transform_inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                         _ForwardIterator2 __result, _BinaryOperation __binary_op, _UnaryOperation __unary_op);
+
+// [adjacent.difference]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+adjacent_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                    _ForwardIterator2 __d_first, _BinaryOperation op);
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+adjacent_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                    _ForwardIterator2 __d_first);
+
+} // namespace std
+#endif /* __PSTL_glue_numeric_defs_H */
diff --git a/pstl/include/pstl/internal/glue_numeric_impl.h b/pstl/include/pstl/internal/glue_numeric_impl.h
new file mode 100644 (file)
index 0000000..cd22e79
--- /dev/null
@@ -0,0 +1,224 @@
+// -*- C++ -*-
+//===-- glue_numeric_impl.h -----------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_glue_numeric_impl_H
+#define __PSTL_glue_numeric_impl_H
+
+#include <functional>
+
+#include "utils.h"
+#include "numeric_impl.h"
+
+namespace std
+{
+
+// [reduce]
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _Tp>
+reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init,
+       _BinaryOperation __binary_op)
+{
+    return transform_reduce(std::forward<_ExecutionPolicy>(__exec), __first, __last, __init, __binary_op,
+                            __pstl::internal::no_op());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _Tp>
+reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init)
+{
+    return transform_reduce(std::forward<_ExecutionPolicy>(__exec), __first, __last, __init, std::plus<_Tp>(),
+                            __pstl::internal::no_op());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, typename iterator_traits<_ForwardIterator>::value_type>
+reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
+{
+    typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+    return transform_reduce(std::forward<_ExecutionPolicy>(__exec), __first, __last, _ValueType{},
+                            std::plus<_ValueType>(), __pstl::internal::no_op());
+}
+
+// [transform.reduce]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _Tp>
+transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                 _ForwardIterator2 __first2, _Tp __init)
+{
+    typedef typename iterator_traits<_ForwardIterator1>::value_type _InputType;
+    using namespace __pstl;
+    return internal::pattern_transform_reduce(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __init, std::plus<_InputType>(),
+        std::multiplies<_InputType>(),
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1,
+          class _BinaryOperation2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _Tp>
+transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                 _ForwardIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
+{
+    using namespace __pstl;
+    return internal::pattern_transform_reduce(
+        std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __init, __binary_op1, __binary_op2,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _Tp>
+transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init,
+                 _BinaryOperation __binary_op, _UnaryOperation __unary_op)
+{
+    using namespace __pstl;
+    return internal::pattern_transform_reduce(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __init, __binary_op, __unary_op,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec));
+}
+
+// [exclusive.scan]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result, _Tp __init)
+{
+    return transform_exclusive_scan(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, __init,
+                                    std::plus<_Tp>(), __pstl::internal::no_op());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation>
+_ForwardIterator2
+exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result, _Tp __init, _BinaryOperation __binary_op)
+{
+    return transform_exclusive_scan(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, __init,
+                                    __binary_op, __pstl::internal::no_op());
+}
+
+// [inclusive.scan]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result)
+{
+    typedef typename iterator_traits<_ForwardIterator1>::value_type _InputType;
+    return transform_inclusive_scan(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
+                                    std::plus<_InputType>(), __pstl::internal::no_op());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result, _BinaryOperation __binary_op)
+{
+    return transform_inclusive_scan(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, __binary_op,
+                                    __pstl::internal::no_op());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+               _ForwardIterator2 __result, _BinaryOperation __binary_op, _Tp __init)
+{
+    return transform_inclusive_scan(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, __binary_op,
+                                    __pstl::internal::no_op(), __init);
+}
+
+// [transform.exclusive.scan]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation,
+          class _UnaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+transform_exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                         _ForwardIterator2 __result, _Tp __init, _BinaryOperation __binary_op,
+                         _UnaryOperation __unary_op)
+{
+    using namespace __pstl;
+    return internal::pattern_transform_scan(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, __unary_op, __init, __binary_op,
+        /*inclusive=*/std::false_type(),
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+// [transform.inclusive.scan]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation,
+          class _UnaryOperation, class _Tp>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+transform_inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                         _ForwardIterator2 __result, _BinaryOperation __binary_op, _UnaryOperation __unary_op,
+                         _Tp __init)
+{
+    using namespace __pstl;
+    return internal::pattern_transform_scan(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, __unary_op, __init, __binary_op,
+        /*inclusive=*/std::true_type(),
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryOperation,
+          class _BinaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+transform_inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                         _ForwardIterator2 __result, _BinaryOperation __binary_op, _UnaryOperation __unary_op)
+{
+    if (__first != __last)
+    {
+        auto __tmp = __unary_op(*__first);
+        *__result = __tmp;
+        return transform_inclusive_scan(std::forward<_ExecutionPolicy>(__exec), ++__first, __last, ++__result,
+                                        __binary_op, __unary_op, __tmp);
+    }
+    else
+    {
+        return __result;
+    }
+}
+
+// [adjacent.difference]
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+adjacent_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                    _ForwardIterator2 __d_first, _BinaryOperation __op)
+{
+
+    if (__first == __last)
+        return __d_first;
+
+    using namespace __pstl;
+    return internal::pattern_adjacent_difference(
+        std::forward<_ExecutionPolicy>(__exec), __first, __last, __d_first, __op,
+        internal::is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec),
+        internal::is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator1, _ForwardIterator2>(__exec));
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
+__pstl::internal::enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
+adjacent_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                    _ForwardIterator2 __d_first)
+{
+    typedef typename iterator_traits<_ForwardIterator1>::value_type _ValueType;
+    return adjacent_difference(std::forward<_ExecutionPolicy>(__exec), __first, __last, __d_first,
+                               std::minus<_ValueType>());
+}
+
+} // namespace std
+
+#endif /* __PSTL_glue_numeric_impl_H_ */
diff --git a/pstl/include/pstl/internal/memory_impl.h b/pstl/include/pstl/internal/memory_impl.h
new file mode 100644 (file)
index 0000000..c0a0b98
--- /dev/null
@@ -0,0 +1,57 @@
+// -*- C++ -*-
+//===-- memory_impl.h -----------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_memory_impl_H
+#define __PSTL_memory_impl_H
+
+#include <iterator>
+
+#include "unseq_backend_simd.h"
+
+namespace __pstl
+{
+namespace internal
+{
+
+//------------------------------------------------------------------------
+// uninitialized_move
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class _OutputIterator>
+_OutputIterator
+brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+                         /*vector=*/std::false_type) noexcept
+{
+    typedef typename std::iterator_traits<_OutputIterator>::value_type _ValueType2;
+    for (; __first != __last; ++__first, ++__result)
+    {
+        ::new (std::addressof(*__result)) _ValueType2(std::move(*__first));
+    }
+    return __result;
+}
+
+template <class _ForwardIterator, class _OutputIterator>
+_OutputIterator
+brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+                         /*vector=*/std::true_type) noexcept
+{
+    typedef typename std::iterator_traits<_OutputIterator>::value_type __ValueType2;
+    typedef typename std::iterator_traits<_ForwardIterator>::reference _ReferenceType1;
+    typedef typename std::iterator_traits<_OutputIterator>::reference _ReferenceType2;
+
+    return unseq_backend::simd_walk_2(
+        __first, __last - __first, __result,
+        [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType2(std::move(__x)); });
+}
+
+} // namespace internal
+} // namespace __pstl
+
+#endif /* __PSTL_memory_impl_H */
diff --git a/pstl/include/pstl/internal/numeric_impl.h b/pstl/include/pstl/internal/numeric_impl.h
new file mode 100644 (file)
index 0000000..001761c
--- /dev/null
@@ -0,0 +1,359 @@
+// -*- C++ -*-
+//===-- numeric_impl.h ----------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __PSTL_numeric_impl_H
+#define __PSTL_numeric_impl_H
+
+#include <iterator>
+#include <type_traits>
+#include <numeric>
+
+#include "pstl_config.h"
+#include "execution_impl.h"
+#include "unseq_backend_simd.h"
+
+#if __PSTL_USE_PAR_POLICIES
+#include "parallel_backend.h"
+#endif
+
+namespace __pstl
+{
+namespace internal
+{
+
+//------------------------------------------------------------------------
+// transform_reduce (version with two binary functions, according to draft N4659)
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
+_Tp
+brick_transform_reduce(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _Tp __init,
+                       _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2,
+                       /*is_vector=*/std::false_type) noexcept
+{
+    return std::inner_product(__first1, __last1, __first2, __init, __binary_op1, __binary_op2);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
+_Tp
+brick_transform_reduce(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _Tp __init,
+                       _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2,
+                       /*is_vector=*/std::true_type) noexcept
+{
+    typedef typename std::iterator_traits<_ForwardIterator1>::difference_type _DifferenceType;
+    return unseq_backend::simd_transform_reduce(
+        __last1 - __first1, __init, __binary_op1,
+        [=, &__binary_op2](_DifferenceType __i) { return __binary_op2(__first1[__i], __first2[__i]); });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1,
+          class _BinaryOperation2, class _IsVector>
+_Tp
+pattern_transform_reduce(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
+                         _ForwardIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1,
+                         _BinaryOperation2 __binary_op2, _IsVector __is_vector,
+                         /*is_parallel=*/std::false_type) noexcept
+{
+    return brick_transform_reduce(__first1, __last1, __first2, __init, __binary_op1, __binary_op2, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, class _Tp,
+          class _BinaryOperation1, class _BinaryOperation2, class _IsVector>
+_Tp
+pattern_transform_reduce(_ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
+                         _RandomAccessIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1,
+                         _BinaryOperation2 __binary_op2, _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+    return internal::except_handler([&]() {
+        return par_backend::parallel_transform_reduce(
+            std::forward<_ExecutionPolicy>(__exec), __first1, __last1,
+            [__first1, __first2, __binary_op2](_RandomAccessIterator1 __i) mutable {
+                return __binary_op2(*__i, *(__first2 + (__i - __first1)));
+            },
+            __init,
+            __binary_op1, // Combine
+            [__first1, __first2, __binary_op1, __binary_op2,
+             __is_vector](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j, _Tp __init) -> _Tp {
+                return internal::brick_transform_reduce(__i, __j, __first2 + (__i - __first1), __init, __binary_op1,
+                                                        __binary_op2, __is_vector);
+            });
+    });
+}
+
+//------------------------------------------------------------------------
+// transform_reduce (version with unary and binary functions)
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation>
+_Tp
+brick_transform_reduce(_ForwardIterator __first, _ForwardIterator __last, _Tp __init, _BinaryOperation __binary_op,
+                       _UnaryOperation __unary_op, /*is_vector=*/std::false_type) noexcept
+{
+    for (; __first != __last; ++__first)
+    {
+        __init = __binary_op(__init, __unary_op(*__first));
+    }
+    return __init;
+}
+
+template <class _ForwardIterator, class _Tp, class _UnaryOperation, class _BinaryOperation>
+_Tp
+brick_transform_reduce(_ForwardIterator __first, _ForwardIterator __last, _Tp __init, _BinaryOperation __binary_op,
+                       _UnaryOperation __unary_op, /*is_vector=*/std::true_type) noexcept
+{
+    typedef typename std::iterator_traits<_ForwardIterator>::difference_type _DifferenceType;
+    return unseq_backend::simd_transform_reduce(
+        __last - __first, __init, __binary_op,
+        [=, &__unary_op](_DifferenceType __i) { return __unary_op(__first[__i]); });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation,
+          class _IsVector>
+_Tp
+pattern_transform_reduce(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Tp __init,
+                         _BinaryOperation __binary_op, _UnaryOperation __unary_op, _IsVector __is_vector,
+                         /*is_parallel=*/std::false_type) noexcept
+{
+    return brick_transform_reduce(__first, __last, __init, __binary_op, __unary_op, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation,
+          class _IsVector>
+_Tp
+pattern_transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init,
+                         _BinaryOperation __binary_op, _UnaryOperation __unary_op, _IsVector __is_vector,
+                         /*is_parallel=*/std::true_type)
+{
+    return except_handler([&]() {
+        return par_backend::parallel_transform_reduce(
+            std::forward<_ExecutionPolicy>(__exec), __first, __last,
+            [__unary_op](_ForwardIterator __i) mutable { return __unary_op(*__i); }, __init, __binary_op,
+            [__unary_op, __binary_op, __is_vector](_ForwardIterator __i, _ForwardIterator __j, _Tp __init) {
+                return brick_transform_reduce(__i, __j, __init, __binary_op, __unary_op, __is_vector);
+            });
+    });
+}
+
+//------------------------------------------------------------------------
+// transform_exclusive_scan
+//
+// walk3 evaluates f(x,y,z) for (x,y,z) drawn from [first1,last1), [first2,...), [first3,...)
+//------------------------------------------------------------------------
+
+// Exclusive form
+template <class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation>
+std::pair<_OutputIterator, _Tp>
+brick_transform_scan(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+                     _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op,
+                     /*Inclusive*/ std::false_type, /*is_vector=*/std::false_type) noexcept
+{
+    for (; __first != __last; ++__first, ++__result)
+    {
+        *__result = __init;
+        __PSTL_PRAGMA_FORCEINLINE
+        __init = __binary_op(__init, __unary_op(*__first));
+    }
+    return std::make_pair(__result, __init);
+}
+
+// Inclusive form
+template <class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation>
+std::pair<_OutputIterator, _Tp>
+brick_transform_scan(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+                     _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op,
+                     /*Inclusive*/ std::true_type, /*is_vector=*/std::false_type) noexcept
+{
+    for (; __first != __last; ++__first, ++__result)
+    {
+        __PSTL_PRAGMA_FORCEINLINE
+        __init = __binary_op(__init, __unary_op(*__first));
+        *__result = __init;
+    }
+    return std::make_pair(__result, __init);
+}
+
+// type is arithmetic and binary operation is a user defined operation.
+template <typename _Tp, typename _BinaryOperation>
+using is_arithmetic_udop = std::integral_constant<bool, std::is_arithmetic<_Tp>::value &&
+                                                            !std::is_same<_BinaryOperation, std::plus<_Tp>>::value>;
+
+// [restriction] - T shall be DefaultConstructible.
+// [violation] - default ctor of T shall set the identity value for binary_op.
+template <class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation,
+          class _Inclusive>
+typename std::enable_if<!is_arithmetic_udop<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type
+brick_transform_scan(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+                     _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op, _Inclusive,
+                     /*is_vector=*/std::true_type) noexcept
+{
+#if (__PSTL_UDS_PRESENT)
+    return unseq_backend::simd_scan(__first, __last - __first, __result, __unary_op, __init, __binary_op, _Inclusive());
+#else
+    // We need to call serial brick here to call function for inclusive and exclusive scan that depends on _Inclusive() value
+    return brick_transform_scan(__first, __last, __result, __unary_op, __init, __binary_op, _Inclusive(),
+                                /*is_vector=*/std::false_type());
+#endif
+}
+
+template <class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation,
+          class _Inclusive>
+typename std::enable_if<is_arithmetic_udop<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type
+brick_transform_scan(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+                     _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op, _Inclusive,
+                     /*is_vector=*/std::true_type) noexcept
+{
+    return brick_transform_scan(__first, __last, __result, __unary_op, __init, __binary_op, _Inclusive(),
+                                /*is_vector=*/std::false_type());
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp,
+          class _BinaryOperation, class _Inclusive, class _IsVector>
+_OutputIterator
+pattern_transform_scan(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
+                       _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op, _Inclusive,
+                       _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
+{
+    return internal::brick_transform_scan(__first, __last, __result, __unary_op, __init, __binary_op, _Inclusive(),
+                                          __is_vector)
+        .first;
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, class _UnaryOperation, class _Tp,
+          class _BinaryOperation, class _Inclusive, class _IsVector>
+typename std::enable_if<!std::is_floating_point<_Tp>::value, _OutputIterator>::type
+pattern_transform_scan(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last,
+                       _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op,
+                       _Inclusive, _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+    typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType;
+
+    return internal::except_handler([&]() {
+        par_backend::parallel_transform_scan(
+            std::forward<_ExecutionPolicy>(__exec), __last - __first,
+            [__first, __unary_op](_DifferenceType __i) mutable { return __unary_op(__first[__i]); }, __init,
+            __binary_op,
+            [__first, __unary_op, __binary_op, __is_vector](_DifferenceType __i, _DifferenceType __j, _Tp __init) {
+                // Execute serial brick_transform_reduce, due to the explicit SIMD vectorization (reduction) requires a commutative operation for the guarantee of correct scan.
+                return internal::brick_transform_reduce(__first + __i, __first + __j, __init, __binary_op, __unary_op,
+                                                        /*__is_vector*/ std::false_type());
+            },
+            [__first, __unary_op, __binary_op, __result, __is_vector](_DifferenceType __i, _DifferenceType __j,
+                                                                      _Tp __init) {
+                return internal::brick_transform_scan(__first + __i, __first + __j, __result + __i, __unary_op, __init,
+                                                      __binary_op, _Inclusive(), __is_vector)
+                    .second;
+            });
+        return __result + (__last - __first);
+    });
+}
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, class _UnaryOperation, class _Tp,
+          class _BinaryOperation, class _Inclusive, class _IsVector>
+typename std::enable_if<std::is_floating_point<_Tp>::value, _OutputIterator>::type
+pattern_transform_scan(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last,
+                       _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op,
+                       _Inclusive, _IsVector __is_vector, /*is_parallel=*/std::true_type)
+{
+    typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType;
+    _DifferenceType __n = __last - __first;
+
+    if (__n <= 0)
+    {
+        return __result;
+    }
+    return except_handler([&]() {
+        par_backend::parallel_strict_scan(
+            std::forward<_ExecutionPolicy>(__exec), __n, __init,
+            [__first, __unary_op, __binary_op, __result, __is_vector](_DifferenceType __i, _DifferenceType __len) {
+                return brick_transform_scan(__first + __i, __first + (__i + __len), __result + __i, __unary_op, _Tp{},
+                                            __binary_op, _Inclusive(), __is_vector)
+                    .second;
+            },
+            __binary_op,
+            [__result, &__binary_op](_DifferenceType __i, _DifferenceType __len, _Tp __initial) {
+                return *(std::transform(__result + __i, __result + __i + __len, __result + __i,
+                                        [&__initial, &__binary_op](const _Tp& __x) {
+                                            __PSTL_PRAGMA_FORCEINLINE
+                                            return __binary_op(__initial, __x);
+                                        }) -
+                         1);
+            },
+            [](_Tp __res) {});
+        return __result + (__last - __first);
+    });
+}
+
+//------------------------------------------------------------------------
+// adjacent_difference
+//------------------------------------------------------------------------
+
+template <class _ForwardIterator, class _OutputIterator, class _BinaryOperation>
+_OutputIterator
+brick_adjacent_difference(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __d_first,
+                          _BinaryOperation __op, /*is_vector*/ std::false_type) noexcept
+{
+    return std::adjacent_difference(__first, __last, __d_first, __op);
+}
+
+template <class _ForwardIterator1, class _ForwardIterator2, class BinaryOperation>
+_ForwardIterator2
+brick_adjacent_difference(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first,
+                          BinaryOperation __op, /*is_vector=*/std::true_type) noexcept
+{
+    assert(__first != __last);
+
+    typedef typename std::iterator_traits<_ForwardIterator1>::reference _ReferenceType1;
+    typedef typename std::iterator_traits<_ForwardIterator2>::reference _ReferenceType2;
+
+    auto __n = __last - __first;
+    *__d_first = *__first;
+    return unseq_backend::simd_walk_3(
+        __first + 1, __n - 1, __first, __d_first + 1,
+        [&__op](_ReferenceType1 __x, _ReferenceType1 __y, _ReferenceType2 __z) { __z = __op(__x, __y); });
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _BinaryOperation,
+          class _IsVector>
+_OutputIterator
+pattern_adjacent_difference(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last,
+                            _OutputIterator __d_first, _BinaryOperation __op, _IsVector __is_vector,
+                            /*is_parallel*/ std::false_type) noexcept
+{
+    return internal::brick_adjacent_difference(__first, __last, __d_first, __op, __is_vector);
+}
+
+template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation,
+          class _IsVector>
+_ForwardIterator2
+pattern_adjacent_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
+                            _ForwardIterator2 __d_first, _BinaryOperation __op, _IsVector __is_vector,
+                            /*is_parallel=*/std::true_type)
+{
+    assert(__first != __last);
+    typedef typename std::iterator_traits<_ForwardIterator1>::reference _ReferenceType1;
+    typedef typename std::iterator_traits<_ForwardIterator2>::reference _ReferenceType2;
+
+    *__d_first = *__first;
+    par_backend::parallel_for(std::forward<_ExecutionPolicy>(__exec), __first, __last - 1,
+                              [&__op, __is_vector, __d_first, __first](_ForwardIterator1 _