summaryrefslogtreecommitdiff
path: root/toxcore/list.h
diff options
context:
space:
mode:
authornotsecure <notsecure@marek.ca>2014-05-19 16:42:09 -0400
committernotsecure <notsecure@marek.ca>2014-05-19 16:42:09 -0400
commitfe66fcc7e4053a7ec7e59d22a1e4847980c90d6a (patch)
treecca09285b2393ec98f85a5acc1d2cc6f83e285ec /toxcore/list.h
parentd27a83820c6af2f0d6d813b9916d324e9ae3d137 (diff)
list
Simple struct with functions to create a list which associates ids with data
Diffstat (limited to 'toxcore/list.h')
-rw-r--r--toxcore/list.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/toxcore/list.h b/toxcore/list.h
new file mode 100644
index 00000000..1fbea7ad
--- /dev/null
+++ b/toxcore/list.h
@@ -0,0 +1,61 @@
1/* list.h
2 *
3 * Simple struct with functions to create a list which associates ids with data
4 * -Allows for finding ids associated with data such as IPs or public keys in a short time
5 * -Should only be used if there are relatively few add/remove calls to the list
6 *
7 * Copyright (C) 2014 Tox project All Rights Reserved.
8 *
9 * This file is part of Tox.
10 *
11 * Tox is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * Tox is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26#include <stdlib.h>
27#include <stdint.h>
28#include <string.h>
29
30typedef struct
31{
32 uint32_t n; //number of elements
33 uint32_t size; //size of the elements
34 void *data; //array of elements
35 int *ids; //array of element ids
36}LIST;
37
38/* Initialize a list, element_size is the size of the elements in the list */
39void list_init(LIST *list, uint32_t element_size);
40
41/* Free a list initiated with list_init */
42void list_free(LIST *list);
43
44/* Retrieve the id of an element in the list
45 *
46 * return value:
47 * >= 0 : id associated with data
48 * -1 : failure
49 */
50int list_find(LIST *list, void *data);
51
52/* Add an element with associated id to the list
53 *
54 * return value:
55 * 1 : success
56 * 0 : failure (data already in list)
57 */
58int list_add(LIST *list, void *data, int id);
59
60/* Remove an element from the list */
61void list_remove(LIST *list, int id);