lib/src/diet.c
struct ival * diet_insert(struct diet * const d,
                          const uint64_t n,
                          const uint8_t c,
                          const ev_tstamp t)
{
    if (splay_empty(d))
        goto new_ival;

    // rotate the interval that contains n or is closest to it to the top
    diet_find(d, n);

    if (n >= splay_root(d)->lo && n <= splay_root(d)->hi) {
        splay_root(d)->t = t;
        return splay_root(d);
    }

    if (n < splay_root(d)->lo) {
        struct ival * const max = find_max(splay_left(splay_root(d), node));

        if (n + 1 == splay_root(d)->lo && c == splay_root(d)->c)
            // we can expand the root to include n
            splay_root(d)->lo--;
        else if (max && max->hi + 1 == n && c == max->c)
            // we can expand the max child to include n
            max->hi++;
        else
            goto new_ival;

        // check if we can merge the new root with its max left child
        if (max &&
            (max->hi == splay_root(d)->lo - 1 && max->c == splay_root(d)->c)) {
            splay_right(max, node) = splay_right(splay_root(d), node);
            max->hi = splay_root(d)->hi;
            struct ival * const old_root = splay_root(d);
            splay_root(d) = splay_left(splay_root(d), node);
            free(old_root);
            d->cnt--;
        }
        splay_root(d)->t = t;
        return splay_root(d);
    }

    if (n > splay_root(d)->hi) {
        struct ival * const min = find_min(splay_right(splay_root(d), node));

        if (n == splay_root(d)->hi + 1 && c == splay_root(d)->c)
            // we can expand the root to include n
            splay_root(d)->hi++;
        else if (min && min->lo - 1 == n && c == min->c)
            // we can expand the min child to include n
            min->lo--;
        else
            goto new_ival;

        // check if we can merge the new root with its min right child
        if (min &&
            (min->lo == splay_root(d)->hi + 1 && min->c == splay_root(d)->c)) {
            splay_left(min, node) = splay_left(splay_root(d), node);
            min->lo = splay_root(d)->lo;
            struct ival * const old_root = splay_root(d);
            splay_root(d) = splay_right(splay_root(d), node);
            free(old_root);
            d->cnt--;
        }
        splay_root(d)->t = t;
        return splay_root(d);
    }

    struct ival * i; // clang doesn't like this statement after the label?
new_ival:
    i = make_ival(n, c, t);
    splay_insert(diet, d, i);
    d->cnt++;
    return i;
}
/// Inserts integer @p n of type @p t into the diet tree @p t.
///
/// @param      d     Diet tree.
/// @param[in]  n     Integer.
/// @param[in]  c     Class.
/// @param[in]  t     Timestamp.
///
/// @return     Pointer to ival containing @p n.
///
Inserts integer @p n of type @p t into the diet tree @p t.
Inserts integer @p n of type @p t into the diet tree @p t .
struct ival * diet_insert(struct diet * const d,
                          const uint64_t n,
                          const uint8_t c,
                          const ev_tstamp t)
{
    if (splay_empty(d))
        goto new_ival;
    diet_find(d, n);
    if (n >= splay_root(d)->lo && n <= splay_root(d)->hi) {
        splay_root(d)->t = t;
        return splay_root(d);
    }
    if (n < splay_root(d)->lo) {
        struct ival * const max = find_max(splay_left(splay_root(d), node));
        if (n + 1 == splay_root(d)->lo && c == splay_root(d)->c)
            splay_root(d)->lo--;
        else if (max && max->hi + 1 == n && c == max->c)
            max->hi++;
        else
            goto new_ival;
        if (max &&
            (max->hi == splay_root(d)->lo - 1 && max->c == splay_root(d)->c)) {
            splay_right(max, node) = splay_right(splay_root(d), node);
            max->hi = splay_root(d)->hi;
            struct ival * const old_root = splay_root(d);
            splay_root(d) = splay_left(splay_root(d), node);
            free(old_root);
            d->cnt--;
        }
        splay_root(d)->t = t;
        return splay_root(d);
    }
    if (n > splay_root(d)->hi) {
        struct ival * const min = find_min(splay_right(splay_root(d), node));
        if (n == splay_root(d)->hi + 1 && c == splay_root(d)->c)
            splay_root(d)->hi++;
        else if (min && min->lo - 1 == n && c == min->c)
            min->lo--;
        else
            goto new_ival;
        if (min &&
            (min->lo == splay_root(d)->hi + 1 && min->c == splay_root(d)->c)) {
            splay_left(min, node) = splay_left(splay_root(d), node);
            min->lo = splay_root(d)->lo;
            struct ival * const old_root = splay_root(d);
            splay_root(d) = splay_right(splay_root(d), node);
            free(old_root);
            d->cnt--;
        }
        splay_root(d)->t = t;
        return splay_root(d);
    }
    struct ival * i; 
new_ival:
    i = make_ival(n, c, t);
    splay_insert(diet, d, i);
    d->cnt++;
    return i;
}
struct ival * diet_insert ( struct diet * const d , const uint64_t n , const uint8_t c , const ev_tstamp t ) { if ( splay_empty ( d ) ) goto new_ival ; diet_find ( d , n ) ; if ( n >= splay_root ( d ) -> lo && n <= splay_root ( d ) -> hi ) { splay_root ( d ) -> t = t ; return splay_root ( d ) ; } if ( n < splay_root ( d ) -> lo ) { struct ival * const max = find_max ( splay_left ( splay_root ( d ) , node ) ) ; if ( n + 1 == splay_root ( d ) -> lo && c == splay_root ( d ) -> c ) splay_root ( d ) -> lo -- ; else if ( max && max -> hi + 1 == n && c == max -> c ) max -> hi ++ ; else goto new_ival ; if ( max && ( max -> hi == splay_root ( d ) -> lo - 1 && max -> c == splay_root ( d ) -> c ) ) { splay_right ( max , node ) = splay_right ( splay_root ( d ) , node ) ; max -> hi = splay_root ( d ) -> hi ; struct ival * const old_root = splay_root ( d ) ; splay_root ( d ) = splay_left ( splay_root ( d ) , node ) ; free ( old_root ) ; d -> cnt -- ; } splay_root ( d ) -> t = t ; return splay_root ( d ) ; } if ( n > splay_root ( d ) -> hi ) { struct ival * const min = find_min ( splay_right ( splay_root ( d ) , node ) ) ; if ( n == splay_root ( d ) -> hi + 1 && c == splay_root ( d ) -> c ) splay_root ( d ) -> hi ++ ; else if ( min && min -> lo - 1 == n && c == min -> c ) min -> lo -- ; else goto new_ival ; if ( min && ( min -> lo == splay_root ( d ) -> hi + 1 && min -> c == splay_root ( d ) -> c ) ) { splay_left ( min , node ) = splay_left ( splay_root ( d ) , node ) ; min -> lo = splay_root ( d ) -> lo ; struct ival * const old_root = splay_root ( d ) ; splay_root ( d ) = splay_right ( splay_root ( d ) , node ) ; free ( old_root ) ; d -> cnt -- ; } splay_root ( d ) -> t = t ; return splay_root ( d ) ; } struct ival * i ; new_ival : i = make_ival ( n , c , t ) ; splay_insert ( diet , d , i ) ; d -> cnt ++ ; return i ; }
Inserts integer @p n of type @p t into the diet tree @p t.
Inserts integer @p n of type @p t into the diet tree @p t .
// rotate the interval that contains n or is closest to it to the top // we can expand the root to include n // we can expand the max child to include n // check if we can merge the new root with its max left child // we can expand the root to include n // we can expand the min child to include n // check if we can merge the new root with its min right child // clang doesn't like this statement after the label?