Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #2 from mquinson/master
[simgrid.git] / examples / msg / kademlia / answer.c
index 4eb095f..5aa893f 100644 (file)
@@ -1,15 +1,14 @@
-/* Copyright (c) 2012. The SimGrid Team.
+/* Copyright (c) 2012-2014. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
+
 #include "answer.h"
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(msg_kademlia_node);
 
-/**
-  * Initialize a node answer object.
-  */
+/** Initialize a node answer object. */
 answer_t answer_init(unsigned int destination_id)
 {
   answer_t answer = xbt_new(s_answer_t, 1);
@@ -20,9 +19,7 @@ answer_t answer_init(unsigned int destination_id)
   return answer;
 }
 
-/**
-  * Destroys a node answer object.
-  */
+/** Destroys a node answer object. */
 void answer_free(answer_t answer)
 {
   unsigned int i;
@@ -33,22 +30,18 @@ void answer_free(answer_t answer)
   xbt_free(answer);
 }
 
-/**
-  * @brief Prints a answer_t, for debugging purposes
-  */
+/** @brief Prints a answer_t, for debugging purposes */
 void answer_print(answer_t answer)
 {
   unsigned int cpt;
   node_contact_t contact;
   XBT_INFO("Searching %08x, size %d", answer->destination_id, answer->size);
   xbt_dynar_foreach(answer->nodes, cpt, contact) {
-    XBT_INFO("Node %08x: %08x is at distance %d", cpt, contact->id,
-             contact->distance);
+    XBT_INFO("Node %08x: %08x is at distance %d", cpt, contact->id, contact->distance);
   }
 }
 
-/**
-  * @brief Merge two answer_t together, only keeping the best nodes
+/** @brief Merge two answer_t together, only keeping the best nodes
   * @param destination the destination in which the nodes will be put
   * @param source the source of the nodes to add
   */
@@ -71,18 +64,21 @@ unsigned int answer_merge(answer_t destination, answer_t source)
   return nb_added;
 }
 
-/**
-  * Helper to sort answer_t objects
-  */
+/** Helper to sort answer_t objects */
 static int _answer_sort_function(const void *e1, const void *e2)
 {
   node_contact_t c1 = *(void **) e1;
   node_contact_t c2 = *(void **) e2;
-  return c1->distance >= c2->distance;
+  if (c1->distance == c2->distance)
+    return 0;
+  else
+    if (c1->distance < c2->distance)
+      return -1;
+    else
+      return 1;
 }
 
-/**
-  * Sorts a answer_t, by node distance.
+/** @brief Sorts a answer_t, by node distance.
   * @param answer the answer to sort
   * @param destination_id the id of the guy we are trying to find
   */
@@ -91,9 +87,7 @@ void answer_sort(answer_t answer)
   xbt_dynar_sort(answer->nodes, &_answer_sort_function);
 }
 
-/**
-  * Trims a answer_t, in order for it to have a size of less or equal
-  * to "bucket_size"
+/** @brief Trims a answer_t, in order for it to have a size of less or equal to "bucket_size"
   * @param answer the answer_t to trim
   */
 void answer_trim(answer_t answer)
@@ -104,12 +98,10 @@ void answer_trim(answer_t answer)
     answer->size--;
     node_contact_free(value);
   }
-  xbt_assert(xbt_dynar_length(answer->nodes) == answer->size,
-             "Wrong size for the answer");
+  xbt_assert(xbt_dynar_length(answer->nodes) == answer->size, "Wrong size for the answer");
 }
 
-/**
-  * Adds the content of a bucket unsigned into a answer object.
+/** @brief Adds the content of a bucket unsigned into a answer object.
   * @param bucket the bucket we have to had unsigned into
   * @param answer the answer object we're going  to put the data in
   * @param destination_id the id of the guy we are trying to find.
@@ -129,16 +121,14 @@ void answer_add_bucket(bucket_t bucket, answer_t answer)
   }
 }
 
-/**
-  * Returns if the id supplied is in the answer.
+/** @brief Returns if the id supplied is in the answer.
   * @param id : id we're looking for
   */
 unsigned int answer_contains(answer_t answer, unsigned int id)
 {
-  unsigned int i = 0, size = xbt_dynar_length(answer->nodes);
+  unsigned int i = 0;
   node_contact_t contact;
-  for (i = 0; i < size; i++) {
-    contact = xbt_dynar_get_as(answer->nodes, i, node_contact_t);
+  xbt_dynar_foreach(answer->nodes, i, contact){
     if (id == contact->id) {
       return 1;
     }
@@ -146,8 +136,7 @@ unsigned int answer_contains(answer_t answer, unsigned int id)
   return 0;
 }
 
-/**
-  * Returns if the destination we are trying to find is found
+/** @brief Returns if the destination we are trying to find is found
   * @param answer the answer
   * @return if the destination is found.
   */
@@ -156,7 +145,6 @@ unsigned int answer_destination_found(answer_t answer)
   if (xbt_dynar_length(answer->nodes) < 1) {
     return 0;
   }
-  node_contact_t contact_tail =
-      xbt_dynar_get_as(answer->nodes, 0, node_contact_t);
+  node_contact_t contact_tail = xbt_dynar_get_as(answer->nodes, 0, node_contact_t);
   return contact_tail->distance == 0;
 }