public inbox for gsl-discuss@sourceware.org
 help / color / mirror / Atom feed
* patch to permutation code
@ 2000-12-29 13:30 vattervi
  2000-12-30 13:57 ` Brian Gough
  0 siblings, 1 reply; 2+ messages in thread
From: vattervi @ 2000-12-29 13:30 UTC (permalink / raw)
  To: gsl-discuss

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/


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: patch to permutation code
  2000-12-29 13:30 patch to permutation code vattervi
@ 2000-12-30 13:57 ` Brian Gough
  0 siblings, 0 replies; 2+ messages in thread
From: Brian Gough @ 2000-12-30 13:57 UTC (permalink / raw)
  To: vattervi; +Cc: gsl-discuss

Thank you. That is a very nice patch. I have added it.

best regards
Brian Gough


vattervi@msu.edu writes:
 > 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
 >
 >

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-12-30 13:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-29 13:30 patch to permutation code vattervi
2000-12-30 13:57 ` Brian Gough

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).