Skip to content

Commit 973b08e

Browse files
committed
tidy utils cpp code
1 parent 3f38673 commit 973b08e

File tree

4 files changed

+48
-56
lines changed

4 files changed

+48
-56
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: spatialcluster
22
Title: R port of redcap
3-
Version: 0.2.0.013
3+
Version: 0.2.0.014
44
Authors@R:
55
person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre"))
66
Description: R port of redcap (Regionalization with dynamically

codemeta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"codeRepository": "https://github.com/mpadge/spatialcluster",
88
"issueTracker": "https://github.com/mpadge/spatialcluster/issues",
99
"license": "https://spdx.org/licenses/GPL-3.0",
10-
"version": "0.2.0.013",
10+
"version": "0.2.0.014",
1111
"programmingLanguage": {
1212
"@type": "ComputerLanguage",
1313
"name": "R",

src/utils.cpp

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
* unordered_set of target indices for each cluster.
1818
*/
1919

20-
bool utils::strfound (const std::string str, const std::string target)
21-
{
20+
bool utils::strfound (const std::string str, const std::string target) {
2221
bool found = false;
2322
if (str.find (target) != std::string::npos)
2423
found = true;
@@ -31,40 +30,36 @@ size_t utils::sets_init (
3130
int2indx_map_t &vert2index_map,
3231
indx2int_map_t &index2vert_map,
3332
indx2int_map_t &index2cl_map,
34-
int2indxset_map_t &cl2index_map)
35-
{
33+
int2indxset_map_t &cl2index_map) {
3634
vert2index_map.clear ();
3735
index2vert_map.clear ();
3836
index2cl_map.clear ();
3937
cl2index_map.clear ();
4038

4139
intset_t vert_set;
42-
for (int i = 0; i < from.size (); i++)
43-
{
40+
for (int i = 0; i < from.size (); i++) {
4441
vert_set.emplace (from [i]);
4542
vert_set.emplace (to [i]);
4643
}
4744
int idx = 0;
48-
for (auto v: vert_set)
49-
{
45+
for (auto v: vert_set) {
5046
index2vert_map.emplace (idx, v);
5147
vert2index_map.emplace (v, idx++);
5248
}
5349

54-
for (int i = 0; i < from.length (); i++)
55-
{
50+
for (int i = 0; i < from.length (); i++) {
5651
size_t fi = vert2index_map.at (from [i]);
5752
indxset_t eset; // only one index for each vert at this stage
5853
eset.insert (fi);
5954
cl2index_map.emplace (fi, eset);
6055
}
61-
for (auto v: vert_set)
62-
{
56+
for (auto v: vert_set) {
6357
// all verts are their own clusters, so cast indx to cli
6458
int cli = static_cast <int> (vert2index_map.at (v));
6559
indxset_t eset;
66-
if (cl2index_map.find (cli) != cl2index_map.end ())
60+
if (cl2index_map.find (cli) != cl2index_map.end ()) {
6761
eset = cl2index_map.at (cli);
62+
}
6863
eset.emplace (cli);
6964
cl2index_map.emplace (cli, eset);
7065
index2cl_map.emplace (static_cast <size_t> (cli), cli);
@@ -90,60 +85,60 @@ size_t utils::find_shortest_connection (
9085
const int2indxset_map_t &cl2index_map,
9186
const int cfrom,
9287
const int cto,
93-
const bool shortest)
94-
{
95-
if (cl2index_map.find (cfrom) == cl2index_map.end ())
88+
const bool shortest) {
89+
if (cl2index_map.find (cfrom) == cl2index_map.end ()) {
9690
Rcpp::stop ("cluster index not found");
97-
if (cl2index_map.find (cto) == cl2index_map.end ())
91+
}
92+
if (cl2index_map.find (cto) == cl2index_map.end ()) {
9893
Rcpp::stop ("cluster index not found");
94+
}
9995

10096
indxset_t index_i = cl2index_map.at (cfrom),
10197
index_j = cl2index_map.at (cto);
10298

10399
double dlim = INFINITE_DOUBLE;
104-
if (!shortest)
100+
if (!shortest) {
105101
dlim = -dlim;
102+
}
106103
size_t short_i = INFINITE_INT, short_j = INFINITE_INT;
107104

108105
// from and to here are directional, so need to examine both directions
109-
for (auto i: index_i)
110-
for (auto j: index_j)
111-
{
106+
for (auto i: index_i) {
107+
for (auto j: index_j) {
112108
arma::uword ia = static_cast <arma::uword> (i),
113109
ja = static_cast <arma::uword> (j);
114110
if ((shortest && d_mat (ia, ja) < dlim) ||
115-
(!shortest && d_mat (ia, ja) > dlim))
116-
{
111+
(!shortest && d_mat (ia, ja) > dlim)) {
117112
dlim = d_mat (ia, ja);
118113
short_i = i;
119114
short_j = j;
120115
} else if ((shortest && d_mat (ja, ia) < dlim) ||
121-
(!shortest && d_mat (ja, ia) > dlim))
122-
{
116+
(!shortest && d_mat (ja, ia) > dlim)) {
123117
dlim = d_mat (ja, ia);
124118
short_i = j;
125119
short_j = i;
126120
}
127121
}
128-
if (dlim == INFINITE_DOUBLE)
122+
}
123+
if (dlim == INFINITE_DOUBLE) {
129124
Rcpp::stop ("no minimal distance; this should not happen");
125+
}
130126

131127
// convert short_i and short_j to a single edge
132128
// TODO: Make a std::map of vert2dist to avoid this loop
133129
size_t shortest_edge = INFINITE_INT;
134-
for (int i = 0; i < from.length (); i++) // int for Rcpp index
135-
{
130+
for (int i = 0; i < from.length (); i++) { // int for Rcpp index
136131
if ((vert2index_map.at (from [i]) == short_i &&
137132
vert2index_map.at (to [i]) == short_j) ||
138133
(vert2index_map.at (from [i]) == short_j &&
139-
vert2index_map.at (to [i]) == short_i))
140-
{
134+
vert2index_map.at (to [i]) == short_i)) {
141135
shortest_edge = static_cast <size_t> (i);
142136
break;
143137
}
144138
}
145-
if (shortest_edge == INFINITE_INT)
146-
Rcpp::stop ("shite");
139+
if (shortest_edge == INFINITE_INT) {
140+
Rcpp::stop ("This shouldn't happen (in utils::find_shortest_connection)");
141+
}
147142

148143
return shortest_edge;
149144
}
@@ -159,20 +154,19 @@ void utils::merge_clusters (
159154
indx2int_map_t &index2cl_map,
160155
int2indxset_map_t &cl2index_map,
161156
const int cluster_from,
162-
const int cluster_to)
163-
{
164-
if (cluster_from < 0)
157+
const int cluster_to) {
158+
if (cluster_from < 0) {
165159
Rcpp::stop ("cluster_from must be non-negative");
166-
if (cluster_to < 0)
160+
}
161+
if (cluster_to < 0) {
167162
Rcpp::stop ("cluster_to must be non-negative");
163+
}
168164

169165
arma::uword cfr = static_cast <arma::uword> (cluster_from),
170166
cto = static_cast <arma::uword> (cluster_to);
171167
// Set all contig_mat (cluster_from, .) to 1
172-
for (arma::uword i = 0; i < contig_mat.n_rows; i++)
173-
{
174-
if (contig_mat (cfr, i) == 1 || contig_mat (i, cfr) == 1)
175-
{
168+
for (arma::uword i = 0; i < contig_mat.n_rows; i++) {
169+
if (contig_mat (cfr, i) == 1 || contig_mat (i, cfr) == 1) {
176170
contig_mat (cfr, i) = contig_mat (i, cfr) = 1;
177171
contig_mat (cto, i) = contig_mat (i, cto) = 1;
178172
}
@@ -181,23 +175,23 @@ void utils::merge_clusters (
181175
indxset_t idx_from = cl2index_map.at (cluster_from),
182176
idx_to = cl2index_map.at (cluster_to);
183177

184-
for (auto i: idx_from)
185-
for (auto j: idx_to)
186-
{
178+
for (auto i: idx_from) {
179+
for (auto j: idx_to) {
187180
arma::uword ia = static_cast <arma::uword> (i),
188181
ja = static_cast <arma::uword> (j);
189182
contig_mat (ia, ja) = contig_mat (ja, ia) = 1;
190183
}
184+
}
191185

192186
// then re-number all cluster numbers in cl2index
193187
cl2index_map.erase (cluster_from);
194188
cl2index_map.erase (cluster_to);
195-
for (auto i: idx_from)
189+
for (auto i: idx_from) {
196190
idx_to.insert (i);
191+
}
197192
cl2index_map.emplace (cluster_to, idx_to);
198193
// and in index2cl:
199-
for (auto i: idx_from)
200-
{
194+
for (auto i: idx_from) {
201195
index2cl_map.erase (i);
202196
index2cl_map.emplace (i, cluster_to);
203197
}
@@ -214,21 +208,20 @@ void utils_slk::mats_init (
214208
const int2indx_map_t &vert2index_map,
215209
arma::Mat <int> &contig_mat,
216210
arma::Mat <double> &d_mat,
217-
bool shortest)
218-
{
211+
bool shortest) {
219212
// arma::uword = unsigned int
220213
const arma::uword n = static_cast <arma::uword> (vert2index_map.size ());
221214

222215
contig_mat = arma::zeros <arma::Mat <int> > (n, n);
223216
//d_mat = arma::zeros <arma::Mat <double> > (n, n);
224217
d_mat.resize (n, n);
225-
if (shortest)
218+
if (shortest) {
226219
d_mat.fill (INFINITE_DOUBLE);
227-
else
220+
} else {
228221
d_mat.fill (-INFINITE_DOUBLE);
222+
}
229223

230-
for (int i = 0; i < from.length (); i++)
231-
{
224+
for (int i = 0; i < from.length (); i++) {
232225
arma::uword fi = static_cast <arma::uword> (vert2index_map.at (from [i])),
233226
ti = static_cast <arma::uword> (vert2index_map.at (to [i]));
234227
contig_mat (fi, ti) = contig_mat (ti, fi) = 1;

src/utils.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ namespace utils {
44

55
bool strfound (const std::string str, const std::string target);
66

7-
struct OneEdge
8-
{
7+
struct OneEdge {
98
int from, to;
109
double dist;
1110
};

0 commit comments

Comments
 (0)