GEOS 3.14.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#pragma once
16
17#include <geos/export.h>
18#include <limits>
19#include <vector>
20
21namespace geos {
22namespace operation {
23namespace cluster {
24
25class UnionFind;
26
27class GEOS_DLL Clusters {
28private:
29 std::vector<std::size_t> m_elemsInCluster; // The IDs of elements that are included in a cluster
30 std::vector<std::size_t> m_starts; // Start position of each cluster in m_elemsInCluster
31 std::size_t m_numElems; // The number of elements from which clusters were generated
32
33public:
34 using const_iterator = decltype(m_elemsInCluster)::const_iterator;
35
36 explicit Clusters(UnionFind & uf, std::vector<std::size_t> elemsInCluster, std::size_t numElems);
37
38 // Get the number of clusters available
39 std::size_t getNumClusters() const {
40 return m_starts.size();
41 }
42
43 // Get the size of a given cluster
44 std::size_t getSize(std::size_t cluster) const {
45 return static_cast<std::size_t>(std::distance(begin(cluster), end(cluster)));
46 }
47
48 // Get a vector containing the cluster ID for each item in `elems`
49 std::vector<std::size_t> getClusterIds(std::size_t noClusterValue = std::numeric_limits<std::size_t>::max()) const;
50
51 // Get an iterator to the first element in a given cluster
52 const_iterator begin(std::size_t cluster) const {
53 return std::next(m_elemsInCluster.begin(), static_cast<std::ptrdiff_t>(m_starts[cluster]));
54 }
55
56 // Get an iterator beyond the last element in a given cluster
57 const_iterator end(std::size_t cluster) const {
58 if (cluster == static_cast<std::size_t>(m_starts.size() - 1)) {
59 return m_elemsInCluster.end();
60 }
61
62 return begin(cluster + 1);
63 }
64
65};
66
67}
68}
69}
Basic namespace for all GEOS functionalities.
Definition geos.h:39