nucleotide_count: iteration 2

This commit is contained in:
Dmitry Kokorin 2016-03-02 00:36:23 +03:00
parent 9dca4fc779
commit c8992eb872
2 changed files with 14 additions and 21 deletions

View file

@ -1,7 +1,6 @@
#include "nucleotide_count.h" #include "nucleotide_count.h"
#include <stdexcept> #include <stdexcept>
#include <unordered_set>
namespace dna { namespace dna {
@ -9,28 +8,19 @@ namespace dna {
using namespace std; using namespace std;
static const unordered_set<char> valid_nucleotides = {'A', 'T', 'C', 'G'}; counter::counter(const string &sequence)
: nucleotide_counts_{ {'A', 0}, {'T', 0}, {'C', 0}, {'G', 0}}
static inline bool is_valid_nucleotide(char c)
{ {
return valid_nucleotides.find(c) != valid_nucleotides.cend(); for (auto c : sequence)
}
counter::counter(const string &str)
{
for (auto c : valid_nucleotides)
nucleotide_counts_[c] = 0;
for (auto c : str) {
if (!is_valid_nucleotide(c))
throw invalid_argument("invalid sequence");
++nucleotide_counts_[c]; ++nucleotide_counts_[c];
}
} }
NucleotideCounts counter::nucleotide_counts() const bool counter::is_valid_nucleotide(char c) const
{
return nucleotide_counts_.count(c);
}
const NucleotideCounts &counter::nucleotide_counts() const
{ {
return nucleotide_counts_; return nucleotide_counts_;
} }

View file

@ -14,10 +14,13 @@ class counter
{ {
NucleotideCounts nucleotide_counts_; NucleotideCounts nucleotide_counts_;
bool is_valid_nucleotide(char c) const;
public: public:
counter(const std::string &str); counter(const std::string &sequence);
NucleotideCounts nucleotide_counts() const;
const NucleotideCounts &nucleotide_counts() const;
int count(char c) const; int count(char c) const;
}; };