diff --git a/include/QF/set.h b/include/QF/set.h index baa5b1699..a10b7dbcd 100644 --- a/include/QF/set.h +++ b/include/QF/set.h @@ -93,6 +93,19 @@ void set_del_iter (set_iter_t *set_iter); */ set_t *set_new (void); +/** Create a new set with space pre-allocated for the specified set size. + + Although sets automatically grow to accommodate new members as necessary, + sometimes the maximum set size is known in advance and it can be more + efficient to grow the set in advance. + + The set is initialized to be the empty set. + + \param size The number of elements for which space is to be allocated. + \return The newly created, empty set. +*/ +set_t *set_new_size (int size); + /** Delete a set that is no longer needed. \param set The set to be deleted. diff --git a/libs/util/set.c b/libs/util/set.c index da1893147..e3483874b 100644 --- a/libs/util/set.c +++ b/libs/util/set.c @@ -107,6 +107,17 @@ set_expand (set_t *set, unsigned x) free (map); } +set_t * +set_new_size (int size) +{ + set_t *set; + + set = set_new (); + set_expand (set, size); + + return set; +} + static inline void _set_add (set_t *set, unsigned x) {