aboutsummaryrefslogtreecommitdiff
path: root/src/multimap.h
blob: 3cc345dd05699ac73b672eb6208fb0e46c245866 (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
/*
 *   NetCosm - a MUD server
 *   Copyright (C) 2016 Franklin Wei
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "hash.h"

/* a multimap implemented as a hash of linked lists */
/* O(1) insertion and lookup */
/* O(n) deletion by value, O(1) deletion by key */

/* there can be both duplicate keys AND values */

void *multimap_init(size_t tabsz,
                    unsigned (*hash_fn)(const void *key),
                    int (*compare_key)(const void *key_a, const void *key_b),
                    int (*compare_val)(const void *val_a, const void *val_b));

void multimap_free(void*);

struct multimap_list {
    const void *key;
    void *val;
    struct multimap_list *next;
};

/* returns a linked list of values with the same key with the length in n_pairs */
const struct multimap_list *multimap_lookup(void *map, const void *key, size_t *n_pairs);

/* returns true if there was no other pair with the same key */
bool multimap_insert(void *map, const void *key, const void *val);

/* delete the pair(s) with the given key and value, returns # deleted */
size_t multimap_delete(void *map, const void *key, const void *val);

/* returns # deleted */
size_t multimap_delete_all(void *map, const void *key);

/* returns a linked list, NOT individual items of a linked list */
/* set map to NULL after the initial call */
const struct multimap_list *multimap_iterate(const void *map, void **save, size_t *n_pairs);

size_t multimap_size(void *map);

void multimap_setfreedata_cb(void *map, void (*)(void*));

void multimap_free(void *ptr);
void *multimap_dup(void *ptr);
void multimap_setdupdata_cb(void *ptr, void *(*cb)(void *ptr));
void *multimap_copy(void *ptr);