1#ifndef WEIGHTEDLOWESS_SORTBY_HPP
2#define WEIGHTEDLOWESS_SORTBY_HPP
9#include <initializer_list>
29 std::vector<size_t> my_permutation;
30 bool my_sorted =
true;
38 template<
typename Sortable_>
39 SortBy(
size_t num_points,
const Sortable_* x) {
54 template<
typename Sortable_>
55 void set(
size_t num_points,
const Sortable_* x) {
57 my_sorted = std::is_sorted(x, x + num_points);
59 my_permutation.resize(num_points);
60 std::iota(my_permutation.begin(), my_permutation.end(),
static_cast<size_t>(0));
61 std::sort(my_permutation.begin(), my_permutation.end(), [&](
size_t left,
size_t right) ->
bool { return x[left] < x[right]; });
67 template<
typename AllData_,
typename Used_>
68 void permute_raw(AllData_& data, Used_* work)
const {
73 size_t num_points = my_permutation.size();
74 std::fill_n(work, num_points, 0);
77 for (
size_t i = 0; i < num_points; ++i) {
83 size_t current = i, replacement = my_permutation[i];
84 while (replacement != i) {
85 if constexpr(std::is_pointer<AllData_>::value) {
86 std::swap(data[current], data[replacement]);
89 std::swap(d[current], d[replacement]);
92 current = replacement;
93 work[replacement] = 1;
94 replacement = my_permutation[replacement];
108 template<
typename Data_,
typename Used_>
109 void permute(Data_* data, Used_* work)
const {
110 permute_raw(data, work);
121 template<
typename Data_,
typename Used_>
122 void permute(std::initializer_list<Data_> data, Used_* work)
const {
123 permute_raw(data, work);
134 template<
typename DataPo
inters_,
typename Used_>
135 void permute(DataPointers_ data, Used_* work)
const {
136 permute_raw(data, work);
147 template<
typename Data_,
typename Used_>
148 void permute(Data_* data, std::vector<Used_>& work)
const {
149 size_t num_points = my_permutation.size();
150 work.resize(num_points);
162 template<
typename Data_,
typename Used_>
163 void permute(std::initializer_list<Data_> data, std::vector<Used_>& work)
const {
164 size_t num_points = my_permutation.size();
165 work.resize(num_points);
177 template<
typename DataPo
inters_,
typename Used_>
178 void permute(DataPointers_ data, std::vector<Used_>& work)
const {
179 size_t num_points = my_permutation.size();
180 work.resize(num_points);
185 template<
typename AllData_,
typename Used_>
186 void unpermute_raw(AllData_& data, Used_* work)
const {
191 size_t num_points = my_permutation.size();
192 std::fill_n(work, num_points, 0);
194 for (
size_t i = 0; i < num_points; ++i) {
200 size_t replacement = my_permutation[i];
201 while (replacement != i) {
202 if constexpr(std::is_pointer<AllData_>::value) {
203 std::swap(data[i], data[replacement]);
205 for (
auto d : data) {
206 std::swap(d[i], d[replacement]);
209 work[replacement] = 1;
210 replacement = my_permutation[replacement];
223 template<
typename Data_,
typename Used_>
225 unpermute_raw(data, work);
235 template<
typename Data_,
typename Used_>
236 void unpermute(std::initializer_list<Data_*> data, Used_* work)
const {
237 unpermute_raw(data, work);
247 template<
typename DataPo
inters_,
typename Used_>
249 unpermute_raw(data, work);
259 template<
typename Data_,
typename Used_>
260 void unpermute(Data_* data, std::vector<Used_>& work)
const {
261 size_t num_points = my_permutation.size();
262 work.resize(num_points);
273 template<
typename Data_,
typename Used_>
274 void unpermute(std::initializer_list<Data_*> data, std::vector<Used_>& work)
const {
275 size_t num_points = my_permutation.size();
276 work.resize(num_points);
287 template<
typename DataPo
inters_,
typename Used_>
288 void unpermute(DataPointers_ data, std::vector<Used_>& work)
const {
289 size_t num_points = my_permutation.size();
290 work.resize(num_points);
Utility class for sorting on a covariate.
Definition SortBy.hpp:27
void unpermute(Data_ *data, std::vector< Used_ > &work) const
Definition SortBy.hpp:260
void unpermute(Data_ *data, Used_ *work) const
Definition SortBy.hpp:224
void unpermute(std::initializer_list< Data_ * > data, std::vector< Used_ > &work) const
Definition SortBy.hpp:274
void unpermute(DataPointers_ data, Used_ *work) const
Definition SortBy.hpp:248
void permute(std::initializer_list< Data_ > data, std::vector< Used_ > &work) const
Definition SortBy.hpp:163
void permute(std::initializer_list< Data_ > data, Used_ *work) const
Definition SortBy.hpp:122
void unpermute(std::initializer_list< Data_ * > data, Used_ *work) const
Definition SortBy.hpp:236
void permute(Data_ *data, std::vector< Used_ > &work) const
Definition SortBy.hpp:148
void permute(DataPointers_ data, std::vector< Used_ > &work) const
Definition SortBy.hpp:178
SortBy(size_t num_points, const Sortable_ *x)
Definition SortBy.hpp:39
void permute(DataPointers_ data, Used_ *work) const
Definition SortBy.hpp:135
void permute(Data_ *data, Used_ *work) const
Definition SortBy.hpp:109
void unpermute(DataPointers_ data, std::vector< Used_ > &work) const
Definition SortBy.hpp:288
void set(size_t num_points, const Sortable_ *x)
Definition SortBy.hpp:55
Namespace for LOWESS functions.
Definition compute.hpp:14