Skip to content

Commit

Permalink
[base] Add an useful algorithm, fix ubsan complaints about murmur3 hash
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Nov 19, 2022
1 parent 82e059d commit 514c7f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/ossia/detail/algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ auto last_before(T&& container, const K& k)
return it;
}

template <typename T, typename K>
auto find_key(T&& vec, const K& key) noexcept
{
return std::find_if(
vec.begin(), vec.end(), [&](const auto& elt) { return elt.first == key; });
}

template <std::size_t N>
struct num
{
Expand Down Expand Up @@ -268,4 +275,16 @@ auto insert_at_end(D& dest, S<T, Alloc>&& src)
std::make_move_iterator(src.end()));
}

// https://stackoverflow.com/questions/45447361/how-to-move-certain-elements-of-stdvector-to-a-new-index-within-the-vector
template <typename T>
void change_item_position(T& v, size_t oldIndex, size_t newIndex)
{
assert(oldIndex < v.size() && newIndex < v.size());

if(oldIndex > newIndex)
std::rotate(v.rend() - oldIndex - 1, v.rend() - oldIndex, v.rend() - newIndex);
else
std::rotate(
v.begin() + oldIndex, v.begin() + oldIndex + 1, v.begin() + newIndex + 1);
}
}
2 changes: 1 addition & 1 deletion src/ossia/detail/murmur3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#pragma once
#include <cstdint>
#pragma clang attribute push( \
__attribute__((no_sanitize("undefined"))), apply_to = function)
__attribute__((no_sanitize("integer"))), apply_to = function)

namespace ossia
{
Expand Down

0 comments on commit 514c7f5

Please sign in to comment.