Convert ctype generation to Red Black tree

This commit is contained in:
Baptiste Daroussin 2015-08-08 21:53:02 +00:00
parent 1fbfaeef7e
commit 6131dc6a88
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/projects/collation/; revision=286481

View File

@ -35,7 +35,7 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/avl.h>
#include <sys/tree.h>
#include <stdio.h>
#include <stdlib.h>
@ -68,24 +68,20 @@ __FBSDID("$FreeBSD$");
#define _E4 0
#define _E5 _CTYPE_T
static avl_tree_t ctypes;
static wchar_t last_ctype;
static int ctype_compare(const void *n1, const void *n2);
typedef struct ctype_node {
wchar_t wc;
int32_t ctype;
int32_t toupper;
int32_t tolower;
avl_node_t avl;
RB_ENTRY(ctype_node) entry;
} ctype_node_t;
typedef struct width_node {
wchar_t start;
wchar_t end;
int8_t width;
avl_node_t avl;
} width_node_t;
RB_HEAD(ctypes, ctype_node) ctypes;
RB_PROTOTYPE(ctypes, ctype_node, entry, ctype_compare);
RB_GENERATE(ctypes, ctype_node, entry, ctype_compare);
static int
ctype_compare(const void *n1, const void *n2)
@ -99,8 +95,7 @@ ctype_compare(const void *n1, const void *n2)
void
init_ctype(void)
{
avl_create(&ctypes, ctype_compare, sizeof (ctype_node_t),
offsetof(ctype_node_t, avl));
RB_INIT(&ctypes);
}
@ -172,17 +167,16 @@ get_ctype(wchar_t wc)
{
ctype_node_t srch;
ctype_node_t *ctn;
avl_index_t where;
srch.wc = wc;
if ((ctn = avl_find(&ctypes, &srch, &where)) == NULL) {
if ((ctn = RB_FIND(ctypes, &ctypes, &srch)) == NULL) {
if ((ctn = calloc(1, sizeof (*ctn))) == NULL) {
errf("out of memory");
return (NULL);
}
ctn->wc = wc;
avl_insert(&ctypes, ctn, where);
RB_INSERT(ctypes, &ctypes, ctn);
}
return (ctn);
}
@ -318,10 +312,9 @@ dump_ctype(void)
rl.mapupper[wc] = wc;
}
for (ctn = avl_first(&ctypes); ctn; ctn = AVL_NEXT(&ctypes, ctn)) {
RB_FOREACH(ctn, ctypes, &ctypes) {
int conflict = 0;
wc = ctn->wc;
/*