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" << endl;
 112 
 113     size_t errors = 0;
 114     for (int i = 1; i < argc; i++) {
 115         if (argv[i][0] == '-' && argv[i][1] == 0) {
 116             handle_input(cin, line, tally);
 117             continue;
 118         }
 119 
 120         if (!handle_file(argv[i], line, tally)) {
 121             errors++;
 122         }
 123     }
 124 
 125     if (argc < 2) {
 126         handle_input(cin, line, tally);
 127     }
 128 
 129     vector<string> sorted;
 130     sorted.reserve(tally.size());
 131     for (auto kv = tally.begin(); kv != tally.end(); kv++) {
 132         sorted.push_back(kv->first);
 133     }
 134 
 135     sort(sorted.begin(), sorted.end(), [&tally](const auto& x, const auto& y) {
 136         auto a = tally[x];
 137         auto b = tally[y];
 138         if (a > b) {
 139             return true;
 140         }
 141         return x < y;
 142     });
 143 
 144     for (const auto& k : sorted) {
 145         cout << k << '\t' << tally[k] << '\n';
 146         if (cout.eof()) {
 147             break;
 148         }
 149     }
 150 
 151     cout.flush();
 152     return errors++;
 153 }
 154 
 155 int main(int argc, char** argv) {
 156     try {
 157         return run(argc, argv) == 0 ? 0 : 1;
 158     } catch (const bad_alloc& e) {
 159         cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl;
 160         return 1;
 161     }
 162 }