move FIRM_NOTHROW, FIRM_PRINTF to obstack.h header
[libfirm] / include / libfirm / adt / cpset.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @date    16.03.2007
23  * @brief   a set of pointers with a custom compare function
24  * @author  Matthias Braun
25  */
26 #ifndef FIRM_ADT_CPSET_H
27 #define FIRM_ADT_CPSET_H
28
29 #include "../begin.h"
30
31 /**
32  * The type of a cpset compare function.
33  *
34  * @param p1   pointer to an element
35  * @param p2   pointer to another element
36  *
37  * @return  1 if the elements are identically, zero else
38  */
39 typedef int (*cpset_cmp_function) (const void *p1, const void *p2);
40
41 /**
42  * The type of a cpset hash function.
43  */
44 typedef unsigned (*cpset_hash_function) (const void *obj);
45
46 #define HashSet          cpset_t
47 #define HashSetIterator  cpset_iterator_t
48 #define HashSetEntry     cpset_hashset_entry_t
49 #define ValueType        void*
50 #define ADDITIONAL_DATA  cpset_cmp_function cmp_function; cpset_hash_function hash_function;
51 #include "hashset.h"
52 #undef ADDITIONAL_DATA
53 #undef ValueType
54 #undef HashSetEntry
55 #undef HashSetIterator
56 #undef HashSet
57
58 typedef struct cpset_t          cpset_t;
59 typedef struct cpset_iterator_t cpset_iterator_t;
60
61 /**
62  * Initializes a cpset
63  *
64  * @param cpset           Pointer to allocated space for the cpset
65  * @param hash_function   The hash function to use
66  * @param cmp_function    The compare function to use
67  */
68 FIRM_API void cpset_init(cpset_t *cpset, cpset_hash_function hash_function,
69                 cpset_cmp_function cmp_function);
70
71 /**
72  * Initializes a cpset
73  *
74  * @param cpset              Pointer to allocated space for the cpset
75  * @param hash_function      The hash function to use
76  * @param cmp_function       The compare function to use
77  * @param expected_elements  Number of elements expected in the cpset (roughly)
78  */
79 FIRM_API void cpset_init_size(cpset_t *cpset, cpset_hash_function hash_function,
80                      cpset_cmp_function cmp_function,
81                      size_t expected_elements);
82
83 /**
84  * Destroys a cpset and frees the memory allocated for hashtable. The memory of
85  * the cpset itself is not freed.
86  *
87  * @param cpset   Pointer to the cpset
88  */
89 FIRM_API void cpset_destroy(cpset_t *cpset);
90
91 /**
92  * Inserts an element into a cpset.
93  *
94  * @param cpset   Pointer to the cpset
95  * @param obj     Element to insert into the cpset
96  * @returns       The element itself or a pointer to an existing element
97  */
98 FIRM_API void* cpset_insert(cpset_t *cpset, void *obj);
99
100 /**
101  * Removes an element from a cpset. Does nothing if the cpset doesn't contain the
102  * element.
103  *
104  * @param cpset   Pointer to the cpset
105  * @param obj     Pointer to remove from the cpset
106  */
107 FIRM_API void cpset_remove(cpset_t *cpset, const void *obj);
108
109 /**
110  * Tests whether a cpset contains a pointer
111  *
112  * @param cpset   Pointer to the cpset
113  * @param obj     The pointer to find
114  * @returns       An equivalent object to @p obj or NULL
115  */
116 FIRM_API void *cpset_find(const cpset_t *cpset, const void *obj);
117
118 /**
119  * Returns the number of pointers contained in the cpset
120  *
121  * @param cpset   Pointer to the cpset
122  * @returns       Number of pointers contained in the cpset
123  */
124 FIRM_API size_t cpset_size(const cpset_t *cpset);
125
126 /**
127  * Initializes a cpset iterator. Sets the iterator before the first element in
128  * the cpset.
129  *
130  * @param iterator   Pointer to already allocated iterator memory
131  * @param cpset       Pointer to the cpset
132  */
133 FIRM_API void cpset_iterator_init(cpset_iterator_t *iterator, const cpset_t *cpset);
134
135 /**
136  * Advances the iterator and returns the current element or NULL if all elements
137  * in the cpset have been processed.
138  * @attention It is not allowed to use cpset_insert or cpset_remove while
139  *            iterating over a cpset.
140  *
141  * @param iterator  Pointer to the cpset iterator.
142  * @returns         Next element in the cpset or NULL
143  */
144 FIRM_API void *cpset_iterator_next(cpset_iterator_t *iterator);
145
146 /**
147  * Removed the element the iterator currently points to
148  *
149  * @param cpset     Pointer to the cpset
150  * @param iterator  Pointer to the cpset iterator.
151  */
152 FIRM_API void cpset_remove_iterator(cpset_t *cpset, const cpset_iterator_t *iterator);
153
154 #include "../end.h"
155
156 #endif