2525#include < string>
2626#include < set>
2727#include < unistd.h>
28- #include < map >
28+ #include < unordered_map >
2929#include " klets.hpp"
3030using namespace std ;
3131
3232void usage () {
3333 printf (
34- " countlets v1.2 Copyright (C) 2019 Benjamin Jean-Marie Tremblay \n "
34+ " countlets v1.3 Copyright (C) 2019 Benjamin Jean-Marie Tremblay \n "
3535 " \n "
3636 " Usage: countlets [options] -i [filename] -o [filename] \n "
3737 " echo [string] | countlets [options] > [filename] \n "
@@ -42,23 +42,23 @@ void usage() {
4242 " format. \n "
4343 " -a <str> A string containing all of the alphabet letters present in the \n "
4444 " sequence. This allows the program not to have to load the entire \n "
45- " sequence into memory to find all of the unique letters. The downside\n "
46- " is that runtime increases more with increasing k. \n "
45+ " sequence into memory to find all of the unique letters. \n "
4746 " -k <int> K-let size. Defaults to 1. \n "
4847 " -n Don't print k-lets with counts of zero. \n "
4948 " -h Show usage. \n "
5049 );
5150}
5251
53- map <string, unsigned int > count_stream (istream &input, vector<string> klets,
52+ unordered_map <string, unsigned int > count_stream (istream &input, vector<string> klets,
5453 unsigned int k) {
5554
5655 char l;
5756
5857 string let;
5958 let.reserve (k + 1 );
6059
61- map<string, unsigned int > counts;
60+ unordered_map<string, unsigned int > counts;
61+ counts.reserve (klets.size ());
6262 for (size_t i = 0 ; i < klets.size (); ++i) {
6363 counts[klets[i]] = 0 ;
6464 }
@@ -199,7 +199,7 @@ int main(int argc, char **argv) {
199199
200200 /* this version only keeps k+1 characters in memory */
201201
202- map <string, unsigned int > counts;
202+ unordered_map <string, unsigned int > counts;
203203
204204 if (alph.length () < 1 ) {
205205 cerr << " Error: could not parse -a option" << endl;
@@ -228,17 +228,15 @@ int main(int argc, char **argv) {
228228 cerr << " Warning: foreign character(s) encountered" << endl;
229229 }
230230
231- map<string, unsigned int >::iterator it;
232-
233231 if (has_out) {
234- for (it = counts. begin (); it != counts. end (); ++it ) {
235- if (it-> second > 0 || !nozero)
236- outfile << it-> first << " \t " << it-> second << endl;
232+ for (size_t i = 0 ; i < klets. size (); ++i ) {
233+ if (counts[klets[i]] > 0 || !nozero)
234+ outfile << klets[i] << " \t " << counts[klets[i]] << " \t " << endl;
237235 }
238236 } else {
239- for (it = counts. begin (); it != counts. end (); ++it ) {
240- if (it-> second > 0 || !nozero)
241- cout << it-> first << " \t " << it-> second << endl;
237+ for (size_t i = 0 ; i < klets. size (); ++i ) {
238+ if (counts[klets[i]] > 0 || !nozero)
239+ cout << klets[i] << " \t " << counts[klets[i]] << " \t " << endl;
242240 }
243241 }
244242
0 commit comments