Some algorithm.hpp clarifications

This commit is contained in:
rexy712 2020-09-20 14:49:05 -07:00
parent 22903db4c1
commit 8873049f9f

View File

@ -21,13 +21,15 @@
#include "utility.hpp" //swap #include "utility.hpp" //swap
#include <cstdint> //SIZE_MAX #include <cstdint> //SIZE_MAX
#include "detail/algorithm.hpp"
#include <cstdlib> //size_t #include <cstdlib> //size_t
#include <type_traits> #include <type_traits>
#include "detail/algorithm.hpp"
namespace rexy{ namespace rexy{
//Requires Iterators to be LegacyRandomAccessIterators
//right is one past the end of the list
template<class Iter, class Compare> template<class Iter, class Compare>
constexpr void quicksort(Iter left, Iter right, const Compare& cmp) constexpr void quicksort(Iter left, Iter right, const Compare& cmp)
noexcept(noexcept(detail::qs_partition(left, right, cmp))) noexcept(noexcept(detail::qs_partition(left, right, cmp)))
@ -40,63 +42,67 @@ namespace rexy{
} }
} }
//Requires Iterators to be LegacyRandomAccessIterators
template<class HIter, class NIter> template<class HIter, class NIter>
constexpr HIter two_way_search(const HIter& hstart, const HIter& hend, const NIter& nstart, const NIter& nend){ constexpr HIter two_way_search(const HIter& hstart, const HIter& hend, const NIter& nstart, const NIter& nend){
size_t j = 0; size_t j = 0;
size_t i = 0; size_t i = 0;
size_t nlen = nend - nstart; size_t nlen = nend - nstart;
size_t hlen = hend - hstart; size_t hlen = hend - hstart;
auto [max_suffix, period] = detail::critical_factorization(nstart, nend); auto [suffix, period] = detail::critical_factorization(nstart, nend);
if(detail::iter_compare(nstart, nstart + period, max_suffix)){ if(detail::iter_compare(nstart, nstart + period, suffix)){
size_t memory = SIZE_MAX; size_t memory = SIZE_MAX;
while(j <= hlen - nlen){ while(j <= hlen - nlen){
i = max(max_suffix, memory) + 1; i = max(suffix, memory) + 1;
//right side //right side
while(i < nlen && nstart[i] == hstart[i + j]){ while(i < nlen && nstart[i] == hstart[i + j]){
++i; ++i;
} }
if(i >= nlen){ if(i >= nlen){
i = max_suffix; i = suffix;
//left side //left side
while(i > memory && nstart[i] == hstart[i + j]){ while(i > memory && nstart[i] == hstart[i + j]){
--i; --i;
} }
if(i <= memory){ if(i <= memory){
return hstart + j; //? return hstart + j;
} }
j += period; j += period;
memory = nlen - period - 1; memory = nlen - period - 1;
}else{ }else{
j += (i - max_suffix); j += (i - suffix);
memory = SIZE_MAX; memory = SIZE_MAX;
} }
} }
}else{ }else{
period = max(max_suffix + 1, nlen - max_suffix - 1) + 1; period = max(suffix + 1, nlen - suffix - 1) + 1;
j = 0; j = 0;
while(j <= hlen - nlen){ while(j <= hlen - nlen){
i = max_suffix + 1; i = suffix + 1;
//right side
while(i < nlen && nstart[i] == hstart[i + j]){ while(i < nlen && nstart[i] == hstart[i + j]){
++i; ++i;
} }
if(i >= nlen){ if(i >= nlen){
i = max_suffix; i = suffix;
//left side
while(i != SIZE_MAX && nstart[i] == hstart[i + j]){ while(i != SIZE_MAX && nstart[i] == hstart[i + j]){
--i; --i;
} }
if(i == SIZE_MAX){ if(i == SIZE_MAX){
return hstart + j; //? return hstart + j;
} }
j += period; j += period;
}else{ }else{
j += (i - max_suffix); j += (i - suffix);
} }
} }
} }
return hend; return hend;
} }
//searcher for use with generic search wrappers
struct two_way_searcher{ struct two_way_searcher{
template<class HIter, class NIter> template<class HIter, class NIter>
constexpr HIter operator()(const HIter& hstart, const HIter& hend, const NIter& nstart, const NIter& nend)const{ constexpr HIter operator()(const HIter& hstart, const HIter& hend, const NIter& nstart, const NIter& nend)const{