From: Florian Forster Date: Mon, 10 May 2010 09:50:43 +0000 (+0200) Subject: population_set_replacement_method: New function. X-Git-Url: https://git.octo.it/?p=libpopulation.git;a=commitdiff_plain population_set_replacement_method: New function. --- diff --git a/src/libpopulation.c b/src/libpopulation.c index fb99db1..c27fd9b 100644 --- a/src/libpopulation.c +++ b/src/libpopulation.c @@ -107,6 +107,7 @@ struct population_s #define POPULATION_FLAG_LISTEN 0x01 #define POPULATION_FLAG_SHUTDOWN 0x02 +#define POPULATION_FLAG_EXPLORE 0x10 int flags; pthread_t listen_thread_id; @@ -489,6 +490,27 @@ int population_set_serialization (population_t *p, /* {{{ */ return (0); } /* }}} int population_set_serialization */ +int population_set_replacement_method (population_t *p, int method) /* {{{ */ +{ + int status = 0; + + if (p == NULL) + return (EINVAL); + + pthread_mutex_lock (&p->lock); + + if (method == POPULATION_REPLACEMENT_EXPLOIT) + p->flags &= ~POPULATION_FLAG_EXPLORE; + else if (method == POPULATION_REPLACEMENT_EXPLORE) + p->flags |= POPULATION_FLAG_EXPLORE; + else + status = EINVAL; + + pthread_mutex_unlock (&p->lock); + + return (0); +} /* }}} int population_set_replacement_method */ + int population_add_peer (population_t *p, const char *node, /* {{{ */ const char *port) { @@ -747,6 +769,10 @@ int population_insert (population_t *p, void *pi_orig) /* {{{ */ chance = (int) (((double) (chance_j + chance_pi)) * (rand() / (RAND_MAX + 1.0))); + + if (p->flags & POPULATION_FLAG_EXPLORE) + chance *= .5; + if (chance < chance_j) /* j looses ;) */ { void *temp0; diff --git a/src/population.h b/src/population.h index 8dc7c73..cc513d4 100644 --- a/src/population.h +++ b/src/population.h @@ -35,6 +35,10 @@ int population_set_size (population_t *p, size_t population_size); int population_set_serialization (population_t *p, pi_serialize_f serialize, pi_unserialize_f unserialize); +#define POPULATION_REPLACEMENT_EXPLOIT 1 +#define POPULATION_REPLACEMENT_EXPLORE 2 +int population_set_replacement_method (population_t *p, int method); + #define POPULATION_DEFAULT_PORT "46835" int population_add_peer (population_t *p, const char *node, const char *port); int population_start_listen_thread (population_t *p,