javascript cartesian product algorithm implementation method
- 2020-05-27 04:13:28
- OfStack
The example of this paper describes the implementation of javascript cartesian product algorithm. Share with you for your reference. The specific analysis is as follows:
Here cartesian products can be generated based on the given object or array
// Cartesian product combination
function descartes(list)
{
//parent on 1 Level index ;count Pointer to the count
var point = {};
var result = [];
var pIndex = null;
var tempCount = 0;
var temp = [];
// Generates a pointer object from a parameter column
for(var index in list)
{
if(typeof list[index] == 'object')
{
point[index] = {'parent':pIndex,'count':0}
pIndex = index;
}
}
// Single-dimensional data structures are returned directly
if(pIndex == null)
{
return list;
}
// The cartesian product is generated dynamically
while(true)
{
for(var index in list)
{
tempCount = point[index]['count'];
temp.push(list[index][tempCount]);
}
// Press into the result array
result.push(temp);
temp = [];
// Check for pointer maxima
while(true)
{
if(point[index]['count']+1 >= list[index].length)
{
point[index]['count'] = 0;
pIndex = point[index]['parent'];
if(pIndex == null)
{
return result;
}
// The assignment parent Check again
index = pIndex;
}
else
{
point[index]['count']++;
break;
}
}
}
}
I hope this article has been helpful to your javascript programming.