Skip to content

Commit

Permalink
Merge pull request #2 from statnet/ext
Browse files Browse the repository at this point in the history
changed way extension package exposed stat registration
  • Loading branch information
ifellows committed Apr 23, 2018
2 parents 7e14a32 + eb4d9b0 commit 655d71c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 39 deletions.
4 changes: 4 additions & 0 deletions inst/examplePackage/LologExtension/R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ lolog_hello_world <- function() {
.Call('_LologExtension_lolog_hello_world', PACKAGE = 'LologExtension')
}

registerMinDegree <- function() {
invisible(.Call('_LologExtension_registerMinDegree', PACKAGE = 'LologExtension'))
}

2 changes: 1 addition & 1 deletion inst/examplePackage/LologExtension/R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.onLoad <- function(libname, pkgname){
# Run registerMinDegree in MinDegree.cpp to allow
# MinDegree to be used in formula
.Call("registerMinDegree", PACKAGE = 'LologExtension')
registerMinDegree()
}

# #You can now use the new MinDegree term e.g.:
Expand Down
18 changes: 5 additions & 13 deletions inst/examplePackage/LologExtension/src/MinDegree.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
// [[Rcpp::depends(lolog)]]
#include "MinDegree.h"
#include "lolog.h"
#include <lolog.h>


/**
* This function registers the new MinDegree statistic so that
* it can be used in lolog formula.
*
* RcppExport means this function can be called from R using
* .C("registerMinDegree")
* see: .onLoad in zzz.R
*/
RcppExport void registerMinDegree(){
using namespace lolog;
Rcpp::XPtr< AbstractStat<Undirected> > ps1(new UndirectedMinDegree());
REGISTER_UNDIRECTED_STATISTIC(ps1);
//[[Rcpp::export()]]
void registerMinDegree(){
Rcpp::XPtr< lolog::AbstractStat<lolog::Undirected> > ps1(new lologext::UndirectedMinDegree());
REGISTER_UNDIRECTED_STATISTIC(ps1);
}
38 changes: 19 additions & 19 deletions inst/examplePackage/LologExtension/src/MinDegree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

#include <Rcpp.h>

#include<lolog.h>
#include<Rcpp.h>
#include<vector>

using namespace lolog;
#include <lolog.h>
#include <Rcpp.h>
#include <vector>

namespace lologext{

using namespace Rcpp;
using namespace std;

Expand All @@ -17,7 +18,7 @@ using namespace std;
* with degree greater than or equal to "degree"
*/
template<class Engine>
class MinDegree : public BaseStat< Engine > {
class MinDegree : public lolog::BaseStat< Engine > {
public:
int degree; //the minimum degree

Expand All @@ -42,7 +43,7 @@ class MinDegree : public BaseStat< Engine > {
}

//Calculate the statistic
virtual void calculate(const BinaryNet<Engine>& net){
virtual void calculate(const lolog::BinaryNet<Engine>& net){
vector<double> v(1,0);
this->stats=v;
this->lastStats = std::vector<double>(1,0.0);
Expand All @@ -54,8 +55,8 @@ class MinDegree : public BaseStat< Engine > {
}

//Update the statistic given a dyad toggle
virtual void dyadUpdate(const BinaryNet<Engine>& net,const int &from,const int &to,const std::vector<int> &order,const int &actorIndex){
BaseOffset<Engine>::resetLastStats();
virtual void dyadUpdate(const lolog::BinaryNet<Engine>& net,const int &from,const int &to,const std::vector<int> &order,const int &actorIndex){
lolog::BaseOffset<Engine>::resetLastStats();
if(!net.hasEdge(from,to)){
if(net.degree(from)==degree-1)
this->stats[0]++;
Expand All @@ -81,16 +82,15 @@ class MinDegree : public BaseStat< Engine > {

};

typedef Stat<Undirected, MinDegree<Undirected> > UndirectedMinDegree;
typedef lolog::Stat<lolog::Undirected, MinDegree<lolog::Undirected> > UndirectedMinDegree;

/**
* This function registers the new MinDegree statistic so that
* it can be used in lolog formula.
*
* RcppExport means this function can be called from R using
* .C("registerMinDegree")
* see: .onLoad in zzz.R
*/
RcppExport void registerMinDegree();



}



void registerMinDegree();

#endif
13 changes: 10 additions & 3 deletions inst/examplePackage/LologExtension/src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}

RcppExport SEXP registerMinDegree();
// registerMinDegree
void registerMinDegree();
RcppExport SEXP _LologExtension_registerMinDegree() {
BEGIN_RCPP
Rcpp::RNGScope rcpp_rngScope_gen;
registerMinDegree();
return R_NilValue;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_LologExtension_lolog_hello_world", (DL_FUNC) &_LologExtension_lolog_hello_world, 0},
{"registerMinDegree", (DL_FUNC) &registerMinDegree, 0},
{"_LologExtension_registerMinDegree", (DL_FUNC) &_LologExtension_registerMinDegree, 0},
{NULL, NULL, 0}
};

Expand Down
5 changes: 2 additions & 3 deletions inst/examplePackage/LologExtension/src/lolog_hello_world.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// [[Rcpp::depends(lolog)]]
#include "lolog_hello_world.h"
#include "lolog.h"
#include <lolog.h>

//' An basic example of a function in C++ using lolog
//' @return a list of a character vector, a numeric vector, and an lolog DirectedNet
Expand All @@ -17,10 +17,9 @@
// [[Rcpp::export]]
Rcpp::List lolog_hello_world(){
using namespace Rcpp;
using namespace lolog;

IntegerMatrix tmp(0,2);
DirectedNet net(tmp,20);
lolog::DirectedNet net(tmp,20);
CharacterVector x = CharacterVector::create( "foo", "bar" ) ;
NumericVector y = NumericVector::create( 0.0, 1.0 ) ;
List z = List::create( x, y, wrap(net) ) ;
Expand Down

0 comments on commit 655d71c

Please sign in to comment.