@note instead of @Note
[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  * @version $Id$
26  */
27 #ifndef FIRM_ADT_CPSET_H
28 #define FIRM_ADT_CPSET_H
29
30 /**
31  * The type of a cpset compare function.
32  *
33  * @param p1   pointer to an element
34  * @param p2   pointer to another element
35  *
36  * @return  1 if the elements are identically, zero else
37  */
38 typedef int (*cpset_cmp_function) (const void *p1, const void *p2);
39
40 /**
41  * The type of a cpset hash function.
42  */
43 typedef unsigned (*cpset_hash_function) (const void *obj);
44
45 #define HashSet          cpset_t
46 #define HashSetIterator  cpset_iterator_t
47 #define HashSetEntry     cpset_hashset_entry_t
48 #define ValueType        void*
49 #define ADDITIONAL_DATA  cpset_cmp_function cmp_function; cpset_hash_function hash_function;
50 #include "hashset.h"
51 #undef ADDITIONAL_DATA
52 #undef ValueType
53 #undef HashSetEntry
54 #undef HashSetIterator
55 #undef HashSet
56
57 typedef struct cpset_t          cpset_t;
58 typedef struct cpset_iterator_t cpset_iterator_t;
59
60 /**
61  * Initializes a cpset
62  *
63  * @param cpset        Pointer to allocated space for the cpset
64  * @param cmp_function The compare function to use
65  */
66 void cpset_init(cpset_t *cpset, cpset_hash_function hash_function,
67                 cpset_cmp_function cmp_function);
68
69 /**
70  * Initializes a cpset
71  *
72  * @param cpset               Pointer to allocated space for the cpset
73  * @param cmp_function        The compare function to use
74  * @param expected_elements   Number of elements expected in the cpset (roughly)
75  */
76 void cpset_init_size(cpset_t *cpset, cpset_hash_function hash_function,
77                      cpset_cmp_function cmp_function,
78                      size_t expected_elements);
79
80 /**
81  * Destroys a cpset and frees the memory allocated for hashtable. The memory of
82  * the cpset itself is not freed.
83  *
84  * @param cpset   Pointer to the cpset
85  */
86 void cpset_destroy(cpset_t *cpset);
87
88 /**
89  * Inserts an element into a cpset.
90  *
91  * @param cpset   Pointer to the cpset
92  * @param obj     Element to insert into the cpset
93  * @returns       The element itself or a pointer to an existing element
94  */
95 void* cpset_insert(cpset_t *cpset, void *obj);
96
97 /**
98  * Removes an element from a cpset. Does nothing if the cpset doesn't contain the
99  * element.
100  *
101  * @param cpset   Pointer to the cpset
102  * @param obj     Pointer to remove from the cpset
103  */
104 void cpset_remove(cpset_t *cpset, const void *obj);
105
106 /**
107  * Tests whether a cpset contains a pointer
108  *
109  * @param cpset   Pointer to the cpset
110  * @param obj     The pointer to find
111  * @returns       An equivalent object to @p obj or NULL
112  */
113 void *cpset_find(const cpset_t *cpset, const void *obj);
114
115 /**
116  * Returns the number of pointers contained in the cpset
117  *
118  * @param cpset   Pointer to the cpset
119  * @returns       Number of pointers contained in the cpset
120  */
121 size_t cpset_size(const cpset_t *cpset);
122
123 /**
124  * Initializes a cpset iterator. Sets the iterator before the first element in
125  * the cpset.
126  *
127  * @param iterator   Pointer to already allocated iterator memory
128  * @param cpset       Pointer to the cpset
129  */
130 void cpset_iterator_init(cpset_iterator_t *iterator, const cpset_t *cpset);
131
132 /**
133  * Advances the iterator and returns the current element or NULL if all elements
134  * in the cpset have been processed.
135  * @attention It is not allowed to use cpset_insert or cpset_remove while
136  *            iterating over a cpset.
137  *
138  * @param iterator  Pointer to the cpset iterator.
139  * @returns         Next element in the cpset or NULL
140  */
141 void *cpset_iterator_next(cpset_iterator_t *iterator);
142
143 /**
144  * Removed the element the iterator currently points to
145  *
146  * @param cpset     Pointer to the cpset
147  * @param iterator  Pointer to the cpset iterator.
148  */
149 void cpset_remove_iterator(cpset_t *cpset, const cpset_iterator_t *iterator);
150
151 #endif /* FIRM_ADT_CPSET_H */