Index: /trunk/SophyaLib/NTools/nbtrixx.cc
===================================================================
--- /trunk/SophyaLib/NTools/nbtrixx.cc	(revision 3684)
+++ /trunk/SophyaLib/NTools/nbtrixx.cc	(revision 3685)
@@ -7,21 +7,15 @@
 namespace SOPHYA {
 
-// attention, Numerical recipes prend des tableaux de 1 a n on remet
-// de 0 a n-1 en decramentant le pointeur du tableau d'entree
- 
-//-------------------------------------------------------------
-#define SWAP_NBTRI(a,b) temp=(a);(a)=(b);(b)=temp;
-#define M 7
-#define NSTACK 50
+typedef int_4 Int; // 32 bit integer
+
+//-------------------------------------------------------------
 template <class T>
-void TabSort(uint_4 n, T* arr_c)
+void TabSort(uint_4 n, T* arr)
 {
-	uint_4 i,ir=n,j,k,l=1,*istack;
-	int jstack=0;
-	T a,temp;
-        T *arr = arr_c - 1; // MODIF
-
-	//istack=lvector(1,NSTACK);
-        istack = new uint_4[NSTACK+1]; // MODIF
+	static const Int M=7, NSTACK=64;
+	Int i,ir,j,k,jstack=-1,l=0;
+	T a;
+	Int* istack = new Int[NSTACK];
+	ir=n-1;
 	for (;;) {
 		if (ir-l < M) {
@@ -34,18 +28,18 @@
 				arr[i+1]=a;
 			}
-			if (jstack == 0) break;
+			if (jstack < 0) break;
 			ir=istack[jstack--];
 			l=istack[jstack--];
 		} else {
 			k=(l+ir) >> 1;
-			SWAP_NBTRI(arr[k],arr[l+1])
+			_NR_SWAP(arr[k],arr[l+1]);
 			if (arr[l] > arr[ir]) {
-				SWAP_NBTRI(arr[l],arr[ir])
+				_NR_SWAP(arr[l],arr[ir]);
 			}
 			if (arr[l+1] > arr[ir]) {
-				SWAP_NBTRI(arr[l+1],arr[ir])
+				_NR_SWAP(arr[l+1],arr[ir]);
 			}
 			if (arr[l] > arr[l+1]) {
-				SWAP_NBTRI(arr[l],arr[l+1])
+				_NR_SWAP(arr[l],arr[l+1]);
 			}
 			i=l+1;
@@ -56,10 +50,10 @@
 				do j--; while (arr[j] > a);
 				if (j < i) break;
-				SWAP_NBTRI(arr[i],arr[j]);
+				_NR_SWAP(arr[i],arr[j]);
 			}
 			arr[l+1]=arr[j];
 			arr[j]=a;
 			jstack += 2;
-			if (jstack > NSTACK) throw("NSTACK too small in sort.");
+			if (jstack >= NSTACK) throw("NSTACK too small in sort.");
 			if (ir-i+1 >= j-l) {
 				istack[jstack]=ir;
@@ -73,26 +67,17 @@
 		}
 	}
-	//free_lvector(istack,1,NSTACK);
-        delete [] istack;  // MODIF
+	delete [] istack;
 }
-#undef M
-#undef NSTACK
-#undef SWAP_NBTRI
-
-//-------------------------------------------------------------
-#define SWAP_NBTRI(a,b) temp=(a);(a)=(b);(b)=temp;
-#define M 7
-#define NSTACK 50
-template <class T>
-void TabSort2(uint_4 n, T *arr_c, T *brr_c)
+
+//-------------------------------------------------------------
+template <class T, class U>
+void TabSort2(uint_4 n, T *arr, U *brr)
 {
-	uint_4 i,ir=n,j,k,l=1,*istack;
-	int jstack=0;
-	T a,b,temp;
-        T *arr = arr_c - 1; // MODIF
-        T *brr = brr_c - 1; // MODIF
-
-	//istack=lvector(1,NSTACK);
-        istack = new uint_4[NSTACK+1]; // MODIF
+	const Int M=7,NSTACK=64;
+	Int i,ir,j,k,jstack=-1,l=0;
+	T a;
+	U b;
+	Int* istack = new Int[NSTACK];
+	ir=n-1;
 	for (;;) {
 		if (ir-l < M) {
@@ -108,27 +93,22 @@
 				brr[i+1]=b;
 			}
-			if (!jstack) {
-			        //free_lvector(istack,1,NSTACK);
-			        delete [] istack;  // MODIF
-				return;
-			}
-			ir=istack[jstack];
-			l=istack[jstack-1];
-			jstack -= 2;
+			if (jstack < 0) break;
+			ir=istack[jstack--];
+			l=istack[jstack--];
 		} else {
 			k=(l+ir) >> 1;
-			SWAP_NBTRI(arr[k],arr[l+1])
-			SWAP_NBTRI(brr[k],brr[l+1])
+			_NR_SWAP(arr[k],arr[l+1]);
+			_NR_SWAP(brr[k],brr[l+1]);
 			if (arr[l] > arr[ir]) {
-				SWAP_NBTRI(arr[l],arr[ir])
-				SWAP_NBTRI(brr[l],brr[ir])
+				_NR_SWAP(arr[l],arr[ir]);
+				_NR_SWAP(brr[l],brr[ir]);
 			}
 			if (arr[l+1] > arr[ir]) {
-				SWAP_NBTRI(arr[l+1],arr[ir])
-				SWAP_NBTRI(brr[l+1],brr[ir])
+				_NR_SWAP(arr[l+1],arr[ir]);
+				_NR_SWAP(brr[l+1],brr[ir]);
 			}
 			if (arr[l] > arr[l+1]) {
-				SWAP_NBTRI(arr[l],arr[l+1])
-				SWAP_NBTRI(brr[l],brr[l+1])
+				_NR_SWAP(arr[l],arr[l+1]);
+				_NR_SWAP(brr[l],brr[l+1]);
 			}
 			i=l+1;
@@ -140,6 +120,6 @@
 				do j--; while (arr[j] > a);
 				if (j < i) break;
-				SWAP_NBTRI(arr[i],arr[j])
-				SWAP_NBTRI(brr[i],brr[j])
+				_NR_SWAP(arr[i],arr[j]);
+				_NR_SWAP(brr[i],brr[j]);
 			}
 			arr[l+1]=arr[j];
@@ -148,5 +128,5 @@
 			brr[j]=b;
 			jstack += 2;
-			if (jstack > NSTACK) throw("NSTACK too small in sort2.");
+			if (jstack >= NSTACK) throw("NSTACK too small in sort2.");
 			if (ir-i+1 >= j-l) {
 				istack[jstack]=ir;
@@ -160,25 +140,18 @@
 		}
 	}
+	delete [] istack;
 }
-#undef M
-#undef NSTACK
-#undef SWAP_NBTRI
-
-//-------------------------------------------------------------
-#define SWAP_NBTRI(a,b) itemp=(a);(a)=(b);(b)=itemp;
-#define M 7
-#define NSTACK 50
+
+
+//-------------------------------------------------------------
 template <class T>
-void TabSortInd(uint_4 n, T *arr_c, uint_4 *indx_c)
+void TabSortInd(uint_4 n, T *arr, uint_4 *indx)
 {
-	uint_4 i,indxt,ir=n,itemp,j,k,l=1;
-	int jstack=0,*istack;
+	const Int M=7,NSTACK=64;
+	Int i,indxt,ir,j,k,jstack=-1,l=0;
 	T a;
-        T *arr = arr_c - 1; // MODIF
-        uint_4 *indx = indx_c - 1; // MODIF
-
-	//istack=ivector(1,NSTACK);
-        istack = new int[NSTACK+1]; // MODIF
-	for (j=1;j<=n;j++) indx[j]=j;
+	Int* istack = new Int[NSTACK];
+	ir=n-1;
+	for (j=0;j<(Int)n;j++) indx[j]=j;
 	for (;;) {
 		if (ir-l < M) {
@@ -192,18 +165,18 @@
 				indx[i+1]=indxt;
 			}
-			if (jstack == 0) break;
+			if (jstack < 0) break;
 			ir=istack[jstack--];
 			l=istack[jstack--];
 		} else {
 			k=(l+ir) >> 1;
-			SWAP_NBTRI(indx[k],indx[l+1]);
+			_NR_SWAP(indx[k],indx[l+1]);
 			if (arr[indx[l]] > arr[indx[ir]]) {
-				SWAP_NBTRI(indx[l],indx[ir])
+				_NR_SWAP(indx[l],indx[ir]);
 			}
 			if (arr[indx[l+1]] > arr[indx[ir]]) {
-				SWAP_NBTRI(indx[l+1],indx[ir])
+				_NR_SWAP(indx[l+1],indx[ir]);
 			}
 			if (arr[indx[l]] > arr[indx[l+1]]) {
-				SWAP_NBTRI(indx[l],indx[l+1])
+				_NR_SWAP(indx[l],indx[l+1]);
 			}
 			i=l+1;
@@ -215,10 +188,10 @@
 				do j--; while (arr[indx[j]] > a);
 				if (j < i) break;
-				SWAP_NBTRI(indx[i],indx[j])
+				_NR_SWAP(indx[i],indx[j]);
 			}
 			indx[l+1]=indx[j];
 			indx[j]=indxt;
 			jstack += 2;
-			if (jstack > NSTACK) throw("NSTACK too small in indexx.");
+			if (jstack >= NSTACK) throw("NSTACK too small in index.");
 			if (ir-i+1 >= j-l) {
 				istack[jstack]=ir;
@@ -232,12 +205,7 @@
 		}
 	}
-	//free_ivector(istack,1,NSTACK);
-        delete [] istack;  // MODIF
-        /* pour avoir un retour d'indice entre 0 et n-1 */
-	for(j=0;j<n;j++) indx_c[j]--;  // MODIF
+	delete [] istack;
 }
-#undef M
-#undef NSTACK
-#undef SWAP_NBTRI
+
 
 //-------------------------------------------------------------
@@ -252,12 +220,14 @@
 #pragma define_template TabSort<r_8>
 
-#pragma define_template TabSort2<int_2>
-#pragma define_template TabSort2<uint_2>
-#pragma define_template TabSort2<int_4>
-#pragma define_template TabSort2<uint_4>
-#pragma define_template TabSort2<int_8>
-#pragma define_template TabSort2<uint_8>
-#pragma define_template TabSort2<r_4>
-#pragma define_template TabSort2<r_8>
+#pragma define_template TabSort2<int_2,int_2>
+#pragma define_template TabSort2<uint_2,uint_2>
+#pragma define_template TabSort2<int_4,int_4>
+#pragma define_template TabSort2<uint_4,uint_4>
+#pragma define_template TabSort2<int_8,int_8>
+#pragma define_template TabSort2<uint_8,uint_8>
+#pragma define_template TabSort2<r_4,r_4>
+#pragma define_template TabSort2<r_8,r_8>
+#pragma define_template TabSort2<r_4,int_4>
+#pragma define_template TabSort2<r_8,int_4>
 
 #pragma define_template TabSortInd<int_2>
@@ -289,4 +259,6 @@
 template void TabSort2(uint_4, r_4*, r_4*);
 template void TabSort2(uint_4, r_8*, r_8*);
+template void TabSort2(uint_4, r_4*, int_4*);
+template void TabSort2(uint_4, r_8*, int_4*);
 
 template void TabSortInd(uint_4, int_2*, uint_4*);
Index: /trunk/SophyaLib/NTools/nbtrixx.h
===================================================================
--- /trunk/SophyaLib/NTools/nbtrixx.h	(revision 3684)
+++ /trunk/SophyaLib/NTools/nbtrixx.h	(revision 3685)
@@ -6,12 +6,15 @@
 namespace SOPHYA {
 
-template <class T>
-void TabSort(uint_4 n, T *arr_c);
+template<class T>
+inline void _NR_SWAP(T &a, T &b) {T dum=a; a=b; b=dum;}
 
 template <class T>
-void TabSort2(uint_4 n, T *arr_c, T *brr_c);
+void TabSort(uint_4 n, T *arr);
+
+  template <class T, class U>
+void TabSort2(uint_4 n, T *arr, U *brr);
 
 template <class T>
-void TabSortInd(uint_4 n, T *arr_c, uint_4 *indx_c);
+void TabSortInd(uint_4 n, T *arr, uint_4 *indx);
 
 }
