GEOS  3.13.0dev
Clusters.h
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  std::size_t getNumClusters() const {
41  return m_starts.size();
42  }
43 
44  // Get the size of a given cluster
45  std::size_t getSize(std::size_t cluster) const {
46  return static_cast<std::size_t>(std::distance(begin(cluster), end(cluster)));
47  }
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  const_iterator begin(std::size_t cluster) const {
54  return std::next(m_elemsInCluster.begin(), static_cast<std::ptrdiff_t>(m_starts[cluster]));
55  }
56 
57  // Get an iterator beyond the last element in a given cluster
58  const_iterator end(std::size_t cluster) const {
59  if (cluster == static_cast<std::size_t>(m_starts.size() - 1)) {
60  return m_elemsInCluster.end();
61  }
62 
63  return begin(cluster + 1);
64  }
65 
66 };
67 
68 }
69 }
70 }
71 
72 #endif
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25