gsl_combination__init.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* combination/init.c
  2. * based on permutation/init.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 <stdlib.h>
  22. #include "gsl_errno.h"
  23. #include "gsl_combination.h"
  24. gsl_combination *
  25. gsl_combination_alloc (const size_t n, const size_t k)
  26. {
  27. gsl_combination * c;
  28. if (n == 0)
  29. {
  30. GSL_ERROR_VAL ("combination parameter n must be positive integer",
  31. GSL_EDOM, 0);
  32. }
  33. if (k > n)
  34. {
  35. GSL_ERROR_VAL ("combination length k must be an integer less than or equal to n",
  36. GSL_EDOM, 0);
  37. }
  38. c = (gsl_combination *) malloc (sizeof (gsl_combination));
  39. if (c == 0)
  40. {
  41. GSL_ERROR_VAL ("failed to allocate space for combination struct",
  42. GSL_ENOMEM, 0);
  43. }
  44. if (k > 0)
  45. {
  46. c->data = (size_t *) malloc (k * sizeof (size_t));
  47. if (c->data == 0)
  48. {
  49. free (c); /* exception in constructor, avoid memory leak */
  50. GSL_ERROR_VAL ("failed to allocate space for combination data",
  51. GSL_ENOMEM, 0);
  52. }
  53. }
  54. else
  55. {
  56. c->data = 0;
  57. }
  58. c->n = n;
  59. c->k = k;
  60. return c;
  61. }
  62. gsl_combination *
  63. gsl_combination_calloc (const size_t n, const size_t k)
  64. {
  65. size_t i;
  66. gsl_combination * c = gsl_combination_alloc (n, k);
  67. if (c == 0)
  68. return 0;
  69. /* initialize combination to identity */
  70. for (i = 0; i < k; i++)
  71. {
  72. c->data[i] = i;
  73. }
  74. return c;
  75. }
  76. void
  77. gsl_combination_init_first (gsl_combination * c)
  78. {
  79. const size_t k = c->k ;
  80. size_t i;
  81. /* initialize combination to identity */
  82. for (i = 0; i < k; i++)
  83. {
  84. c->data[i] = i;
  85. }
  86. }
  87. void
  88. gsl_combination_init_last (gsl_combination * c)
  89. {
  90. const size_t k = c->k ;
  91. size_t i;
  92. size_t n = c->n;
  93. /* initialize combination to identity */
  94. for (i = 0; i < k; i++)
  95. {
  96. c->data[i] = n - k + i;
  97. }
  98. }
  99. void
  100. gsl_combination_free (gsl_combination * c)
  101. {
  102. if (c->k > 0) free (c->data);
  103. free (c);
  104. }