Switched from map to unordered_map where possible

This commit is contained in:
aristocratos 2021-05-18 22:11:34 +02:00
parent 9b837535bd
commit 806045c0a6
8 changed files with 75 additions and 83 deletions

View File

@ -1,7 +1,7 @@
PREFIX ?= /usr/local
DOCDIR ?= $(PREFIX)/share/btop/doc
CXX = g++
override CXXFLAGS += -std=c++20 -pthread -Wall -Wextra
override CXXFLAGS += -std=c++20 -O3 -pthread -Wall -Wextra -pedantic
INCLUDES = -I./src
btop: btop.cpp

View File

@ -18,6 +18,7 @@ tab-size = 4
#include <string>
#include <array>
#include <vector>
#include <thread>
#include <future>
@ -45,14 +46,14 @@ tab-size = 4
#endif
#endif
using std::string, std::vector, std::map, std::atomic, std::endl, std::cout, std::views::iota;
using std::string, std::vector, std::array, std::map, std::atomic, std::endl, std::cout, std::views::iota;
using namespace Tools;
//? ------------------------------------------------- GLOBALS ---------------------------------------------------------
namespace Global {
const vector<vector<string>> Banner_src = {
const vector<array<string, 2>> Banner_src = {
{"#E62525", "██████╗ ████████╗ ██████╗ ██████╗"},
{"#CD2121", "██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗ ██╗ ██╗"},
{"#B31D1D", "██████╔╝ ██║ ██║ ██║██████╔╝ ██████╗██████╗"},
@ -64,7 +65,7 @@ namespace Global {
const string Version = "0.0.1";
string banner;
uint banner_width;
const uint banner_width = 49;
}
@ -90,22 +91,16 @@ void argumentParser(int argc, char **argv){
}
//* Generate the btop++ banner
auto createBanner(){
struct out_vals {
uint w;
string s;
};
string createBanner(){
size_t z = 0;
uint width=0, new_len=0;
string b_color, bg, fg, out, oc, letter;
bool truecolor = Config::getB("truecolor");
int bg_i;
for (auto line: Global::Banner_src) {
if ((new_len = ulen(line[1])) > width) width = new_len;
fg = Theme::hex_to_color(line[0], !truecolor);
bg_i = 120-z*12;
bg = Theme::dec_to_color(bg_i, bg_i, bg_i, !truecolor);
for (uint i = 0; i < line[1].size(); i += 3) {
for (size_t i = 0; i < line[1].size(); i += 3) {
if (line[1][i] == ' '){
letter = ' ';
i -= 2;
@ -117,12 +112,11 @@ auto createBanner(){
out += letter;
oc = b_color;
}
z++;
if (z < Global::Banner_src.size()) out += Mv::l(new_len) + Mv::d(1);
if (++z < Global::Banner_src.size()) out += Mv::l(ulen(line[1])) + Mv::d(1);
}
out += Mv::r(18 - Global::Version.size()) + Fx::i + Theme::dec_to_color(0,0,0, !truecolor, "bg") +
Theme::dec_to_color(150, 150, 150, !truecolor) + "v" + Global::Version + Fx::reset;
return out_vals {width, out};
Theme::dec_to_color(150, 150, 150, !truecolor) + "v" + Global::Version + Fx::ui;
return out;
}
@ -171,9 +165,7 @@ int main(int argc, char **argv){
Theme::set(Global::Default_theme);
//? Create the btop++ banner
auto [banner_width, banner] = createBanner();
Global::banner_width = move(banner_width);
Global::banner = move(banner);
Global::banner = createBanner();
//* ------------------------------------------------ TESTING ------------------------------------------------------
@ -190,7 +182,7 @@ int main(int argc, char **argv){
cout << Mv::r(Term::width / 2 - Global::banner_width / 2) << Global::banner << endl;
// cout << string(Term::width - 1, '-') << endl;
int ill;
int ill = 0;
for (int i : iota(0, (int)Term::width)){
ill = (i <= (int)Term::width / 2) ? i : ill - 1;
cout << Theme::g("used")[ill] << "-";
@ -198,8 +190,7 @@ int main(int argc, char **argv){
cout << Fx::reset << endl;
//* Test theme
if (true) {
if (false) {
cout << "Theme generation took " << time_ms() - thts << "ms" << endl;
cout << "Colors:" << endl;
@ -282,7 +273,6 @@ int main(int argc, char **argv){
uint64_t tsl, timestamp2;
uint timer = 2000;
bool filtering = false;
vector<string> sorting;
bool reversing = false;
int sortint = Proc::sort_map["cpu lazy"];
vector<string> greyscale;
@ -303,19 +293,19 @@ int main(int argc, char **argv){
while (key != "q") {
timestamp = time_ms();
tsl = timestamp + timer;
auto plist = Proc::collect(Proc::sort_vector[sortint], reversing, filter);
auto plist = Proc::collect(Proc::sort_array[sortint], reversing, filter);
timestamp2 = time_ms();
timestamp = timestamp2 - timestamp;
ostring.clear();
lc = 0;
filter_cur = (filtering) ? Fx::bl + "" + Fx::reset : "";
ostring = Mv::save + Mv::u(2) + Mv::r(20) + trans(rjust("Filter: " + filter + filter_cur + string(Term::width / 3, ' ') +
"Sorting: " + string(Proc::sort_vector[sortint]), Term::width - 25, true, filtering)) + Mv::restore;
"Sorting: " + string(Proc::sort_array[sortint]), Term::width - 25, true, filtering)) + Mv::restore;
for (Proc::proc_info& procs : plist){
ostring += Mv::r(1) + greyscale[lc] + rjust(to_string(procs.pid), 8) + " " + ljust(procs.name, 16) + " " + ljust(procs.cmd, Term::width - 66, true) + " " +
rjust(to_string(procs.threads), 5) + " " + ljust(procs.user, 10) + " " + rjust(floating_humanizer(procs.mem, true), 5) + string(11, ' ');
ostring += (procs.cpu_p > 100) ? rjust(to_string(procs.cpu_p), 3) + " " : rjust(to_string(procs.cpu_p), 4);
for (auto& p : plist){
ostring += Mv::r(1) + greyscale[lc] + rjust(to_string(p.pid), 8) + " " + ljust(p.name, 16) + " " + ljust(p.cmd, Term::width - 66, true) + " " +
rjust(to_string(p.threads), 5) + " " + ljust(p.user, 10) + " " + rjust(floating_humanizer(p.mem, true), 5) + string(11, ' ');
ostring += (p.cpu_p > 100) ? rjust(to_string(p.cpu_p), 3) + " " : rjust(to_string(p.cpu_p), 4);
ostring += "\n";
if (lc++ > Term::height - 21) break;
}
@ -326,18 +316,17 @@ int main(int argc, char **argv){
while (time_ms() < tsl) {
if (Input::poll(tsl - time_ms())) key = Input::get();
else { key.clear() ; continue; }
// if (key != "") continue;
if (filtering) {
if (key == "enter") filtering = false;
else if (key == "backspace") {if (!filter.empty()) filter = uresize(filter, ulen(filter) - 1);}
else if (key == "space") filter.push_back(' ');
else if (ulen(key) == 1 ) filter.append(key);
else { key.clear() ; continue; }
else { key.clear(); continue; }
break;
}
else if (key == "q") break;
else if (key == "left") { if (--sortint < 0) sortint = (int)Proc::sort_vector.size() - 1; }
else if (key == "right") { if (++sortint > (int)Proc::sort_vector.size() - 1) sortint = 0; }
else if (key == "left") { if (--sortint < 0) sortint = (int)Proc::sort_array.size() - 1; }
else if (key == "right") { if (++sortint > (int)Proc::sort_array.size() - 1) sortint = 0; }
else if (key == "f") filtering = true;
else if (key == "r") reversing = !reversing;
else if (key == "delete") filter.clear();

View File

@ -21,11 +21,11 @@ tab-size = 4
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <btop_tools.h>
using std::string, std::vector, std::map;
using std::string, std::vector, std::unordered_map;
using namespace Tools;
@ -35,7 +35,7 @@ namespace Config {
bool changed = false;
map<string, string> strings = {
unordered_map<string, string> strings = {
{"color_theme", "Default"},
{"shown_boxes", "cpu mem net proc"},
{"proc_sorting", "cpu lazy"},
@ -52,7 +52,7 @@ namespace Config {
{"net_iface", ""},
{"log_level", "WARNING"}
};
map<string, bool> bools = {
unordered_map<string, bool> bools = {
{"theme_background", true},
{"truecolor", true},
{"proc_reversed", false},
@ -84,12 +84,12 @@ namespace Config {
{"show_battery", true},
{"show_init", false}
};
map<string, int> ints = {
unordered_map<string, int> ints = {
{"update_ms", 2000},
{"proc_update_mult", 2},
{"tree_depth", 3}
};
};
}
//* Return config value <name> as a bool
bool& getB(string name){
@ -128,6 +128,6 @@ namespace Config {
(void)source;
return true;
}
};
}
#endif

View File

@ -20,17 +20,18 @@ tab-size = 4
#define _btop_globs_included_ 1
#include <string>
#include <map>
#include <vector>
#include <array>
#include <atomic>
#include <unordered_map>
using std::string, std::vector, std::map, std::atomic;
using std::string, std::vector, std::unordered_map, std::array, std::atomic;
namespace Global {
atomic<bool> stop_all(false);
const map<string, string> Default_theme = {
const unordered_map<string, string> Default_theme = {
{ "main_bg", "#00" },
{ "main_fg", "#cc" },
{ "title", "#ee" },
@ -75,7 +76,7 @@ namespace Global {
{ "process_end", "#d45454" }
};
const map<string, map<string, vector<string>>> Menus = {
const unordered_map<string, unordered_map<string, vector<string>>> Menus = {
{ "options", {
{ "normal", {
"┌─┐┌─┐┌┬┐┬┌─┐┌┐┌┌─┐",
@ -115,8 +116,8 @@ namespace Global {
};
//? Units for floating_humanizer function
const vector<string> Units_bit = {"bit", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib", "Bib", "GEb"};
const vector<string> Units_byte = {"Byte", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", "BiB", "GEB"};
const array<string, 11> Units_bit = {"bit", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib", "Bib", "GEb"};
const array<string, 11> Units_byte = {"Byte", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", "BiB", "GEB"};
}
@ -132,7 +133,7 @@ namespace Symbols {
const string div_up = "";
const string div_down = "";
const vector<string> superscript = { "", "¹", "²", "³", "", "", "", "", "", "" };
const array<string, 10> superscript = { "", "¹", "²", "³", "", "", "", "", "", "" };
}
#endif

View File

@ -20,13 +20,13 @@ tab-size = 4
#define _btop_input_included_ 1
#include <string>
#include <map>
#include <unordered_map>
#include <iostream>
#include <btop_globs.h>
#include <btop_tools.h>
using std::string, std::map, std::cin;
using std::string, std::unordered_map, std::cin;
using namespace Tools;
@ -34,7 +34,7 @@ using namespace Tools;
namespace Input {
namespace {
//* Map for translating key codes to readable values
const map<string, string> Key_escapes = {
const unordered_map<string, string> Key_escapes = {
{"\033", "escape"},
{"\n", "enter"},
{" ", "space"},
@ -69,7 +69,7 @@ namespace Input {
{"[23~", "f11"},
{"[24~", "f12"}
};
};
}
//* Last entered key
string last = "";
@ -108,6 +108,6 @@ namespace Input {
last.clear();
}
};
}
#endif

View File

@ -21,7 +21,8 @@ tab-size = 4
#include <string>
#include <vector>
#include <map>
#include <array>
#include <unordered_map>
#include <atomic>
#include <fstream>
#include <filesystem>
@ -34,7 +35,7 @@ tab-size = 4
#include <btop_tools.h>
using std::string, std::vector, std::map, std::ifstream, std::atomic, std::numeric_limits, std::streamsize;
using std::string, std::vector, std::array, std::ifstream, std::atomic, std::numeric_limits, std::streamsize, std::unordered_map;
namespace fs = std::filesystem;
using namespace Tools;
@ -65,8 +66,8 @@ namespace Proc {
string name, cmd, user;
uint64_t cpu_t = 0, cpu_s = 0;
};
map<uint, p_cache> cache;
map<string, string> uid_user;
unordered_map<uint, p_cache> cache;
unordered_map<string, string> uid_user;
fs::path passwd_path;
fs::file_time_type passwd_time;
uint counter = 0;
@ -76,7 +77,7 @@ namespace Proc {
atomic<bool> stop (false);
atomic<bool> running (false);
vector<string> sort_vector = {
array<string, 8> sort_array = {
"pid",
"name",
"command",
@ -86,7 +87,7 @@ namespace Proc {
"cpu direct",
"cpu lazy",
};
map<string, uint> sort_map;
unordered_map<string, uint> sort_map;
//* proc_info: pid, name, cmd, threads, user, mem, cpu_p, cpu_c
struct proc_info {
@ -146,8 +147,8 @@ namespace Proc {
return procs;
}
pid_str = fs::path(d.path()).filename();
cpu = 0.0;
rss_mem = 0;
cpu = 0.0; cpu_s = 0.0; cpu_t = 0;
rss_mem = 0; threads = 0;
new_cache = false;
if (d.is_directory() && isdigit(pid_str[0])) {
pid = stoul(pid_str);
@ -284,7 +285,7 @@ namespace Proc {
//* Clear dead processes from cache at a regular interval
if (++counter >= 10000 || (filter.empty() && cache.size() > procs.size() + 100)) {
map<uint, p_cache> r_cache;
unordered_map<uint, p_cache> r_cache;
counter = 0;
for (auto& p : c_pids) r_cache[p] = cache[p];
cache = move(r_cache);
@ -301,9 +302,9 @@ namespace Proc {
tstamp = time_ms();
passwd_path = (fs::exists(fs::path("/etc/passwd"))) ? fs::path("/etc/passwd") : passwd_path;
uint i = 0;
for (auto& item : sort_vector) sort_map[item] = i++;
for (auto& item : sort_array) sort_map[item] = i++;
}
};
}

View File

@ -23,13 +23,14 @@ tab-size = 4
#include <cmath>
#include <vector>
#include <map>
#include <unordered_map>
#include <ranges>
#include <btop_globs.h>
#include <btop_tools.h>
#include <btop_config.h>
using std::string, std::round, std::vector, std::map, std::stoi, std::views::iota, std::array;
using std::string, std::round, std::vector, std::map, std::stoi, std::views::iota, std::array, std::unordered_map;
using namespace Tools;
namespace Theme {
@ -98,16 +99,16 @@ namespace Theme {
else return pre + to_string(r) + ";" + to_string(g) + ";" + to_string(b) + "m";
}
//* Return a map of "r", "g", "b", 0-255 values for a 24-bit color escape string
map<string, int> esc_to_rgb(string c_string){
map<string, int> rgb = {{"r", 0}, {"g", 0}, {"b", 0}};
//* Return an array of red, green and blue, 0-255 values for a 24-bit color escape string
auto esc_to_rgb(string c_string){
array<int, 3> rgb = {-1, -1, -1};
if (c_string.size() >= 14){
c_string.erase(0, 7);
auto c_split = ssplit(c_string, ";");
if (c_split.size() == 3){
rgb["r"] = stoi(c_split[0]);
rgb["g"] = stoi(c_split[1]);
rgb["b"] = stoi(c_split[2].erase(c_split[2].size()));
rgb[0] = stoi(c_split[0]);
rgb[1] = stoi(c_split[1]);
rgb[2] = stoi(c_split[2].erase(c_split[2].size()));
}
}
return rgb;
@ -115,9 +116,9 @@ namespace Theme {
namespace {
map<string, string> colors;
map<string, array<int, 3>> rgbs;
map<string, array<string, 101>> gradients;
unordered_map<string, string> colors;
unordered_map<string, array<int, 3>> rgbs;
unordered_map<string, array<string, 101>> gradients;
//* Convert hex color to a array of decimals
array<int, 3> hex_to_dec(string hexa){
@ -141,7 +142,7 @@ namespace Theme {
}
//* Generate colors and rgb decimal vectors for the theme
void generateColors(map<string, string>& source){
void generateColors(unordered_map<string, string>& source){
vector<string> t_rgb;
string depth;
colors.clear(); rgbs.clear();
@ -209,7 +210,7 @@ namespace Theme {
//* Set current theme using <source> map
void set(map<string, string> source){
void set(unordered_map<string, string> source){
generateColors(source);
generateGradients();
Term::fg = colors.at("main_fg");
@ -222,16 +223,16 @@ namespace Theme {
return colors.at(name);
}
//* Return vector of escape codes for color gradient <name>
//* Return array of escape codes for color gradient <name>
auto g(string name){
return gradients.at(name);
}
//* Return vector of red, green and blue in decimal for color <name>
//* Return array of red, green and blue in decimal for color <name>
auto dec(string name){
return rgbs.at(name);
}
};
}
#endif

View File

@ -33,7 +33,7 @@ tab-size = 4
#include <btop_globs.h>
using std::string, std::vector, std::map, std::regex, std::max, std::to_string, std::cin;
using std::string, std::vector, std::regex, std::max, std::to_string, std::cin;
//? ------------------------------------------------- NAMESPACES ------------------------------------------------------
@ -82,7 +82,7 @@ namespace Fx {
string uncolor(string& s){
return regex_replace(s, color_regex, "");
}
};
}
//* Collection of escape codes and functions for cursor manipulation
namespace Mv {
@ -106,7 +106,7 @@ namespace Mv {
//* Restore saved cursor postion
const string restore = Fx::e + "u";
};
}
//* Collection of escape codes and functions for terminal manipulation
namespace Term {
@ -138,7 +138,7 @@ namespace Term {
height = w.ws_row;
return resized;
}
};
}
//* Hide terminal cursor
@ -214,7 +214,7 @@ namespace Term {
initialized = false;
}
}
};
}
//? --------------------------------------------------- FUNCTIONS -----------------------------------------------------