Skip to content

Commit 83a9ebe

Browse files
committed
faster -a counting
1 parent 1805a11 commit 83a9ebe

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

src/countlets.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,29 @@ void usage() {
4949

5050
vector<int> count_stream(istream &input, vector<string> klets, int k) {
5151

52-
int nlets = klets.size();
53-
vector<int> counts(nlets, 0);
52+
vector<int> counts(klets.size(), 0);
5453
char l;
5554
string let;
5655
let.reserve(k + 1);
5756

57+
vector<string>::iterator let_i;
58+
5859
while (input >> l) {
5960
let += l;
61+
6062
if (let.length() >= k) {
63+
6164
if (let.length() == k + 1) let = let.substr(1, k);
62-
for (int i = 0; i < nlets; ++i) {
63-
if (let.compare(klets[i]) == 0) {
64-
++counts[i];
65-
break;
66-
}
67-
if (i == nlets - 1) {
68-
cerr << "Error: found unknown letter ["
69-
<< l << "]" << endl;
65+
66+
let_i = lower_bound(klets.begin(), klets.end(), let);
67+
if (let_i == klets.end()) {
68+
cerr << "Error: found unknown letter [" << l << "]" << endl;
7069
exit(EXIT_FAILURE);
7170
}
72-
}
71+
++counts[let_i - klets.begin()];
72+
7373
}
74+
7475
}
7576

7677
return counts;
@@ -82,13 +83,12 @@ int main(int argc, char **argv) {
8283
/* variables */
8384

8485
int k{1};
85-
int opt, seqlen, alphlen, alignlen;
86+
int opt, alphlen, alignlen;
8687
ifstream seqfile;
8788
ofstream outfile;
8889
bool has_file{false}, has_out{false}, has_alph{false};
89-
char l;
9090
set<int> lets_set;
91-
vector<char> letters, lets_uniq;
91+
vector<char> lets_uniq;
9292
vector<string> klets;
9393
vector<int> counts;
9494
string alph;
@@ -150,6 +150,10 @@ int main(int argc, char **argv) {
150150

151151
/* this version loads the entire sequence into memory */
152152

153+
vector<char> letters;
154+
int seqlen;
155+
char l;
156+
153157
if (!has_file) {
154158
while (cin >> l) letters.push_back(l);
155159
} else {

0 commit comments

Comments
 (0)