Last active
October 9, 2015 11:38
-
-
Save Constellation/3498498 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var module = (function (exports) { | |
var NameSequence, | |
ZeroSequenceCache; | |
NameSequence = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); | |
ZeroSequenceCache = []; | |
function stringRepeat(str, num) { | |
var result = ''; | |
for (num |= 0; num > 0; num >>>= 1, str += str) { | |
if (num & 1) { | |
result += str; | |
} | |
} | |
return result; | |
} | |
function zeroSequence(num) { | |
var res = ZeroSequenceCache[num]; | |
if (res !== undefined) { | |
return res; | |
} | |
res = stringRepeat('0', num); | |
ZeroSequenceCache[num] = res; | |
return res; | |
} | |
function generateNextName(name) { | |
var ch, index, cur; | |
cur = name.length - 1; | |
do { | |
ch = name.charAt(cur); | |
index = NameSequence.indexOf(ch); | |
if (index !== (NameSequence.length - 1)) { | |
return name.substring(0, cur) + NameSequence[index + 1] + zeroSequence(name.length - (cur + 1)); | |
} | |
--cur; | |
} while (cur >= 0); | |
return 'a' + zeroSequence(name.length); | |
} | |
exports.generateNextName = generateNextName; | |
return exports; | |
})({}); | |
var name = 'a'; | |
for (var i = 0; i < 10000000000; ++i) { | |
print(name); | |
name = module.generateNextName(name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment