third-party/CART/cart.cpp
int cart_iter_prefix(cart_tree *t, const unsigned char *key, int key_len, cart_callback cb, void *data) {
    cart_node **child;
    cart_node *n = t->root;
    int prefix_len, depth = 0;
    while (n) {
        // Might be a leaf
        if (IS_LEAF(n)) {
            n = (cart_node*)LEAF_RAW(n);
            // Check if the expanded path matches
            if (!leaf_prefix_matches((cart_leaf*)n, key, key_len)) {
                cart_leaf *l = (cart_leaf*)n;
                return cb(data, (const unsigned char*)l->key, l->key_len, l->value);
            }
            return 0;
        }

        // If the depth matches the prefix, we need to handle this node
        if (depth == key_len) {
            cart_leaf *l = minimum(n);
            if (!leaf_prefix_matches(l, key, key_len))
               return recursive_iter(n, cb, data);
            return 0;
        }

        // Bail if the prefix does not match
        if (n->partial_len) {
            prefix_len = prefix_mismatch(n, key, key_len, depth);

            // Guard if the mis-match is longer than the MAX_PREFIX_LEN
            if ((uint32_t)prefix_len > n->partial_len) {
                prefix_len = n->partial_len;
            }

            // If there is no match, search is terminated
            if (!prefix_len) {
                return 0;

            // If we've matched the prefix, iterate on this node
            } else if (depth + prefix_len == key_len) {
                return recursive_iter(n, cb, data);
            }

            // if there is a full match, go deeper
            depth = depth + n->partial_len;
        }

        // Recursively search
        child = find_child(n, key[depth]);
        n = (child) ? *child : NULL;
        depth++;
    }
    return 0;
}
/**
 * Iterates through the entries pairs in the map,
 * invoking a callback for each that matches a given prefix.
 * The call back gets a key, value for each and returns an integer stop value.
 * If the callback returns non-zero, then the iteration stops.
 * @arg t The tree to iterate over
 * @arg prefix The prefix of keys to read
 * @arg prefix_len The length of the prefix
 * @arg cb The callback function to invoke
 * @arg data Opaque handle passed to the callback
 * @return 0 on success, or the return of the callback.
 */
Iterates through the entries pairs in the map,
invoking a callback for each that matches a given prefix.
The call back gets a key, value for each and returns an integer stop value.
If the callback returns non-zero, then the iteration stops.
Iterates through the entries pairs in the map invoking a callback for each that matches a given prefix . The call back gets a key value for each and returns an integer stop value . If the callback returns non - zero then the iteration stops .
int cart_iter_prefix(cart_tree *t, const unsigned char *key, int key_len, cart_callback cb, void *data) {
    cart_node **child;
    cart_node *n = t->root;
    int prefix_len, depth = 0;
    while (n) {
        if (IS_LEAF(n)) {
            n = (cart_node*)LEAF_RAW(n);
            if (!leaf_prefix_matches((cart_leaf*)n, key, key_len)) {
                cart_leaf *l = (cart_leaf*)n;
                return cb(data, (const unsigned char*)l->key, l->key_len, l->value);
            }
            return 0;
        }
        if (depth == key_len) {
            cart_leaf *l = minimum(n);
            if (!leaf_prefix_matches(l, key, key_len))
               return recursive_iter(n, cb, data);
            return 0;
        }
        if (n->partial_len) {
            prefix_len = prefix_mismatch(n, key, key_len, depth);
            if ((uint32_t)prefix_len > n->partial_len) {
                prefix_len = n->partial_len;
            }
            if (!prefix_len) {
                return 0;
            } else if (depth + prefix_len == key_len) {
                return recursive_iter(n, cb, data);
            }
            depth = depth + n->partial_len;
        }
        child = find_child(n, key[depth]);
        n = (child) ? *child : NULL;
        depth++;
    }
    return 0;
}
int cart_iter_prefix ( cart_tree * t , const unsigned char * key , int key_len , cart_callback cb , void * data ) { cart_node * * child ; cart_node * n = t -> root ; int prefix_len , depth = 0 ; while ( n ) { if ( IS_LEAF ( n ) ) { n = ( cart_node * ) LEAF_RAW ( n ) ; if ( ! leaf_prefix_matches ( ( cart_leaf * ) n , key , key_len ) ) { cart_leaf * l = ( cart_leaf * ) n ; return cb ( data , ( const unsigned char * ) l -> key , l -> key_len , l -> value ) ; } return 0 ; } if ( depth == key_len ) { cart_leaf * l = minimum ( n ) ; if ( ! leaf_prefix_matches ( l , key , key_len ) ) return recursive_iter ( n , cb , data ) ; return 0 ; } if ( n -> partial_len ) { prefix_len = prefix_mismatch ( n , key , key_len , depth ) ; if ( ( uint32_t ) prefix_len > n -> partial_len ) { prefix_len = n -> partial_len ; } if ( ! prefix_len ) { return 0 ; } else if ( depth + prefix_len == key_len ) { return recursive_iter ( n , cb , data ) ; } depth = depth + n -> partial_len ; } child = find_child ( n , key [ depth ] ) ; n = ( child ) ? * child : NULL ; depth ++ ; } return 0 ; }
Iterates through the entries pairs in the map,
 invoking a callback for each that matches a given prefix.
Iterates through the entries pairs in the map invoking a callback for each that matches a given prefix .
// Might be a leaf // Check if the expanded path matches // If the depth matches the prefix, we need to handle this node // Bail if the prefix does not match // Guard if the mis-match is longer than the MAX_PREFIX_LEN // If there is no match, search is terminated // If we've matched the prefix, iterate on this node // if there is a full match, go deeper // Recursively search