Data-SortedSet-Shared

 view release on metacpan or  search on metacpan

sortedset.h  view on Meta::CPAN

            /* Layer B: corrupt child index -- drop the tree rather than point
               the root outside the node pool */
            hdr->root = SS_NONE; hdr->leftmost = SS_NONE; hdr->rightmost = SS_NONE; hdr->height = 0;
        }
    }
    hdr->count--;
}

static inline void ss_wphase(SsHandle *h, uint8_t p) {
    __atomic_signal_fence(__ATOMIC_SEQ_CST);
    __atomic_store_n(&h->hdr->wphase, p, __ATOMIC_RELAXED);
    __atomic_signal_fence(__ATOMIC_SEQ_CST);
}

/* Give member the score, moving it from old when had.  0 on a corrupt full index. */
static int ss_put_locked(SsHandle *h, int64_t member, int had, double old, double score) {
    ss_wphase(h, SS_WP_TREE);
    if (had) ss_tree_del(h, old, member);
    ss_tree_add(h, score, member);
    ss_wphase(h, SS_WP_INDEX);
    int ok = ss_idx_set(h, member, score);
    ss_wphase(h, SS_WP_NONE);
    return ok;
}

static void ss_drop_locked(SsHandle *h, int64_t member, double old) {
    ss_wphase(h, SS_WP_TREE);
    ss_tree_del(h, old, member);
    ss_wphase(h, SS_WP_INDEX);
    ss_idx_del(h, member);
    ss_wphase(h, SS_WP_NONE);
}

/* add: 1 (new), 0 (existing -- score updated if changed), -1 (full) */
static int ss_add_locked(SsHandle *h, int64_t member, double score) {
    double old;
    if (ss_idx_get(h, member, &old)) {
        if (old != score) {
            /* re-insert: check headroom BEFORE the delete, so a refusal leaves
               the set untouched rather than dropping the member */
            if (!ss_add_has_headroom(h)) return -1;
            ss_put_locked(h, member, 1, old, score);
        }
        return 0;
    }
    if (h->hdr->count >= h->hdr->max_entries) return -1;
    if (!ss_add_has_headroom(h)) return -1;
    if (!ss_put_locked(h, member, 0, 0, score)) return -1;   /* corrupt full index: fail the add */
    return 1;
}

/* remove: 1 if removed, 0 if absent */
static int ss_remove_locked(SsHandle *h, int64_t member) {
    double old;
    if (!ss_idx_get(h, member, &old)) return 0;
    ss_drop_locked(h, member, old);
    return 1;
}

/* incr by delta. *out = new score. returns 1 (created), 0 (updated), -1 (full),
   -2 (result is NaN) */
static int ss_incr_locked(SsHandle *h, int64_t member, double delta, double *out) {
    double old;
    if (ss_idx_get(h, member, &old)) {
        double ns = old + delta; *out = ns;
        if (ns != ns) return -2;
        if (ns != old) {
            if (!ss_add_has_headroom(h)) return -1;
            ss_put_locked(h, member, 1, old, ns);
        }
        return 0;
    }
    if (h->hdr->count >= h->hdr->max_entries) return -1;
    *out = delta;
    if (delta != delta) return -2;
    if (!ss_add_has_headroom(h)) return -1;
    if (!ss_put_locked(h, member, 0, 0, delta)) return -1;   /* corrupt full index: fail the incr */
    return 1;
}

/* pop the min (max=0) or max (max=1): 0 if empty, else 1 with *m,*s */
static int ss_pop_locked(SsHandle *h, int max, int64_t *m, double *s) {
    if (h->hdr->root == SS_NONE) return 0;
    uint32_t li = max ? h->hdr->rightmost : h->hdr->leftmost;
    if (!ss_node_ok(h, li)) return 0;                 /* corrupt leftmost/rightmost */
    SsNode *nd = &h->nodes[li];
    int pos = max ? nd->num - 1 : 0;
    *m = nd->members[pos]; *s = nd->scores[pos];
    ss_drop_locked(h, *m, *s);
    return 1;
}

/* ---- structural validator (debug / tests) ---- */
static long ss_check_rec(SsHandle *h, uint32_t nidx, int depth, int is_root,
                         double *ps, int64_t *pm, int *hp, int *leaf_depth) {
    if (!ss_node_ok(h, nidx)) return -1;              /* corrupt child index */
    SsNode *nd = &h->nodes[nidx];
    if (nd->num < 1 || nd->num > SS_ORDER) return -1;
    if (!is_root && nd->num < SS_MIN) return -1;
    if (nd->is_leaf) {
        if (*leaf_depth < 0) *leaf_depth = depth;
        else if (*leaf_depth != depth) return -1;
        for (int i = 0; i < nd->num; i++) {
            if (*hp && ss_key_cmp(*ps, *pm, nd->scores[i], nd->members[i]) >= 0) return -1;
            *ps = nd->scores[i]; *pm = nd->members[i]; *hp = 1;
        }
        return nd->num;
    }
    long total = 0;
    for (int i = 0; i < nd->num; i++) {
        long c = ss_check_rec(h, nd->children[i], depth + 1, 0, ps, pm, hp, leaf_depth);
        if (c < 0 || (uint32_t)c != nd->counts[i]) return -1;
        total += c;
    }
    return total;
}
static int ss_validate_tree(SsHandle *h) {
    SsHeader *hdr = h->hdr;
    if (hdr->root == SS_NONE) return hdr->count == 0 && hdr->leftmost == SS_NONE;
    double ps = 0; int64_t pm = 0; int hp = 0, ld = -1;
    long total = ss_check_rec(h, hdr->root, 0, 1, &ps, &pm, &hp, &ld);



( run in 1.075 second using v1.01-cache-2.11-cpan-60884a8df76 )