From c8992eb872adb96c1f1d87e56f8378a363de42c2 Mon Sep 17 00:00:00 2001 From: Dmitry Kokorin Date: Wed, 2 Mar 2016 00:36:23 +0300 Subject: [PATCH] nucleotide_count: iteration 2 --- cpp/nucleotide-count/nucleotide_count.cpp | 28 ++++++++--------------- cpp/nucleotide-count/nucleotide_count.h | 7 ++++-- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/cpp/nucleotide-count/nucleotide_count.cpp b/cpp/nucleotide-count/nucleotide_count.cpp index fea82c4..e591522 100644 --- a/cpp/nucleotide-count/nucleotide_count.cpp +++ b/cpp/nucleotide-count/nucleotide_count.cpp @@ -1,7 +1,6 @@ #include "nucleotide_count.h" #include -#include namespace dna { @@ -9,28 +8,19 @@ namespace dna { using namespace std; -static const unordered_set 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_; } diff --git a/cpp/nucleotide-count/nucleotide_count.h b/cpp/nucleotide-count/nucleotide_count.h index 9017300..e6244d0 100644 --- a/cpp/nucleotide-count/nucleotide_count.h +++ b/cpp/nucleotide-count/nucleotide_count.h @@ -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; };