Skip to content

Instantly share code, notes, and snippets.

@slwu89
slwu89 / eapply.c
Last active March 17, 2018 04:13
rewrite of eapply without memory allocation for return values (for calling functions just for side effects)
/* ################################################################################
* BEGIN UTILITY FUNCTIONS
################################################################################ */
#define NONEMPTY_(_FRAME_) \
CHAR(PRINTNAME(TAG(_FRAME_)))[0] != '.' && CAR(_FRAME_) != R_UnboundValue
static int FrameSize(SEXP frame, int all)
{
int count = 0;
@slwu89
slwu89 / R_C_example1.c
Last active March 17, 2018 00:58
example of using named arguments and ... in R's C API
SEXP call_function(SEXP call, SEXP rho){
/* advance to 2nd element (CAR) of call pairlist; 1st element is just the call_function function */
SEXP args = CDR(call);
/* get function (2rd element) and advance to 3th element */
SEXP funSymbol = install("FUN");
args = CDR(args);
/* make the function call */
@slwu89
slwu89 / threadSafeSingleton.cpp
Created January 13, 2018 06:42
thread safe c++11 singleton
#include <iostream>
using namespace std;
class CSingleton final
{
public:
static CSingleton* GetInstance();
void set_x(const int& x_){x = x_;};
int get_x(){return x;};
@slwu89
slwu89 / haversineDist.cpp
Created January 12, 2018 19:49
haversine distance from R matrices
#include <Rcpp.h>
#include <math.h>
#include <iostream>
using namespace Rcpp;
/* Convert degrees to radians */
inline double deg2rad(const double& deg){return deg*M_PI/180;};
/* Earth mean radius [km] */
const static double R_earth = 6371;
@slwu89
slwu89 / move.cpp
Created January 5, 2018 00:30
move some unique_ptr between containers
// human.hpp
#ifndef human_hpp
#define human_hpp
#include <stdio.h>
#include <string>
#include <iostream>
class human {
@slwu89
slwu89 / logging.cpp
Last active December 19, 2017 04:15
stupid simple logging; not thread safe, havent deleted copy assignment/constructor
#include <fstream>
#include <string>
#include <iostream>
/* singleton for logging */
class logger {
private:
std::ofstream output;
static logger* l_instance;
@slwu89
slwu89 / suicide2.cpp
Last active December 14, 2017 17:15
more detailed version of object suicide
#include <iostream>
#include <memory>
#include <utility>
#include <string>
#include <vector>
class human;
typedef std::unique_ptr<human> human_ptr;
typedef std::vector<human_ptr> human_vector;
@slwu89
slwu89 / prng_class.hpp
Created December 13, 2017 03:03
quick & dirty prng
#ifndef test_rng_hpp
#define test_rng_hpp
#include <random>
#include <stdio.h>
class rng_test {
public:
rng_test(const uint_least32_t &seed) : rng(seed) {
runif = std::uniform_real_distribution<double>(0,1);
@slwu89
slwu89 / suicide.cpp
Last active December 12, 2017 20:04
logical way to do object suicide with pointers, also an example of move semantics
#include <iostream>
#include <memory.h>
#include <utility>
#include <vector>
class human;
typedef std::unique_ptr<human> human_ptr;
typedef std::vector<human_ptr> humans_obj;
class human {
@slwu89
slwu89 / singleton.cpp
Last active December 13, 2017 05:09
how to access a singleton data member from another class
#include <iostream>
class GlobalClass
{
int m_value;
static GlobalClass *s_instance;
GlobalClass(int v = 0){
std::cout << "global singleton being born at " << this << std::endl;
m_value = v;
};