-
Notifications
You must be signed in to change notification settings - Fork 9
/
dict.h
144 lines (118 loc) · 5.08 KB
/
dict.h
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
/*-------------------------------------------------------------------------*/
/**
@file dict.h
@author N. Devillard
@date Apr 2011
@brief Dictionary object
This file implements a basic string/string associative array that
grows when needed to store all inserted key/value pairs. The implementation
is very closely based on the Python dictionary, without the associated
Pythonisms and with specification on string/string.
*/
/*--------------------------------------------------------------------------*/
#ifndef _DICT_H_
#define _DICT_H_
/*---------------------------------------------------------------------------
Includes
---------------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
/*---------------------------------------------------------------------------
Additional types
---------------------------------------------------------------------------*/
/** Keypair: holds a key/value pair. Key must be a hashable C string */
typedef struct _keypair_ {
char * key ;
char * val ;
unsigned hash ;
} keypair ;
/** Dict is the only type needed for clients of the dict object */
typedef struct _dict_ {
unsigned fill ;
unsigned used ;
unsigned size ;
keypair * table ;
} dict ;
/*-------------------------------------------------------------------------*/
/**
@brief Allocate a new dictionary object
@return Newly allocated dict, to be freed with dict_free()
Constructor for the dict object.
*/
/*--------------------------------------------------------------------------*/
dict * dict_new(void);
/*-------------------------------------------------------------------------*/
/**
@brief Deallocate a dictionary object
@param d dict to deallocate
@return void
This function will deallocate a dictionary and all data it holds.
*/
/*--------------------------------------------------------------------------*/
void dict_free(dict * d);
/*-------------------------------------------------------------------------*/
/**
@brief Add an item to a dictionary
@param d dict to add to
@param key Key for the element to be inserted
@param val Value to associate to the key
@return 0 if Ok, something else in case of error
Insert an element into a dictionary. If an element already exists with
the same key, it is overwritten and the previous associated data are freed.
*/
/*--------------------------------------------------------------------------*/
int dict_add(dict * d, char * key, char * val);
/*-------------------------------------------------------------------------*/
/**
@brief Get an item from a dictionary
@param d dict to get item from
@param key Key to look for
@param defval Value to return if key is not found in dict
@return Element found, or defval
Get the value associated to a given key in a dict. If the key is not found,
defval is returned.
*/
/*--------------------------------------------------------------------------*/
char * dict_get(dict * d, char * key, char * defval);
/*-------------------------------------------------------------------------*/
/**
@brief Delete an item in a dictionary
@param d dict where item is to be deleted
@param key Key to look for
@return 0 if Ok, something else in case of error
Delete an item in a dictionary. Will return 0 if item was correctly
deleted and -1 if the item could not be found or an error occurred.
*/
/*--------------------------------------------------------------------------*/
int dict_del(dict * d, char * key);
/*-------------------------------------------------------------------------*/
/**
@brief Enumerate a dictionary
@param d dict to browse
@param rank Rank to start the next (linear) search
@param key Enumerated key (modified)
@param val Enumerated value (modified)
@return int rank of the next item to enumerate, or -1 if end reached
Enumerate a dictionary by returning all the key/value pairs it contains.
Start the iteration by providing rank=0 and two pointers that will be
modified to references inside the dict.
The returned value is the immediate successor to the one being returned,
or -1 if the end of dict was reached.
Do not free or modify the returned key/val pointers.
See dict_dump() for usage example.
*/
/*--------------------------------------------------------------------------*/
int dict_enumerate(dict * d, int rank, char ** key, char ** val);
/*-------------------------------------------------------------------------*/
/**
@brief Dump dict contents to an opened file pointer
@param d dict to dump
@param out File to output data to
@return void
Dump the contents of a dictionary to an opened file pointer.
It is Ok to pass 'stdout' or 'stderr' as file pointers.
This function is mostly meant for debugging purposes.
*/
/*--------------------------------------------------------------------------*/
void dict_dump(dict * d, FILE * out);
#endif