forked from github/server
faster lookup for get_param with tries
eliminate unnecessary constatn from rand.c
This commit is contained in:
parent
efc87a16e3
commit
06f8ba9ee4
4 changed files with 52 additions and 57 deletions
2
critbit
2
critbit
|
@ -1 +1 @@
|
|||
Subproject commit dfe57a077222c6b572da61a79dc0687f81c10055
|
||||
Subproject commit 934c2dd94d41da19637a76a1a8b3dfeb7aa8524d
|
|
@ -1024,32 +1024,61 @@ void init_locale(struct locale *lang)
|
|||
}
|
||||
|
||||
typedef struct param {
|
||||
struct param *next;
|
||||
char *name;
|
||||
char *data;
|
||||
critbit_tree cb;
|
||||
} param;
|
||||
|
||||
size_t pack_keyval(const char *key, const char *value, char *data, size_t len) {
|
||||
size_t klen = strlen(key);
|
||||
size_t vlen = strlen(value);
|
||||
assert(klen + vlen + 2 + sizeof(vlen) <= len);
|
||||
memcpy(data, key, klen + 1);
|
||||
memcpy(data + klen + 1, value, vlen + 1);
|
||||
return klen + vlen + 2;
|
||||
}
|
||||
|
||||
void set_param(struct param **p, const char *key, const char *value)
|
||||
{
|
||||
struct param *par;
|
||||
assert(p);
|
||||
|
||||
par = *p;
|
||||
if (!par && value) {
|
||||
*p = par = calloc(1, sizeof(param));
|
||||
}
|
||||
if (par) {
|
||||
void *match;
|
||||
size_t klen = strlen(key) + 1;
|
||||
if (cb_find_prefix(&par->cb, key, klen, &match, 1, 0) > 0) {
|
||||
const char * kv = (const char *)match;
|
||||
size_t vlen = strlen(kv + klen) + 1;
|
||||
cb_erase(&par->cb, kv, klen + vlen);
|
||||
++global.cookie;
|
||||
}
|
||||
}
|
||||
if (value) {
|
||||
char data[512];
|
||||
size_t sz = pack_keyval(key, value, data, sizeof(data));
|
||||
if (cb_insert(&par->cb, data, sz) == CB_SUCCESS) {
|
||||
++global.cookie;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void free_params(struct param **pp) {
|
||||
while (*pp) {
|
||||
param *p = *pp;
|
||||
free(p->name);
|
||||
free(p->data);
|
||||
*pp = p->next;
|
||||
param *p = *pp;
|
||||
if (p) {
|
||||
cb_clear(&p->cb);
|
||||
free(p);
|
||||
}
|
||||
*pp = 0;
|
||||
}
|
||||
|
||||
const char *get_param(const struct param *p, const char *key)
|
||||
{
|
||||
while (p != NULL) {
|
||||
int cmp = strcmp(p->name, key);
|
||||
if (cmp == 0) {
|
||||
return p->data;
|
||||
}
|
||||
else if (cmp > 0) {
|
||||
break;
|
||||
}
|
||||
p = p->next;
|
||||
void *match;
|
||||
if (p && cb_find_prefix(&p->cb, key, strlen(key)+1, &match, 1, 0) > 0) {
|
||||
cb_get_kv_ex(match, &match);
|
||||
return (const char *)match;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1135,40 +1164,6 @@ double get_param_flt(const struct param *p, const char *key, double def)
|
|||
return str ? atof(str) : def;
|
||||
}
|
||||
|
||||
void set_param(struct param **p, const char *key, const char *data)
|
||||
{
|
||||
struct param *par;
|
||||
|
||||
++global.cookie;
|
||||
while (*p != NULL) {
|
||||
int cmp = strcmp((*p)->name, key);
|
||||
if (cmp == 0) {
|
||||
par = *p;
|
||||
free(par->data);
|
||||
if (data) {
|
||||
par->data = _strdup(data);
|
||||
}
|
||||
else {
|
||||
*p = par->next;
|
||||
free(par->name);
|
||||
free(par);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if (cmp > 0) {
|
||||
break;
|
||||
}
|
||||
p = &(*p)->next;
|
||||
}
|
||||
if (data) {
|
||||
par = malloc(sizeof(param));
|
||||
par->name = _strdup(key);
|
||||
par->data = _strdup(data);
|
||||
par->next = *p;
|
||||
*p = par;
|
||||
}
|
||||
}
|
||||
|
||||
void kernel_done(void)
|
||||
{
|
||||
/* calling this function releases memory assigned to static variables, etc.
|
||||
|
|
|
@ -130,6 +130,8 @@ static void test_get_set_param(CuTest * tc)
|
|||
set_param(&par, "bar", "foo");
|
||||
CuAssertStrEquals(tc, "bar", get_param(par, "foo"));
|
||||
CuAssertStrEquals(tc, "foo", get_param(par, "bar"));
|
||||
set_param(&par, "bar", "bar");
|
||||
CuAssertStrEquals(tc, "bar", get_param(par, "bar"));
|
||||
set_param(&par, "bar", NULL);
|
||||
CuAssertPtrEquals(tc, NULL, (void *)get_param(par, "bar"));
|
||||
free_params(&par);
|
||||
|
@ -175,11 +177,11 @@ static void test_forbiddenid(CuTest *tc) {
|
|||
CuSuite *get_config_suite(void)
|
||||
{
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
SUITE_ADD_TEST(suite, test_forbiddenid);
|
||||
SUITE_ADD_TEST(suite, test_getunit);
|
||||
SUITE_ADD_TEST(suite, test_read_unitid);
|
||||
SUITE_ADD_TEST(suite, test_get_set_param);
|
||||
SUITE_ADD_TEST(suite, test_param_int);
|
||||
SUITE_ADD_TEST(suite, test_param_flt);
|
||||
SUITE_ADD_TEST(suite, test_forbiddenid);
|
||||
SUITE_ADD_TEST(suite, test_getunit);
|
||||
SUITE_ADD_TEST(suite, test_read_unitid);
|
||||
return suite;
|
||||
}
|
||||
|
|
|
@ -26,8 +26,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <float.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define M_PIl 3.1415926535897932384626433832795029L /* pi */
|
||||
|
||||
/* NormalRand aus python, random.py geklaut, dort ist Referenz auf
|
||||
* den Algorithmus. mu = Mittelwert, sigma = Standardabweichung.
|
||||
* http://de.wikipedia.org/wiki/Standardabweichung#Diskrete_Gleichverteilung.2C_W.C3.BCrfel
|
||||
|
|
Loading…
Reference in a new issue