Revision 7b60aa43 src/PeerSet/peerset_ops.c

View differences:

src/PeerSet/peerset_ops.c
51 51
  return p;
52 52
}
53 53

  
54
static int peer_cmp(const void *a, const void *b)
55
{
56
  return nodeid_cmp(((const struct peer *)a)->id, ((const struct peer *)b)->id);
57
}
58

  
54 59
int peerset_add_peer(struct peerset *h, struct nodeID *id)
55 60
{
56 61
  struct peer *e;
......
75 80
  timerclear(&e->bmap_timestamp);
76 81
  e->cb_size = INT_MAX;
77 82

  
83
  qsort(h->elements, (size_t) h->n_elements, sizeof(h->elements[0]), peer_cmp);
84

  
78 85
  return h->n_elements;
79 86
}
80 87

  
......
115 122
  return -1;
116 123
}
117 124

  
125
static int nodeid_peer_cmp(const void *id, const void *peer)
126
{
127
  return nodeid_cmp( (const struct nodeID *) id, ((const struct peer *) peer)->id);
128
}
129

  
118 130
int peerset_check(const struct peerset *h, const struct nodeID *id)
119 131
{
120
  int i;
132
  struct peer *p;
121 133

  
122
  for (i = 0; i < h->n_elements; i++) {
123
    if (nodeid_equal(h->elements[i].id, id)) {
124
      return i;
125
    }
126
  }
134
  p = bsearch(id, h->elements, (size_t) h->n_elements, sizeof(h->elements[0]), nodeid_peer_cmp);
127 135

  
128
  return -1;
136
  return p ? p - h->elements : -1;
129 137
}
130 138

  
131 139
void peerset_clear(struct peerset *h, int size)

Also available in: Unified diff