+#if SIZEOF_VOID_P == SIZEOF_INT
+typedef unsigned int p_int;
+#elif SIZEOF_VOID_P == SIZEOF_LONG
+typedef unsigned long p_int;
+#elif SIZEOF_VOID_P == SIZEOF_LONG_LONG
+typedef unsigned long long p_int;
+#else
+typedef unsigned long p_int;
+#endif
+
+/** Truncated multiplication with golden ratio, for computing hash values. */
+inline unsigned golden_ratio_hash(p_int n)
+{
+ // This function works much better when fast arithmetic with at
+ // least 64 significant bits is available.
+#if SIZEOF_LONG >= 8
+ // So 'long' has 64 bits. Excellent! We prefer it because it might be
+ // more efficient than 'long long'.
+ unsigned long l = n * 0x4f1bbcddUL;
+ return (unsigned)l;
+#elif SIZEOF_LONG_LONG >= 8
+ // This requires 'long long' (or an equivalent 64 bit type)---which is,
+ // unfortunately, not ANSI-C++-compliant.
+ // (Yet C99 demands it, which is reason for hope.)
+ unsigned long long l = n * 0x4f1bbcddULL;
+ return (unsigned)l;
+#else
+ // Without a type with 64 significant bits do the multiplication manually
+ // by splitting n up into the lower and upper two bytes.
+ const unsigned n0 = (n & 0x0000ffffU);
+ const unsigned n1 = (n & 0xffff0000U) >> 16;
+ return (n0 * 0x0000bcddU) + ((n1 * 0x0000bcddU + n0 * 0x00004f1bU) << 16);
+#endif
+}
+
+/* Compute the sign of a permutation of a container, with and without an
+ explicitly supplied comparison function. If the sign returned is 1 or -1,
+ the container is sorted after the operation. */
+template <class It>
+int permutation_sign(It first, It last)
+{
+ if (first == last)
+ return 0;
+ --last;
+ if (first == last)
+ return 0;
+ It flag = first;
+ int sign = 1;
+
+ do {
+ It i = last, other = last;
+ --other;
+ bool swapped = false;
+ while (i != first) {
+ if (*i < *other) {
+ std::iter_swap(other, i);
+ flag = other;
+ swapped = true;
+ sign = -sign;
+ } else if (!(*other < *i))
+ return 0;
+ --i; --other;
+ }
+ if (!swapped)
+ return sign;
+ ++flag;
+ if (flag == last)
+ return sign;
+ first = flag;
+ i = first; other = first;
+ ++other;
+ swapped = false;
+ while (i != last) {
+ if (*other < *i) {
+ std::iter_swap(i, other);
+ flag = other;
+ swapped = true;
+ sign = -sign;
+ } else if (!(*i < *other))
+ return 0;
+ ++i; ++other;
+ }
+ if (!swapped)
+ return sign;
+ last = flag;
+ --last;
+ } while (first != last);
+
+ return sign;
+}
+
+template <class It, class Cmp, class Swap>
+int permutation_sign(It first, It last, Cmp comp, Swap swapit)
+{
+ if (first == last)
+ return 0;
+ --last;
+ if (first == last)
+ return 0;
+ It flag = first;
+ int sign = 1;
+
+ do {
+ It i = last, other = last;
+ --other;
+ bool swapped = false;
+ while (i != first) {
+ if (comp(*i, *other)) {
+ swapit(*other, *i);
+ flag = other;
+ swapped = true;
+ sign = -sign;
+ } else if (!comp(*other, *i))
+ return 0;
+ --i; --other;
+ }
+ if (!swapped)
+ return sign;
+ ++flag;
+ if (flag == last)
+ return sign;
+ first = flag;
+ i = first; other = first;
+ ++other;
+ swapped = false;
+ while (i != last) {
+ if (comp(*other, *i)) {
+ swapit(*i, *other);
+ flag = other;
+ swapped = true;
+ sign = -sign;
+ } else if (!comp(*i, *other))
+ return 0;
+ ++i; ++other;
+ }
+ if (!swapped)
+ return sign;
+ last = flag;
+ --last;
+ } while (first != last);
+
+ return sign;
+}
+
+/* Implementation of shaker sort, only compares adjacent elements. */
+template <class It, class Cmp, class Swap>
+void shaker_sort(It first, It last, Cmp comp, Swap swapit)
+{
+ if (first == last)
+ return;
+ --last;
+ if (first == last)
+ return;
+ It flag = first;
+
+ do {
+ It i = last, other = last;
+ --other;
+ bool swapped = false;
+ while (i != first) {
+ if (comp(*i, *other)) {
+ swapit(*other, *i);
+ flag = other;
+ swapped = true;
+ }
+ --i; --other;
+ }
+ if (!swapped)
+ return;
+ ++flag;
+ if (flag == last)
+ return;
+ first = flag;
+ i = first; other = first;
+ ++other;
+ swapped = false;
+ while (i != last) {
+ if (comp(*other, *i)) {
+ swapit(*i, *other);
+ flag = other;
+ swapped = true;
+ }
+ ++i; ++other;
+ }
+ if (!swapped)
+ return;
+ last = flag;
+ --last;
+ } while (first != last);
+}
+
+/* In-place cyclic permutation of a container (no copying, only swapping). */
+template <class It, class Swap>
+void cyclic_permutation(It first, It last, It new_first, Swap swapit)
+{
+ unsigned num = last - first;
+again:
+ if (first == new_first || num < 2)
+ return;
+
+ unsigned num1 = new_first - first, num2 = last - new_first;
+ if (num1 >= num2) {
+ It a = first, b = new_first;
+ while (b != last) {
+ swapit(*a, *b);
+ ++a; ++b;
+ }
+ if (num1 > num2) {
+ first += num2;
+ num = num1;
+ goto again;
+ }
+ } else {
+ It a = new_first, b = last;
+ do {
+ --a; --b;
+ swapit(*a, *b);
+ } while (a != first);
+ last -= num1;
+ num = num2;
+ goto again;
+ }
+}
+
+
+// Collection of `construct on first use' wrappers for safely avoiding
+// internal object replication without running into the `static
+// initialization order fiasco'. This chest of numbers helps speed up
+// the library but should not be used outside it since it is
+// potentially confusing.
+
+class ex;