Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Mutable.nextPermutation and add {next/prev}permutation(By) #498

Merged
merged 7 commits into from
Aug 19, 2024

Commits on Jul 27, 2024

  1. doc refactor: Mutable.nextPermutation

    Clarified that `Data.Vector.*.Mutable.nextPermutation` does not
    update the vector when the original state is the last permutation.
    gksato committed Jul 27, 2024
    Configuration menu
    Copy the full SHA
    310cb7c View commit details
    Browse the repository at this point in the history
  2. Optimize Generic.Mutable.nextPermutation

    This implements some optimization of `nextPermutation` from
    `Data.Vector.Generic.Mutable`. The main content of this
    re-implementation is the following two points:
    
    1. Wrapping the whole implementation in `stToPrim`. This
       allows the compiler to optimize the code better.
    2. When finding the rightmost increasing pair v[k]<v[k+1], we now
       search from the right, instead of from the left. This allows us to
       abort the search as soon as we find such a pair, giving
       average-case constant performance, instead of best-case linear
       in the previous implementation.
    gksato committed Jul 27, 2024
    Configuration menu
    Copy the full SHA
    9eca380 View commit details
    Browse the repository at this point in the history
  3. Add {next,prev}Permutation{,By} to *.Mutable

    This adds the following three companions to the already existing
    `Data.Vector.*.Mutable.nextPermutation`:
    
    - `Data.Vector.*.Mutable.nextPermutationBy`
    - `Data.Vector.*.Mutable.prevPermutation`
    - `Data.Vector.*.Mutable.prevPermutationBy`
    
    They are all implemented in terms of the already existing
    `Data.Vector.Generic.Mutable.nextPermutationLt`.
    gksato committed Jul 27, 2024
    Configuration menu
    Copy the full SHA
    cc689f2 View commit details
    Browse the repository at this point in the history
  4. Add INLINE to Generic.Mutable.nextPermutationByLt

    The function `Data.Vector.Generic.Mutable.nextPermutationByLt` is
    the unified implementation for the family of functions
    `Data.Vector.*.Mutable.{next,prev}Permutation{,By}`.
    By adding INLINE pragma to it, we may expect the some performance gain
    from specialization.
    gksato committed Jul 27, 2024
    Configuration menu
    Copy the full SHA
    063c8ef View commit details
    Browse the repository at this point in the history

Commits on Aug 3, 2024

  1. Add tests for Mutable.{next/prev}Permutation

    This adds the following three tests for the pair of functions
    `Data.Vector.*.Mutable.{next/prev}Permutation` in
    `vector/tests/Tests/Move.hs`:
    
    1. `testRevPermutations`: For n=1,..,7, repeatedly applying
    `prevPermutation` to a vector `[n,n-1..1]` produces all n! permutations
    of the vector in reverse order, and applying the function to the
    lexicographically smallest permutation doesn't change the vector.
    2. `testNPPermutationsIsId`: Applying `nextPermutation` followed by
    `prevPermutation` to a vector produces the original vector.
    Note that this function uses modified versions of `nextPermutation`
    and `prevPermutation` that reverse the vector if the original function
    returns `False`, rendering those two functions bijective.
    3. `testPNPermutationsIsId`: Applying `prevPermutation` followed by
    `nextPermutation` to a vector produces the original vector. The same
    caveat as above applies here.
    gksato committed Aug 3, 2024
    Configuration menu
    Copy the full SHA
    54c59a6 View commit details
    Browse the repository at this point in the history

Commits on Aug 18, 2024

  1. Add changelog + @since for next/prevPermutation

    This adds a changelog entry and `@since` annotations for:
    - Optimization of `Data.Vector.Generic.Mutable.nextPermutation`
    - Addition of `Data.Vector.Generic.Mutable.prevPermutation(By)`
    - Addition of `Data.Vector.Generic.Mutable.nextPermutationBy`
    
    This also tweaks the haddock comments of the functions for
    `Data.Vector.*.Mutable.(next|prev)Permutation(By)?` for a better
    readability.
    gksato committed Aug 18, 2024
    Configuration menu
    Copy the full SHA
    b21bf11 View commit details
    Browse the repository at this point in the history
  2. Add benchmarks for Mutable.(next|prev)Permutation

    Implement benchmarks to test performance of nextPermutation and
    prevPermutation on mutable vectors. Tests include:
    
    - Looping through all permutations on small vectors
    - Applying bijective versions n times on:
      - Ascending permutations of size n
      - Descending permutations of size n
      - Random permutations of size n
    - For a baseline, copying a vector of size n once. Benchmarks for
      bijective permutations begins with such a copy, and you might want
      to remove the impact of copying from the results.
    
    Benchmarks cover both forward (next) and reverse (prev) operations.
    gksato committed Aug 18, 2024
    Configuration menu
    Copy the full SHA
    7415257 View commit details
    Browse the repository at this point in the history