generalized-vectors.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004,
  2. * 2005, 2006, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include "libguile/_scm.h"
  23. #include "libguile/__scm.h"
  24. #include "libguile/array-handle.h"
  25. #include "libguile/generalized-arrays.h"
  26. #include "libguile/generalized-vectors.h"
  27. struct scm_t_vector_ctor
  28. {
  29. SCM tag;
  30. SCM (*ctor)(SCM, SCM);
  31. };
  32. #define VECTOR_CTORS_N_STATIC_ALLOC 20
  33. static struct scm_t_vector_ctor vector_ctors[VECTOR_CTORS_N_STATIC_ALLOC];
  34. static int num_vector_ctors_registered = 0;
  35. void
  36. scm_i_register_vector_constructor (SCM type, SCM (*ctor)(SCM, SCM))
  37. {
  38. if (num_vector_ctors_registered >= VECTOR_CTORS_N_STATIC_ALLOC)
  39. /* need to increase VECTOR_CTORS_N_STATIC_ALLOC, buster */
  40. abort ();
  41. else
  42. {
  43. vector_ctors[num_vector_ctors_registered].tag = type;
  44. vector_ctors[num_vector_ctors_registered].ctor = ctor;
  45. num_vector_ctors_registered++;
  46. }
  47. }
  48. SCM_DEFINE (scm_make_generalized_vector, "make-generalized-vector", 2, 1, 0,
  49. (SCM type, SCM len, SCM fill),
  50. "Make a generalized vector")
  51. #define FUNC_NAME s_scm_make_generalized_vector
  52. {
  53. int i;
  54. for (i = 0; i < num_vector_ctors_registered; i++)
  55. if (scm_is_eq (vector_ctors[i].tag, type))
  56. return vector_ctors[i].ctor(len, fill);
  57. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, type, "array type");
  58. }
  59. #undef FUNC_NAME
  60. int
  61. scm_is_generalized_vector (SCM obj)
  62. {
  63. int ret = 0;
  64. if (scm_is_array (obj))
  65. {
  66. scm_t_array_handle h;
  67. scm_array_get_handle (obj, &h);
  68. ret = scm_array_handle_rank (&h) == 1;
  69. scm_array_handle_release (&h);
  70. }
  71. return ret;
  72. }
  73. #define SCM_VALIDATE_VECTOR_WITH_HANDLE(pos, val, handle) \
  74. scm_generalized_vector_get_handle (val, handle)
  75. void
  76. scm_generalized_vector_get_handle (SCM vec, scm_t_array_handle *h)
  77. {
  78. scm_array_get_handle (vec, h);
  79. if (scm_array_handle_rank (h) != 1)
  80. {
  81. scm_array_handle_release (h);
  82. scm_wrong_type_arg_msg (NULL, 0, vec, "vector");
  83. }
  84. }
  85. size_t
  86. scm_c_generalized_vector_length (SCM v)
  87. {
  88. return scm_c_array_length (v);
  89. }
  90. SCM
  91. scm_c_generalized_vector_ref (SCM v, ssize_t idx)
  92. {
  93. return scm_c_array_ref_1 (v, idx);
  94. }
  95. void
  96. scm_c_generalized_vector_set_x (SCM v, ssize_t idx, SCM val)
  97. {
  98. scm_c_array_set_1_x (v, val, idx);
  99. }
  100. void
  101. scm_init_generalized_vectors ()
  102. {
  103. #include "libguile/generalized-vectors.x"
  104. }
  105. /*
  106. Local Variables:
  107. c-file-style: "gnu"
  108. End:
  109. */