Генератор для перебора бесконечных ветвящихся неповторяющихся последовательностей (неслучайных чисел с шагом 1).
module.exports = (function(){
var self = {}
self.constructor = function()
{
if(!(this instanceof self.constructor))
{
return new (Function.prototype.bind.apply(self.constructor, [null].concat(Array.prototype.slice.call(arguments))))
}
this.pointer = -1
this.sequence = []
this.exhausted = false
this.roundcount = 0
}
self.debug = function()
{
console.log(JSON.stringify([this.exhausted,this.sequence,this.pointer,this.roundcount]))
}
self.currentRound = function()
{
return this.roundcount
}
self.currentValue = function()
{
return this.sequence[this.pointer]
}
self.nextValue = function(min,max)
{
this.pointer++
if(this.sequence[this.pointer] == null)
{
this.sequence[this.pointer] = { max : max, min : min, val : min }
}
return this.sequence[this.pointer].val
}
self.nextRound = function()
{
if(this.sequence[this.pointer] != null
&& ++this.sequence[this.pointer].val > this.sequence[this.pointer].max)
{
this.sequence.splice(this.pointer,1)
this.pointer--
if(this.pointer == -1)
{
this.exhausted = true
}
else
{
this.nextRound()
}
}
this.pointer = -1
this.roundcount++
return
}
self.constructor.prototype = self
return self.constructor
})()
NotRand = require('./NotRand.js')
var i = 0;
var a = NotRand()
while(!a.exhausted)
{
var b = []
b.push(a.nextValue(1,1))
b.push(a.nextValue(1,4))
if(b[1] == 3)
{
b.push(a.nextValue(1,3))
b.push(a.nextValue(1,3))
}
else
{
b.push(a.nextValue(1,2))
}
a.nextRound()
console.log(b)
}
console.log(a.currentRound())
$ node pereborjs.js [ 1, 1, 1 ] [ 1, 1, 2 ] [ 1, 2, 1 ] [ 1, 2, 2 ] [ 1, 3, 1, 1 ] [ 1, 3, 1, 2 ] [ 1, 3, 1, 3 ] [ 1, 3, 2, 1 ] [ 1, 3, 2, 2 ] [ 1, 3, 2, 3 ] [ 1, 3, 3, 1 ] [ 1, 3, 3, 2 ] [ 1, 3, 3, 3 ] [ 1, 4, 1 ] [ 1, 4, 2 ] 23