B2G/gecko/toolkit/components/places/ClusterLib.js
/**
   * Once we decide to merge two clusters in the cluster method, actually
   * merge them.  Alters the given state of the algorithm.
   *
   * @param clusters
   *        The array of all remaining clusters
   * @param distances
   *        Cached distances between pairs of clusters
   * @param neighbors
   *        Cached closest clusters
   * @param clustersByKey
   *        Array of all clusters, indexed by key
   * @param cluster1
   *        First cluster to merge
   * @param cluster2
   *        Second cluster to merge
   */
Once we decide to merge two clusters in the cluster method, actually
merge them.  Alters the given state of the algorithm.


Once we decide to merge two clusters in the cluster method actually merge them . Alters the given state of the algorithm .
function HC_mergeClus(clusters, distances, neighbors,
                                       clustersByKey, cluster1, cluster2) {
    let merged = { item: this.merge(cluster1.item, cluster2.item),
                   left: cluster1,
                   right: cluster2,
                   key: cluster1.key,
                   size: cluster1.size + cluster2.size };
    clusters[cluster1.index] = merged;
    clusters.splice(cluster2.index, 1);
    clustersByKey[cluster1.key] = merged;
    for (let i = 0; i < clusters.length; i++) {
      var ci = clusters[i];
      var dist;
      if (cluster1.key == ci.key) {
        dist = Infinity;
      } else if (this.merge == clusterlib.single_linkage) {
        dist = distances[cluster1.key][ci.key];
        if (distances[cluster1.key][ci.key] >
            distances[cluster2.key][ci.key]) {
          dist = distances[cluster2.key][ci.key];
        }
      } else if (this.merge == clusterlib.complete_linkage) {
        dist = distances[cluster1.key][ci.key];
        if (distances[cluster1.key][ci.key] <
            distances[cluster2.key][ci.key]) {
          dist = distances[cluster2.key][ci.key];
        }
      } else if (this.merge == clusterlib.average_linkage) {
        dist = (distances[cluster1.key][ci.key] * cluster1.size
              + distances[cluster2.key][ci.key] * cluster2.size)
              / (cluster1.size + cluster2.size);
      } else {
        dist = this.distance(ci.item, cluster1.item);
      }
      distances[cluster1.key][ci.key] = distances[ci.key][cluster1.key]
                                      = dist;
    }
    for (let i = 0; i < clusters.length; i++) {
      var key1 = clusters[i].key;
      if (neighbors[key1] == cluster1.key ||
          neighbors[key1] == cluster2.key) {
        let minKey = key1;
        for (let j = 0; j < clusters.length; j++) {
          var key2 = clusters[j].key;
          if (distances[key1][key2] < distances[key1][minKey]) {
            minKey = key2;
          }
        }
        neighbors[key1] = minKey;
      }
      clusters[i].index = i;
    }
  }
function HC_mergeClus ( clusters , distances , neighbors , clustersByKey , cluster1 , cluster2 ) { let merged = { item : this . merge ( cluster1 . item , cluster2 . item ) , left : cluster1 , right : cluster2 , key : cluster1 . key , size : cluster1 . size + cluster2 . size } ; clusters [ cluster1 . index ] = merged ; clusters . splice ( cluster2 . index , 1 ) ; clustersByKey [ cluster1 . key ] = merged ; for ( let i = 0 ; i < clusters . length ; i ++ ) { var ci = clusters [ i ] ; var dist ; if ( cluster1 . key == ci . key ) { dist = Infinity ; } else if ( this . merge == clusterlib . single_linkage ) { dist = distances [ cluster1 . key ] [ ci . key ] ; if ( distances [ cluster1 . key ] [ ci . key ] > distances [ cluster2 . key ] [ ci . key ] ) { dist = distances [ cluster2 . key ] [ ci . key ] ; } } else if ( this . merge == clusterlib . complete_linkage ) { dist = distances [ cluster1 . key ] [ ci . key ] ; if ( distances [ cluster1 . key ] [ ci . key ] < distances [ cluster2 . key ] [ ci . key ] ) { dist = distances [ cluster2 . key ] [ ci . key ] ; } } else if ( this . merge == clusterlib . average_linkage ) { dist = ( distances [ cluster1 . key ] [ ci . key ] * cluster1 . size + distances [ cluster2 . key ] [ ci . key ] * cluster2 . size ) / ( cluster1 . size + cluster2 . size ) ; } else { dist = this . distance ( ci . item , cluster1 . item ) ; } distances [ cluster1 . key ] [ ci . key ] = distances [ ci . key ] [ cluster1 . key ] = dist ; } for ( let i = 0 ; i < clusters . length ; i ++ ) { var key1 = clusters [ i ] . key ; if ( neighbors [ key1 ] == cluster1 . key || neighbors [ key1 ] == cluster2 . key ) { let minKey = key1 ; for ( let j = 0 ; j < clusters . length ; j ++ ) { var key2 = clusters [ j ] . key ; if ( distances [ key1 ] [ key2 ] < distances [ key1 ] [ minKey ] ) { minKey = key2 ; } } neighbors [ key1 ] = minKey ; } clusters [ i ] . index = i ; } }
Once we decide to merge two clusters in the cluster method, actually
 merge them.
Once we decide to merge two clusters in the cluster method actually merge them .
// update distances with new merged cluster
 // update cached neighbors
