2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
23 * @brief a set of pointers with a custom compare function
24 * @author Matthias Braun
26 #ifndef FIRM_ADT_CPSET_H
27 #define FIRM_ADT_CPSET_H
31 * @defgroup Pointer Set (custom Compare)
32 * A pointer set with user-definable compare function
37 * The type of a cpset compare function.
39 * @param p1 pointer to an element
40 * @param p2 pointer to another element
42 * @return 1 if the elements are identically, zero else
44 typedef int (*cpset_cmp_function) (const void *p1, const void *p2);
47 * The type of a cpset hash function.
49 typedef unsigned (*cpset_hash_function) (const void *obj);
53 #define HashSet cpset_t
54 #define HashSetIterator cpset_iterator_t
55 #define HashSetEntry cpset_hashset_entry_t
56 #define ValueType void*
57 #define ADDITIONAL_DATA cpset_cmp_function cmp_function; cpset_hash_function hash_function;
59 #undef ADDITIONAL_DATA
62 #undef HashSetIterator
67 /** a pointer set with custom compare function */
68 typedef struct cpset_t cpset_t;
69 /** iterator over a pointer set with custom compare function
71 typedef struct cpset_iterator_t cpset_iterator_t;
76 * @param cpset Pointer to allocated space for the cpset
77 * @param hash_function The hash function to use
78 * @param cmp_function The compare function to use
80 void cpset_init(cpset_t *cpset, cpset_hash_function hash_function,
81 cpset_cmp_function cmp_function);
86 * @param cpset Pointer to allocated space for the cpset
87 * @param hash_function The hash function to use
88 * @param cmp_function The compare function to use
89 * @param expected_elements Number of elements expected in the cpset (roughly)
91 void cpset_init_size(cpset_t *cpset, cpset_hash_function hash_function,
92 cpset_cmp_function cmp_function,
93 size_t expected_elements);
96 * Destroys a cpset and frees the memory allocated for hashtable. The memory of
97 * the cpset itself is not freed.
99 * @param cpset Pointer to the cpset
101 void cpset_destroy(cpset_t *cpset);
104 * Inserts an element into a cpset.
106 * @param cpset Pointer to the cpset
107 * @param obj Element to insert into the cpset
108 * @returns The element itself or a pointer to an existing element
110 void* cpset_insert(cpset_t *cpset, void *obj);
113 * Removes an element from a cpset. Does nothing if the cpset doesn't contain the
116 * @param cpset Pointer to the cpset
117 * @param obj Pointer to remove from the cpset
119 void cpset_remove(cpset_t *cpset, const void *obj);
122 * Tests whether a cpset contains a pointer
124 * @param cpset Pointer to the cpset
125 * @param obj The pointer to find
126 * @returns An equivalent object to @p obj or NULL
128 void *cpset_find(const cpset_t *cpset, const void *obj);
131 * Returns the number of pointers contained in the cpset
133 * @param cpset Pointer to the cpset
134 * @returns Number of pointers contained in the cpset
136 size_t cpset_size(const cpset_t *cpset);
139 * Initializes a cpset iterator. Sets the iterator before the first element in
142 * @param iterator Pointer to already allocated iterator memory
143 * @param cpset Pointer to the cpset
145 void cpset_iterator_init(cpset_iterator_t *iterator, const cpset_t *cpset);
148 * Advances the iterator and returns the current element or NULL if all elements
149 * in the cpset have been processed.
150 * @attention It is not allowed to use cpset_insert or cpset_remove while
151 * iterating over a cpset.
153 * @param iterator Pointer to the cpset iterator.
154 * @returns Next element in the cpset or NULL
156 void *cpset_iterator_next(cpset_iterator_t *iterator);
159 * Removed the element the iterator currently points to
161 * @param cpset Pointer to the cpset
162 * @param iterator Pointer to the cpset iterator.
164 void cpset_remove_iterator(cpset_t *cpset, const cpset_iterator_t *iterator);