B2G/gecko/toolkit/components/places/ClusterLib.js
/**
   * Run the hierarchical clustering algorithm on the given items to produce
   * a final set of clusters.  Uses the parameters set in the constructor.
   *
   * @param items
   *        An array of "things" to cluster - this is the domain-specific
   *        collection you're trying to cluster (colors, points, etc.)
   * @param snapshotGap
   *        How many iterations of the clustering algorithm to wait between
   *        calling the snapshotCallback
   * @param snapshotCallback
   *        If provided, will be called as clusters are merged to let you view
   *        the progress of the algorithm.  Passed the current array of
   *        clusters, cached distances, and cached closest clusters.
   *
   * @return An array of merged clusters.  The represented item can be
   *         found in the "item" property of the cluster.
   */
Run the hierarchical clustering algorithm on the given items to produce
a final set of clusters.  Uses the parameters set in the constructor.

@param items
An array of "things" to cluster - this is the domain-specific
collection you're trying to cluster (colors, points, etc.)
@param snapshotGap
How many iterations of the clustering algorithm to wait between
calling the snapshotCallback
@param snapshotCallback
If provided, will be called as clusters are merged to let you view
the progress of the algorithm.  Passed the current array of
clusters, cached distances, and cached closest clusters.

@return An array of merged clusters.  The represented item can be
found in the "item" property of the cluster.
Run the hierarchical clustering algorithm on the given items to produce a final set of clusters . Uses the parameters set in the constructor . @param items An array of " things " to cluster - this is the domain - specific collection you ' re trying to cluster ( colors points etc . ) @param snapshotGap How many iterations of the clustering algorithm to wait between calling the snapshotCallback @param snapshotCallback If provided will be called as clusters are merged to let you view the progress of the algorithm . Passed the current array of clusters cached distances and cached closest clusters . @return An array of merged clusters . The represented item can be found in the " item " property of the cluster .
function HC_cluster(items, snapshotGap, snapshotCallback) {
    let clusters = [];
    let distances = [];
    let neighbors = [];
    let clustersByKey = [];
    for (let index = 0; index < items.length; index++) {
      let cluster = {
        item: items[index],
        key: index,
        index: index,
        size: 1
      };
      clusters[index] = cluster;
      clustersByKey[index] = cluster;
      distances[index] = [];
      neighbors[index] = 0;
    }
    for (let i = 0; i < clusters.length; i++) {
      for (let j = 0; j <= i; j++) {
        var dist = (i == j) ? Infinity :
          this.distance(clusters[i].item, clusters[j].item);
        distances[i][j] = dist;
        distances[j][i] = dist;
        if (dist < distances[i][neighbors[i]]) {
          neighbors[i] = j;
        }
      }
    }
    let next = null, i = 0;
    for (; next = this.closestClusters(clusters, distances, neighbors); i++) {
      if (snapshotCallback && (i % snapshotGap) == 0) {
        snapshotCallback(clusters);
      }
      this.mergeClusters(clusters, distances, neighbors, clustersByKey,
                         clustersByKey[next[0]], clustersByKey[next[1]]);
    }
    return clusters;
  }
function HC_cluster ( items , snapshotGap , snapshotCallback ) { let clusters = [ ] ; let distances = [ ] ; let neighbors = [ ] ; let clustersByKey = [ ] ; for ( let index = 0 ; index < items . length ; index ++ ) { let cluster = { item : items [ index ] , key : index , index : index , size : 1 } ; clusters [ index ] = cluster ; clustersByKey [ index ] = cluster ; distances [ index ] = [ ] ; neighbors [ index ] = 0 ; } for ( let i = 0 ; i < clusters . length ; i ++ ) { for ( let j = 0 ; j <= i ; j ++ ) { var dist = ( i == j ) ? Infinity : this . distance ( clusters [ i ] . item , clusters [ j ] . item ) ; distances [ i ] [ j ] = dist ; distances [ j ] [ i ] = dist ; if ( dist < distances [ i ] [ neighbors [ i ] ] ) { neighbors [ i ] = j ; } } } let next = null , i = 0 ; for ( ; next = this . closestClusters ( clusters , distances , neighbors ) ; i ++ ) { if ( snapshotCallback && ( i % snapshotGap ) == 0 ) { snapshotCallback ( clusters ) ; } this . mergeClusters ( clusters , distances , neighbors , clustersByKey , clustersByKey [ next [ 0 ] ] , clustersByKey [ next [ 1 ] ] ) ; } return clusters ; }
Run the hierarchical clustering algorithm on the given items to produce
 a final set of clusters.
Run the hierarchical clustering algorithm on the given items to produce a final set of clusters .
// array of all remaining clusters
 // 2D matrix of distances between each pair of clusters, indexed by key
 // closest cluster key for each cluster, indexed by key
 // an array of all clusters, but indexed by key
 // set up clusters from the initial items array
 // the item this cluster represents
 // a unique key for this cluster, stays constant unless merged itself
 // index of cluster in clusters array, can change during any merge
 // how many clusters have been merged into this one
 // initialize distance matrix and cached neighbors
 // merge the next two closest clusters until none of them are close enough
