[Tutor] fake defrag revisited

R. Alan Monroe amonroe at columbus.rr.com
Sat Oct 1 08:06:36 CEST 2011


I'm revisiting the fake defrag program I posted about a few months
ago. The concept is basically a screensaver or light show where you
can enjoy watching entropy being reversed as colored blocks order
themselves visually.

I set it aside for a while because it was too slow, but I finally came
up with a better algorithm for the simulated file creation & deletion.
So I can throroughly scramble my imaginary files.

Now I want to come up with a simulated defragging, but I wish it for
cosmetic reasons to visit the various areas of the drive in a way that
appears random, to make it less boring.

The fake "drive" is 64 blocks (shown as 8x8 grid onscreen) for test
purposes.

The files as-is (fragged):
freelist:[1, 3, 6, 7, 9, 10, 11, 13, 14, 15, 16, 18, 22, 23, 25, 26, 27, 28, 30, 31, 32, 33, 34, 36, 38, 39, 40, 41, 42, 43, 44, 46, 47, 49, 52, 57, 58, 59, 60, 61, 62, 63]
1000: [45, 0]
1002: [56, 19, 5, 35, 8]
1014: [21, 54, 2, 20, 53, 17, 12, 4, 37, 48]
1013: [50, 51, 55, 24, 29]

I can predict the arrangement of the files in the ideal end state
(defragged) by sorting the filenames in ascending order then assign
them blocks based on an incrementing number and the files' known
sizes:
freelist: [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]
1000: [0, 1]
1002: [2, 3, 4, 5, 6]
1013: [7, 8, 9, 10, 11]
1014: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21]


Which gives this list of before and after blocks: i.e. block 0 is
destined to live at 1 after defrag, block 1 is destined to live at 22,
etc.

[(0, 1), (1, 22), (2, 14), (3, 23), (4, 19), (5, 4), (6, 24), (7, 25),
(8, 6), (9, 26), (10, 27), (11, 28), (12, 18), (13, 29), (14, 30),
(15, 31), (16, 32), (17, 17), (18, 33), (19, 3), (20, 15), (21, 12),
(22, 34), (23, 35), (24, 10), (25, 36), (26, 37), (27, 38 ), (28, 39),
(29, 11), (30, 40), (31, 41), (32, 42), (33, 43), (34, 44), (35, 5),
(36, 45), (37, 20), (38, 46), (39, 47), (40, 48), (41, 49), (42, 50),
(43, 51), (44, 52), (45, 0), (46, 53), (47, 54), (48, 21), (49, 55),
(50, 7), (51, 8), (52, 56), (53, 16), (54, 13), (55, 9), (56, 2), (57,
57), (58, 58), (59, 59), (60, 60), (61, 61), (62, 62), (63, 63)]

I initially thought I could just do a random.shuffle on this list to
achieve the cosmetic randomness, until I realized the real problem is
magically determining the correct sequence in which to perform the
moves without ruining a future move inadverently.

If I move 0-to-1 first, I've now ruined the future 1-to-22 which ought
to have taken place in advance.

Is there a deterministic-yet-seemingly-random algorithm out there
whose name I wasn't aware of to be able to google it?

Alan



More information about the Tutor mailing list