gsl_combination__combination.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* combination/combination.c
  2. * based on permutation/permutation.c by Brian Gough
  3. *
  4. * Copyright (C) 2001 Szymon Jaroszewicz
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include "gsl__config.h"
  21. #include "gsl_errno.h"
  22. #include "gsl_combination.h"
  23. size_t
  24. gsl_combination_n (const gsl_combination * c)
  25. {
  26. return c->n ;
  27. }
  28. size_t
  29. gsl_combination_k (const gsl_combination * c)
  30. {
  31. return c->k ;
  32. }
  33. size_t *
  34. gsl_combination_data (const gsl_combination * c)
  35. {
  36. return c->data ;
  37. }
  38. #ifndef HIDE_INLINE_STATIC
  39. size_t
  40. gsl_combination_get (const gsl_combination * c, const size_t i)
  41. {
  42. if (gsl_check_range)
  43. {
  44. if (i >= c->k) /* size_t is unsigned, can't be negative */
  45. {
  46. GSL_ERROR_VAL ("index out of range", GSL_EINVAL, 0);
  47. }
  48. }
  49. return c->data[i];
  50. }
  51. #endif
  52. int
  53. gsl_combination_valid (gsl_combination * c)
  54. {
  55. const size_t n = c->n ;
  56. const size_t k = c->k ;
  57. size_t i, j ;
  58. if( k > n )
  59. {
  60. GSL_ERROR("combination has k greater than n", GSL_FAILURE) ;
  61. }
  62. for (i = 0; i < k; i++)
  63. {
  64. const size_t ci = c->data[i];
  65. if (ci >= n)
  66. {
  67. GSL_ERROR("combination index outside range", GSL_FAILURE) ;
  68. }
  69. for (j = 0; j < i; j++)
  70. {
  71. if (c->data[j] == ci)
  72. {
  73. GSL_ERROR("duplicate combination index", GSL_FAILURE) ;
  74. }
  75. if (c->data[j] > ci)
  76. {
  77. GSL_ERROR("combination indices not in increasing order",
  78. GSL_FAILURE) ;
  79. }
  80. }
  81. }
  82. return GSL_SUCCESS;
  83. }
  84. int
  85. gsl_combination_next (gsl_combination * c)
  86. {
  87. /* Replaces c with the next combination (in the standard lexicographical
  88. * ordering). Returns GSL_FAILURE if there is no next combination.
  89. */
  90. const size_t n = c->n;
  91. const size_t k = c->k;
  92. size_t *data = c->data;
  93. size_t i;
  94. if(k == 0)
  95. {
  96. return GSL_FAILURE;
  97. }
  98. i = k - 1;
  99. while(i > 0 && data[i] == n - k + i)
  100. {
  101. i--;
  102. }
  103. if(i == 0 && data[i] == n - k)
  104. {
  105. return GSL_FAILURE;
  106. }
  107. data[i]++;
  108. for(; i < k - 1; i++)
  109. {
  110. data[i + 1] = data[i] + 1;
  111. }
  112. return GSL_SUCCESS;
  113. }
  114. int
  115. gsl_combination_prev (gsl_combination * c)
  116. {
  117. /* Replaces c with the previous combination (in the standard
  118. * lexicographical ordering). Returns GSL_FAILURE if there is no
  119. * previous combination.
  120. */
  121. const size_t n = c->n;
  122. const size_t k = c->k;
  123. size_t *data = c->data;
  124. size_t i;
  125. if(k == 0)
  126. {
  127. return GSL_FAILURE;
  128. }
  129. i = k - 1;
  130. while(i > 0 && data[i] == data[i-1] + 1)
  131. {
  132. i--;
  133. }
  134. if(i == 0 && data[i] == 0)
  135. {
  136. return GSL_FAILURE;
  137. }
  138. data[i++]--;
  139. for(; i < k; i++)
  140. {
  141. data[i] = n - k + i;
  142. }
  143. return GSL_SUCCESS;
  144. }
  145. int
  146. gsl_combination_memcpy (gsl_combination * dest, const gsl_combination * src)
  147. {
  148. const size_t src_n = src->n;
  149. const size_t src_k = src->k;
  150. const size_t dest_n = dest->n;
  151. const size_t dest_k = dest->k;
  152. if (src_n != dest_n || src_k != dest_k)
  153. {
  154. GSL_ERROR ("combination lengths are not equal", GSL_EBADLEN);
  155. }
  156. {
  157. size_t j;
  158. for (j = 0; j < src_k; j++)
  159. {
  160. dest->data[j] = src->data[j];
  161. }
  162. }
  163. return GSL_SUCCESS;
  164. }