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

Add rcutils_string_array_sort function #248

Merged
merged 9 commits into from
Jun 11, 2020
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
20 changes: 18 additions & 2 deletions include/rcutils/types/string_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ rcutils_string_array_fini(rcutils_string_array_t * string_array);

/// Compare two string arrays.
/**
* The two string arrays are compared according to lexographical order.
* The two string arrays are compared according to lexicographical order.
*
* \param[in] sa0 The first string array.
* \param[in] sa1 The second string array.
cottsay marked this conversation as resolved.
Show resolved Hide resolved
* \param[out] res Negative value if `lhs` appears before `rhs` in lexographical order.
* \param[out] res Negative value if `lhs` appears before `rhs` in lexicographical order.
* Zero if `lhs` and `rhs` are equal.
* Positive value if `lhs` appears after `rhs in lexographical order.
* \return `RCUTILS_RET_OK` if successful, or
Expand All @@ -133,6 +133,22 @@ rcutils_string_array_cmp(
const rcutils_string_array_t * rhs,
int * res);

/// Sort a string array according to lexicographical order.
/**
* This function changes the order of the entries in a string array so that
* they are in lexicographically ascending order. Empty entries are placed at the
cottsay marked this conversation as resolved.
Show resolved Hide resolved
* end of the array.
*
* \param[inout] string_array object whose elements should be sorted.
* \return `RCUTILS_RET_OK` if successful, or
* \return `RCUTILS_RET_INVALID_ARGUMENT` for invalid arguments, or
* \return `RCUTILS_RET_ERROR` if an unknown error occurs
*/
RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t
rcutils_string_array_sort(rcutils_string_array_t * string_array);

#ifdef __cplusplus
}
#endif
Expand Down
31 changes: 31 additions & 0 deletions src/string_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@ rcutils_string_array_cmp(
return RCUTILS_RET_OK;
}

static int
rcutils_string_array_sort_compare(const void * lhs, const void * rhs)
{
const char * left = *(const char **)lhs;
const char * right = *(const char **)rhs;
if (NULL == left) {
return NULL == right ? 0 : 1;
} else if (NULL == right) {
return -1;
}
return strcmp(left, right);
}

rcutils_ret_t
rcutils_string_array_sort(rcutils_string_array_t * string_array)
{
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
string_array, "string_array is null", return RCUTILS_RET_INVALID_ARGUMENT);

if (1 >= string_array->size) {
return RCUTILS_RET_OK;
}

RCUTILS_CHECK_FOR_NULL_WITH_MSG(
string_array->data, "string_array->data is null", return RCUTILS_RET_INVALID_ARGUMENT);

qsort(string_array->data, string_array->size, sizeof(char *), rcutils_string_array_sort_compare);

return RCUTILS_RET_OK;
}

#ifdef __cplusplus
}
#endif
62 changes: 62 additions & 0 deletions test/test_string_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "gtest/gtest.h"

#include "./allocator_testing_utils.h"
#include "rcutils/error_handling.h"
#include "rcutils/types/string_array.h"

#ifdef _WIN32
Expand Down Expand Up @@ -130,3 +131,64 @@ TEST(test_string_array, string_array_cmp) {
ret = rcutils_string_array_fini(&incomplete_string_array);
ASSERT_EQ(RCUTILS_RET_OK, ret);
}

TEST(test_string_array, string_array_sort) {
auto allocator = rcutils_get_default_allocator();
cottsay marked this conversation as resolved.
Show resolved Hide resolved
rcutils_ret_t ret;

ret = rcutils_string_array_sort(nullptr);
cottsay marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_EQ(RCUTILS_RET_INVALID_ARGUMENT, ret);
rcutils_reset_error();

rcutils_string_array_t sa0 = rcutils_get_zero_initialized_string_array();
ret = rcutils_string_array_sort(&sa0);
EXPECT_EQ(RCUTILS_RET_OK, ret);

ret = rcutils_string_array_init(&sa0, 8, &allocator);
ASSERT_EQ(RCUTILS_RET_OK, ret);

// Already in order
for (size_t i = 0; i < sa0.size; i++) {
const char val[] = {static_cast<char>('a' + i), '\0'};
sa0.data[i] = strdup(val);
cottsay marked this conversation as resolved.
Show resolved Hide resolved
}

ret = rcutils_string_array_sort(&sa0);
EXPECT_EQ(RCUTILS_RET_OK, ret);
for (size_t i = 0; i < sa0.size; i++) {
const char val[] = {static_cast<char>('a' + i), '\0'};
EXPECT_STREQ(val, sa0.data[i]);
}

// Reverse order
for (size_t i = 0; i < sa0.size; i++) {
const char val[] = {static_cast<char>('a' + sa0.size - 1 - i), '\0'};
free(sa0.data[i]);
sa0.data[i] = strdup(val);
cottsay marked this conversation as resolved.
Show resolved Hide resolved
}

ret = rcutils_string_array_sort(&sa0);
EXPECT_EQ(RCUTILS_RET_OK, ret);
for (size_t i = 0; i < sa0.size; i++) {
const char val[] = {static_cast<char>('a' + i), '\0'};
EXPECT_STREQ(val, sa0.data[i]);
}

// Make some entries empty
for (size_t i = 0; i < sa0.size / 2; i++) {
sa0.allocator.deallocate(sa0.data[i], sa0.allocator.state);
sa0.data[i] = nullptr;
}

ret = rcutils_string_array_sort(&sa0);
EXPECT_EQ(RCUTILS_RET_OK, ret);
for (size_t i = 0; i < sa0.size / 2; i++) {
const char val[] = {static_cast<char>('a' + i + (sa0.size / 2)), '\0'};
EXPECT_STREQ(val, sa0.data[i]);
}
for (size_t i = sa0.size / 2; i < sa0.size; i++) {
EXPECT_STREQ(nullptr, sa0.data[i]);
}

ASSERT_EQ(RCUTILS_RET_OK, rcutils_string_array_fini(&sa0));
}