mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
AK: Make NonnullPtrVectors shuffleable
AK::shuffle() doesn't work on these, because their operator[] returns a reference to the pointed-at value, instead of to the NonnullPtr itself.
This commit is contained in:
parent
ccabc8e930
commit
c7a66882f4
Notes:
sideshowbarker
2024-07-17 09:47:09 +09:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/SerenityOS/serenity/commit/c7a66882f4 Pull-request: https://github.com/SerenityOS/serenity/pull/17103 Reviewed-by: https://github.com/linusg
13
AK/Random.h
13
AK/Random.h
@ -63,6 +63,19 @@ inline void shuffle(Collection& collection)
|
||||
}
|
||||
}
|
||||
|
||||
// shuffle() implementation for NonnullPtrVector, since its operator[] returns a reference to the pointed-at value
|
||||
// instead of the pointer itself.
|
||||
template<typename Collection>
|
||||
requires(requires(Collection collection) { collection.ptr_at(0); })
|
||||
inline void shuffle(Collection& collection)
|
||||
{
|
||||
// Fisher-Yates shuffle
|
||||
for (size_t i = collection.size() - 1; i >= 1; --i) {
|
||||
size_t j = get_random_uniform(i + 1);
|
||||
AK::swap(collection.ptr_at(i), collection.ptr_at(j));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
|
Loading…
Reference in New Issue
Block a user