JS multiple arrays with the Cartesian product Cartesian product algorithm implemented method of Example

Cartesian product codes js implemented algorithm, or an array of data objects generated Cartesian product, and describes an example of a multiple array javascript Cartesian product, and an algorithm implemented java Example Cartesian product codes.

A, javascript Cartesian product algorithm code

Example, according to the object or array generated Cartesian product.

// Cartesian product composition 
function Descartes (List) {
   // index on a parent; count count pointer 
  var Point = {};
   var Result = [];
   var Pindex = null ;
   var tempCount = 0 ;
   var TEMP = [ ];
   // parameter object from the pointer sequence generation 
  for ( var index in List) {
     IF ( typeof List [index] == ' Object ' ) { 
      Point [index] = {
         ' parent ': Pindex,
         ' COUNT ' : 0 
      } 
      Pindex = index; 
    } 
  } 
  // single dimensional data structure directly back 
  IF (Pindex == null ) {
     return List; 
  } 
  // dynamically generated Cartesian product of 
  the while ( to true ) {
     for ( var index in List) { 
      tempCount = Point [index] [ ' COUNT ' ]; 
      temp.push (List [index] [tempCount]); 
    } 
    // pressed into the result array
    result.push (TEMP); 
    TEMP = [];
     // check pointer Maximizing 
    the while ( to true ) {
       IF (Point [index] [ ' COUNT ' ] + . 1 > = List [index] .length) { 
        Point [index ] [ ' COUNT ' ] = 0 ; 
        Pindex = Point [index] [ ' parent ' ];
         IF (Pindex == null ) {
           return Result; 
        } 
        // assignment parent check again 
        index =Pindex; 
      } the else { 
        Point [index] [ ' COUNT ' ] ++ ;
         BREAK ; 
      } 
    } 
  } 
} 

calling the method: 

var Result = Descartes ({ ' AA ' : [ ' A ' , ' B ' , ' C ' , ' D ' ], ' BB ' : [ ' $ ' , ' % ' , '^' , ' & ' ]}); 
Alert (Result); // Result is the Cartesian product

 

 

Two, js achieve multiple array Cartesian product

example:

<script>
(function() {
  dwn = function(a) {
    document.writeln(a + "<br />")
  };
  //笛卡尔积
  var Cartesian = function(a, b) {
    var ret = [];
    for (var i = 0; i < a.length; i++) {
      for (var j = 0; j < b.length; j++) {
        ret.push(ft(a[i], b[j]));
      }
    }
    return ret;
  }
  var ft = function(a, b) {
    if (! (a instanceof Array)) a = [a];
    var ret = Array.call(null, a);
    ret.push(b);
    return ret;
  }
  //多个一起做笛卡尔积
  multiCartesian = function(data) {
    var len = data.length;
    if (len == 0) return [];
    else if (len == 1) return data[0];
    else {
      var r = data[0];
      for (var i = 1; i < len; i++) {
        r = Cartesian(r, data[i]);
      }
      return r;
    }
  }
})();
var data = [['a', 'b', 'c'], [1, 2, 3, 4], ['A', 'B'], ['#', '@', '+'], ['Mary', 'Terry', 'KYO']];
var r = multiCartesian(data);
for (var i = 0; i < r.length; i++) {
  dwn("(" + r[i] + ")");
}
</script>

 

To deepen the understanding of the Cartesian product, the method recommended here a tutorial java Cartesian product: //www.jb51.net/article/129585.htm , you can refer to.

More related content on JavaScript interested readers can view the site topic: " JavaScript data structures and algorithms skills summary ," " JavaScript math usage summary ," " JavaScript summary sorting algorithm ", " JavaScript traversal algorithm and skills summary " " JavaScript search algorithm skills summary " and " JavaScript error and debugging skills summary "

Guess you like

Origin www.cnblogs.com/plBlog/p/11775261.html