Skip to content

Commit

Permalink
Use kvec
Browse files Browse the repository at this point in the history
  • Loading branch information
stewid committed Nov 2, 2019
1 parent 0e9abdc commit 2023e3f
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 28 deletions.
90 changes: 90 additions & 0 deletions src/kvec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* The MIT License
Copyright (c) 2008, by Attractive Chaos <attractor@live.co.uk>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/*
An example:
#include "kvec.h"
int main() {
kvec_t(int) array;
kv_init(array);
kv_push(int, array, 10); // append
kv_a(int, array, 20) = 5; // dynamic
kv_A(array, 20) = 4; // static
kv_destroy(array);
return 0;
}
*/

/*
2008-09-22 (0.1.0):
* The initial version.
*/

#ifndef AC_KVEC_H
#define AC_KVEC_H

#include <stdlib.h>

#define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))

#define kvec_t(type) struct { size_t n, m; type *a; }
#define kv_init(v) ((v).n = (v).m = 0, (v).a = 0)
#define kv_destroy(v) free((v).a)
#define kv_A(v, i) ((v).a[(i)])
#define kv_pop(v) ((v).a[--(v).n])
#define kv_size(v) ((v).n)
#define kv_max(v) ((v).m)

#define kv_resize(type, v, s) ((v).m = (s), (v).a = (type*)realloc((v).a, sizeof(type) * (v).m))

#define kv_copy(type, v1, v0) do { \
if ((v1).m < (v0).n) kv_resize(type, v1, (v0).n); \
(v1).n = (v0).n; \
memcpy((v1).a, (v0).a, sizeof(type) * (v0).n); \
} while (0) \

#define kv_push(type, v, x) do { \
if ((v).n == (v).m) { \
(v).m = (v).m? (v).m<<1 : 2; \
(v).a = (type*)realloc((v).a, sizeof(type) * (v).m); \
} \
(v).a[(v).n++] = (x); \
} while (0)

#define kv_pushp(type, v) (((v).n == (v).m)? \
((v).m = ((v).m? (v).m<<1 : 2), \
(v).a = (type*)realloc((v).a, sizeof(type) * (v).m), 0) \
: 0), ((v).a + ((v).n++))

#define kv_a(type, v, i) (((v).m <= (size_t)(i)? \
((v).m = (v).n = (i) + 1, kv_roundup32((v).m), \
(v).a = (type*)realloc((v).a, sizeof(type) * (v).m), 0) \
: (v).n <= (size_t)(i)? (v).n = (i) + 1 \
: 0), (v).a[(i)])

#endif
66 changes: 38 additions & 28 deletions src/trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define R_NO_REMAP
#define STRICT_R_HEADERS

#include "kvec.h"
#include <algorithm>
#include <map>
#include <set>
Expand Down Expand Up @@ -605,6 +606,16 @@ SEXP networkSummary(const SEXP src,
{
const char *names[] = {"inDegree", "outDegree",
"ingoingContactChain", "outgoingContactChain", ""};
kvec_t(int) ingoingContactChain;
kvec_t(int) outgoingContactChain;
kvec_t(int) inDegree;
kvec_t(int) outDegree;
SEXP result, vec;

kv_init(ingoingContactChain);
kv_init(outgoingContactChain);
kv_init(inDegree);
kv_init(outDegree);

if (check_arguments(src, dst, t, root, inBegin, inEnd,
outBegin, outEnd, numberOfIdentifiers))
Expand All @@ -617,11 +628,6 @@ SEXP networkSummary(const SEXP src,
Rf_xlength(t),
INTEGER(numberOfIdentifiers)[0]);

std::vector<int> ingoingContactChain;
std::vector<int> outgoingContactChain;
std::vector<int> inDegree;
std::vector<int> outDegree;

for (R_xlen_t i = 0, end = Rf_xlength(root); i < end; ++i) {
VisitedNodes visitedNodesIngoing(INTEGER(numberOfIdentifiers)[0]);
VisitedNodes visitedNodesOutgoing(INTEGER(numberOfIdentifiers)[0]);
Expand All @@ -640,38 +646,42 @@ SEXP networkSummary(const SEXP src,
visitedNodesOutgoing,
false);

ingoingContactChain.push_back(visitedNodesIngoing.N() - 1);
outgoingContactChain.push_back(visitedNodesOutgoing.N() - 1);
kv_push(int, ingoingContactChain, visitedNodesIngoing.N() - 1);
kv_push(int, outgoingContactChain, visitedNodesOutgoing.N() - 1);

inDegree.push_back(degree(lookup.first,
INTEGER(root)[i] - 1,
INTEGER(inBegin)[i],
INTEGER(inEnd)[i]));
kv_push(int, inDegree, degree(lookup.first,
INTEGER(root)[i] - 1,
INTEGER(inBegin)[i],
INTEGER(inEnd)[i]));

outDegree.push_back(degree(lookup.second,
INTEGER(root)[i] - 1,
INTEGER(outBegin)[i],
INTEGER(outEnd)[i]));
kv_push(int, outDegree, degree(lookup.second,
INTEGER(root)[i] - 1,
INTEGER(outBegin)[i],
INTEGER(outEnd)[i]));
}

SEXP result, vec;
PROTECT(result = Rf_mkNamed(VECSXP, names));

SET_VECTOR_ELT(result, 0, vec = Rf_allocVector(INTSXP, inDegree.size()));
for (size_t i = 0; i < inDegree.size(); ++i)
INTEGER(vec)[i] = inDegree[i];
SET_VECTOR_ELT(result, 0, vec = Rf_allocVector(INTSXP, kv_size(inDegree)));
for (size_t i = 0; i < kv_size(inDegree); ++i)
INTEGER(vec)[i] = kv_A(inDegree, i);

SET_VECTOR_ELT(result, 1, vec = Rf_allocVector(INTSXP, kv_size(outDegree)));
for (size_t i = 0; i < kv_size(outDegree); ++i)
INTEGER(vec)[i] = kv_A(outDegree, i);

SET_VECTOR_ELT(result, 1, vec = Rf_allocVector(INTSXP, outDegree.size()));
for (size_t i = 0; i < outDegree.size(); ++i)
INTEGER(vec)[i] = outDegree[i];
SET_VECTOR_ELT(result, 2, vec = Rf_allocVector(INTSXP, kv_size(ingoingContactChain)));
for (size_t i = 0; i < kv_size(ingoingContactChain); ++i)
INTEGER(vec)[i] = kv_A(ingoingContactChain, i);

SET_VECTOR_ELT(result, 2, vec = Rf_allocVector(INTSXP, ingoingContactChain.size()));
for (size_t i = 0; i < ingoingContactChain.size(); ++i)
INTEGER(vec)[i] = ingoingContactChain[i];
SET_VECTOR_ELT(result, 3, vec = Rf_allocVector(INTSXP, kv_size(outgoingContactChain)));
for (size_t i = 0; i < kv_size(outgoingContactChain); ++i)
INTEGER(vec)[i] = kv_A(outgoingContactChain, i);

SET_VECTOR_ELT(result, 3, vec = Rf_allocVector(INTSXP, outgoingContactChain.size()));
for (size_t i = 0; i < outgoingContactChain.size(); ++i)
INTEGER(vec)[i] = outgoingContactChain[i];
kv_destroy(ingoingContactChain);
kv_destroy(outgoingContactChain);
kv_destroy(inDegree);
kv_destroy(outDegree);

UNPROTECT(1);

Expand Down

1 comment on commit 2023e3f

@lintr-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R/network-summary.R:262:1: style: functions should have cyclomatic complexity of less than 15, this has 38.

setMethod("NetworkSummary",
^

R/shortest-paths.R:169:1: style: functions should have cyclomatic complexity of less than 15, this has 41.

setMethod("ShortestPaths",
^

R/trace.R:161:1: style: functions should have cyclomatic complexity of less than 15, this has 56.

Trace <- function(movements,
^

R/tree.R:135:1: style: functions should have cyclomatic complexity of less than 15, this has 48.

position_tree <- function(tree,
^

Please sign in to comment.