Skip to content

Commit

Permalink
[algorithm] Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Apr 17, 2024
1 parent daa3867 commit 220425f
Show file tree
Hide file tree
Showing 19 changed files with 28 additions and 34 deletions.
2 changes: 1 addition & 1 deletion include/etl/_algorithm/binary_search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ template <typename ForwardIt, typename T, typename Compare>
[[nodiscard]] constexpr auto binary_search(ForwardIt first, ForwardIt last, T const& value, Compare comp) -> bool
{
first = etl::lower_bound(first, last, value, comp);
return (!(first == last) and !(comp(value, *first)));
return first != last and not comp(value, *first);
}

template <typename ForwardIt, typename T>
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/find_if_not.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ template <typename InputIt, typename Predicate>
[[nodiscard]] constexpr auto find_if_not(InputIt first, InputIt last, Predicate pred) noexcept -> InputIt
{
for (; first != last; ++first) {
if (!pred(*first)) {
if (not pred(*first)) {
return first;
}
}
Expand Down
4 changes: 2 additions & 2 deletions include/etl/_algorithm/includes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ template <typename InputIt1, typename InputIt2, typename Compare>
includes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp) -> bool
{
for (; first2 != last2; ++first1) {
if (first1 == last1 || comp(*first2, *first1)) {
if (first1 == last1 or comp(*first2, *first1)) {
return false;
}
if (!comp(*first1, *first2)) {
if (not comp(*first1, *first2)) {
++first2;
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/is_partitioned.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ template <typename InputIt, typename Predicate>
[[nodiscard]] constexpr auto is_partitioned(InputIt first, InputIt last, Predicate p) -> bool
{
for (; first != last; ++first) {
if (!p(*first)) {
if (not p(*first)) {
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/is_permutation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ template <typename ForwardIt1, typename ForwardIt2>
}

auto m = etl::count(fDiff2, last2, *i);
if (m == 0 || etl::count(i, last, *i) != m) {
if (m == 0 or etl::count(i, last, *i) != m) {
return false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions include/etl/_algorithm/minmax_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ template <typename ForwardIt, typename Compare>
auto min = first;
auto max = first;

if (first == last || ++first == last) {
if (first == last or ++first == last) {
return {min, max};
}

Expand All @@ -32,7 +32,7 @@ template <typename ForwardIt, typename Compare>
if (++first == last) {
if (comp(*i, *min)) {
min = i;
} else if (!(comp(*i, *max))) {
} else if (not comp(*i, *max)) {
max = i;
}
break;
Expand All @@ -42,14 +42,14 @@ template <typename ForwardIt, typename Compare>
if (comp(*first, *min)) {
min = first;
}
if (!(comp(*i, *max))) {
if (not comp(*i, *max)) {
max = i;
}
} else {
if (comp(*i, *min)) {
min = i;
}
if (!(comp(*first, *max))) {
if (not comp(*first, *max)) {
max = first;
}
}
Expand Down
4 changes: 2 additions & 2 deletions include/etl/_algorithm/mismatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ template <typename InputIt1, typename InputIt2, typename Predicate>
mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, Predicate pred) -> pair<InputIt1, InputIt2>
{
for (; first1 != last1; ++first1, (void)++first2) {
if (!pred(*first1, *first2)) {
if (not pred(*first1, *first2)) {
break;
}
}
Expand All @@ -45,7 +45,7 @@ template <typename InputIt1, typename InputIt2, typename Predicate>
mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Predicate pred) -> pair<InputIt1, InputIt2>
{
for (; first1 != last1 and first2 != last2; ++first1, (void)++first2) {
if (!pred(*first1, *first2)) {
if (not pred(*first1, *first2)) {
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/partition_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ template <typename ForwardIt, typename Predicate>
[[nodiscard]] constexpr auto partition_point(ForwardIt first, ForwardIt last, Predicate p) -> ForwardIt
{
for (; first != last; ++first) {
if (!p(*first)) {
if (not p(*first)) {
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/remove_copy_if.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ template <typename InputIt, typename OutputIt, typename Predicate>
constexpr auto remove_copy_if(InputIt first, InputIt last, OutputIt destination, Predicate p) -> OutputIt
{
for (; first != last; ++first, (void)++destination) {
if (!p(*first)) {
if (not p(*first)) {
*destination = *first;
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/remove_if.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ template <typename ForwardIt, typename Predicate>

if (first != last) {
for (auto i = first; ++i != last;) {
if (!pred(*i)) {
if (not pred(*i)) {
*first++ = etl::move(*i);
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ template <typename FwdIt1, typename FwdIt2, typename Predicate>
if (it == last) {
return last;
}
if (!pred(*it, *sIt)) {
if (not pred(*it, *sIt)) {
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/set_difference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
if (comp(*first1, *first2)) {
*destination++ = *first1++;
} else {
if (!comp(*first2, *first1)) {
if (not comp(*first2, *first1)) {
++first1;
}
++first2;
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/set_intersection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last
if (comp(*first1, *first2)) {
++first1;
} else {
if (!comp(*first2, *first1)) {
if (not comp(*first2, *first1)) {
*dest++ = *first1++;
}
++first2;
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/set_union.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Outp
}

*destination = *first1;
if (!comp(*first1, *first2)) {
if (not comp(*first1, *first2)) {
++first2;
}
++first1;
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/shift_left.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace etl {
/// \brief Shifts the elements in the range [first, last) by n positions.
///
/// \details Shifts the elements towards the beginning of the range. If n == 0
/// || n >= last - first, there are no effects. If n < 0, the behavior is
/// or n >= last - first, there are no effects. If n < 0, the behavior is
/// undefined. Otherwise, for every integer i in [0, last - first - n), moves
/// the element originally at position first + n + i to position first + i. The
/// moves are performed in increasing order of i starting from ​0​.
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/shift_right.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace etl {
/// \brief Shifts the elements in the range [first, last) by n positions.
///
/// \details Shifts the elements towards the end of the range.
/// If n <= 0 || n >= last - first, there are no effects. Otherwise, for
/// If n <= 0 or n >= last - first, there are no effects. Otherwise, for
/// every integer i in [0, last - first - n), moves the element originally
/// at position first + i to position first + n + i.
///
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/unique.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ constexpr auto unique(ForwardIt first, ForwardIt last, Predicate pred) -> Forwar

auto result = first;
while (++first != last) {
if (!pred(*result, *first) and ++result != first) {
if (not pred(*result, *first) and ++result != first) {
*result = etl::move(*first);
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/etl/_algorithm/unique_copy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ constexpr auto unique_copy(InputIt first, InputIt last, OutputIt destination, Pr
*destination = *first;

while (++first != last) {
if (!pred(*destination, *first)) {
if (not pred(*destination, *first)) {
*++destination = *first;
}
}
Expand Down
16 changes: 5 additions & 11 deletions include/etl/_algorithm/upper_bound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,18 @@ namespace etl {
/// that is greater than `value`, or last if no such element is found.
///
/// The range `[first, last)` must be partitioned with respect to the
/// expression `!(value < element)` or `!comp(value, element)`, i.e., all
/// expression `not (value < element)` or `not comp(value, element)`, i.e., all
/// elements for which the expression is true must precede all elements for
/// which the expression is false. A fully-sorted range meets this criterion.
template <typename ForwardIt, typename T, typename Compare>
[[nodiscard]] constexpr auto upper_bound(ForwardIt first, ForwardIt last, T const& value, Compare comp) -> ForwardIt
{
using diff_t = typename etl::iterator_traits<ForwardIt>::difference_type;

ForwardIt it{};
diff_t count{};
diff_t step{};
count = etl::distance(first, last);

auto count = etl::distance(first, last);
while (count > 0) {
it = first;
step = count / 2;
auto it = first;
auto step = count / 2;
etl::advance(it, step);
if (!comp(value, *it)) {
if (not comp(value, *it)) {
first = ++it;
count -= step + 1;
} else {
Expand Down

0 comments on commit 220425f

Please sign in to comment.