X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/96cedde3cdbc0b8ffc3f096a1b65d021b0226f99..e080adacaf1ba847ad467f5b8d21da385636ed3c:/examples/s4u/dht-kademlia/answer.cpp diff --git a/examples/s4u/dht-kademlia/answer.cpp b/examples/s4u/dht-kademlia/answer.cpp index 4c53034944..bbe546c036 100644 --- a/examples/s4u/dht-kademlia/answer.cpp +++ b/examples/s4u/dht-kademlia/answer.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2012-2019. The SimGrid Team. +/* Copyright (c) 2012-2020. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -10,37 +10,30 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(kademlia_node); namespace kademlia { -bool sortbydistance(const std::pair& a, const std::pair& b) +/** @brief Prints an Answer, for debugging purposes */ +void Answer::print() const { - return (a.second < b.second); -} - -/** @brief Prints a answer_t, for debugging purposes */ -void Answer::print() -{ - XBT_INFO("Searching %08x, size %u", destination_id_, size_); + XBT_INFO("Searching %08x, size %zu", destination_id_, nodes_.size()); unsigned int i = 0; - for (auto contact : nodes) + for (auto const& contact : nodes_) XBT_INFO("Node %08x: %08x is at distance %u", i++, contact.first, contact.second); } /** @brief Merge two answers together, only keeping the best nodes * @param source the source of the nodes to add */ -unsigned int Answer::merge(Answer* source) +unsigned int Answer::merge(const Answer* source) { if (this == source) return 0; unsigned int nb_added = 0; - for (auto contact : source->nodes) { - if (std::find(nodes.begin(), nodes.end(), contact) == nodes.end()) { - nodes.push_back(contact); - size_++; + for (auto const& contact : source->nodes_) { + if (std::find(nodes_.begin(), nodes_.end(), contact) == nodes_.end()) { + nodes_.push_back(contact); nb_added++; } } - std::sort(nodes.begin(), nodes.end(), sortbydistance); trim(); return nb_added; } @@ -48,35 +41,33 @@ unsigned int Answer::merge(Answer* source) /** @brief Trims an Answer, in order for it to have a size of less or equal to "bucket_size" */ void Answer::trim() { - while (size_ > BUCKET_SIZE) { - nodes.pop_back(); - size_--; - } - xbt_assert(nodes.size() == size_, "Wrong size for the answer"); + // sort by distance + std::sort(nodes_.begin(), nodes_.end(), + [](const std::pair& a, const std::pair& b) { + return (a.second < b.second); + }); + if (nodes_.size() > BUCKET_SIZE) + nodes_.resize(BUCKET_SIZE); } /** @brief Returns if the destination we are trying to find is found * @return if the destination is found. */ -bool Answer::destinationFound() +bool Answer::destinationFound() const { - if (nodes.empty()) - return 0; - - return (*nodes.begin()).second == 0; + return not nodes_.empty() && nodes_.begin()->second == 0; } -/** @brief Adds the content of a bucket unsigned into a answer object. - * @param bucket the bucket we have to had unsigned into - */ -void Answer::addBucket(Bucket* bucket) +/** @brief Adds the content of a bucket unsigned into an answer object. + * @param bucket the bucket we have to had unsigned into + */ +void Answer::addBucket(const Bucket* bucket) { xbt_assert((bucket != nullptr), "Provided a NULL bucket"); - for (auto id : bucket->nodes) { + for (auto const& id : bucket->nodes_) { unsigned int distance = id ^ destination_id_; - nodes.push_back(std::pair(id, distance)); - size_++; + nodes_.emplace_back(id, distance); } } -} +} // namespace kademlia