mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-04 17:35:42 +02:00
Use a hash table of binary trees for string pooling, not just a binary tree
This commit is contained in:
@@ -2,11 +2,23 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "pool.h"
|
#include "pool.h"
|
||||||
|
|
||||||
static struct pool_val *pool1(struct pool *p, char *s, int type, int (*compare)(const char *, const char *)) {
|
#define POOL_WIDTH 256
|
||||||
struct pool_val **v = &(p->vals);
|
|
||||||
|
static int hash(char *s) {
|
||||||
|
int h = 0;
|
||||||
|
for (; *s; s++) {
|
||||||
|
h = h * 37 + *s;
|
||||||
|
}
|
||||||
|
h = h & 0xFF;
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct pool_val *pool(struct pool *p, char *s, int type) {
|
||||||
|
int h = hash(s);
|
||||||
|
struct pool_val **v = &(p->vals[h]);
|
||||||
|
|
||||||
while (*v != NULL) {
|
while (*v != NULL) {
|
||||||
int cmp = compare(s, (*v)->s);
|
int cmp = strcmp(s, (*v)->s);
|
||||||
|
|
||||||
if (cmp == 0) {
|
if (cmp == 0) {
|
||||||
cmp = type - (*v)->type;
|
cmp = type - (*v)->type;
|
||||||
@@ -41,7 +53,8 @@ static struct pool_val *pool1(struct pool *p, char *s, int type, int (*compare)(
|
|||||||
}
|
}
|
||||||
|
|
||||||
int is_pooled(struct pool *p, char *s, int type) {
|
int is_pooled(struct pool *p, char *s, int type) {
|
||||||
struct pool_val **v = &(p->vals);
|
int h = hash(s);
|
||||||
|
struct pool_val **v = &(p->vals[h]);
|
||||||
|
|
||||||
while (*v != NULL) {
|
while (*v != NULL) {
|
||||||
int cmp = strcmp(s, (*v)->s);
|
int cmp = strcmp(s, (*v)->s);
|
||||||
@@ -62,10 +75,6 @@ int is_pooled(struct pool *p, char *s, int type) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct pool_val *pool(struct pool *p, char *s, int type) {
|
|
||||||
return pool1(p, s, type, strcmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
void pool_free1(struct pool *p, void (*func)(void *)) {
|
void pool_free1(struct pool *p, void (*func)(void *)) {
|
||||||
while (p->head != NULL) {
|
while (p->head != NULL) {
|
||||||
if (func != NULL) {
|
if (func != NULL) {
|
||||||
@@ -79,6 +88,8 @@ void pool_free1(struct pool *p, void (*func)(void *)) {
|
|||||||
|
|
||||||
p->head = NULL;
|
p->head = NULL;
|
||||||
p->tail = NULL;
|
p->tail = NULL;
|
||||||
|
|
||||||
|
free(p->vals);
|
||||||
p->vals = NULL;
|
p->vals = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +103,7 @@ void pool_free_strings(struct pool *p) {
|
|||||||
|
|
||||||
void pool_init(struct pool *p, int n) {
|
void pool_init(struct pool *p, int n) {
|
||||||
p->n = n;
|
p->n = n;
|
||||||
p->vals = NULL;
|
p->vals = calloc(POOL_WIDTH, sizeof(struct pool_val *));
|
||||||
p->head = NULL;
|
p->head = NULL;
|
||||||
p->tail = NULL;
|
p->tail = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user