Stride Reference Manual  - generated for commit 9643b11
Median.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, 2019, Jan Broeckhove and Bistromatics group.
14  */
15 
16 #pragma once
17 
18 #include <algorithm>
19 #include <cstddef>
20 #include <memory>
21 #include <utility>
22 #include <vector>
23 
24 namespace geopop {
25 namespace kd {
26 
27 template <typename P, std::size_t D>
28 std::size_t Median(const std::vector<P>& points)
29 {
30  if (points.empty())
31  return 0;
32 
33  using C = std::pair<decltype(points[0].template Get<D>()), std::size_t>;
34  std::vector<C> sorting;
35  for (std::size_t i = 0; i < points.size(); i++) {
36  sorting.emplace_back(points[i].template Get<D>(), i);
37  }
38  std::sort(sorting.begin(), sorting.end());
39  return sorting[sorting.size() / 2].second;
40 }
41 
42 } // namespace kd
43 } // namespace geopop
std::size_t Median(const std::vector< P > &points)
Definition: Median.h:28
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21