#ifndef __KMEANS__
#define __KMEANS__
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include <iostream>

/// kmeans clustering
class kmeans
{
 public:
  kmeans();
  ~kmeans();

  int load( double *pts, int ndata );

  /// load data 
  int load( double **pts, int ndata,
            int np );

  /// load with out copying data
  int loadnocopy( std::vector< double* > &pts, int np );
  int cluster( int nclusters );

  int clear();
  int clearclusters();

  double *center( int i )
  {
    return centers[i];
  }

  int ncenters()
  {
    return centers.size();
  }

  int clustersize( int c );

  /// returns the cluster the datapoint belongs to
  int classify( double *d );

 private:

  /// calculate distance between two vectors
  double distance(double *a, double *b);

  /// adds a = a + b
  int add(double *a, double *b);
  int divide( double *a, double v );

  int group();
  int recalcenter();

  std::vector< double * > datapts;
  std::vector< int > datapts_class;
  std::vector< double * > centers;
  std::vector< int > csize;
  int npts;
  int nparams;

  int nocopy;
};

#endif
