Index: cpp/frams/model/similarity/EMD/emd.c
===================================================================
--- cpp/frams/model/similarity/EMD/emd.c	(revision 1061)
+++ cpp/frams/model/similarity/EMD/emd.c	(revision 1062)
@@ -30,7 +30,4 @@
 */
 
-
-#define MAX_SIG_SIZE1  (MAX_SIG_SIZE+1)  /* FOR THE POSIBLE DUMMY FEATURE */
-
 /* NEW TYPES DEFINITION */
 
@@ -53,11 +50,7 @@
 
 /* GLOBAL VARIABLE DECLARATION */
-static int _n1, _n2;                          /* SIGNATURES SIZES */
-static float _CM[MAX_SIG_SIZE1][MAX_SIG_SIZE1];/* THE COST MATRIX */
-static node2_t _XV[MAX_SIG_SIZE1*2];            /* THE BASIC VARIABLES VECTOR */
+
 /* VARIABLES TO HANDLE _X EFFICIENTLY */
 static node2_t *_EndX, *_EnterX;
-static char _IsX[MAX_SIG_SIZE1][MAX_SIG_SIZE1];
-static node2_t *_RowsX[MAX_SIG_SIZE1], *_ColsX[MAX_SIG_SIZE1];
 static double _maxW;
 static float _maxC;
@@ -65,13 +58,14 @@
 /* DECLARATION OF FUNCTIONS */
 static float init(signature_t *Signature1, signature_t *Signature2,
-		  float (*Dist)(feature_t *, feature_t *));
-static void findBasicVariables(node1_t *U, node1_t *V);
-static int isOptimal(node1_t *U, node1_t *V);
-static int findLoop(node2_t **Loop);
-static void newSol();
-static void russel(double *S, double *D);
+		  float (*Dist)(feature_t *, feature_t *), int _n1, int _n2,
+        float **_CM, node2_t *_XV, char **_IsX, node2_t **_RowsX, node2_t **_ColsX);
+static void findBasicVariables(node1_t *U, node1_t *V, int _n1, int _n2, float **_CM, char **_IsX);
+static int isOptimal(node1_t *U, node1_t *V, int _n1, int _n2, float **_CM, char **_IsX);
+static int findLoop(node2_t **Loop, int _n1, int _n2, node2_t *_XV, node2_t **_RowsX, node2_t **_ColsX);
+static void newSol(int _n1, int _n2, node2_t *_XV, char **_IsX, node2_t **_RowsX, node2_t **_ColsX);
+static void russel(double *S, double *D, int _n1, int _n2, float **_CM, char **_IsX, node2_t **_RowsX, node2_t **_ColsX);
 static void addBasicVariable(int minI, int minJ, double *S, double *D, 
 			     node1_t *PrevUMinI, node1_t *PrevVMinJ,
-			     node1_t *UHead);
+			     node1_t *UHead, char **_IsX, node2_t **_RowsX, node2_t **_ColsX);
 #if DEBUG_LEVEL > 0
 static void printSolution();
@@ -101,14 +95,27 @@
 float emd(signature_t *Signature1, signature_t *Signature2,
 	  float (*Dist)(feature_t *, feature_t *),
-	  flow_t *Flow, int *FlowSize)
+	  flow_t *Flow, int *FlowSize, int _n1, int _n2)
 {
   int itr;
+  int max_n = (_n1 > _n2) ? _n1 : _n2;
   double totalCost;
   float w;
   node2_t *XP;
   flow_t *FlowP;
-  node1_t U[MAX_SIG_SIZE1], V[MAX_SIG_SIZE1];
-
-  w = init(Signature1, Signature2, Dist);
+  node1_t U[max_n], V[max_n];
+  /* THE COST MATRIX */
+  float** _CM = new float*[_n1];
+  char** _IsX = new char*[_n1];
+  for(int k = 0; k < _n1; ++k)
+  {
+    _CM[k] = new float[_n2];
+    _IsX[k] = new char[_n2];
+  }
+  /* THE BASIC VARIABLES VECTOR */  
+  node2_t *_XV = new node2_t[max_n*2];
+  node2_t **_RowsX = new node2_t*[max_n*2];
+  node2_t **_ColsX = new node2_t*[max_n*2];
+  
+  w = init(Signature1, Signature2, Dist, _n1, _n2, _CM, _XV, _IsX, _RowsX, _ColsX);
 
 #if DEBUG_LEVEL > 1
@@ -122,12 +129,12 @@
 	{
 	  /* FIND BASIC VARIABLES */
-	  findBasicVariables(U, V);
+	  findBasicVariables(U, V, _n1, _n2, _CM, _IsX);
 	  
 	  /* CHECK FOR OPTIMALITY */
-	  if (isOptimal(U, V))
+	  if (isOptimal(U, V, _n1, _n2, _CM, _IsX))
 	    break;
 	  
 	  /* IMPROVE SOLUTION */
-	  newSol();
+	  newSol(_n1, _n2, _XV, _IsX, _RowsX, _ColsX);
 	  
 #if DEBUG_LEVEL > 1
@@ -172,5 +179,16 @@
 #endif
 
-  /* RETURN THE NORMALIZED COST == EMD */
+  for(int k = 0; k < _n1; ++k)
+  {
+    delete[] _CM[k];
+    delete[] _IsX[k];
+  }
+  delete[] _CM;
+  delete[] _IsX;
+  delete[] _XV;
+  delete[] _RowsX;
+  delete[] _ColsX;  
+  
+  /* RETURN THE NORMALIZED COST == EMD */    
   return (float)(totalCost / w);
 }
@@ -184,19 +202,13 @@
 **********************/
 static float init(signature_t *Signature1, signature_t *Signature2, 
-		  float (*Dist)(feature_t *, feature_t *))
+		  float (*Dist)(feature_t *, feature_t *), int _n1, int _n2,
+        float **_CM, node2_t *_XV, char **_IsX, node2_t **_RowsX, node2_t **_ColsX)
 {
   int i, j;
+  int max_n = (_n1 > _n2) ? _n1 : _n2;
   double sSum, dSum, diff;
   feature_t *P1, *P2;
-  double S[MAX_SIG_SIZE1], D[MAX_SIG_SIZE1];
- 
-  _n1 = Signature1->n;
-  _n2 = Signature2->n;
-
-  if (_n1 > MAX_SIG_SIZE || _n2 > MAX_SIG_SIZE)
-    {
-      fprintf(stderr, "emd: Signature size is limited to %d\n", MAX_SIG_SIZE);
-      exit(1);
-    }
+  double S[max_n], D[max_n];
+
   
   /* COMPUTE THE DISTANCE MATRIX */
@@ -257,5 +269,5 @@
 
   /* FIND INITIAL SOLUTION */
-  russel(S, D);
+  russel(S, D, _n1, _n2, _CM, _IsX, _RowsX, _ColsX);
 
   _EnterX = _EndX++;  /* AN EMPTY SLOT (ONLY _n1+_n2-1 BASIC VARIABLES) */
@@ -268,5 +280,5 @@
     findBasicVariables
  **********************/
-static void findBasicVariables(node1_t *U, node1_t *V)
+static void findBasicVariables(node1_t *U, node1_t *V, int _n1, int _n2, float **_CM, char **_IsX)
 {
   int i, j, found;
@@ -406,5 +418,5 @@
     isOptimal
  **********************/
-static int isOptimal(node1_t *U, node1_t *V)
+static int isOptimal(node1_t *U, node1_t *V, int _n1, int _n2, float **_CM, char **_IsX)
 {    
   double delta, deltaMin;
@@ -451,10 +463,10 @@
     newSol
 **********************/
-static void newSol()
+static void newSol(int _n1, int _n2, node2_t * _XV, char **_IsX, node2_t **_RowsX, node2_t **_ColsX)
 {
-    int i, j, k;
+    int i, j, k, max_n = (_n1 > _n2) ? _n1 : _n2;
     double xMin;
     int steps;
-    node2_t *Loop[2*MAX_SIG_SIZE1], *CurX, *LeaveX;
+    node2_t *Loop[2*max_n], *CurX, *LeaveX;
  
 #if DEBUG_LEVEL > 3
@@ -473,5 +485,5 @@
 
     /* FIND A CHAIN REACTION */
-    steps = findLoop(Loop);
+    steps = findLoop(Loop, _n1, _n2, _XV, _RowsX, _ColsX);
 
     /* FIND THE LARGEST VALUE IN THE LOOP */
@@ -529,9 +541,9 @@
     findLoop
 **********************/
-static int findLoop(node2_t **Loop)
+static int findLoop(node2_t **Loop, int _n1, int _n2, node2_t *_XV, node2_t **_RowsX, node2_t **_ColsX)
 {
-  int i, steps;
+  int i, steps, max_n = (_n1 > _n2) ? _n1 : _n2;
   node2_t **CurX, *NewX;
-  char IsUsed[2*MAX_SIG_SIZE1]; 
+  char IsUsed[2*max_n]; 
  
   for (i=0; i < _n1+_n2; i++)
@@ -623,12 +635,12 @@
     russel
 **********************/
-static void russel(double *S, double *D)
+static void russel(double *S, double *D, int _n1, int _n2, float **_CM, char **_IsX, node2_t **_RowsX, node2_t **_ColsX)
 {
-  int i, j, found, minI, minJ;
+  int i, j, found, minI, minJ, max_n = (_n1 > _n2) ? _n1 : _n2;
   double deltaMin, oldVal, diff;
   double** Delta = new double*[_n1];
   for(int k = 0; k < _n1; ++k)
     Delta[k] = new double[_n2];
-  node1_t Ur[MAX_SIG_SIZE1], Vr[MAX_SIG_SIZE1];
+  node1_t Ur[max_n], Vr[max_n];
   node1_t uHead, *CurU, *PrevU;
   node1_t vHead, *CurV, *PrevV;
@@ -720,5 +732,5 @@
       /* ADD X[minI][minJ] TO THE BASIS, AND ADJUST SUPPLIES AND COST */
       Remember = PrevUMinI->Next;
-      addBasicVariable(minI, minJ, S, D, PrevUMinI, PrevVMinJ, &uHead);
+      addBasicVariable(minI, minJ, S, D, PrevUMinI, PrevVMinJ, &uHead, _IsX, _RowsX, _ColsX);
 
       /* UPDATE THE NECESSARY Delta[][] */
@@ -779,6 +791,6 @@
     } while (uHead.Next != NULL || vHead.Next != NULL);
     for(int k = 0; k < _n1; ++k)
-      delete [] Delta[k];
-    delete [] Delta;
+      delete[] Delta[k];
+    delete[] Delta;
 }
 
@@ -791,5 +803,5 @@
 static void addBasicVariable(int minI, int minJ, double *S, double *D, 
 			     node1_t *PrevUMinI, node1_t *PrevVMinJ,
-			     node1_t *UHead)
+			     node1_t *UHead, char **_IsX, node2_t **_RowsX, node2_t **_ColsX)
 {
   double T;
Index: cpp/frams/model/similarity/EMD/emd.h
===================================================================
--- cpp/frams/model/similarity/EMD/emd.h	(revision 1061)
+++ cpp/frams/model/similarity/EMD/emd.h	(revision 1062)
@@ -17,5 +17,4 @@
 
 /* DEFINITIONS */
-#define MAX_SIG_SIZE   1000
 #define MAX_ITERATIONS 500
 //#define INFINITY       1e20
