main: rework preprocessor invocation
[cparser] / adt / strset.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #ifndef _FIRM_STRSET_H_
21 #define _FIRM_STRSET_H_
22
23 #define HashSet          strset_t
24 #define HashSetIterator  strset_iterator_t
25 #define HashSetEntry     strset_entry_t
26 #define ValueType        const char*
27 #include "hashset.h"
28 #undef ValueType
29 #undef HashSetEntry
30 #undef HashSetIterator
31 #undef HashSet
32
33 typedef struct strset_t           strset_t;
34 typedef struct strset_iterator_t  strset_iterator_t;
35
36 /**
37  * Initializes a strset
38  *
39  * @param strset   Pointer to allocated space for the strset
40  */
41 void strset_init(strset_t *strset);
42
43 /**
44  * Initializes a strset
45  *
46  * @param strset                Pointer to allocated space for the strset
47  * @param expected_elements   Number of elements expected in the strset (rougly)
48  */
49 void strset_init_size(strset_t *strset, size_t expected_elements);
50
51 /**
52  * Destroys a strset and frees the memory allocated for hashtable. The memory of
53  * the strset itself is not freed.
54  *
55  * @param strset   Pointer to the strset
56  */
57 void strset_destroy(strset_t *strset);
58
59 /**
60  * Inserts a string into a strset.
61  *
62  * @param strset   Pointer to the strset
63  * @param ptr      Pointer to insert into the strset
64  * @returns        @p ptr if the string was inserted into the set,
65  *                 otherwise a pointer to the string already in the set
66  */
67 const char *strset_insert(strset_t *strset, const char *ptr);
68
69 /**
70  * Removes an element from a strset. Does nothing if the strset doesn't contain the
71  * element.
72  *
73  * @param strset   Pointer to the strset
74  * @param ptr    Pointer to remove from the strset
75  */
76 void strset_remove(strset_t *strset, const char *ptr);
77
78 /**
79  * Tests whether a strset contains a pointer
80  *
81  * @param strset   Pointer to the strset
82  * @param ptr    The pointer to test
83  * @returns      1 @p strset contains the @p ptr, 0 otherwise
84  */
85 const char* strset_find(const strset_t *strset, const char *ptr);
86
87 /**
88  * Returns the number of pointers contained in the strset
89  *
90  * @param strset   Pointer to the strset
91  * @returns      Number of pointers contained in the strset
92  */
93 size_t strset_size(const strset_t *strset);
94
95 /**
96  * Initializes a strset iterator. Sets the iterator before the first element in
97  * the strset.
98  *
99  * @param iterator   Pointer to already allocated iterator memory
100  * @param strset       Pointer to the strset
101  */
102 void strset_iterator_init(strset_iterator_t *iterator, const strset_t *strset);
103
104 /**
105  * Advances the iterator and returns the current element or NULL if all elements
106  * in the strset have been processed.
107  * @attention It is not allowed to use strset_insert or strset_remove while
108  *            iterating over a strset.
109  *
110  * @param iterator  Pointer to the strset iterator.
111  * @returns         Next element in the strset or NULL
112  */
113 const char *strset_iterator_next(strset_iterator_t *iterator);
114
115 /**
116  * Removes the string from the set that the iterator currently points to
117  *
118  * @param strset    Pointer to the strset
119  * @param iter      Pointer to the iterator
120  */
121 void strset_remove_iterator(strset_t *strset,
122                             const strset_iterator_t *iterator);
123
124 #endif