JavaScript Cartesian product code demonstration

From , 4 Years ago, written in JavaScript, viewed 221 times.
URL https://pastebin.vip/view/e0854e3c
  1. //笛卡儿积组合
  2. function descartes(list)
  3. {
  4.     //parent上一级索引;count指针计数
  5.     var point  = {};
  6.  
  7.     var result = [];
  8.     var pIndex = null;
  9.     var tempCount = 0;
  10.     var temp   = [];
  11.  
  12.     //根据参数列生成指针对象
  13.     for(var index in list)
  14.     {
  15.         if(typeof list[index] == 'object')
  16.         {
  17.             point[index] = {'parent':pIndex,'count':0}
  18.             pIndex = index;
  19.         }
  20.     }
  21.  
  22.     //单维度数据结构直接返回
  23.     if(pIndex == null)
  24.     {
  25.         return list;
  26.     }
  27.  
  28.     //动态生成笛卡尔积
  29.     while(true)
  30.     {
  31.         for(var index in list)
  32.         {
  33.             tempCount = point[index]['count'];
  34.             temp.push(list[index][tempCount]);
  35.         }
  36.  
  37.         //压入结果数组
  38.         result.push(temp);
  39.         temp = [];
  40.  
  41.         //检查指针最大值问题
  42.         while(true)
  43.         {
  44.             if(point[index]['count']+1 >= list[index].length)
  45.             {
  46.                 point[index]['count'] = 0;
  47.                 pIndex = point[index]['parent'];
  48.                 if(pIndex == null)
  49.                 {
  50.                     return result;
  51.                 }
  52.  
  53.                 //赋值parent进行再次检查
  54.                 index = pIndex;
  55.             }
  56.             else
  57.             {
  58.                 point[index]['count']++;
  59.                 break;
  60.             }
  61.         }
  62.     }
  63. }
  64. //javascript/7013

Reply to "JavaScript Cartesian product code demonstration"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website