File: bully.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 bully bully.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 "bully [files...]\n"
  56 "\n"
  57 "Tally/count each distinct line, also emitting bullet points. The output is\n"
  58 " a TSV table, with headers.\n"
  59 "";
  60 
  61 typedef map<string, uint64_t> string_tally;
  62 
  63 void handle_input(istream& in, string& line, string_tally& tally) {
  64     while (getline(in, line)) {
  65         tally[line]++;
  66     }
  67 }
  68 
  69 bool handle_file(const char* path, string& line, string_tally& tally) {
  70     ifstream f(path);
  71     if (f.is_open()) {
  72         handle_input(f, line, tally);
  73         return true;
  74     }
  75 
  76     cerr << ERROR_STYLE "can't open file named '" << path << "'" RESET_STYLE << endl;
  77     return false;
  78 }
  79 
  80 // run returns the number of errors
  81 int run(int argc, char** argv) {
  82     string line;
  83     string_tally tally;
  84 
  85     if (argc > 1) {
  86         if (
  87             strcmp(argv[1], "-h") == 0 ||
  88             strcmp(argv[1], "-help") == 0 ||
  89             strcmp(argv[1], "--h") == 0 ||
  90             strcmp(argv[1], "--help") == 0
  91         ) {
  92             cout << info;
  93             return 0;
  94         }
  95     }
  96 
  97     size_t dashes = 0;
  98     for (int i = 1; i < argc; i++) {
  99         if (argv[i][0] == '-' && argv[i][1] == 0) {
 100             dashes++;
 101         }
 102     }
 103 
 104     if (dashes > 1) {
 105         cerr << ERROR_STYLE "can't use dash (stdin) more than once" RESET_STYLE << endl;
 106         return 1;
 107     }
 108 
 109     cin.tie(NULL);
 110     ios_base::sync_with_stdio(false);
 111 
 112     cout << "value\ttally\tbullets" << endl;
 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> sorted;
 131     sorted.reserve(tally.size());
 132     for (auto kv = tally.begin(); kv != tally.end(); kv++) {
 133         sorted.push_back(kv->first);
 134     }
 135 
 136     sort(sorted.begin(), sorted.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 : sorted) {
 146         auto n = tally[k];
 147         cout << k << '\t' << n << '\t';
 148         while (n > 0) {
 149             // cout << "●";
 150             cout << "";
 151             n--;
 152         }
 153         cout << '\n';
 154 
 155         if (cout.eof()) {
 156             break;
 157         }
 158     }
 159 
 160     cout.flush();
 161     return errors;
 162 }
 163 
 164 int main(int argc, char** argv) {
 165     try {
 166         return run(argc, argv) == 0 ? 0 : 1;
 167     } catch (const bad_alloc& e) {
 168         cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl;
 169         return 1;
 170     }
 171 }