topology.js
function compareCells(a, b) {
  var n = a.length
    , t = a.length - b.length
    , min = Math.min
  if(t) {
    return t
  }
  switch(n) {
    case 0:
      return 0;
    case 1:
      return a[0] - b[0];
    case 2:
      var d = a[0]+a[1]-b[0]-b[1]
      if(d) {
        return d
      }
      return min(a[0],a[1]) - min(b[0],b[1])
    case 3:
      var l1 = a[0]+a[1]
        , m1 = b[0]+b[1]
      d = l1+a[2] - (m1+b[2])
      if(d) {
        return d
      }
      var l0 = min(a[0], a[1])
        , m0 = min(b[0], b[1])
        , d  = min(l0, a[2]) - min(m0, b[2])
      if(d) {
        return d
      }
      return min(l0+a[2], l1) - min(m0+b[2], m1)
    
    //TODO: Maybe optimize n=4 as well?
    
    default:
      var as = a.slice(0)
      as.sort()
      var bs = b.slice(0)
      bs.sort()
      for(var i=0; i<n; ++i) {
        t = as[i] - bs[i]
        if(t) {
          return t
        }
      }
      return 0
  }
}
function compareCells(a, b) {
  var n = a.length
    , t = a.length - b.length
    , min = Math.min
  if(t) {
    return t
  }
  switch(n) {
    case 0:
      return 0;
    case 1:
      return a[0] - b[0];
    case 2:
      var d = a[0]+a[1]-b[0]-b[1]
      if(d) {
        return d
      }
      return min(a[0],a[1]) - min(b[0],b[1])
    case 3:
      var l1 = a[0]+a[1]
        , m1 = b[0]+b[1]
      d = l1+a[2] - (m1+b[2])
      if(d) {
        return d
      }
      var l0 = min(a[0], a[1])
        , m0 = min(b[0], b[1])
        , d  = min(l0, a[2]) - min(m0, b[2])
      if(d) {
        return d
      }
      return min(l0+a[2], l1) - min(m0+b[2], m1)
    
    //TODO: Maybe optimize n=4 as well?
    
    default:
      var as = a.slice(0)
      as.sort()
      var bs = b.slice(0)
      bs.sort()
      for(var i=0; i<n; ++i) {
        t = as[i] - bs[i]
        if(t) {
          return t
        }
      }
      return 0
  }
}
function compareCells ( a , b ) { var n = a . length , t = a . length - b . length , min = Math . min if ( t ) { return t } switch ( n ) { case 0 : return 0 ; case 1 : return a [ 0 ] - b [ 0 ] ; case 2 : var d = a [ 0 ] + a [ 1 ] - b [ 0 ] - b [ 1 ] if ( d ) { return d } return min ( a [ 0 ] , a [ 1 ] ) - min ( b [ 0 ] , b [ 1 ] ) case 3 : var l1 = a [ 0 ] + a [ 1 ] , m1 = b [ 0 ] + b [ 1 ] d = l1 + a [ 2 ] - ( m1 + b [ 2 ] ) if ( d ) { return d } var l0 = min ( a [ 0 ] , a [ 1 ] ) , m0 = min ( b [ 0 ] , b [ 1 ] ) , d = min ( l0 , a [ 2 ] ) - min ( m0 , b [ 2 ] ) if ( d ) { return d } return min ( l0 + a [ 2 ] , l1 ) - min ( m0 + b [ 2 ] , m1 ) //TODO: Maybe optimize n=4 as well? default : var as = a . slice ( 0 ) as . sort ( ) var bs = b . slice ( 0 ) bs . sort ( ) for ( var i = 0 ; i < n ; ++ i ) { t = as [ i ] - bs [ i ] if ( t ) { return t } } return 0 } }
Ranks a pair of cells up to permutation
Ranks a pair of cells up to permutation