Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More const parameter for our element array class [1/3]. #1079

Merged
merged 7 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions src/t8_data/t8_containers.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,14 @@ t8_element_array_resize (t8_element_array_t *element_array, size_t new_count)
sc_array_resize (&element_array->array, new_count);
t8_element_t *first_new_elem;
/* Get the first newly allocated element */
first_new_elem = t8_element_array_index_locidx (element_array, old_count);
first_new_elem = t8_element_array_index_locidx_mutable (element_array, old_count);
/* Call t8_element_init on all new elements */
element_array->scheme->t8_element_init (new_count - old_count, first_new_elem);
}
else if (old_count > new_count) {
t8_element_t *first_old_elem;
/* Get the first element to deinit */
first_old_elem = t8_element_array_index_locidx (element_array, new_count);
first_old_elem = t8_element_array_index_locidx_mutable (element_array, new_count);
element_array->scheme->t8_element_deinit (old_count - new_count, first_old_elem);
sc_array_resize (&element_array->array, new_count);
}
Expand All @@ -199,12 +199,12 @@ t8_element_array_resize (t8_element_array_t *element_array, size_t new_count)
}

void
t8_element_array_copy (t8_element_array_t *dest, t8_element_array_t *src)
t8_element_array_copy (t8_element_array_t *dest, const t8_element_array_t *src)
{
T8_ASSERT (t8_element_array_is_valid (dest));
T8_ASSERT (t8_element_array_is_valid (src));
T8_ASSERT (dest->scheme == src->scheme);
sc_array_copy (&dest->array, &src->array);
sc_array_copy (&dest->array, (sc_array_t *) &src->array); /* need to convert src->array to non-const */
lukasdreyer marked this conversation as resolved.
Show resolved Hide resolved
}

t8_element_t *
Expand All @@ -229,22 +229,35 @@ t8_element_array_push_count (t8_element_array_t *element_array, size_t count)
return new_elements;
}

t8_element_t *
t8_element_array_index_locidx (t8_element_array_t *element_array, t8_locidx_t index)
const t8_element_t *
t8_element_array_index_locidx (const t8_element_array_t *element_array, t8_locidx_t index)
{
T8_ASSERT (t8_element_array_is_valid (element_array));
return (t8_element_t *) t8_sc_array_index_locidx (&element_array->array, index);
return (const t8_element_t *) t8_sc_array_index_locidx (&element_array->array, index);
}

t8_element_t *
t8_element_array_index_int (t8_element_array_t *element_array, int index)
const t8_element_t *
t8_element_array_index_int (const t8_element_array_t *element_array, int index)
{
T8_ASSERT (t8_element_array_is_valid (element_array));
return (t8_element_t *) sc_array_index_int (&element_array->array, index);
return (const t8_element_t *) sc_array_index_int ((sc_array_t *) &element_array->array,
index); /* Need to convert element_array->array to non-const */
}

t8_element_t *
t8_element_array_index_locidx_mutable (t8_element_array_t *element_array, t8_locidx_t index)
{
return (t8_element_t *) t8_element_array_index_locidx (element_array, index);
}

t8_element_t *
t8_element_array_index_int_mutable (t8_element_array_t *element_array, int index)
{
return (t8_element_t *) t8_element_array_index_int (element_array, index);
}

t8_eclass_scheme_c *
t8_element_array_get_scheme (t8_element_array_t *element_array)
const t8_eclass_scheme_c *
t8_element_array_get_scheme (const t8_element_array_t *element_array)
{
T8_ASSERT (t8_element_array_is_valid (element_array));
return element_array->scheme;
Expand All @@ -258,14 +271,14 @@ t8_element_array_get_count (const t8_element_array_t *element_array)
}

size_t
t8_element_array_get_size (t8_element_array_t *element_array)
t8_element_array_get_size (const t8_element_array_t *element_array)
{
T8_ASSERT (t8_element_array_is_valid (element_array));
return element_array->scheme->t8_element_size ();
}

t8_element_t *
t8_element_array_get_data (t8_element_array_t *element_array)
t8_element_array_get_data (const t8_element_array_t *element_array)
{
T8_ASSERT (t8_element_array_is_valid (element_array));

Expand All @@ -277,8 +290,16 @@ t8_element_array_get_data (t8_element_array_t *element_array)
}
}

const sc_array_t *
t8_element_array_get_array (const t8_element_array_t *element_array)
{
T8_ASSERT (t8_element_array_is_valid (element_array));

return &element_array->array;
}

sc_array_t *
t8_element_array_get_array (t8_element_array_t *element_array)
t8_element_array_get_array_mutable (t8_element_array_t *element_array)
{
T8_ASSERT (t8_element_array_is_valid (element_array));

Expand All @@ -289,7 +310,7 @@ void
t8_element_array_reset (t8_element_array_t *element_array)
{
T8_ASSERT (t8_element_array_is_valid (element_array));
t8_element_t *first_elem = t8_element_array_index_locidx (element_array, 0);
t8_element_t *first_elem = t8_element_array_index_locidx_mutable (element_array, 0);
size_t count = t8_element_array_get_count (element_array);
element_array->scheme->t8_element_deinit (count, first_elem);
sc_array_reset (&element_array->array);
Expand All @@ -299,7 +320,7 @@ void
t8_element_array_truncate (t8_element_array_t *element_array)
{
T8_ASSERT (t8_element_array_is_valid (element_array));
t8_element_t *first_elem = t8_element_array_index_locidx (element_array, 0);
t8_element_t *first_elem = t8_element_array_index_locidx_mutable (element_array, 0);
size_t count = t8_element_array_get_count (element_array);
element_array->scheme->t8_element_deinit (count, first_elem);
sc_array_truncate (&element_array->array);
Expand Down
51 changes: 39 additions & 12 deletions src/t8_data/t8_containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ t8_element_array_resize (t8_element_array_t *element_array, size_t new_count);
* \param [in] src Array used as source of new data, will not be changed.
*/
void
t8_element_array_copy (t8_element_array_t *dest, t8_element_array_t *src);
t8_element_array_copy (t8_element_array_t *dest, const t8_element_array_t *src);

/** Enlarge an array by one element.
* \param [in, ou] element_array Array structure to be modified.
Expand All @@ -157,30 +157,48 @@ t8_element_array_push (t8_element_array_t *element_array);
t8_element_t *
t8_element_array_push_count (t8_element_array_t *element_array, size_t count);

/** Return a given element in an array.
/** Return a given element in an array. Const version.
* \param [in] element_array Array of elements.
* \param [in] index The index of an element within the array.
* \return A pointer to the element stored at position \a index in
* \a element_array.
*/
const t8_element_t *
t8_element_array_index_locidx (const t8_element_array_t *element_array, t8_locidx_t index);

/** Return a given element in an array. Const version.
* \param [in] element_array Array of elements.
* \param [in] index The index of an element within the array.
* \return A pointer to the element stored at position \a index in
* \a element_array.
*/
const t8_element_t *
t8_element_array_index_int (const t8_element_array_t *element_array, int index);

/** Return a given element in an array. Mutable version.
* \param [in] element_array Array of elements.
* \param [in] index The index of an element within the array.
* \return A pointer to the element stored at position \a index in
* \a element_array.
*/
t8_element_t *
t8_element_array_index_locidx (t8_element_array_t *element_array, t8_locidx_t index);
t8_element_array_index_locidx_mutable (t8_element_array_t *element_array, t8_locidx_t index);

/** Return a given element in an array.
/** Return a given element in an array. Mutable version.
* \param [in] element_array Array of elements.
* \param [in] index The index of an element within the array.
* \return A pointer to the element stored at position \a index in
* \a element_array.
*/
t8_element_t *
t8_element_array_index_int (t8_element_array_t *element_array, int index);
t8_element_array_index_int_mutable (t8_element_array_t *element_array, int index);

/** Return the eclass scheme associated to a t8_element_array.
* \param [in] element_array Array of elements.
* \return The eclass scheme stored at \a element_array.
*/
t8_eclass_scheme_c *
t8_element_array_get_scheme (t8_element_array_t *element_array);
const t8_eclass_scheme_c *
t8_element_array_get_scheme (const t8_element_array_t *element_array);

/** Return the number of elements stored in a t8_element_array_t.
* \param [in] element_array Array structure.
Expand All @@ -193,22 +211,31 @@ t8_element_array_get_count (const t8_element_array_t *element_array);
* \return The size (in bytes) of a single element in \a element_array.
*/
size_t
t8_element_array_get_size (t8_element_array_t *element_array);
t8_element_array_get_size (const t8_element_array_t *element_array);

/** Return a pointer to the real data array stored in a t8_element_array.
/** Return a const pointer to the real data array stored in a t8_element_array.
* \param [in] element_array Array structure.
* \return A pointer to the stored data. If the number of stored
* elements is 0, then NULL is returned.
*/
t8_element_t *
t8_element_array_get_data (t8_element_array_t *element_array);
t8_element_array_get_data (const t8_element_array_t *element_array);
lukasdreyer marked this conversation as resolved.
Show resolved Hide resolved

/** Return a const pointer to the sc_array stored in a t8_element_array.
* \param [in] element_array Array structure.
* \return A const pointer to the sc_array storing the data.
* \note The data cannot be modified.
*/
const sc_array_t *
t8_element_array_get_array (const t8_element_array_t *element_array);

/** Return a pointer to the sc_array stored in a t8_element_array.
/** Return a mutable pointer to the sc_array stored in a t8_element_array.
* \param [in] element_array Array structure.
* \return A pointer to the sc_array storing the data.
* \note The data can be modified.
*/
sc_array_t *
t8_element_array_get_array (t8_element_array_t *element_array);
t8_element_array_get_array_mutable (t8_element_array_t *element_array);

/** Sets the array count to zero and frees all elements.
* \param [in,out] element_array Array structure to be reset.
Expand Down
2 changes: 1 addition & 1 deletion src/t8_forest/t8_forest.c
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ t8_forest_get_element (t8_forest_t forest, t8_locidx_t lelement_id, t8_locidx_t
tree = t8_forest_get_tree (forest, ltree);
if (tree->elements_offset <= lelement_id
&& lelement_id < tree->elements_offset + (t8_locidx_t) t8_element_array_get_count (&tree->elements)) {
return t8_element_array_index_locidx (&tree->elements, lelement_id - tree->elements_offset);
return t8_element_array_index_locidx_mutable (&tree->elements, lelement_id - tree->elements_offset);
}
/* The element was not found.
* This case is covered by the first if and should therefore never happen. */
Expand Down
11 changes: 7 additions & 4 deletions src/t8_forest/t8_forest_adapt.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ t8_forest_pos (t8_forest_t forest, t8_eclass_scheme_c *ts, t8_element_array_t *t
* maximum number of member. */
t8_locidx_t el_iter; /* Loop running variable */
t8_locidx_t pos = -1;
t8_element_t *element_compare;
const t8_element_t *element_compare;
for (el_iter = 1; el_iter < (t8_locidx_t) num_siblings && el_iter < elements_in_array; el_iter++) {
pos = telements_pos - el_iter;
T8_ASSERT (0 <= pos && pos < elements_in_array);
Expand Down Expand Up @@ -242,7 +242,8 @@ t8_forest_adapt_coarsen_recursive (t8_forest_t forest, t8_locidx_t ltreeid, t8_l
}
#endif
for (ielement = 0; ielement < (t8_locidx_t) num_siblings && pos + ielement < elements_in_array; ielement++) {
fam[ielement] = t8_element_array_index_locidx (telements, pos + ielement);
/* TODO: In a future version, fam[ielement] should be const and we should call t8_element_array_index_locidx (the const version). */
fam[ielement] = t8_element_array_index_locidx_mutable (telements, pos + ielement);
}

int num_elements_to_adapt_callback;
Expand Down Expand Up @@ -473,7 +474,8 @@ t8_forest_adapt (t8_forest_t forest)
}
#endif
for (zz = 0; zz < num_siblings && el_considered + (t8_locidx_t) zz < num_el_from; zz++) {
elements_from[zz] = t8_element_array_index_locidx (telements_from, el_considered + (t8_locidx_t) zz);
/* TODO: In a future version elements_from[zz] should be const and we should call t8_element_array_index_locidx (the const version). */
elements_from[zz] = t8_element_array_index_locidx_mutable (telements_from, el_considered + (t8_locidx_t) zz);
lukasdreyer marked this conversation as resolved.
Show resolved Hide resolved
/* This is a quick check whether we build up a family here and could
* abort early if not.
* If the child id of the current element is not zz, then it cannot
Expand Down Expand Up @@ -553,7 +555,8 @@ t8_forest_adapt (t8_forest_t forest)
else {
(void) t8_element_array_push_count (telements, num_children);
for (zz = 0; zz < num_children; zz++) {
elements[zz] = t8_element_array_index_locidx (telements, el_inserted + zz);
/* TODO: In a future version elements_from[zz] should be const and we should call t8_element_array_index_locidx (the const version). */
elements[zz] = t8_element_array_index_locidx_mutable (telements, el_inserted + zz);
}
tscheme->t8_element_children (elements_from[0], num_children, elements);
el_inserted += (t8_locidx_t) num_children;
Expand Down
Loading