File: tally.cpp
   1 /*
   2 The MIT License (MIT)
   3 
   4 Copyright © 2020-2025 pacman64
   5 
   6 Permission is hereby granted, free of charge, to any person obtaining a copy of
   7 this software and associated documentation files (the “Software”), to deal
   8 in the Software without restriction, including without limitation the rights to
   9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10 of the Software, and to permit persons to whom the Software is furnished to do
  11 so, subject to the following conditions:
  12 
  13 The above copyright notice and this permission notice shall be included in all
  14 copies or substantial portions of the Software.
  15 
  16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22 SOFTWARE.
  23 */
  24 
  25 /*
  26 You can build this command-line app by running
  27 
  28 c++ -Wall -s -O3 -march=native -mtune=native -flto -o tally tally.cpp
  29 */
  30 
  31 #include <algorithm>
  32 #include <cstdint>
  33 #include <cstring>
  34 #include <fstream>
  35 #include <iostream>
  36 #include <map>
  37 #include <new>
  38 #include <string>
  39 #include <vector>
  40 
  41 #ifdef RED_ERRORS
  42 #define ERROR_STYLE "\x1b[38;2;204;0;0m"
  43 #ifdef __APPLE__
  44 #define ERROR_STYLE "\x1b[31m"
  45 #endif
  46 #define RESET_STYLE "\x1b[0m"
  47 #else
  48 #define ERROR_STYLE ""
  49 #define RESET_STYLE ""
  50 #endif
  51 
  52 using namespace std;
  53 
  54 const string info = ""
  55 "tally [files...]\n"
  56 "\n"
  57 "Tally/count each distinct line. The output is a TSV table, with headers.\n"
  58 "";
  59 
  60 typedef map<string, uint64_t> string_tally;
  61 
  62 void handle_input(istream& in, string& line, string_tally& tally) {
  63     while (getline(in, line)) {
  64         tally[line]++;
  65     }
  66 }
  67 
  68 bool handle_file(const char* path, string& line, string_tally& tally) {
  69     ifstream f(path);
  70     if (f.is_open()) {
  71         handle_input(f, line, tally);
  72         return true;
  73     }
  74 
  75     cerr << ERROR_STYLE "can't open file named '" << path << "'" RESET_STYLE << endl;
  76     return false;
  77 }
  78 
  79 // run returns the number of errors
  80 int run(int argc, char** argv) {
  81     string line;
  82     string_tally tally;
  83 
  84     if (argc > 1) {
  85         if (
  86             strcmp(argv[1], "-h") == 0 ||
  87             strcmp(argv[1], "-help") == 0 ||
  88             strcmp(argv[1], "--h") == 0 ||
  89             strcmp(argv[1], "--help") == 0
  90         ) {
  91             cout << info;
  92             return 0;
  93         }
  94     }
  95 
  96     size_t dashes = 0;
  97     for (int i = 1; i < argc; i++) {
  98         if (argv[i][0] == '-' && argv[i][1] == 0) {
  99             dashes++;
 100         }
 101     }
 102 
 103     if (dashes > 1) {
 104         cerr << ERROR_STYLE "can't use dash (stdin) more than once" RESET_STYLE << endl;
 105         return 1;
 106     }
 107 
 108     cin.tie(NULL);
 109     ios_base::sync_with_stdio(false);
 110 
 111     cout << "value\ttally\n";
 112     cout.flush();
 113 
 114     size_t errors = 0;
 115     for (int i = 1; i < argc; i++) {
 116         if (argv[i][0] == '-' && argv[i][1] == 0) {
 117             handle_input(cin, line, tally);
 118             continue;
 119         }
 120 
 121         if (!handle_file(argv[i], line, tally)) {
 122             errors++;
 123         }
 124     }
 125 
 126     if (argc < 2) {
 127         handle_input(cin, line, tally);
 128     }
 129 
 130     vector<string> lines;
 131     lines.reserve(tally.size());
 132     for (auto kv = tally.begin(); kv != tally.end(); kv++) {
 133         lines.push_back(kv->first);
 134     }
 135 
 136     sort(lines.begin(), lines.end(), [&tally](const auto& x, const auto& y) {
 137         auto a = tally[x];
 138         auto b = tally[y];
 139         if (a > b) {
 140             return true;
 141         }
 142         return x < y;
 143     });
 144 
 145     for (const auto& k : lines) {
 146         cout << k << '\t' << tally[k] << '\n';
 147         if (cout.eof()) {
 148             break;
 149         }
 150     }
 151 
 152     cout.flush();
 153     return errors++;
 154 }
 155 
 156 int main(int argc, char** argv) {
 157     try {
 158         return run(argc, argv) == 0 ? 0 : 1;
 159     } catch (const bad_alloc& e) {
 160         cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl;
 161         return 1;
 162     }
 163 }