This is the mail archive of the
gsl-discuss@sources.redhat.com
mailing list for the GSL project.
patch to permutation code
- To: gsl-discuss at sources dot redhat dot com
- Subject: patch to permutation code
- From: vattervi at msu dot edu
- Date: Fri, 29 Dec 2000 21:30:19 GMT
Here are diffs to permutation/gsl_permutation.h and permutation/permutation.c
to implement the new functions gsl_permutation_next and gsl_permutation_prev.
They do the same thing as the .next and .prev methods in the STL.
Vince
Index: gsl_permutation.h
===================================================================
RCS file: /cvs/gsl/gsl/permutation/gsl_permutation.h,v
retrieving revision 1.10
diff -u -r1.10 gsl_permutation.h
--- gsl_permutation.h 2000/06/05 19:27:37 1.10
+++ gsl_permutation.h 2000/12/29 21:06:03
@@ -62,6 +62,8 @@
int gsl_permutation_valid (gsl_permutation * p);
void gsl_permutation_reverse (gsl_permutation * p);
int gsl_permutation_inverse (gsl_permutation * inv, const gsl_permutation * p);
+int gsl_permutation_next (gsl_permutation * p);
+int gsl_permutation_prev (gsl_permutation * p);
extern int gsl_check_range;
Index: permutation.c
===================================================================
RCS file: /cvs/gsl/gsl/permutation/permutation.c,v
retrieving revision 1.9
diff -u -r1.9 permutation.c
--- permutation.c 2000/06/05 19:27:37 1.9
+++ permutation.c 2000/12/29 21:05:21
@@ -135,3 +135,86 @@
return GSL_SUCCESS ;
}
+
+int
+gsl_permutation_next (gsl_permutation * p)
+{
+ /* Replaces p with the next permutation (in the standard lexiographical
+ * ordering). Returns GSL_FAILURE if there is no next permutation.
+ */
+ const size_t size = p->size;
+ size_t i = size - 2;
+ size_t j, k;
+
+ while ((p->data[i] > p->data[i+1]) && (i != 0))
+ {
+ i--;
+ }
+
+ if ((i == 0) && (p->data[0] > p->data[1]))
+ {
+ return GSL_FAILURE;
+ }
+
+ k = i + 1;
+
+ for (j = i + 2; j < size; j++ )
+ {
+ if ((p->data[j] > p->data[i]) && (p->data[j] < p->data[k]))
+ {
+ k = j;
+ }
+ }
+
+ gsl_permutation_swap(p, i, k);
+
+ for (j = i + 1; j <= ((size + i) / 2); j++)
+ {
+ size_t tmp = p->data[j];
+ p->data[j] = p->data[size + i - j];
+ p->data[size + i - j] = tmp;
+ }
+
+ return GSL_SUCCESS;
+}
+
+int
+gsl_permutation_prev (gsl_permutation * p)
+{
+ const size_t size = p->size;
+ size_t i = size - 2;
+ size_t j, k;
+
+ while ((p->data[i] < p->data[i+1]) && (i != 0))
+ {
+ i--;
+ }
+
+ if ((i == 0) && (p->data[0] < p->data[1]))
+ {
+ return GSL_FAILURE;
+ }
+
+ k = i + 1;
+
+ for (j = i + 2; j < size; j++ )
+ {
+ if ((p->data[j] < p->data[i]) && (p->data[j] > p->data[k]))
+ {
+ k = j;
+ }
+ }
+
+ gsl_permutation_swap(p, i, k);
+
+ for (j = i + 1; j <= ((size + i) / 2); j++)
+ {
+ size_t tmp = p->data[j];
+ p->data[j] = p->data[size + i - j];
+ p->data[size + i - j] = tmp;
+ }
+
+ return GSL_SUCCESS;
+}
+
+
---------------------------------------------
This message was sent using Endymion MailMan.
http://www.endymion.com/products/mailman/