Skip to content
Permalink
Browse files

Clean up code, add more cpp11 features for performance

 - Fix typos in global.xmh
 - Remove dead zeta.cpp file
 - Establish a constexpr army
 - Convert NULL to nullptr
  • Loading branch information...
zryan3
zryan3 committed Sep 27, 2016
1 parent 56f23c7 commit 81a88ae8f054091a23ae19cceda1e89892ad1af4
@@ -21,13 +21,13 @@ XM( (output)(block)(before), std::string, ,
XM( (output)(block)(after), std::string, ,
"Text that will be written after every replicate.")
XM( (output)(block)(between), std::string, ,
"Text that will be written betwee replicates.")
"Text that will be written between replicates.")

//XM( (output)(vars), bool, true)
XM( (output)(file), std::string, ,
"Path to the output file.")
XM( (output)(split), bool, false,
"Ouput each replicate to its own file.")
"Output each replicate to its own file.")
XM( (output)(append), bool, false,
"Append results to existing file.")
XM( (output)(label), bool, false,
@@ -105,7 +105,7 @@ inline boost::uint32_t create_random_seed() {
boost::uint32_t v = static_cast<boost::uint32_t>(getpid());
v += ((v << 15) + (v >> 3)) + 0x6ba658b3; // Spread 5-decimal PID over 32-bit number
v^=(v<<17); v^=(v>>13); v^=(v<<5);
v += static_cast<boost::uint32_t>(time(NULL));
v += static_cast<boost::uint32_t>(time(nullptr));
v^=(v<<17); v^=(v>>13); v^=(v<<5);
return (v == 0) ? 0x6a27d958 : (v & 0x7FFFFFFF); // return at most a 31-bit seed
}
@@ -18,7 +18,7 @@ namespace dawg {

class output {
public:
output() : do_op(&output::print_aln), p_out(NULL), format_id(0),
output() : do_op(&output::print_aln), p_out(nullptr), format_id(0),
rep(0), label_width(0),
do_append(false), do_split(false), do_label(false),
split_id_offset(0) { }
@@ -28,7 +28,7 @@ class output {

inline bool operator()(const alignment& aln) {
open_next();
if(p_out == NULL)
if(p_out == nullptr)
return false;
std::ostream &out = *p_out;
if(!do_split)
@@ -92,7 +92,7 @@ class output {

template<class T>
bool output::set_format(T format) {
static const char format_keys[][10] = {
static constexpr char format_keys[][10] = {
"aln", "poo", "fasta", "fsa",
"nexus", "phylip"
};
@@ -22,7 +22,7 @@ class rate_model {

template<typename It>
bool create(const std::string &rname, It first, It last) {
static std::string name_keys[] = {
static const std::string name_keys[] = {
std::string("const"),
std::string("gamma-invariant"),
std::string("zero")
@@ -29,12 +29,12 @@ class residue {
typedef boost::uint64_t data_type;


static const data_type base_mask = UINT64_C(0x00000000000000FF);
static const data_type branch_mask = UINT64_C(0x0000FFFFFFFFFF00);
static const data_type rate_mask = UINT64_C(0xFFFF000000000000);
static const data_type rate_shift = 48;
static const data_type base_bit_width = 8;
static const data_type branch_inc = 0x100;
static constexpr data_type base_mask = UINT64_C(0x00000000000000FF);
static constexpr data_type branch_mask = UINT64_C(0x0000FFFFFFFFFF00);
static constexpr data_type rate_mask = UINT64_C(0xFFFF000000000000);
static constexpr data_type rate_shift = 48;
static constexpr data_type base_bit_width = 8;
static constexpr data_type branch_inc = 0x100;

inline data_type base() const { return data_ & base_mask; }
inline void base(data_type b) { data_ = (b & base_mask) | (data_ & ~base_mask); }
@@ -85,10 +85,10 @@ class residue_exchange {

inline bool model(unsigned int type, unsigned int code, bool rna,
bool lowercase, bool markins, bool keepempty) {
static const char sIns[] = "-+";
static constexpr char sIns[] = "-+";
// table for going from base->char
// TODO: Allow codons to be translated into aa
static const char mods[] =
static constexpr char mods[] =
"ACGT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-" // DNA
"acgt!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-" // dna
"ACGU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-" // RNA
@@ -122,7 +122,7 @@ class residue_exchange {
;
// tables for going from char->base
// 1 genetic code is 80 elements long
static const char rmods[] = {
static constexpr char rmods[] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,-1, 1, // DNA & RNA
-1,-1,-1, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 3, 3,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,-1, 1,-1,-1,-1, 2,-1,-1,-1,-1,
@@ -214,7 +214,7 @@ class residue_exchange {

// cod64 -> codon number
static inline unsigned int cod64_to_codon(char c) {
static const char a[] = {
static constexpr char a[] = {
// cod64 -> codon number
54,55,56,57,58,59,60,61,62,63,-1,-1,-1,11,-1,-1,10, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9,12,13,15,16,14,17,18,19,20,21,22,23,24,
@@ -260,7 +260,7 @@ class residue_exchange {
explicit residue_exchange(int m=DNA) { model(m,0,0,false,false,false); }

inline static const char* get_protein_code(unsigned int code) {
static const char s[] =
static constexpr char s[] =
"FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG"
"FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG"
"FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIMMTTTTNNKKSS**VVVVAAAADDEEGGGG"
@@ -157,7 +157,7 @@ class subst_model {

template<typename It1, typename It2>
bool subst_model::create(const char *mod_name, unsigned int code, It1 first1, It1 last1, It2 first2, It2 last2) {
static const char name_keys[][16] = {
static constexpr char name_keys[][16] = {
"jc", "gtr", "k2p", "hky", "f84", "f81", "tn", "tn-f04",
"equ", "aagtr", "lg", "wag", "wagstar", "jtt-dcmut", "dayhoff-dcmut", "molphy",
"codgtr", "codmg", "codmg-equ", "codmg-aap", "codmg-cp",
@@ -104,15 +104,15 @@ inline void trick::section::conv(const std::string& ss, std::string& r) {
}

inline void trick::section::conv(const std::string& ss, double& r) {
r = strtod(ss.c_str(), NULL);
r = strtod(ss.c_str(), nullptr);
}

inline void trick::section::conv(const std::string& ss, unsigned int& r) {
r = strtoul(ss.c_str(), NULL, 0);
r = strtoul(ss.c_str(), nullptr, 0);
}

inline void trick::section::conv(const std::string& ss, int& r) {
r = strtol(ss.c_str(), NULL, 0);
r = strtol(ss.c_str(), nullptr, 0);
}

// A value is false if it is equal to 0, f, false, off, no, or blank
@@ -83,7 +83,7 @@ double dawg::mutt::rand_gamma(double a, double b) {
************************************************************/

/* tabulated values for the height of the Ziggurat levels */
static const double ytab[128] = {
static constexpr double ytab[128] = {
1, 0.963598623011, 0.936280813353, 0.913041104253,
0.892278506696, 0.873239356919, 0.855496407634, 0.838778928349,
0.822902083699, 0.807732738234, 0.793171045519, 0.779139726505,
@@ -120,7 +120,7 @@ static const double ytab[128] = {

/* tabulated values for 2^24 times x[i]/x[i+1],
* used to accept for U*x[i+1]<=x[i] without any floating point operations */
static const boost::uint32_t ktab[128] = {
static constexpr boost::uint32_t ktab[128] = {
0, 12590644, 14272653, 14988939,
15384584, 15635009, 15807561, 15933577,
16029594, 16105155, 16166147, 16216399,
@@ -156,7 +156,7 @@ static const boost::uint32_t ktab[128] = {
};

/* tabulated values of 2^{-24}*x[i] */
static const double wtab[128] = {
static constexpr double wtab[128] = {
1.62318314817e-08, 2.16291505214e-08, 2.54246305087e-08, 2.84579525938e-08,
3.10340022482e-08, 3.33011726243e-08, 3.53439060345e-08, 3.72152672658e-08,
3.8950989572e-08, 4.05763964764e-08, 4.21101548915e-08, 4.35664624904e-08,
@@ -26,20 +26,20 @@ bool dawg::output::open(const char *file_name, unsigned int max_rep,
last_rep = max_rep;
rep = 0;
cs_range format(file_name, file_name);
const char *mid = NULL;
if(file_name != NULL && file_name[0] != '\0') {
const char *mid = nullptr;
if(file_name != nullptr && file_name[0] != '\0') {
mid = strchr(file_name, ':');
#ifdef BOOST_WINDOWS
if(mid != NULL && mid != file_name+1) {
if(mid != nullptr && mid != file_name+1) {
#else
if(mid != NULL) {
if(mid != nullptr) {
#endif
// format:file
format = boost::make_iterator_range(file_name, mid);
file_name = mid+1;
// find extension point for later
mid = strrchr(file_name, '.');
} else if((mid = strrchr(file_name, '.')) != NULL) {
} else if((mid = strrchr(file_name, '.')) != nullptr) {
// file.format
format = boost::make_iterator_range(mid+1, (const char*)strchr(mid+1, '\0'));
}
@@ -52,7 +52,7 @@ bool dawg::output::open(const char *file_name, unsigned int max_rep,
current_label.assign(label_width, '0');
do_label = label;

if(file_name != NULL && file_name[0] != '\0' && strcmp(file_name, "-") != 0) {
if(file_name != nullptr && file_name[0] != '\0' && strcmp(file_name, "-") != 0) {
// set append and split options before we open the file
do_append = append;
do_split = split;
@@ -62,7 +62,7 @@ bool dawg::output::open(const char *file_name, unsigned int max_rep,
return DAWG_ERROR("unable to open output file \'" << file_name << "\'.");
} else {
// setup output_filename
if(mid == NULL) {
if(mid == nullptr) {
split_file_name.assign(file_name);
split_file_name.append(1, '-');
split_file_name.append(current_label);
@@ -91,7 +91,7 @@ bool dawg::output::open_file(const char* file_name) {
(do_append ? ios_base::app : ios_base::trunc);
fout.open(file_name, om);
if(!fout.is_open()) {
set_ostream(NULL);
set_ostream(nullptr);
return false;
}
set_ostream(fout);
@@ -214,7 +214,7 @@ void dawg::output::print_phylip(const alignment& aln) {
void dawg::output::print_nexus(const alignment& aln) {
ostream &out = *p_out;

static char datatypes[][10] = {
static constexpr char datatypes[][10] = {
"DNA", "RNA", "PROTEIN", "DNA",
"DNA", "RNA", "PROTEIN", "DNA"
};
@@ -16,7 +16,7 @@ using namespace dawg;

bool trick::parse_file(trick& p, const char *cs) {
bool ret;
if(cs == NULL || strcmp(cs, "")==0 || strcmp(cs, "-")==0) {
if(cs == nullptr || strcmp(cs, "")==0 || strcmp(cs, "-")==0) {
ret = p.parse_stream(std::cin);
} else {
std::ifstream is(cs);
No changes.
@@ -95,15 +95,15 @@ unif01_Gen *create_gen(unsigned int u) {
g->seed(u);
gen->state = g;
gen->name = &name[0];
gen->param = NULL;
gen->param = nullptr;
gen->GetU01 = &get_u01;
gen->GetBits = &get_bits;
gen->Write = &write_gen;
return gen;
}

void delete_gen(unif01_Gen *gen) {
if(NULL == gen)
if(nullptr == gen)
return;
delete static_cast<mutt_gen_default*>(gen->state);
delete gen;

0 comments on commit 81a88ae

Please sign in to comment.
You can’t perform that action at this time.