Skip to content

Commit

Permalink
Shortcut for vector find and contains
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Mar 3, 2024
1 parent 4545b35 commit acb1ef5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 2 additions & 3 deletions terrain/instancing/voxel_instance_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ void VoxelInstanceLibrary::add_item(int p_id, Ref<VoxelInstanceLibraryItem> item
if (generator.is_valid()) {
PackedItems::Lod &lod = _packed_items.lods[item->get_lod_index()];
MutexLock mlock(_packed_items.mutex);
if (!contains(to_span_const(lod.items),
[id](const PackedItem &existing_item) { return existing_item.id == id; })) {
if (!contains(lod.items, [id](const PackedItem &existing_item) { return existing_item.id == id; })) {
PackedItem packed_item;
packed_item.id = id;
packed_item.generator = generator;
Expand Down Expand Up @@ -67,7 +66,7 @@ void VoxelInstanceLibrary::remove_item(int p_id) {
{
MutexLock mlock(_packed_items.mutex);
size_t index;
if (find(to_span_const(lod.items), index, [id](const PackedItem &pi) { return pi.id == id; })) {
if (find(lod.items, index, [id](const PackedItem &pi) { return pi.id == id; })) {
lod.items.erase(lod.items.begin() + index);
}
}
Expand Down
10 changes: 10 additions & 0 deletions util/containers/container_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ bool find(Span<const T> items, size_t &out_index, TPredicate predicate) {
return false;
}

template <typename T, typename TPredicate>
bool find(const std::vector<T> &vec, size_t &out_index, TPredicate predicate) {
return find(to_span_const(vec), out_index, predicate);
}

template <typename T>
bool contains(Span<const T> items, const T &v) {
for (const T &item : items) {
Expand All @@ -206,6 +211,11 @@ bool contains(Span<const T> items, TPredicate predicate) {
return false;
}

template <typename T, typename TPredicate>
bool contains(const std::vector<T> &vec, TPredicate predicate) {
return contains(to_span_const(vec), predicate);
}

} // namespace zylann

#endif // ZN_CONTAINER_FUNCS_H

0 comments on commit acb1ef5

Please sign in to comment.