Add a function to pre-allocate space for a large set.

When the maximum set size is large but constant, and members will be added
one at a time, growing the set dynamically is not very efficient.
This commit is contained in:
Bill Currie 2013-03-07 10:27:30 +09:00
parent 5950803462
commit eb2828e11c
2 changed files with 24 additions and 0 deletions

View file

@ -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.

View file

@ -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)
{