<email@andreas-eberle.com>
<http://www.gnu.org/licenses/>
#ifndef EBERLE_MERGESORT_LCP_LOSERTREE_H_
#define EBERLE_MERGESORT_LCP_LOSERTREE_H_
#include <utility>
#include "../tools/eberle-utilities.h"
#include "../tools/eberle-lcp-losertree.h"
#include "../sequential/eberle-inssort-lcp.h"
namespace eberle_mergesort
{
using namespace eberle_lcp_utils;
typedef unsigned char* string;
template<unsigned K>
static inline void
eberle_mergesort_losertree_lcp_kway(string* strings, const LcpCacheStringPtr& tmp,
const LcpCacheStringPtr& output, size_t length)
{
if (length <= 2 * K)
{
return eberle_inssort_lcp::inssort_lcp(strings, output, length);
}
std::pair<size_t, size_t> ranges[K];
eberle_utils::calculateRanges(ranges, K, length);
for (unsigned i = 0; i < K; i++)
{
const size_t offset = ranges[i].first;
const size_t size = ranges[i].second;
eberle_mergesort_losertree_lcp_kway<K>(strings + offset,
output.sub(offset, size), tmp.sub(offset, size), size);
}
LcpStringLoserTree<K> loserTree(tmp, ranges);
loserTree.writeElementsToStream(output);
}
template<unsigned K>
static inline void
eberle_mergesort_losertree_lcp_kway(string* strings, const LcpCacheStringPtr& outputPtr)
{
size_t length = outputPtr.size;
LcpCacheStringPtr tmpPtr(new string[length], new lcp_t[length], new char_type[length], length);
eberle_mergesort_losertree_lcp_kway<K>(strings, tmpPtr, outputPtr, length);
delete[] tmpPtr.strings;
delete[] tmpPtr.lcps;
delete[] tmpPtr.cachedChars;
}
template<unsigned K>
static inline void
eberle_mergesort_losertree_kway(string *strings, size_t n)
{
lcp_t* outputLcps = new lcp_t[n];
string* tmpStrings = new string[n];
lcp_t* tmpLcps = new lcp_t[n];
char_type* outputCache = new char_type[n];
char_type* tmpCache = new char_type[n];
LcpCacheStringPtr output(strings, outputLcps, outputCache, n);
LcpCacheStringPtr tmp(tmpStrings, tmpLcps, tmpCache, n);
eberle_mergesort_losertree_lcp_kway<K>(strings, tmp, output, n);
#ifdef EBERLE_LCP_LOSERTREE_MERGESORT_CHECK_LCPS
stringtools::verify_lcp_cache(output.strings, output.lcps, output.cachedChars, output.size, 0);
#endif
delete[] outputLcps;
delete[] tmpStrings;
delete[] tmpLcps;
delete[] outputCache;
delete[] tmpCache;
}
void
eberle_mergesort_losertree_4way(string *strings, size_t n)
{
eberle_mergesort_losertree_kway<4>(strings, n);
}
void
eberle_mergesort_losertree_16way(string *strings, size_t n)
{
eberle_mergesort_losertree_kway<16>(strings, n);
}
void
eberle_mergesort_losertree_32way(string *strings, size_t n)
{
eberle_mergesort_losertree_kway<32>(strings, n);
}
void
eberle_mergesort_losertree_64way(string *strings, size_t n)
{
eberle_mergesort_losertree_kway<64>(strings, n);
}
CONTESTANT_REGISTER(eberle_mergesort_losertree_4way,
"eberle/mergesort_lcp_losertree_4way",
"Mergesort with lcp aware Losertree by Andreas Eberle")
CONTESTANT_REGISTER(eberle_mergesort_losertree_16way,
"eberle/mergesort_lcp_losertree_16way",
"Mergesort with lcp aware Losertree by Andreas Eberle")
CONTESTANT_REGISTER(eberle_mergesort_losertree_32way,
"eberle/mergesort_lcp_losertree_32way",
"Mergesort with lcp aware Losertree by Andreas Eberle")
CONTESTANT_REGISTER(eberle_mergesort_losertree_64way,
"eberle/mergesort_lcp_losertree_64way",
"Mergesort with lcp aware Losertree by Andreas Eberle")
}
#endif