summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-08-30 21:13:40 -0400
committerirungentoo <irungentoo@gmail.com>2013-08-30 21:13:40 -0400
commita93980e1443972c3284db21e314e6a58bcdacdb0 (patch)
tree1b72f825316fe23952a6886627fe30fc17007548
parentecddafd3832d145f2d7bdb84bb83ba385ff3cff0 (diff)
Cleaned up pull request.
-rw-r--r--toxcore/DHT.c17
-rw-r--r--toxcore/DHT.h8
2 files changed, 15 insertions, 10 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 02e0ed08..50bddbd6 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -56,8 +56,16 @@
56#define NAT_PING_REQUEST 0 56#define NAT_PING_REQUEST 0
57#define NAT_PING_RESPONSE 1 57#define NAT_PING_RESPONSE 1
58 58
59/* Used in the comparison function for sorting lists of Client_data. */
60typedef struct {
61 Client_data c1;
62 Client_data c2;
63} ClientPair;
64
65/* Create the declaration for a quick sort for ClientPair structures. */
66declare_quick_sort(ClientPair);
59/* Create the quicksort function. See misc_tools.h for the definition. */ 67/* Create the quicksort function. See misc_tools.h for the definition. */
60make_quick_sort(ClientPair); 68make_quick_sort(ClientPair);
61 69
62Client_data *DHT_get_close_list(DHT *dht) 70Client_data *DHT_get_close_list(DHT *dht)
63{ 71{
@@ -95,8 +103,10 @@ static int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2)
95static int client_id_cmp(ClientPair p1, ClientPair p2) 103static int client_id_cmp(ClientPair p1, ClientPair p2)
96{ 104{
97 int c = id_closest(p1.c1.client_id, p1.c2.client_id, p2.c2.client_id); 105 int c = id_closest(p1.c1.client_id, p1.c2.client_id, p2.c2.client_id);
106
98 if (c == 2) 107 if (c == 2)
99 return -1; 108 return -1;
109
100 return c; 110 return c;
101} 111}
102 112
@@ -292,7 +302,7 @@ static int replace_bad( Client_data *list,
292 return 1; 302 return 1;
293} 303}
294 304
295/*Sort the list. It will be sorted from furthest to closest. 305/*Sort the list. It will be sorted from furthest to closest.
296 * Turns list into data that quick sort can use and reverts it back. 306 * Turns list into data that quick sort can use and reverts it back.
297 */ 307 */
298static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id) 308static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id)
@@ -302,11 +312,14 @@ static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_i
302 uint32_t i; 312 uint32_t i;
303 313
304 memcpy(cd.client_id, comp_client_id, CLIENT_ID_SIZE); 314 memcpy(cd.client_id, comp_client_id, CLIENT_ID_SIZE);
315
305 for (i = 0; i < length; ++i) { 316 for (i = 0; i < length; ++i) {
306 pairs[i].c1 = cd; 317 pairs[i].c1 = cd;
307 pairs[i].c2 = list[i]; 318 pairs[i].c2 = list[i];
308 } 319 }
320
309 ClientPair_quick_sort(pairs, length, client_id_cmp); 321 ClientPair_quick_sort(pairs, length, client_id_cmp);
322
310 for (i = 0; i < length; ++i) 323 for (i = 0; i < length; ++i)
311 list[i] = pairs[i].c2; 324 list[i] = pairs[i].c2;
312} 325}
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 88073779..23a664bb 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -55,12 +55,6 @@ typedef struct {
55 uint64_t ret_timestamp; 55 uint64_t ret_timestamp;
56} Client_data; 56} Client_data;
57 57
58/* Used in the comparison function for sorting lists of Client_data. */
59typedef struct {
60 Client_data c1;
61 Client_data c2;
62} ClientPair;
63
64/*----------------------------------------------------------------------------------*/ 58/*----------------------------------------------------------------------------------*/
65 59
66typedef struct { 60typedef struct {
@@ -106,8 +100,6 @@ typedef struct {
106} DHT; 100} DHT;
107/*----------------------------------------------------------------------------------*/ 101/*----------------------------------------------------------------------------------*/
108 102
109/* Create the declaration for a quick sort for ClientPair structures. */
110declare_quick_sort(ClientPair);
111 103
112Client_data *DHT_get_close_list(DHT *dht); 104Client_data *DHT_get_close_list(DHT *dht);
113 105