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

Unique Value Utilities #464

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ cc_library(
"include/albatross/src/indexing/group_by.hpp",
"include/albatross/src/indexing/subset.hpp",
"include/albatross/src/indexing/traits.hpp",
"include/albatross/src/indexing/unique.hpp",
"include/albatross/src/linalg/block_diagonal.hpp",
"include/albatross/src/linalg/block_symmetric.hpp",
"include/albatross/src/linalg/block_utils.hpp",
Expand Down Expand Up @@ -244,6 +245,7 @@ swift_cc_test(
"tests/test_traits_indexing.cc",
"tests/test_tune.cc",
"tests/test_utils.h",
"tests/test_unique.cc",
"tests/test_variant_utils.cc",
"tests/test_vector_utils.cc",
],
Expand Down
1 change: 1 addition & 0 deletions include/albatross/Indexing
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <albatross/src/indexing/subset.hpp>
#include <albatross/src/indexing/filter.hpp>
#include <albatross/src/indexing/apply.hpp>
#include <albatross/src/indexing/unique.hpp>

#include "utils/AsyncUtils"

Expand Down
69 changes: 69 additions & 0 deletions include/albatross/src/indexing/unique.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2023 Swift Navigation Inc.
* Contact: Swift Navigation <[email protected]>
*
* This source is subject to the license found in the file 'LICENSE' which must
* be distributed together with this source. All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*/

#ifndef ALBATROSS_INDEXING_UNIQUE_HPP_
#define ALBATROSS_INDEXING_UNIQUE_HPP_

namespace albatross {

template <typename ValueType>
inline std::set<ValueType> unique_values(const std::vector<ValueType> &values) {
return std::set<ValueType>(values.begin(), values.end());
}

template <typename ValueType, typename ApplyFunction,
typename ApplyType = typename details::value_only_apply_result<
ApplyFunction, ValueType>::type,
typename std::enable_if<details::is_valid_value_only_apply_function<
ApplyFunction, ValueType>::value &&
!std::is_same<void, ApplyType>::value,
int>::type = 0>
inline std::set<ApplyType> unique_values(const std::vector<ValueType> &xs,
ApplyFunction &&f) {
std::set<ApplyType> output;
for (const auto &v : xs) {
output.emplace(f(v));
}
return output;
}

// unique_value
// assumes there is one single value in the iterator
// and will return that value or assert if that assumption is false

template <typename ValueType>
inline ValueType unique_value(const std::set<ValueType> &values) {
assert(values.size() == 1 && "expected a single unique value");
return *values.begin();
}

template <typename ValueType>
inline ValueType unique_value(const std::vector<ValueType> &values) {
const auto unique_vals = unique_values(values);
return unique_value<ValueType>(unique_vals);
}

template <typename ValueType, typename ApplyFunction,
typename ApplyType = typename details::value_only_apply_result<
ApplyFunction, ValueType>::type,
typename std::enable_if<details::is_valid_value_only_apply_function<
ApplyFunction, ValueType>::value &&
!std::is_same<void, ApplyType>::value,
int>::type = 0>
inline ApplyType unique_value(const std::vector<ValueType> &xs,
ApplyFunction &&f) {
return unique_value(unique_values(xs, std::forward<ApplyFunction>(f)));
}

} // namespace albatross

#endif /* ALBATROSS_INDEXING_UNIQUE_HPP_ */
72 changes: 72 additions & 0 deletions tests/test_unique.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2023 Swift Navigation Inc.
* Contact: Swift Navigation <[email protected]>
*
* This source is subject to the license found in the file 'LICENSE' which must
* be distributed together with this source. All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*/

#include <albatross/Indexing>
#include <gtest/gtest.h>

namespace albatross {

// Empty input

TEST(test_unique, unique_values_empty) {
std::vector<int> empty = {};
EXPECT_EQ(unique_values(empty).size(), 0);
}

TEST(test_unique, unique_value_empty) {
std::vector<int> empty = {};
EXPECT_DEATH({ unique_value(empty); }, "Assertion");
}

TEST(test_unique, unique_values_function_empty) {
std::vector<int> empty = {};
auto foo = [](const int &x) { return x + 1; };
EXPECT_EQ(unique_values(empty, foo).size(), 0);
}

// Identical inputs

TEST(test_unique, unique_value_identical) {
std::vector<int> values = {3, 3, 3};
EXPECT_EQ(unique_value(values), 3);
}

// Mixed input

TEST(test_unique, unique_values_mixed) {
std::vector<int> values = {3, 1, 5, 1, 3};
std::set<int> expected = {1, 3, 5};
EXPECT_EQ(unique_values(values), expected);
}

TEST(test_unique, unique_value_function_mixed) {
std::vector<int> values = {3, 1, 5, 2};
auto foo = [](const auto &) -> double { return 4.; };
EXPECT_EQ(unique_value(values, foo), 4.);
}

TEST(test_unique, unique_value_mixed) {
std::vector<int> values = {3, 1, 5, 1, 3};
EXPECT_DEATH({ unique_value(values); }, "Assertion");
}

TEST(test_unique, unique_values_function_mixed) {
std::vector<int> values = {3, 1, 5, 1, 3};
auto foo = [](const int &x) { return x + 1; };

const auto applied = apply(values, foo);
std::set<int> expected(applied.begin(), applied.end());

EXPECT_EQ(unique_values(values, foo), expected);
}

} // namespace albatross
Loading