Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset e8e31d9 in mainline


Ignore:
Timestamp:
2012-07-24T21:20:12Z (9 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master
Children:
842ed146
Parents:
ff788c3
Message:

Bithenge: add bithenge_node_get

Location:
uspace/app/bithenge
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/tree.c

    rff788c3 re8e31d9  
    8080}
    8181
     82typedef struct {
     83        bithenge_node_t *key;
     84        bithenge_node_t **out;
     85} get_for_each_data_t;
     86
     87static int get_for_each_func(bithenge_node_t *key, bithenge_node_t *value,
     88    void *raw_data)
     89{
     90        get_for_each_data_t *data = (get_for_each_data_t *)raw_data;
     91        bool equal = bithenge_node_equal(key, data->key);
     92        bithenge_node_dec_ref(key);
     93        if (equal) {
     94                *data->out = value;
     95                return EEXIST;
     96        }
     97        bithenge_node_dec_ref(value);
     98        return EOK;
     99}
     100
     101/** Get a child of a node. Takes ownership of the key. If the node does not
     102 * provide this function, for_each will be used as an alternative, which may be
     103 * very slow.
     104 * @memberof bithenge_node_t
     105 * @param self The internal node to find a child of.
     106 * @param key The key to search for.
     107 * @param[out] out Holds the found node.
     108 * @return EOK on success, ENOENT if not found, or another error code from
     109 * errno.h. */
     110int bithenge_node_get(bithenge_node_t *self, bithenge_node_t *key,
     111    bithenge_node_t **out)
     112{
     113        assert(self->type == BITHENGE_NODE_INTERNAL);
     114        if (self->internal_ops->get)
     115                return self->internal_ops->get(self, key, out);
     116        *out = NULL;
     117        get_for_each_data_t data = {key, out};
     118        int rc = bithenge_node_for_each(self, get_for_each_func, &data);
     119        bithenge_node_dec_ref(key);
     120        if (rc == EEXIST && *out)
     121                return EOK;
     122        if (rc == EOK)
     123                rc = ENOENT;
     124        bithenge_node_dec_ref(*out);
     125        return rc;
     126}
     127
    82128typedef struct
    83129{
  • uspace/app/bithenge/tree.h

    rff788c3 re8e31d9  
    9494        /** @copydoc bithenge_node_t::bithenge_node_for_each */
    9595        int (*for_each)(bithenge_node_t *self, bithenge_for_each_func_t func, void *data);
     96        /** @copydoc bithenge_node_t::bithenge_node_get */
     97        int (*get)(bithenge_node_t *self, bithenge_node_t *key,
     98            bithenge_node_t **out);
    9699        /** Destroys the internal node.
    97100         * @param self The node to destroy. */
     
    131134        return self->internal_ops->for_each(self, func, data);
    132135}
     136
     137int bithenge_node_get(bithenge_node_t *, bithenge_node_t *,
     138    bithenge_node_t **);
    133139
    134140/** Get the value of a boolean node.
Note: See TracChangeset for help on using the changeset viewer.