1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
//
// Copyright (C) 2014 Greg Landrum
// Adapted from pseudo-code from Roger Sayle
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <RDGeneral/export.h>
#ifndef HANOISORT_H_
#define HANOISORT_H_
#include <cstring>
#include <iostream>
#include <cassert>
#include <cstdlib>
#if defined(_MSC_VER)
#pragma warning(push, 1)
#pragma warning(disable: 4800)
#endif
namespace RDKit {
template <typename CompareFunc>
bool hanoi(int *base, int nel, int *temp, int *count, int *changed,
CompareFunc compar) {
// std::cerr<<" hanoi: "<<nel<< " start " << (*base)+1 << std::endl;
int *b1, *b2;
int *t1, *t2;
int *s1, *s2;
int n1, n2;
int result;
int *ptr;
if (nel == 1) {
count[base[0]] = 1;
return false;
} else if (nel == 2) {
n1 = base[0];
n2 = base[1];
int stat =
(/*!changed || */ changed[n1] || changed[n2]) ? compar(n1, n2) : 0;
if (stat == 0) {
count[n1] = 2;
count[n2] = 0;
return false;
} else if (stat < 0) {
count[n1] = 1;
count[n2] = 1;
return false;
} else /* stat > 0 */ {
count[n1] = 1;
count[n2] = 1;
base[0] = n2; /* temp[0] = n2; */
base[1] = n1; /* temp[1] = n1; */
return false; /* return True; */
}
}
n1 = nel / 2;
n2 = nel - n1;
b1 = base;
t1 = temp;
b2 = base + n1;
t2 = temp + n1;
if (hanoi(b1, n1, t1, count, changed, compar)) {
if (hanoi(b2, n2, t2, count, changed, compar)) {
s2 = t2;
} else
s2 = b2;
result = false;
ptr = base;
s1 = t1;
} else {
if (hanoi(b2, n2, t2, count, changed, compar)) {
s2 = t2;
} else
s2 = b2;
result = true;
ptr = temp;
s1 = b1;
}
while (true) {
assert(*s1 != *s2);
int stat =
(/*!changed || */ changed[*s1] || changed[*s2]) ? compar(*s1, *s2) : 0;
int len1 = count[*s1];
int len2 = count[*s2];
assert(len1 > 0);
assert(len2 > 0);
if (stat == 0) {
count[*s1] = len1 + len2;
count[*s2] = 0;
memmove(ptr, s1, len1 * sizeof(int));
ptr += len1;
n1 -= len1;
if (n1 == 0) {
if (ptr != s2) memmove(ptr, s2, n2 * sizeof(int));
return result;
}
s1 += len1;
// std::cerr<<" cpy: "<<*s1<<" "<<*s2<<" "<<len2<<std::endl;
memmove(ptr, s2, len2 * sizeof(int));
ptr += len2;
n2 -= len2;
if (n2 == 0) {
memmove(ptr, s1, n1 * sizeof(int));
return result;
}
s2 += len2;
} else if (stat < 0 && len1 > 0) {
memmove(ptr, s1, len1 * sizeof(int));
ptr += len1;
n1 -= len1;
if (n1 == 0) {
if (ptr != s2) memmove(ptr, s2, n2 * sizeof(int));
return result;
}
s1 += len1;
} else if (stat > 0 && len2 > 0) /* stat > 0 */ {
memmove(ptr, s2, len2 * sizeof(int));
ptr += len2;
n2 -= len2;
if (n2 == 0) {
memmove(ptr, s1, n1 * sizeof(int));
return result;
}
s2 += len2;
} else {
assert(0);
}
}
}
template <typename CompareFunc>
void hanoisort(int *base, int nel, int *count, int *changed,
CompareFunc compar) {
int *temp;
temp = (int *)malloc(nel * sizeof(int));
if (hanoi(base, nel, temp, count, changed, compar))
memmove(base, temp, nel * sizeof(int));
free(temp);
}
}
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#endif /* HANOISORT_H_ */
|