generalized-vectors.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. void
  61. scm_generalized_vector_get_handle (SCM vec, scm_t_array_handle *h)
  62. {
  63. scm_array_get_handle (vec, h);
  64. if (scm_array_handle_rank (h) != 1)
  65. {
  66. scm_array_handle_release (h);
  67. scm_wrong_type_arg_msg (NULL, 0, vec, "vector");
  68. }
  69. }
  70. void
  71. scm_init_generalized_vectors ()
  72. {
  73. #include "libguile/generalized-vectors.x"
  74. }
  75. /*
  76. Local Variables:
  77. c-file-style: "gnu"
  78. End:
  79. */