I'm currently working on a task which makes use of sparse sets. In a particular function I am asked to initialise a sparse set data structure in the range of [0..max_value]. Hence I assume that malloc() must be used. However I believe that I am implementing this function incorrectly, as the unit-tests do not work. (capacity is the maximum number of elements that can be stored, while max_value is the maximum integer value to be stored)
sparse_set_ptr ss_init(sparse_set_index_t capacity, sparse_set_elem_t max_value) {
struct sparse_set *d_arr = malloc(sizeof(capacity));
struct sparse_set *s_arr = malloc(sizeof(max_value) + 1);
return d_arr->sparseSetPtr, s_arr->sparseSetPtr;
}
And this is the struct in question:
struct sparse_set {
int *d_arr;
int *s_arr;
sparse_set_ptr sparseSetPtr;
};
Any help is appreciated :)
EDIT: The given definition of a sparse set is the following:
A sparse set is a data structure used to store small sets of data. Its main use is in applications where a fixed number of items/elements need to be tracked. Sparse sets are used in compilers, development frameworks and more. This data structure offers extremely fast lookup, insertion and deletion times while also being cache-friendly. As with normal sets, duplicate items are not accepted in sparse sets.
A sparse set consists of two arrays working in tandem. These two arrays are referred to as the dense array and the sparse array.
The dense array, as its name suggests, is a compact array that stores its elements sequentially, with no gaps between the elements.
On the other hand, the sparse array is used as a lookup list, and stores indices that map to elements in the dense array. Whenever an operation is performed, the dense and the sparse array must be kept in sync. For example, consider a sparse set ranging over the numbers [0..7]. This means it can only store the elements 0, 1, 2, 3, 4, 5, 6, and 7. Suppose we want to store a maximum of four elements. This means that the sparse set will contain a sparse array of size 8 and a dense array of size 4.