77
88#include " topPorts.hpp"
99
10- #include < functional>
1110#include < algorithm>
1211#include < array>
1312#include < cstdint>
13+ #include < functional>
1414#include < limits>
15- #include < vector>
1615#include < string>
16+ #include < vector>
1717
1818namespace ipxp {
1919
2020TopPorts::TopPorts (size_t top_ports_count) noexcept
21- : m_top_ports_count(top_ports_count)
21+ : m_top_ports_count(top_ports_count)
2222{
2323}
2424
2525std::string TopPorts::PortStats::to_string () const noexcept
2626{
27- return std::to_string (port) + " [" +
28- (protocol == Protocol::TCP ? " TCP " : " UDP " ) + " ] - " + std::to_string (frequency);
27+ return std::to_string (port) + " [" + (protocol == Protocol::TCP ? " TCP " : " UDP " ) + " ] - "
28+ + std::to_string (frequency);
2929}
3030
31- bool update_port_buffer (std::vector<TopPorts::PortStats>& port_buffer, TopPorts::PortStats port_stats) noexcept {
32- auto port_pos = std::lower_bound (port_buffer.begin (), port_buffer.end (), port_stats.frequency ,
33- [](const TopPorts::PortStats& port_frequency, size_t count) {
34- return port_frequency.frequency >= count;
35- });
36-
37- if (port_pos != port_buffer.end ()) {
38- std::copy_backward (port_pos, std::prev (port_buffer.end ()), port_buffer.end ());
39- *port_pos = port_stats;
40- return true ;
41- // ports_inserted = std::min<size_t>(m_top_ports_count, ports_inserted + 1);
42- }
43- return false ;
44- };
31+ bool update_port_buffer (
32+ std::vector<TopPorts::PortStats>& port_buffer,
33+ TopPorts::PortStats port_stats) noexcept
34+ {
35+ auto port_pos = std::lower_bound (
36+ port_buffer.begin (),
37+ port_buffer.end (),
38+ port_stats.frequency ,
39+ [](const TopPorts::PortStats& port_frequency, size_t count) {
40+ return port_frequency.frequency >= count;
41+ });
42+
43+ if (port_pos != port_buffer.end ()) {
44+ std::copy_backward (port_pos, std::prev (port_buffer.end ()), port_buffer.end ());
45+ *port_pos = port_stats;
46+ return true ;
47+ }
48+ return false ;
49+ };
4550
4651std::vector<TopPorts::PortStats> TopPorts::get_top_ports () const noexcept
4752{
48- std::vector<PortStats> port_buffer (m_top_ports_count);
49- size_t ports_inserted = 0 ;
50-
51- std::for_each (m_tcp_port_frequencies.begin (), m_tcp_port_frequencies.end (), [&, port = uint16_t {0 }](size_t frequency) mutable {
52- ports_inserted += update_port_buffer (port_buffer, {port++, frequency, PortStats::Protocol::TCP});
53- });
54- std::for_each (m_udp_port_frequencies.begin (), m_udp_port_frequencies.end (), [&, port = uint16_t {0 }](size_t frequency) mutable {
55- ports_inserted += update_port_buffer (port_buffer, {port++, frequency, PortStats::Protocol::UDP});
56- });
57-
58- port_buffer.resize (std::min (m_top_ports_count, ports_inserted));
59- return port_buffer;
53+ std::vector<PortStats> port_buffer (m_top_ports_count);
54+ size_t ports_inserted = 0 ;
55+
56+ std::for_each (
57+ m_tcp_port_frequencies.begin (),
58+ m_tcp_port_frequencies.end (),
59+ [&, port = uint16_t {0 }](size_t frequency) mutable {
60+ ports_inserted
61+ += update_port_buffer (port_buffer, {port++, frequency, PortStats::Protocol::TCP});
62+ });
63+ std::for_each (
64+ m_udp_port_frequencies.begin (),
65+ m_udp_port_frequencies.end (),
66+ [&, port = uint16_t {0 }](size_t frequency) mutable {
67+ ports_inserted
68+ += update_port_buffer (port_buffer, {port++, frequency, PortStats::Protocol::UDP});
69+ });
70+
71+ port_buffer.resize (std::min (m_top_ports_count, ports_inserted));
72+ return port_buffer;
6073}
6174
6275} // namespace ipxp
0 commit comments