Skip to content

Commit

Permalink
Clean up code, add more cpp11 features for performance
Browse files Browse the repository at this point in the history
 - Fix typos in global.xmh
 - Remove dead zeta.cpp file
 - Establish a constexpr army
 - Convert NULL to nullptr
  • Loading branch information
zryan3 committed Oct 21, 2016
1 parent 56f23c7 commit 81a88ae
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/include/dawg/details/global.xmh
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ XM( (output)(block)(before), std::string, ,
XM( (output)(block)(after), std::string, , XM( (output)(block)(after), std::string, ,
"Text that will be written after every replicate.") "Text that will be written after every replicate.")
XM( (output)(block)(between), std::string, , 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)(vars), bool, true)
XM( (output)(file), std::string, , XM( (output)(file), std::string, ,
"Path to the output file.") "Path to the output file.")
XM( (output)(split), bool, false, XM( (output)(split), bool, false,
"Ouput each replicate to its own file.") "Output each replicate to its own file.")
XM( (output)(append), bool, false, XM( (output)(append), bool, false,
"Append results to existing file.") "Append results to existing file.")
XM( (output)(label), bool, false, XM( (output)(label), bool, false,
Expand Down
2 changes: 1 addition & 1 deletion src/include/dawg/mutt.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ inline boost::uint32_t create_random_seed() {
boost::uint32_t v = static_cast<boost::uint32_t>(getpid()); 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 << 15) + (v >> 3)) + 0x6ba658b3; // Spread 5-decimal PID over 32-bit number
v^=(v<<17); v^=(v>>13); v^=(v<<5); 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); v^=(v<<17); v^=(v>>13); v^=(v<<5);
return (v == 0) ? 0x6a27d958 : (v & 0x7FFFFFFF); // return at most a 31-bit seed return (v == 0) ? 0x6a27d958 : (v & 0x7FFFFFFF); // return at most a 31-bit seed
} }
Expand Down
6 changes: 3 additions & 3 deletions src/include/dawg/output.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace dawg {


class output { class output {
public: 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), rep(0), label_width(0),
do_append(false), do_split(false), do_label(false), do_append(false), do_split(false), do_label(false),
split_id_offset(0) { } split_id_offset(0) { }
Expand All @@ -28,7 +28,7 @@ class output {


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


template<class T> template<class T>
bool output::set_format(T format) { bool output::set_format(T format) {
static const char format_keys[][10] = { static constexpr char format_keys[][10] = {
"aln", "poo", "fasta", "fsa", "aln", "poo", "fasta", "fsa",
"nexus", "phylip" "nexus", "phylip"
}; };
Expand Down
2 changes: 1 addition & 1 deletion src/include/dawg/rate.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class rate_model {


template<typename It> template<typename It>
bool create(const std::string &rname, It first, It last) { 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("const"),
std::string("gamma-invariant"), std::string("gamma-invariant"),
std::string("zero") std::string("zero")
Expand Down
22 changes: 11 additions & 11 deletions src/include/dawg/residue.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class residue {
typedef boost::uint64_t data_type; typedef boost::uint64_t data_type;




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


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


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


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


inline static const char* get_protein_code(unsigned int code) { 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**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG" "FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG"
"FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIMMTTTTNNKKSS**VVVVAAAADDEEGGGG" "FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIMMTTTTNNKKSS**VVVVAAAADDEEGGGG"
Expand Down
2 changes: 1 addition & 1 deletion src/include/dawg/subst.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class subst_model {


template<typename It1, typename It2> template<typename It1, typename It2>
bool subst_model::create(const char *mod_name, unsigned int code, It1 first1, It1 last1, It2 first2, It2 last2) { 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", "jc", "gtr", "k2p", "hky", "f84", "f81", "tn", "tn-f04",
"equ", "aagtr", "lg", "wag", "wagstar", "jtt-dcmut", "dayhoff-dcmut", "molphy", "equ", "aagtr", "lg", "wag", "wagstar", "jtt-dcmut", "dayhoff-dcmut", "molphy",
"codgtr", "codmg", "codmg-equ", "codmg-aap", "codmg-cp", "codgtr", "codmg", "codmg-equ", "codmg-aap", "codmg-cp",
Expand Down
6 changes: 3 additions & 3 deletions src/include/dawg/trick.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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) { 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) { 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) { 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 // A value is false if it is equal to 0, f, false, off, no, or blank
Expand Down
6 changes: 3 additions & 3 deletions src/lib/mutt.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ double dawg::mutt::rand_gamma(double a, double b) {
************************************************************/ ************************************************************/


/* tabulated values for the height of the Ziggurat levels */ /* 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, 1, 0.963598623011, 0.936280813353, 0.913041104253,
0.892278506696, 0.873239356919, 0.855496407634, 0.838778928349, 0.892278506696, 0.873239356919, 0.855496407634, 0.838778928349,
0.822902083699, 0.807732738234, 0.793171045519, 0.779139726505, 0.822902083699, 0.807732738234, 0.793171045519, 0.779139726505,
Expand Down Expand Up @@ -120,7 +120,7 @@ static const double ytab[128] = {


/* tabulated values for 2^24 times x[i]/x[i+1], /* 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 */ * 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, 0, 12590644, 14272653, 14988939,
15384584, 15635009, 15807561, 15933577, 15384584, 15635009, 15807561, 15933577,
16029594, 16105155, 16166147, 16216399, 16029594, 16105155, 16166147, 16216399,
Expand Down Expand Up @@ -156,7 +156,7 @@ static const boost::uint32_t ktab[128] = {
}; };


/* tabulated values of 2^{-24}*x[i] */ /* 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, 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.10340022482e-08, 3.33011726243e-08, 3.53439060345e-08, 3.72152672658e-08,
3.8950989572e-08, 4.05763964764e-08, 4.21101548915e-08, 4.35664624904e-08, 3.8950989572e-08, 4.05763964764e-08, 4.21101548915e-08, 4.35664624904e-08,
Expand Down
18 changes: 9 additions & 9 deletions src/lib/output.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ bool dawg::output::open(const char *file_name, unsigned int max_rep,
last_rep = max_rep; last_rep = max_rep;
rep = 0; rep = 0;
cs_range format(file_name, file_name); cs_range format(file_name, file_name);
const char *mid = NULL; const char *mid = nullptr;
if(file_name != NULL && file_name[0] != '\0') { if(file_name != nullptr && file_name[0] != '\0') {
mid = strchr(file_name, ':'); mid = strchr(file_name, ':');
#ifdef BOOST_WINDOWS #ifdef BOOST_WINDOWS
if(mid != NULL && mid != file_name+1) { if(mid != nullptr && mid != file_name+1) {
#else #else
if(mid != NULL) { if(mid != nullptr) {
#endif #endif
// format:file // format:file
format = boost::make_iterator_range(file_name, mid); format = boost::make_iterator_range(file_name, mid);
file_name = mid+1; file_name = mid+1;
// find extension point for later // find extension point for later
mid = strrchr(file_name, '.'); mid = strrchr(file_name, '.');
} else if((mid = strrchr(file_name, '.')) != NULL) { } else if((mid = strrchr(file_name, '.')) != nullptr) {
// file.format // file.format
format = boost::make_iterator_range(mid+1, (const char*)strchr(mid+1, '\0')); format = boost::make_iterator_range(mid+1, (const char*)strchr(mid+1, '\0'));
} }
Expand All @@ -52,7 +52,7 @@ bool dawg::output::open(const char *file_name, unsigned int max_rep,
current_label.assign(label_width, '0'); current_label.assign(label_width, '0');
do_label = label; 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 // set append and split options before we open the file
do_append = append; do_append = append;
do_split = split; do_split = split;
Expand All @@ -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 << "\'."); return DAWG_ERROR("unable to open output file \'" << file_name << "\'.");
} else { } else {
// setup output_filename // setup output_filename
if(mid == NULL) { if(mid == nullptr) {
split_file_name.assign(file_name); split_file_name.assign(file_name);
split_file_name.append(1, '-'); split_file_name.append(1, '-');
split_file_name.append(current_label); split_file_name.append(current_label);
Expand Down Expand Up @@ -91,7 +91,7 @@ bool dawg::output::open_file(const char* file_name) {
(do_append ? ios_base::app : ios_base::trunc); (do_append ? ios_base::app : ios_base::trunc);
fout.open(file_name, om); fout.open(file_name, om);
if(!fout.is_open()) { if(!fout.is_open()) {
set_ostream(NULL); set_ostream(nullptr);
return false; return false;
} }
set_ostream(fout); set_ostream(fout);
Expand Down Expand Up @@ -214,7 +214,7 @@ void dawg::output::print_phylip(const alignment& aln) {
void dawg::output::print_nexus(const alignment& aln) { void dawg::output::print_nexus(const alignment& aln) {
ostream &out = *p_out; ostream &out = *p_out;


static char datatypes[][10] = { static constexpr char datatypes[][10] = {
"DNA", "RNA", "PROTEIN", "DNA", "DNA", "RNA", "PROTEIN", "DNA",
"DNA", "RNA", "PROTEIN", "DNA" "DNA", "RNA", "PROTEIN", "DNA"
}; };
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parse.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using namespace dawg;


bool trick::parse_file(trick& p, const char *cs) { bool trick::parse_file(trick& p, const char *cs) {
bool ret; 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); ret = p.parse_stream(std::cin);
} else { } else {
std::ifstream is(cs); std::ifstream is(cs);
Expand Down
Empty file removed src/lib/zeta.cpp
Empty file.
4 changes: 2 additions & 2 deletions tests/Unit/Dawg/testu01.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ unif01_Gen *create_gen(unsigned int u) {
g->seed(u); g->seed(u);
gen->state = g; gen->state = g;
gen->name = &name[0]; gen->name = &name[0];
gen->param = NULL; gen->param = nullptr;
gen->GetU01 = &get_u01; gen->GetU01 = &get_u01;
gen->GetBits = &get_bits; gen->GetBits = &get_bits;
gen->Write = &write_gen; gen->Write = &write_gen;
return gen; return gen;
} }


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

0 comments on commit 81a88ae

Please sign in to comment.