#include <sys/cdefs.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <wchar.h>
#include "cmap.h"
static struct cmapnode *cmap_splay(struct cmapnode *, wint_t);
struct cmap *
cmap_alloc(void)
{
struct cmap *cm;
cm = malloc(sizeof(*cm));
if (cm == NULL)
return (NULL);
cm->cm_root = NULL;
cm->cm_def = CM_DEF_SELF;
cm->cm_havecache = false;
cm->cm_min = cm->cm_max = 0;
return (cm);
}
bool
cmap_add(struct cmap *cm, wint_t from, wint_t to)
{
struct cmapnode *cmn, *ncmn;
cm->cm_havecache = false;
if (cm->cm_root == NULL) {
cmn = malloc(sizeof(*cmn));
if (cmn == NULL)
return (false);
cmn->cmn_from = from;
cmn->cmn_to = to;
cmn->cmn_left = cmn->cmn_right = NULL;
cm->cm_root = cmn;
cm->cm_min = cm->cm_max = from;
return (true);
}
cmn = cm->cm_root = cmap_splay(cm->cm_root, from);
if (cmn->cmn_from == from) {
cmn->cmn_to = to;
return (true);
}
ncmn = malloc(sizeof(*ncmn));
if (ncmn == NULL)
return (false);
ncmn->cmn_from = from;
ncmn->cmn_to = to;
if (from < cmn->cmn_from) {
ncmn->cmn_left = cmn->cmn_left;
ncmn->cmn_right = cmn;
cmn->cmn_left = NULL;
} else {
ncmn->cmn_right = cmn->cmn_right;
ncmn->cmn_left = cmn;
cmn->cmn_right = NULL;
}
if (from < cm->cm_min)
cm->cm_min = from;
if (from > cm->cm_max)
cm->cm_max = from;
cm->cm_root = ncmn;
return (true);
}
wint_t
cmap_lookup_hard(struct cmap *cm, wint_t ch)
{
if (cm->cm_root != NULL) {
cm->cm_root = cmap_splay(cm->cm_root, ch);
if (cm->cm_root->cmn_from == ch)
return (cm->cm_root->cmn_to);
}
return (cm->cm_def == CM_DEF_SELF ? ch : cm->cm_def);
}
void
cmap_cache(struct cmap *cm)
{
wint_t ch;
for (ch = 0; ch < CM_CACHE_SIZE; ch++)
cm->cm_cache[ch] = cmap_lookup_hard(cm, ch);
cm->cm_havecache = true;
}
wint_t
cmap_default(struct cmap *cm, wint_t def)
{
wint_t old;
old = cm->cm_def;
cm->cm_def = def;
cm->cm_havecache = false;
return (old);
}
static struct cmapnode *
cmap_splay(struct cmapnode *t, wint_t ch)
{
struct cmapnode N, *l, *r, *y;
assert(t != NULL);
N.cmn_left = N.cmn_right = NULL;
l = r = &N;
for (;;) {
if (ch < t->cmn_from) {
if (t->cmn_left != NULL &&
ch < t->cmn_left->cmn_from) {
y = t->cmn_left;
t->cmn_left = y->cmn_right;
y->cmn_right = t;
t = y;
}
if (t->cmn_left == NULL)
break;
r->cmn_left = t;
r = t;
t = t->cmn_left;
} else if (ch > t->cmn_from) {
if (t->cmn_right != NULL &&
ch > t->cmn_right->cmn_from) {
y = t->cmn_right;
t->cmn_right = y->cmn_left;
y->cmn_left = t;
t = y;
}
if (t->cmn_right == NULL)
break;
l->cmn_right = t;
l = t;
t = t->cmn_right;
} else
break;
}
l->cmn_right = t->cmn_left;
r->cmn_left = t->cmn_right;
t->cmn_left = N.cmn_right;
t->cmn_right = N.cmn_left;
return (t);
}