dictionary.c 4.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Simple dictionary implementation using a linked list.
 * FIXME: a skip list would be faster.
 *
 * Copyright 2005 Juan Lang
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24
 */
#include <assert.h>
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "dictionary.h"
25 26 27
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(storage);
28 29 30 31 32 33 34 35 36 37 38 39 40 41

struct dictionary_entry
{
    void *key;
    void *value;
    struct dictionary_entry *next;
};

struct dictionary
{
    comparefunc comp;
    destroyfunc destroy;
    void *extra;
    struct dictionary_entry *head;
42
    UINT num_entries;
43 44 45 46 47 48
};

struct dictionary *dictionary_create(comparefunc c, destroyfunc d, void *extra)
{
    struct dictionary *ret;

49
    TRACE("(%p, %p, %p)\n", c, d, extra);
50 51 52 53 54 55 56 57 58
    if (!c)
        return NULL;
    ret = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dictionary));
    if (ret)
    {
        ret->comp = c;
        ret->destroy = d;
        ret->extra = extra;
        ret->head = NULL;
59
        ret->num_entries = 0;
60
    }
61
    TRACE("returning %p\n", ret);
62 63 64 65 66
    return ret;
}

void dictionary_destroy(struct dictionary *d)
{
67
    TRACE("(%p)\n", d);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    if (d)
    {
        struct dictionary_entry *p;

        for (p = d->head; p; )
        {
            struct dictionary_entry *next = p->next;

            if (d->destroy)
                d->destroy(p->key, p->value, d->extra);
            HeapFree(GetProcessHeap(), 0, p);
            p = next;
        }
        HeapFree(GetProcessHeap(), 0, d);
    }
}

85 86 87 88 89
UINT dictionary_num_entries(struct dictionary *d)
{
    return d ? d->num_entries : 0;
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/* Returns the address of the pointer to the node containing k.  (It returns
 * the address of either h->head or the address of the next member of the
 * prior node.  It's useful when you want to delete.)
 * Assumes h and prev are not NULL.
 */
static struct dictionary_entry **dictionary_find_internal(struct dictionary *d,
 const void *k)
{
    struct dictionary_entry **ret = NULL;
    struct dictionary_entry *p;

    assert(d);
    /* special case for head containing the desired element */
    if (d->head && d->comp(k, d->head->key, d->extra) == 0)
        ret = &d->head;
    for (p = d->head; !ret && p && p->next; p = p->next)
    {
        if (d->comp(k, p->next->key, d->extra) == 0)
            ret = &p->next;
    }
    return ret;
}

void dictionary_insert(struct dictionary *d, const void *k, const void *v)
{
    struct dictionary_entry **prior;

117
    TRACE("(%p, %p, %p)\n", d, k, v);
118 119 120 121 122 123 124 125 126 127 128
    if (!d)
        return;
    if ((prior = dictionary_find_internal(d, k)))
    {
        if (d->destroy)
            d->destroy((*prior)->key, (*prior)->value, d->extra);
        (*prior)->key = (void *)k;
        (*prior)->value = (void *)v;
    }
    else
    {
129 130
        struct dictionary_entry *elem = HeapAlloc(GetProcessHeap(), 0,
                                            sizeof(struct dictionary_entry));
131 132 133 134 135 136 137

        if (!elem)
            return;
        elem->key = (void *)k;
        elem->value = (void *)v;
        elem->next = d->head;
        d->head = elem;
138
        d->num_entries++;
139 140 141 142 143 144 145 146
    }
}

BOOL dictionary_find(struct dictionary *d, const void *k, void **value)
{
    struct dictionary_entry **prior;
    BOOL ret = FALSE;

147
    TRACE("(%p, %p, %p)\n", d, k, value);
148 149 150 151 152 153 154 155 156
    if (!d)
        return FALSE;
    if (!value)
        return FALSE;
    if ((prior = dictionary_find_internal(d, k)))
    {
        *value = (*prior)->value;
        ret = TRUE;
    }
157
    TRACE("returning %d (%p)\n", ret, *value);
158 159 160 161 162 163 164
    return ret;
}

void dictionary_remove(struct dictionary *d, const void *k)
{
    struct dictionary_entry **prior, *temp;

165
    TRACE("(%p, %p)\n", d, k);
166 167 168 169 170 171 172 173 174
    if (!d)
        return;
    if ((prior = dictionary_find_internal(d, k)))
    {
        temp = *prior;
        if (d->destroy)
            d->destroy((*prior)->key, (*prior)->value, d->extra);
        *prior = (*prior)->next;
        HeapFree(GetProcessHeap(), 0, temp);
175
        d->num_entries--;
176 177 178
    }
}

179
void dictionary_enumerate(struct dictionary *d, enumeratefunc e, void *closure)
180 181 182
{
    struct dictionary_entry *p;

183
    TRACE("(%p, %p, %p)\n", d, e, closure);
184 185 186 187 188
    if (!d)
        return;
    if (!e)
        return;
    for (p = d->head; p; p = p->next)
189 190
        if (!e(p->key, p->value, d->extra, closure))
            break;
191
}