16 inline static size_t ArgMaxMT(
const std::vector<VAL_T>& array) {
21 num_threads = omp_get_num_threads();
23 int step = std::max(1, (
static_cast<int>(array.size()) + num_threads - 1) / num_threads);
24 std::vector<size_t> arg_maxs(num_threads, 0);
25 #pragma omp parallel for schedule(static, 1)
26 for (
int i = 0; i < num_threads; ++i) {
27 size_t start = step * i;
28 if (start >= array.size()) {
continue; }
29 size_t end = std::min(array.size(), start + step);
30 size_t arg_max = start;
31 for (
size_t j = start + 1; j < end; ++j) {
32 if (array[j] > array[arg_max]) {
36 arg_maxs[i] = arg_max;
38 size_t ret = arg_maxs[0];
39 for (
int i = 1; i < num_threads; ++i) {
40 if (array[arg_maxs[i]] > array[ret]) {
46 inline static size_t ArgMax(
const std::vector<VAL_T>& array) {
50 if (array.size() > 1024) {
51 return ArgMaxMT(array);
54 for (
size_t i = 1; i < array.size(); ++i) {
55 if (array[i] > array[arg_max]) {
63 inline static size_t ArgMin(
const std::vector<VAL_T>& array) {
68 for (
size_t i = 1; i < array.size(); ++i) {
69 if (array[i] < array[arg_min]) {
76 inline static size_t ArgMax(
const VAL_T* array,
size_t n) {
81 for (
size_t i = 1; i < n; ++i) {
82 if (array[i] > array[arg_max]) {
89 inline static size_t ArgMin(
const VAL_T* array,
size_t n) {
94 for (
size_t i = 1; i < n; ++i) {
95 if (array[i] < array[arg_min]) {
102 inline static void Partition(std::vector<VAL_T>* arr,
int start,
int end,
int* l,
int* r) {
110 std::vector<VAL_T>& ref = *arr;
111 VAL_T v = ref[end - 1];
113 while (ref[++i] > v);
114 while (v > ref[--j]) {
if (j == start) {
break; } }
115 if (i >= j) {
break; }
117 if (ref[i] == v) { p++;
std::swap(ref[p], ref[i]); }
118 if (v == ref[j]) { q--;
std::swap(ref[j], ref[q]); }
123 for (
int k = start; k <= p; k++, j--) {
std::swap(ref[k], ref[j]); }
124 for (
int k = end - 2; k >= q; k--, i++) {
std::swap(ref[i], ref[k]); }
130 inline static int ArgMaxAtK(std::vector<VAL_T>* arr,
int start,
int end,
int k) {
131 if (start >= end - 1) {
136 Partition(arr, start, end, &l, &r);
138 if ((k > l && k < r) || (l == start - 1 && r == end - 1)) {
141 return ArgMaxAtK(arr, start, l + 1, k);
143 return ArgMaxAtK(arr, r, end, k);
148 inline static void MaxK(
const std::vector<VAL_T>& array,
int k, std::vector<VAL_T>* out) {
153 for (
auto val : array) {
156 if (
static_cast<size_t>(k) >= array.size()) {
159 ArgMaxAtK(out, 0,
static_cast<int>(out->size()), k - 1);
160 out->erase(out->begin() + k, out->end());
163 inline static void Assign(std::vector<VAL_T>* array, VAL_T t,
size_t n) {
165 for (
size_t i = 0; i < array->size(); ++i) {
170 inline static bool CheckAllZero(
const std::vector<VAL_T>& array) {
171 for (
size_t i = 0; i < array.size(); ++i) {
172 if (array[i] != VAL_T(0)) {
179 inline static bool CheckAll(
const std::vector<VAL_T>& array, VAL_T t) {
180 for (
size_t i = 0; i < array.size(); ++i) {
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.hpp:24418