Pages

Thursday, December 30, 2010

Grouping Algorithm

/**
 * Grouping Algorithm
 * This algorithm will do select just a distinct value of the data.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 2.0
 */

/**
 * A constructor service.
 *
 * @param data array of data.
 */
function Grouping(data){
    this.data = data;
}

/**
 * A private method that provide slice functionality of the 
 * array of data
 *
 * @param data data to be sliced.
 * @return sliced of array
 */
Grouping.prototype.slice = function(data){
    var value = data[0];
    var newData = new Array();
    var k = 0;
    for(i=1;i<data.length;i++){
        if(data[i]!=value){
            newData[k]=data[i];
            k++;
        }
    }
    return newData;
};

/**
 * A method that gives what this class intended. 
 * This gives a distinct
 * value of array in the form of array.
 *
 * @return distinct value of array
 */
Grouping.prototype.create = function(){
    var dataValue = new Array();
    var k = 0;
    var loop = true;
    var data = this.data;
    while(loop){
        dataValue[k] = data[0];
        data = this.slice(data);
        k++;
        if(data.length==0)
            loop = false;
        }
        return dataValue;
};


/**
 * This method provide serive on how many a value counting in the data.
 *
 * @return array counting value.
 */
Grouping.prototype.valueCounting = function(){
    var groupedData = this.create();
    var countValue = new Array();
    var data = this.data;
    for(i=0;i<groupedData.length;i++){
        var k = 1;
        for(j=0;j<data.length;j++){
            if(groupedData[i]==data[j]){
                countValue[i]=k;
                k++;
            }
        }
    }
    return countValue;
};


/**
 * A method to map distinct value and how many occurence of 
 * the value
 * in the data
 *
 * @return two dimensional array in the form [n][2]
 */
Grouping.prototype.mapping = function(){
    var data = this.data;
    var dimData = new Array();
    var value = this.create();
    var countValue = this.valueCounting();
    var len = value.length;
    for(i=0;i<len;i++){
        dimData[i] = new Array();
        dimData[i][0] = value[i];
        dimData[i][1] = countValue[i];
    }
    return dimData;
};

No comments:

Post a Comment