Revision 9c3d2b9e
src/PeerSet/peerset_ops.c | ||
---|---|---|
139 | 139 |
|
140 | 140 |
static int peerset_check_insert_pos(const struct peerset *h, const struct nodeID *id) |
141 | 141 |
{ |
142 |
int i;
|
|
142 |
int a, b, c, r;
|
|
143 | 143 |
|
144 |
for (i = h->n_elements - 1; i >= 0; i--) { |
|
145 |
int c; |
|
144 |
if (! h->n_elements) { |
|
145 |
return 0; |
|
146 |
} |
|
146 | 147 |
|
147 |
c = nodeid_peer_cmp(id, &h->elements[i]); |
|
148 |
if (c > 0) { |
|
149 |
break; |
|
150 |
} else if (c == 0) { |
|
151 |
return -1; |
|
148 |
a = 0; |
|
149 |
b = c = h->n_elements - 1; |
|
150 |
|
|
151 |
while ((r = nodeid_peer_cmp(id, &h->elements[b])) != 0) { |
|
152 |
if (r > 0) { |
|
153 |
if (b == c) { |
|
154 |
return b + 1; |
|
155 |
} else { |
|
156 |
a = b + 1; |
|
157 |
} |
|
158 |
} else { |
|
159 |
if (b == a) { |
|
160 |
return b; |
|
161 |
} else { |
|
162 |
c = b; |
|
163 |
} |
|
152 | 164 |
} |
165 |
b = (a + c) / 2; |
|
153 | 166 |
} |
154 | 167 |
|
155 |
return i+1;
|
|
168 |
return -1;
|
|
156 | 169 |
} |
157 | 170 |
|
158 | 171 |
void peerset_clear(struct peerset *h, int size) |
Also available in: Unified diff