<commit_before>#include "aa_map.h"

#define safecall(v, f, ...) ((f) ? (f(__VA_ARGS__)) : (v))
#define unit                {}

typedef enum { L = 0, R = 1 } lr;

struct aa_node;

struct aa_tree {
    struct aa_node *root;

    size_t itemsize;

    int (*cmp)(void*,void*);
    void (*cpy)(void*,void*);

    void *allocator;
    void *(*alloc)(void*,size_t);
    void  (*dealloc)(void*,void*);
};

const size_t aa_tree_size = sizeof(struct aa_tree);

struct aa_node {
    size_t level;
    struct aa_node *child[2];
    uint8_t data[0];
};

static struct aa_node aa_node_bottom = { .level = 0
                                       , .child = { &aa_node_bottom
                                                  , &aa_node_bottom
                                                  }
                                       };

static struct aa_node *aa_node_null = &aa_node_bottom;

const size_t aa_node_size = sizeof(struct aa_node);

static inline void *aa_value( struct aa_node *n ) {
    return (void*)n->data;
}

static inline struct aa_node* l(struct aa_node *n) {
    return n->child[L];
}

static inline struct aa_node* r(struct aa_node *n) {
    return n->child[R];
}

static inline bool aa_nil( struct aa_node *n ) {
    return !n || n == aa_node_null;
}

static inline bool aa_leaf( struct aa_node *n ) {
    return aa_nil(l(n)) && aa_nil(r(n));
}

struct aa_tree *aa_tree_create( size_t memsize
                              , void *mem
                              , size_t itemsize
                              , int (*cmp)(void*,void*)
                              , void (*cpy)(void*,void*)
                              , void *allocator
                              , void *(*alloc)(void*,size_t)
                              , void  (*dealloc)(void*,void*) ) {


    if( memsize < aa_tree_size ) {
        return 0;
    }

    struct aa_tree *t = mem;
    t->root = aa_node_null;
    t->itemsize = itemsize;
    t->cmp = cmp;
    t->cpy = cpy;
    t->allocator = allocator;
    t->alloc = alloc;
    t->dealloc = dealloc;

    return t;
}

static struct aa_node *aa_node_init( struct aa_tree *t
                                   , struct aa_node *n
                                   , void *v) {

    if( !n ) {
        return n;
    }

    *n = aa_node_bottom;
    n->level = 1;
    n->child[L] = n->child[R] = aa_node_null;
    t->cpy(&n->data[0], v);

    return n;
}

static struct aa_node *aa_node_create(struct aa_tree *t, void *v) {
    struct aa_node *n = t->alloc(t->allocator, aa_node_size + t->itemsize);

    if( !n ) {
        return 0;
    }

    return aa_node_init(t, n, v);
}

static inline size_t dir_of(int cmp) {
    return cmp < 0 ? L : R;
}

static void aa_node_destroy( struct aa_tree *t, struct aa_node *v ) {
    if( aa_nil(v) ) {
        return;
    }
    aa_node_destroy(t, l(v));
    aa_node_destroy(t, r(v));
    t->dealloc(t->allocator, v);
}

static inline struct aa_node* aa_skew(struct aa_node *n) {

    if( aa_nil(n) ) {
        return n;
    }

    if( n->child[L]->level == n->level && n->level ) {
        struct aa_node *tmp = n->child[L];
        n->child[L] = tmp->child[R];
        tmp->child[R] = n;
        n = tmp;
    }

    return n;
}

static inline struct aa_node* aa_split(struct aa_node *n)
{
    if( aa_nil(n) ) {
        return n;
    }

    if(n->child[R]->child[R]->level == n->level && n->level != 0) {
        struct aa_node *tmp = n->child[R];

        n->child[R] = tmp->child[L];
        tmp->child[L] = n;
        n = tmp;
        n->level++;
    }

    return n;
}

static struct aa_node *aa_node_insert( struct aa_tree *t
                                     , struct aa_node *to
                                     , void *v
                                     , void *cc
                                     , void (*fn)(void*,void*,bool)
                                     ) {

    if( to == aa_node_null ) {
        struct aa_node *n = aa_node_create(t, v);<commit_msg>screwed, fixing<commit_after>#include "aa_map.h"

#define safecall(v, f, ...) ((f) ? (f(__VA_ARGS__)) : (v))
#define unit                {}

typedef enum { L = 0, R = 1 } lr;

struct aa_node;

struct aa_tree {
    struct aa_node *root;

    size_t itemsize;

    int (*cmp)(void*,void*);
    void (*cpy)(void*,void*);

    void *allocator;
    void *(*alloc)(void*,size_t);
    void  (*dealloc)(void*,void*);
};

const size_t aa_tree_size = sizeof(struct aa_tree);

struct aa_node {
    size_t level;
    struct aa_node *child[2];
    uint8_t data[0];
};

static struct aa_node aa_node_bottom = { .level = 0
                                       , .child = { &aa_node_bottom
                                                  , &aa_node_bottom
                                                  }
                                       };

static struct aa_node *aa_node_null = &aa_node_bottom;

const size_t aa_node_size = sizeof(struct aa_node);

static inline void *aa_value( struct aa_node *n ) {
    return (void*)n->data;
}

static inline struct aa_node* l(struct aa_node *n) {
    return n->child[L];
}

static inline struct aa_node* r(struct aa_node *n) {
    return n->child[R];
}

static inline bool aa_nil( struct aa_node *n ) {
    return !n || n == aa_node_null;
}

static inline bool aa_leaf( struct aa_node *n ) {
    return aa_nil(l(n)) && aa_nil(r(n));
}

struct aa_tree *aa_tree_create( size_t memsize
                              , void *mem
                              , size_t itemsize
                              , int (*cmp)(void*,void*)
                              , void (*cpy)(void*,void*)
                              , void *allocator
                              , void *(*alloc)(void*,size_t)
                              , void  (*dealloc)(void*,void*) ) {


    if( memsize < aa_tree_size ) {
        return 0;
    }

    struct aa_tree *t = mem;
    t->root = aa_node_null;
    t->itemsize = itemsize;
    t->cmp = cmp;
    t->cpy = cpy;
    t->allocator = allocator;
    t->alloc = alloc;
    t->dealloc = dealloc;

    return t;
}

static inline size_t dir_of(int cmp) {
    return cmp < 0 ? L : R;
}

static void aa_node_destroy( struct aa_tree *t, struct aa_node *v ) {
    if( aa_nil(v) ) {
        return;
    }
    aa_node_destroy(t, l(v));
    aa_node_destroy(t, r(v));
    t->dealloc(t->allocator, v);
}

static inline struct aa_node* aa_skew(struct aa_node *n) {

    if( aa_nil(n) ) {
        return n;
    }

    if( n->child[L]->level == n->level && n->level ) {
        struct aa_node *tmp = n->child[L];
        n->child[L] = tmp->child[R];
        tmp->child[R] = n;
        n = tmp;
    }

    return n;
}

static inline struct aa_node* aa_split(struct aa_node *n)
{
    if( aa_nil(n) ) {
        return n;
    }

    if(n->child[R]->child[R]->level == n->level && n->level != 0) {
        struct aa_node *tmp = n->child[R];

        n->child[R] = tmp->child[L];
        tmp->child[L] = n;
        n = tmp;
        n->level++;
    }

    return n;
}

static struct aa_node *aa_node_init( struct aa_tree *t
                                   , struct aa_node *n
                                   , void *v) {

    if( !n ) {
        return n;
    }

    *n = aa_node_bottom;
    n->level = 1;
    n->child[L] = n->child[R] = aa_node_null;
    t->cpy(&n->data[0], v);

    return n;
}

static struct aa_node *aa_node_create(struct aa_tree *t, void *v) {
    struct aa_node *n = t->alloc(t->allocator, aa_node_size + t->itemsize);

    if( !n ) {
        return 0;
    }

    return aa_node_init(t, n, v);
}

static struct aa_node *aa_node_insert( struct aa_tree *t
                                     , struct aa_node *to
                                     , void *v
                                     , void *cc
                                     , void (*fn)(void*,void*,bool)
                                     ) {

    if( to == aa_node_null ) {
        struct aa_node *n = aa_node_create(t, v);
{'fraction_non_alphanumeric': 0.13631778058007565, 'fraction_numerical': 0.003026481715006305, 'mean_word_length': 1.583387622149837, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4162118', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2671, 'n_words': 634}