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 <stdexcept>
#include <unordered_set>
namespace dna {
@ -9,28 +8,19 @@ namespace dna {
using namespace std;
static const unordered_set<char> valid_nucleotides = {'A', 'T', 'C', 'G'};
static inline bool is_valid_nucleotide(char c)
counter::counter(const string &sequence)
: nucleotide_counts_{ {'A', 0}, {'T', 0}, {'C', 0}, {'G', 0}}
{
return valid_nucleotides.find(c) != valid_nucleotides.cend();
}
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");
for (auto c : sequence)
++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_;
}

View file

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