Skip to content

Commit

Permalink
convert structs with single operator() to funcs
Browse files Browse the repository at this point in the history
Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Dec 20, 2024
1 parent 8da6a9b commit 8556d8d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 69 deletions.
7 changes: 3 additions & 4 deletions src/data/chunk_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ struct chunk_list_earliest_modified {
rak::timer m_time;
};

struct chunk_list_sort_index {
bool operator () (ChunkListNode* node1, ChunkListNode* node2) {
return node1->index() < node2->index();
}
bool
chunk_list_sort_index(ChunkListNode* node1, ChunkListNode* node2) {
return node1->index() < node2->index();
};

inline bool
Expand Down
39 changes: 15 additions & 24 deletions src/download/download_constructor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,19 @@

namespace torrent {

struct download_constructor_is_single_path {
bool operator () (Object::map_type::const_reference v) const {
return
std::strncmp(v.first.c_str(), "name.", sizeof("name.") - 1) == 0 &&
v.second.is_string();
}
bool
download_constructor_is_single_path(Object::map_type::const_reference v) {
return v.first.find("name.") == 0 && v.second.is_string();
};

struct download_constructor_is_multi_path {
bool operator () (Object::map_type::const_reference v) const {
return
std::strncmp(v.first.c_str(), "path.", sizeof("path.") - 1) == 0 &&
v.second.is_list();
}
bool
download_constructor_is_multi_path(Object::map_type::const_reference v) {
return v.first.find("path.") == 0 && v.second.is_list();
};

struct download_constructor_encoding_match :
public std::binary_function<const Path&, const char*, bool> {

bool operator () (const Path& p, const char* enc) {
return strcasecmp(p.encoding().c_str(), enc) == 0;
}
bool
download_constructor_encoding_match(const Path& p, const char* enc) {
return strcasecmp(p.encoding().c_str(), enc) == 0;
};

void
Expand Down Expand Up @@ -114,7 +105,7 @@ DownloadConstructor::parse_name(const Object& b) {
pathList.back().push_back(b.get_key_string("name"));

for (Object::map_const_iterator itr = b.as_map().begin();
(itr = std::find_if(itr, b.as_map().end(), download_constructor_is_single_path())) != b.as_map().end();
(itr = std::find_if(itr, b.as_map().end(), download_constructor_is_single_path)) != b.as_map().end();
++itr) {
pathList.push_back(Path());
pathList.back().set_encoding(itr->first.substr(sizeof("name.") - 1));
Expand Down Expand Up @@ -277,7 +268,7 @@ DownloadConstructor::parse_single_file(const Object& b, uint32_t chunkSize) {
pathList.back().push_back(b.get_key_string("name"));

for (Object::map_const_iterator itr = b.as_map().begin();
(itr = std::find_if(itr, b.as_map().end(), download_constructor_is_single_path())) != b.as_map().end();
(itr = std::find_if(itr, b.as_map().end(), download_constructor_is_single_path)) != b.as_map().end();
++itr) {
pathList.push_back(Path());
pathList.back().set_encoding(itr->first.substr(sizeof("name.") - 1));
Expand Down Expand Up @@ -311,8 +302,8 @@ DownloadConstructor::parse_multi_files(const Object& b, uint32_t chunkSize) {

Object::map_const_iterator itr = listItr->as_map().begin();
Object::map_const_iterator last = listItr->as_map().end();
while ((itr = std::find_if(itr, last, download_constructor_is_multi_path())) != last) {

while ((itr = std::find_if(itr, last, download_constructor_is_multi_path)) != last) {
pathList.push_back(create_path(itr->second.as_list(), itr->first.substr(sizeof("path.") - 1)));
++itr;
}
Expand Down Expand Up @@ -363,9 +354,9 @@ DownloadConstructor::choose_path(std::list<Path>* pathList) {

for ( ; encodingFirst != encodingLast; ++encodingFirst) {
auto itr = std::find_if(pathFirst, pathLast, [encodingFirst](const Path& p) {
return download_constructor_encoding_match()(p, encodingFirst->c_str());
return download_constructor_encoding_match(p, encodingFirst->c_str());
});

if (itr != pathLast)
pathList->splice(pathFirst, *pathList, itr);
}
Expand Down
22 changes: 9 additions & 13 deletions src/download/download_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,9 @@ DownloadMain::receive_tracker_request() {
m_tracker_controller->start_requesting();
}

struct SocketAddressCompact_less {
bool operator () (const SocketAddressCompact& a, const SocketAddressCompact& b) const {
return (a.addr < b.addr) || ((a.addr == b.addr) && (a.port < b.port));
}
bool
SocketAddressCompact_less(const SocketAddressCompact& a, const SocketAddressCompact& b) {
return (a.addr < b.addr) || ((a.addr == b.addr) && (a.port < b.port));
};

void
Expand Down Expand Up @@ -400,18 +399,16 @@ DownloadMain::do_peer_exchange() {
pcb->do_peer_exchange();
}

std::sort(current.begin(), current.end(), SocketAddressCompact_less());
std::sort(current.begin(), current.end(), SocketAddressCompact_less);

ProtocolExtension::PEXList added;
added.reserve(current.size());
std::set_difference(current.begin(), current.end(), m_ut_pex_list.begin(), m_ut_pex_list.end(),
std::back_inserter(added), SocketAddressCompact_less());
std::set_difference(current.begin(), current.end(), m_ut_pex_list.begin(), m_ut_pex_list.end(), std::back_inserter(added), SocketAddressCompact_less);

ProtocolExtension::PEXList removed;
removed.reserve(m_ut_pex_list.size());
std::set_difference(m_ut_pex_list.begin(), m_ut_pex_list.end(), current.begin(), current.end(),
std::back_inserter(removed), SocketAddressCompact_less());

std::set_difference(m_ut_pex_list.begin(), m_ut_pex_list.end(), current.begin(), current.end(), std::back_inserter(removed), SocketAddressCompact_less);

if (current.size() > m_info->max_size_pex_list()) {
// This test is only correct as long as we have a constant max
// size.
Expand All @@ -424,11 +421,10 @@ DownloadMain::do_peer_exchange() {
// Create the new m_ut_pex_list by removing any 'removed'
// addresses from the original list and then adding the new
// addresses.
m_ut_pex_list.erase(std::set_difference(m_ut_pex_list.begin(), m_ut_pex_list.end(), removed.begin(), removed.end(),
m_ut_pex_list.begin(), SocketAddressCompact_less()), m_ut_pex_list.end());
m_ut_pex_list.erase(std::set_difference(m_ut_pex_list.begin(), m_ut_pex_list.end(), removed.begin(), removed.end(), m_ut_pex_list.begin(), SocketAddressCompact_less), m_ut_pex_list.end());
m_ut_pex_list.insert(m_ut_pex_list.end(), added.begin(), added.end());

std::sort(m_ut_pex_list.begin(), m_ut_pex_list.end(), SocketAddressCompact_less());
std::sort(m_ut_pex_list.begin(), m_ut_pex_list.end(), SocketAddressCompact_less);

} else {
m_ut_pex_list.swap(current);
Expand Down
7 changes: 3 additions & 4 deletions src/protocol/request_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ struct request_list_same_piece {
Piece m_piece;
};

struct request_list_keep_request {
bool operator () (const BlockTransfer* d) {
return d->is_valid();
}
bool
request_list_keep_request(const BlockTransfer* d) {
return d->is_valid();
};

RequestList::~RequestList() {
Expand Down
15 changes: 8 additions & 7 deletions src/torrent/download/choke_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

namespace torrent {

struct choke_manager_less {
bool operator () (choke_queue::value_type v1, choke_queue::value_type v2) const { return v1.weight < v2.weight; }
bool
choke_manager_less(choke_queue::value_type v1, choke_queue::value_type v2) {
return v1.weight < v2.weight;
};

static inline bool
Expand Down Expand Up @@ -70,10 +71,10 @@ choke_queue::prepare_weights(group_stats gs) {

for (group_container_type::iterator itr = m_group_container.begin(), last = m_group_container.end(); itr != last; itr++) {
m_heuristics_list[m_heuristics].slot_choke_weight((*itr)->mutable_unchoked()->begin(), (*itr)->mutable_unchoked()->end());
std::sort((*itr)->mutable_unchoked()->begin(), (*itr)->mutable_unchoked()->end(), choke_manager_less());
std::sort((*itr)->mutable_unchoked()->begin(), (*itr)->mutable_unchoked()->end(), choke_manager_less);

m_heuristics_list[m_heuristics].slot_unchoke_weight((*itr)->mutable_queued()->begin(), (*itr)->mutable_queued()->end());
std::sort((*itr)->mutable_queued()->begin(), (*itr)->mutable_queued()->end(), choke_manager_less());
std::sort((*itr)->mutable_queued()->begin(), (*itr)->mutable_queued()->end(), choke_manager_less);

// Aggregate the statistics... Remember to update them after
// optimistic/pessimistic unchokes.
Expand Down Expand Up @@ -220,10 +221,10 @@ choke_queue::balance() {
void
choke_queue::balance_entry(group_entry* entry) {
m_heuristics_list[m_heuristics].slot_choke_weight(entry->mutable_unchoked()->begin(), entry->mutable_unchoked()->end());
std::sort(entry->mutable_unchoked()->begin(), entry->mutable_unchoked()->end(), choke_manager_less());
std::sort(entry->mutable_unchoked()->begin(), entry->mutable_unchoked()->end(), choke_manager_less);

m_heuristics_list[m_heuristics].slot_unchoke_weight(entry->mutable_queued()->begin(), entry->mutable_queued()->end());
std::sort(entry->mutable_queued()->begin(), entry->mutable_queued()->end(), choke_manager_less());
std::sort(entry->mutable_queued()->begin(), entry->mutable_queued()->end(), choke_manager_less);

int count = 0;
unsigned int min_slots = std::min(entry->min_slots(), entry->max_slots());
Expand Down Expand Up @@ -427,7 +428,7 @@ void
choke_manager_allocate_slots(choke_queue::iterator first, choke_queue::iterator last,
uint32_t max, uint32_t* weights, choke_queue::target_type* target) {
// Sorting the connections from the lowest to highest value.
// TODO: std::sort(first, last, choke_manager_less());
// TODO: std::sort(first, last, choke_manager_less);

// 'weightTotal' only contains the weight of targets that have
// connections to unchoke. When all connections are in a group are
Expand Down
22 changes: 5 additions & 17 deletions src/torrent/tracker_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,9 @@ TrackerList::has_active_not_scrape_in_group(uint32_t group) const {
return std::find_if(begin_group(group), end_group(group), std::mem_fun(&Tracker::is_busy_not_scrape)) != end_group(group);
}

// Need a custom predicate because the is_usable function is virtual.
struct tracker_usable_t : public std::unary_function<TrackerList::value_type, bool> {
bool operator () (const TrackerList::value_type& value) const { return value->is_usable(); }
};

bool
TrackerList::has_usable() const {
return std::find_if(begin(), end(), tracker_usable_t()) != end();
return std::any_of(begin(), end(), [](auto value) { return value->is_usable(); });
}

unsigned int
Expand All @@ -101,7 +96,7 @@ TrackerList::count_active() const {

unsigned int
TrackerList::count_usable() const {
return std::count_if(begin(), end(), tracker_usable_t());
return std::count_if(begin(), end(), [](auto value) { return value->is_usable(); });
}

void
Expand Down Expand Up @@ -219,24 +214,17 @@ TrackerList::insert_url(unsigned int group, const std::string& url, bool extra_t

TrackerList::iterator
TrackerList::find_url(const std::string& url) {
return std::find_if(begin(), end(), std::bind(std::equal_to<std::string>(), url,
std::bind(&Tracker::url, std::placeholders::_1)));
return std::find_if(begin(), end(), [&url](auto tracker) { return tracker->url() == url; });
}

TrackerList::iterator
TrackerList::find_usable(iterator itr) {
while (itr != end() && !tracker_usable_t()(*itr))
++itr;

return itr;
return std::find_if(itr, end(), [](auto value) { return value->is_usable(); });
}

TrackerList::const_iterator
TrackerList::find_usable(const_iterator itr) const {
while (itr != end() && !tracker_usable_t()(*itr))
++itr;

return itr;
return std::find_if(itr, end(), [](auto value) { return value->is_usable(); });
}

TrackerList::iterator
Expand Down

0 comments on commit 8556d8d

Please sign in to comment.