MCPcopy Create free account
hub / github.com/KaisenAmin/c_std / algorithm_search_n

Function algorithm_search_n

algorithm/algorithm.c:2004–2030  ·  view source on GitHub ↗

* @brief Searches for a sequence of identical elements within a range. * * This function searches for the first occurrence of a sequence of `count` identical elements * (each element matching `val`) within the range `[first, last)`. * * @param first Pointer to the start of the range. * @param last Pointer to the end of the range. * @param size Size of each element in bytes. * @param count

Source from the content-addressed store, hash-verified

2002
2003/**
2004 * @brief Generates the next lexicographical permutation of a range.
2005 *
2006 * This function rearranges the elements in the range `[first, last)` into the next lexicographical
2007 * permutation, as determined by the comparison function `comp`. If such a permutation is not possible,
2008 * the range is rearranged into the smallest lexicographical order (sorted in ascending order).
2009 *
2010 * @param first Pointer to the start of the range.
2011 * @param last Pointer to one past the end of the range.
2012 * @param size Size of each element in bytes.
2013 * @param comp Comparison function used to compare elements.
2014 * It should return `true` if the first element is less than the second, `false` otherwise.
2015 *
2016 * @return `true` if the next permutation was generated, `false` if the range was reset to the smallest permutation.
2017 */
2018bool algorithm_next_permutation(void *first, void *last, size_t size, CompareFuncBool comp) {
2019 ALGORITHM_LOG("[algorithm_next_permutation] Info: Generating next permutation.");
2020 if (first == last) {
2021 ALGORITHM_LOG("[algorithm_next_permutation] Warning: Empty range, returning false.");
2022 return false;
2023 }
2024
2025 char *i = (char *)last - size;
2026 while (i != (char *)first) {
2027 char *j = i;
2028 i -= size;
2029
2030 if (comp(i, j)) {
2031 char *k = (char *)last - size;
2032 while (!comp(i, k)) {
2033 k -= size;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected