summaryrefslogtreecommitdiff
path: root/src/libaudcore/multihash.cc
blob: 160239c9a24d710895d32c26475d4bc164e3b657 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * multihash.c
 * Copyright 2013-2014 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "multihash.h"

EXPORT void HashBase::add (Node * node, unsigned hash)
{
    if (! buckets)
    {
        buckets = new Node *[InitialSize]();
        size = InitialSize;
    }

    unsigned b = hash & (size - 1);
    node->next = buckets[b];
    node->hash = hash;
    buckets[b] = node;

    used ++;
    if (used > size)
        resize (size << 1);
}

EXPORT HashBase::Node * HashBase::lookup (MatchFunc match, const void * data,
 unsigned hash, NodeLoc * loc) const
{
    if (! buckets)
        return nullptr;

    unsigned b = hash & (size - 1);
    Node * * node_ptr = & buckets[b];
    Node * node = * node_ptr;

    while (1)
    {
        if (! node)
            return nullptr;

        if (node->hash == hash && match (node, data))
            break;

        node_ptr = & node->next;
        node = * node_ptr;
    }

    if (loc)
    {
        loc->ptr = node_ptr;
        loc->next = node->next;
    }

    return node;
}

EXPORT void HashBase::remove (const NodeLoc & loc)
{
    * loc.ptr = loc.next;

    used --;
    if (used < size >> 2 && size > InitialSize)
        resize (size >> 1);
}

EXPORT void HashBase::iterate (FoundFunc func, void * state)
{
    for (unsigned b = 0; b < size; b ++)
    {
        Node * * ptr = & buckets[b];
        Node * node = * ptr;

        while (node)
        {
            Node * next = node->next;

            if (func (node, state))
            {
                * ptr = next;
                used --;
            }
            else
                ptr = & node->next;

            node = next;
        }
    }

    if (used < size >> 2 && size > InitialSize)
        resize (size >> 1);
}

void HashBase::resize (unsigned new_size)
{
    Node * * new_buckets = new Node *[new_size]();

    for (unsigned b1 = 0; b1 < size; b1 ++)
    {
        Node * node = buckets[b1];

        while (node)
        {
            Node * next = node->next;

            unsigned b2 = node->hash & (new_size - 1);
            node->next = new_buckets[b2];
            new_buckets[b2] = node;

            node = next;
        }
    }

    delete[] buckets;
    buckets = new_buckets;
    size = new_size;
}

EXPORT int MultiHash::lookup (const void * data, unsigned hash, AddFunc add,
 FoundFunc found, void * state)
{
    const unsigned c = (hash >> Shift) & (Channels - 1);
    HashBase & channel = channels[c];

    int status = 0;
    tiny_lock (& locks[c]);

    HashBase::NodeLoc loc;
    Node * node = channel.lookup (match, data, hash, & loc);

    if (node)
    {
        status |= Found;
        if (found && found (node, state))
        {
            status |= Removed;
            channel.remove (loc);
        }
    }
    else if (add && (node = add (data, state)))
    {
        status |= Added;
        channel.add (node, hash);
    }

    tiny_unlock (& locks[c]);
    return status;
}

EXPORT void MultiHash::iterate (FoundFunc func, void * state)
{
    for (TinyLock & lock : locks)
        tiny_lock (& lock);

    for (HashBase & channel : channels)
        channel.iterate (func, state);

    for (TinyLock & lock : locks)
        tiny_unlock (& lock);
}