summaryrefslogtreecommitdiff
path: root/ot_vector.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_vector.c')
-rw-r--r--ot_vector.c24
1 files changed, 7 insertions, 17 deletions
diff --git a/ot_vector.c b/ot_vector.c
index 29bdd49..f7481f1 100644
--- a/ot_vector.c
+++ b/ot_vector.c
@@ -17,9 +17,7 @@
17#include "uint16.h" 17#include "uint16.h"
18 18
19static int vector_compare_peer(const void *peer1, const void *peer2 ) { 19static int vector_compare_peer(const void *peer1, const void *peer2 ) {
20 int32_t cmp = READ32(peer2,0) - READ32(peer1,0); 20 return memcmp( peer1, peer2, OT_PEER_COMPARE_SIZE );
21 if (cmp == 0) cmp = READ16(peer2,4) - READ16(peer1,4);
22 return cmp;
23} 21}
24 22
25/* This function gives us a binary search that returns a pointer, even if 23/* This function gives us a binary search that returns a pointer, even if
@@ -30,19 +28,14 @@ static int vector_compare_peer(const void *peer1, const void *peer2 ) {
30*/ 28*/
31void *binary_search( const void * const key, const void * base, const size_t member_count, const size_t member_size, 29void *binary_search( const void * const key, const void * base, const size_t member_count, const size_t member_size,
32 size_t compare_size, int *exactmatch ) { 30 size_t compare_size, int *exactmatch ) {
33 size_t offs, mc = member_count; 31 size_t mc = member_count;
34 int8_t *lookat = ((int8_t*)base) + member_size * (mc >> 1); 32 int8_t *lookat = ((int8_t*)base) + member_size * (mc >> 1);
35 int32_t key_cache = READ32(key,0);
36 *exactmatch = 1; 33 *exactmatch = 1;
37 34
38 while( mc ) { 35 while( mc ) {
39 int32_t cmp = READ32(lookat,0) - key_cache; 36 int32_t cmp = memcmp( lookat, key, compare_size );
40 if (cmp == 0) { 37 if( cmp == 0 )
41 for( offs = 4; cmp == 0 && offs < compare_size; offs += 4 ) 38 return (void *)lookat;
42 cmp = READ32(lookat,offs) - READ32(key,offs);
43 if( cmp == 0 )
44 return (void *)lookat;
45 }
46 39
47 if (cmp < 0) { 40 if (cmp < 0) {
48 base = (void*)(lookat + member_size); 41 base = (void*)(lookat + member_size);
@@ -60,13 +53,10 @@ void *binary_search( const void * const key, const void * base, const size_t mem
60ot_peer *binary_search_peer( const ot_peer * const peer, const ot_peer * base, const size_t member_count, int *exactmatch ) { 53ot_peer *binary_search_peer( const ot_peer * const peer, const ot_peer * base, const size_t member_count, int *exactmatch ) {
61 size_t mc = member_count; 54 size_t mc = member_count;
62 const ot_peer *lookat = base + (mc >> 1); 55 const ot_peer *lookat = base + (mc >> 1);
63 int32_t low = READ32(peer,0);
64 int16_t high = READ16(peer,4);
65 *exactmatch = 1; 56 *exactmatch = 1;
66 57
67 while( mc ) { 58 while( mc ) {
68 int32_t cmp = READ32(lookat,0) - low; 59 int32_t cmp = memcmp(lookat,peer,OT_PEER_COMPARE_SIZE );
69 if(cmp == 0) cmp = READ16(lookat,4) - high;
70 if(cmp == 0) return (ot_peer*)lookat; 60 if(cmp == 0) return (ot_peer*)lookat;
71 61
72 if (cmp < 0) { 62 if (cmp < 0) {
@@ -84,7 +74,7 @@ ot_peer *binary_search_peer( const ot_peer * const peer, const ot_peer * base, c
84 74
85 75
86static uint8_t vector_hash_peer( ot_peer *peer, int bucket_count ) { 76static uint8_t vector_hash_peer( ot_peer *peer, int bucket_count ) {
87 unsigned int hash = 5381, i = 6; 77 unsigned int hash = 5381, i = OT_PEER_COMPARE_SIZE;
88 uint8_t *p = (uint8_t*)peer; 78 uint8_t *p = (uint8_t*)peer;
89 while( i-- ) hash += (hash<<5) + *(p++); 79 while( i-- ) hash += (hash<<5) + *(p++);
90 return hash % bucket_count; 80 return hash % bucket_count;