Skip to content

Instantly share code, notes, and snippets.

@i-e-b
Last active August 29, 2017 15:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i-e-b/3905490 to your computer and use it in GitHub Desktop.
Save i-e-b/3905490 to your computer and use it in GitHub Desktop.
Sequence masks and code for old Hypercard fade
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Scramble_test {
public class Scrambler {
UInt32 mask = 0;
UInt32 value = 1;
UInt32 length = 0;
public Scrambler (UInt32 Length) {
SetLength(Length);
}
public IEnumerable<UInt32> Values () {
if (length < 1) yield break;
do {
value = ((value & 1) != 0u) ? ((value >> 1) ^ mask) : (value >> 1);
if (value <= length) yield return value - 1;
} while (value != 1u);
yield break;
}
private void SetLength (UInt32 Length) {
mask = GetMask(Length);
value = 1u;
length = Length;
}
private UInt32 GetMask (UInt32 r) {
// Jump table of magic numbers:
if (r < 0x00000004) return 0x00000003u;
if (r < 0x00000008) return 0x00000006u;
if (r < 0x00000010) return 0x0000000Cu;
if (r < 0x00000020) return 0x00000014u;
if (r < 0x00000040) return 0x00000030u;
if (r < 0x00000080) return 0x00000060u;
if (r < 0x00000100) return 0x000000B8u;
if (r < 0x00000200) return 0x00000110u;
if (r < 0x00000400) return 0x00000240u;
if (r < 0x00000800) return 0x00000500u;
if (r < 0x00001000) return 0x00000CA0u;
if (r < 0x00002000) return 0x00001B00u;
if (r < 0x00004000) return 0x00003500u;
if (r < 0x00008000) return 0x00006000u;
if (r < 0x00010000) return 0x0000B400u;
if (r < 0x00020000) return 0x00012000u;
if (r < 0x00040000) return 0x00020400u;
if (r < 0x00080000) return 0x00072000u;
if (r < 0x00100000) return 0x00090000u;
if (r < 0x00200000) return 0x00140000u;
if (r < 0x00400000) return 0x00300000u;
if (r < 0x00800000) return 0x00400000u;
if (r < 0x01000000) return 0x00D80000u;
if (r < 0x02000000) return 0x01200000u;
if (r < 0x04000000) return 0x03880000u;
if (r < 0x08000000) return 0x07200000u;
if (r < 0x10000000) return 0x09000000u;
if (r < 0x20000000) return 0x14000000u;
if (r < 0x40000000) return 0x32800000u;
if (r < 0x80000000) return 0x48000000u;
if (r <= 0xFFFFFFFF) return 0xA3000000u;
// if you need anymore than this, you should do your own paging.
return 0xA3000000u; // this'll send your range checker nuts!
}
}
}
static long seqMasks [ /* n^(0..32) */ ] =
{0x0, 0x0, 0x03, 0x06, 0x0C, 0x14, 0x30, 0x60, 0xB8, 0x0110, 0x0240,
0x0500, 0x0CA0, 0x1B00, 0x3500, 0x6000, 0xB400, 0x00012000, 0x00020400,
0x00072000, 0x00090000, 0x00140000, 0x00300000, 0x00400000, 0x00D80000,
0x01200000, 0x03880000, 0x07200000, 0x09000000, 0x14000000, 0x32800000,
0x48000000, 0xA3000000
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment