/src/geos/include/geos/operation/cluster/Clusters.h
Line | Count | Source (jump to first uncovered line) |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (C) 2021-2022 Daniel Baston |
7 | | * |
8 | | * This is free software; you can redistribute and/or modify it under |
9 | | * the terms of the GNU Lesser General Public Licence as published |
10 | | * by the Free Software Foundation. |
11 | | * See the COPYING file for more information. |
12 | | * |
13 | | **********************************************************************/ |
14 | | |
15 | | #ifndef GEOS_OPERATION_CLUSTER_CLUSTERS |
16 | | #define GEOS_OPERATION_CLUSTER_CLUSTERS |
17 | | |
18 | | #include <geos/export.h> |
19 | | #include <limits> |
20 | | #include <vector> |
21 | | |
22 | | namespace geos { |
23 | | namespace operation { |
24 | | namespace cluster { |
25 | | |
26 | | class UnionFind; |
27 | | |
28 | | class GEOS_DLL Clusters { |
29 | | private: |
30 | | std::vector<std::size_t> m_elemsInCluster; // The IDs of elements that are included in a cluster |
31 | | std::vector<std::size_t> m_starts; // Start position of each cluster in m_elemsInCluster |
32 | | std::size_t m_numElems; // The number of elements from which clusters were generated |
33 | | |
34 | | public: |
35 | | using const_iterator = decltype(m_elemsInCluster)::const_iterator; |
36 | | |
37 | | explicit Clusters(UnionFind & uf, std::vector<std::size_t> elemsInCluster, std::size_t numElems); |
38 | | |
39 | | // Get the number of clusters available |
40 | 0 | std::size_t getNumClusters() const { |
41 | 0 | return m_starts.size(); |
42 | 0 | } |
43 | | |
44 | | // Get the size of a given cluster |
45 | 0 | std::size_t getSize(std::size_t cluster) const { |
46 | 0 | return static_cast<std::size_t>(std::distance(begin(cluster), end(cluster))); |
47 | 0 | } |
48 | | |
49 | | // Get a vector containing the cluster ID for each item in `elems` |
50 | | std::vector<std::size_t> getClusterIds(std::size_t noClusterValue = std::numeric_limits<std::size_t>::max()) const; |
51 | | |
52 | | // Get an iterator to the first element in a given cluster |
53 | 0 | const_iterator begin(std::size_t cluster) const { |
54 | 0 | return std::next(m_elemsInCluster.begin(), static_cast<std::ptrdiff_t>(m_starts[cluster])); |
55 | 0 | } |
56 | | |
57 | | // Get an iterator beyond the last element in a given cluster |
58 | 0 | const_iterator end(std::size_t cluster) const { |
59 | 0 | if (cluster == static_cast<std::size_t>(m_starts.size() - 1)) { |
60 | 0 | return m_elemsInCluster.end(); |
61 | 0 | } |
62 | | |
63 | 0 | return begin(cluster + 1); |
64 | 0 | } |
65 | | |
66 | | }; |
67 | | |
68 | | } |
69 | | } |
70 | | } |
71 | | |
72 | | #endif |