Anonymous user
Best shuffle: Difference between revisions
Improved D version
(Removed some casts from C version) |
(Improved D version) |
||
Line 305:
pure nothrow void bestShuffle(in char[] txt, char[] result) {
enum
enum
const
if (len == 0)
return;
Line 317:
// how many of each character?
foreach (char c; txt) {
counts[c]++;
Line 325:
}
assert(fmax > 0 && fmax <= len);
// how long can our cyclic groups be?▼
const int grp = 1 + (len - 1) / fmax;▼
// how many of them are full length?▼
const int lng = 1 + (len - 1) % fmax;▼
// all character positions, grouped by character
if ((len *
ptr1 = cast(
for (
if (counts[ch])
foreach (size_t j
if (
ndx1[i] = j;
i++;
Line 346 ⟶ 340:
// regroup them for cycles
if ((len *
ptr2 = cast(
for (
ndx2[i] = ndx1[n];
n += fmax;
Line 358 ⟶ 352:
}
}
▲ // how long can our cyclic groups be?
▲ // how many of them are full length?
// rotate each group
for (
foreach (size_t k; 1 .. glen)
ndx1[j + k - 1] = ndx2[j + k];
ndx1[j + glen - 1] = first;
Line 370:
// result is original permuted according to our cyclic groups
foreach (size_t i; 0 .. len)
result[ndx2[i]] = txt[ndx1[i]];
}
|