gsl_sort__subsetind_source.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* sort/subsetind_source.c
  2. *
  3. * Copyright (C) 1999,2000,2001 Thomas Walter, Brian Gough
  4. *
  5. * This is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 3, or (at your option) any
  8. * later version.
  9. *
  10. * This source is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. /* find the k-th smallest elements of the vector data, in ascending order */
  16. int
  17. FUNCTION (gsl_sort, smallest_index) (size_t * p, const size_t k,
  18. const BASE * src, const size_t stride,
  19. const size_t n)
  20. {
  21. size_t i, j;
  22. BASE xbound;
  23. if (k > n)
  24. {
  25. GSL_ERROR ("subset length k exceeds vector length n", GSL_EINVAL);
  26. }
  27. if (k == 0 || n == 0)
  28. {
  29. return GSL_SUCCESS;
  30. }
  31. /* take the first element */
  32. j = 1;
  33. xbound = src[0 * stride];
  34. p[0] = 0;
  35. /* examine the remaining elements */
  36. for (i = 1; i < n; i++)
  37. {
  38. size_t i1;
  39. BASE xi = src[i * stride];
  40. if (j < k)
  41. {
  42. j++;
  43. }
  44. else if (xi >= xbound)
  45. {
  46. continue;
  47. }
  48. for (i1 = j - 1; i1 > 0 ; i1--)
  49. {
  50. if (xi > src[p[i1 - 1] * stride])
  51. break;
  52. p[i1] = p[i1 - 1];
  53. }
  54. p[i1] = i;
  55. xbound = src[p[j-1] * stride];
  56. }
  57. return GSL_SUCCESS;
  58. }
  59. int
  60. FUNCTION (gsl_sort_vector,smallest_index) (size_t * p, const size_t k,
  61. const TYPE (gsl_vector) * v)
  62. {
  63. return FUNCTION (gsl_sort, smallest_index) (p, k, v->data, v->stride, v->size);
  64. }
  65. int
  66. FUNCTION (gsl_sort, largest_index) (size_t * p, const size_t k,
  67. const BASE * src, const size_t stride,
  68. const size_t n)
  69. {
  70. size_t i, j;
  71. BASE xbound;
  72. if (k > n)
  73. {
  74. GSL_ERROR ("subset length k exceeds vector length n", GSL_EINVAL);
  75. }
  76. if (k == 0 || n == 0)
  77. {
  78. return GSL_SUCCESS;
  79. }
  80. /* take the first element */
  81. j = 1;
  82. xbound = src[0 * stride];
  83. p[0] = 0;
  84. /* examine the remaining elements */
  85. for (i = 1; i < n; i++)
  86. {
  87. size_t i1;
  88. BASE xi = src[i * stride];
  89. if (j < k)
  90. {
  91. j++;
  92. }
  93. else if (xi <= xbound)
  94. {
  95. continue;
  96. }
  97. for (i1 = j - 1; i1 > 0 ; i1--)
  98. {
  99. if (xi < src[stride * p[i1 - 1]])
  100. break;
  101. p[i1] = p[i1 - 1];
  102. }
  103. p[i1] = i;
  104. xbound = src[stride * p[j-1]];
  105. }
  106. return GSL_SUCCESS;
  107. }
  108. int
  109. FUNCTION (gsl_sort_vector,largest_index) (size_t * p, const size_t k,
  110. const TYPE (gsl_vector) * v)
  111. {
  112. return FUNCTION (gsl_sort, largest_index) (p, k, v->data, v->stride, v->size);
  113. }