summaryrefslogtreecommitdiff
path: root/lib/hmapx.c
blob: bb9ec11c5746e3e509b7c115beee1210e7128172 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright (c) 2011, 2012 Nicira, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>

#include "hmapx.h"

#include "hash.h"

static struct hmapx_node *
hmapx_find__(const struct hmapx *map, const void *data, size_t hash)
{
    struct hmapx_node *node;

    HMAP_FOR_EACH_IN_BUCKET (node, hmap_node, hash, &map->map) {
        if (node->data == data) {
            return node;
        }
    }
    return NULL;
}

static struct hmapx_node *
hmapx_add__(struct hmapx *map, void *data, size_t hash)
{
    struct hmapx_node *node = xmalloc(sizeof *node);
    node->data = data;
    hmap_insert(&map->map, &node->hmap_node, hash);
    return node;
}

/* Initializes 'map' as an empty set of pointers. */
void
hmapx_init(struct hmapx *map)
{
    hmap_init(&map->map);
}

/* Destroys 'map'. */
void
hmapx_destroy(struct hmapx *map)
{
    if (map) {
        hmapx_clear(map);
        hmap_destroy(&map->map);
    }
}

/* Initializes 'map' to contain the same pointers as 'orig'. */
void
hmapx_clone(struct hmapx *map, const struct hmapx *orig)
{
    struct hmapx_node *node;

    hmapx_init(map);
    hmap_reserve(&map->map, hmapx_count(orig));

    HMAP_FOR_EACH (node, hmap_node, &orig->map) {
        hmapx_add__(map, node->data, node->hmap_node.hash);
    }
}

/* Exchanges the contents of 'a' and 'b'. */
void
hmapx_swap(struct hmapx *a, struct hmapx *b)
{
    hmap_swap(&a->map, &b->map);
}

/* Adjusts 'map' so that it is still valid after it has been moved around in
 * memory (e.g. due to realloc()). */
void
hmapx_moved(struct hmapx *map)
{
    hmap_moved(&map->map);
}

/* Returns true if 'map' contains no nodes, false if it contains at least one
 * node. */
bool
hmapx_is_empty(const struct hmapx *map)
{
    return hmap_is_empty(&map->map);
}

/* Returns the number of nodes in 'map'. */
size_t
hmapx_count(const struct hmapx *map)
{
    return hmap_count(&map->map);
}

/* Adds 'data' to 'map'.  If 'data' is new, returns the new hmapx_node;
 * otherwise (if a 'data' already existed in 'map'), returns NULL. */
struct hmapx_node *
hmapx_add(struct hmapx *map, void *data)
{
    uint32_t hash = hash_pointer(data, 0);
    return (hmapx_find__(map, data, hash)
            ? NULL
            : hmapx_add__(map, data, hash));
}

/* Adds 'data' to 'map'.  Assert-fails if 'data' was already in 'map'. */
void
hmapx_add_assert(struct hmapx *map, void *data)
{
    ovs_assert(hmapx_add(map, data));
}

/* Removes all of the nodes from 'map'. */
void
hmapx_clear(struct hmapx *map)
{
    struct hmapx_node *node;

    HMAPX_FOR_EACH_SAFE (node, map) {
        hmapx_delete(map, node);
    }
}

/* Deletes 'node' from 'map' and frees 'node'. */
void
hmapx_delete(struct hmapx *map, struct hmapx_node *node)
{
    hmap_remove(&map->map, &node->hmap_node);
    free(node);
}

/* Searches for 'data' in 'map'.  If found, deletes it and returns true.  If
 * not found, returns false without modifying 'map'. */
bool
hmapx_find_and_delete(struct hmapx *map, const void *data)
{
    struct hmapx_node *node = hmapx_find(map, data);
    if (node) {
        hmapx_delete(map, node);
    }
    return node != NULL;
}

/* Searches for 'data' in 'map' and deletes it.  Assert-fails if 'data' is not
 * in 'map'. */
void
hmapx_find_and_delete_assert(struct hmapx *map, const void *data)
{
    ovs_assert(hmapx_find_and_delete(map, data));
}

/* Searches for 'data' in 'map'.  Returns its node, if found, otherwise a null
 * pointer. */
struct hmapx_node *
hmapx_find(const struct hmapx *map, const void *data)
{
    return hmapx_find__(map, data, hash_pointer(data, 0));
}

/* Returns true if 'map' contains 'data', false otherwise. */
bool
hmapx_contains(const struct hmapx *map, const void *data)
{
    return hmapx_find(map, data) != NULL;
}

/* Returns true if 'a' and 'b' contain the same pointers, false otherwise. */
bool
hmapx_equals(const struct hmapx *a, const struct hmapx *b)
{
    struct hmapx_node *node;

    if (hmapx_count(a) != hmapx_count(b)) {
        return false;
    }

    HMAP_FOR_EACH (node, hmap_node, &a->map) {
        if (!hmapx_find__(b, node->data, node->hmap_node.hash)) {
            return false;
        }
    }

    return true;
}