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 utilities for iterables #350

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
44 changes: 44 additions & 0 deletions include/albatross/src/utils/iter_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2018 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_ITER_UTILS_H
#define ALBATROSS_ITER_UTILS_H

namespace albatross {

/*
* Convenience function instead of using the find == end
* method of checking if a value exists in an iterable
*/
template <typename Iterable, typename K,
typename std::enable_if_t<is_iterable<Iterable>, int> = 0>>
bool found(const Iterable &iterable, const K &key) {
return std::find(iterable.begin(), iterable.end(), key) != iterable.end();
}

template <typename Iterable, typename K,
typename std::enable_if_t<is_iterable<Iterable>, int> = 0>>
void insert_if_not_found(const Iterable &iterable, const K &key) {
if (!found(iterable, key)) {
std::back_inserter(iterable) = key;
}
}

template <typename Iterable, typename K,
typename std::enable_if_t<is_iterable<Iterable>, int> = 0>
>
void eliminate(const K &key, Iterable *iterable) {
std::erase(std::remove(iterable->begin(), iterabled->end(), key));
}

} // namespace albatross
#endif
42 changes: 42 additions & 0 deletions include/albatross/src/utils/traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2022 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_UTILS_TRAITS_H
#define ALBATROSS_UTILS_TRAITS_H

namespace albatross {

namespace detail {
// Taken from here:
// https://stackoverflow.com/questions/13830158/check-if-a-variable-type-is-iterable
using std::begin;
using std::end;

template <typename T>
auto is_iterable_impl(int)
-> decltype (
begin(std::declval<T&>()) != end(std::declval<T&>()), // begin/end and operator !=
void(), // Handle evil operator ,
++std::declval<decltype(begin(std::declval<T&>()))&>(), // operator ++
void(*begin(std::declval<T&>())), // operator*
std::true_type{});

template <typename T>
std::false_type is_iterable_impl(...);

}

template <typename T>
using is_iterable = decltype(detail::is_iterable_impl<T>(0));

} // namespace albatross
#endif