1 /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004,
2 * 2005, 2006, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
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.
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.
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
27 #include "libguile/_scm.h"
28 #include "libguile/__scm.h"
30 #include "libguile/array-handle.h"
31 #include "libguile/generalized-arrays.h"
32 #include "libguile/generalized-vectors.h"
35 struct scm_t_vector_ctor
38 SCM (*ctor
)(SCM
, SCM
);
41 #define VECTOR_CTORS_N_STATIC_ALLOC 20
42 static struct scm_t_vector_ctor vector_ctors
[VECTOR_CTORS_N_STATIC_ALLOC
];
43 static int num_vector_ctors_registered
= 0;
46 scm_i_register_vector_constructor (SCM type
, SCM (*ctor
)(SCM
, SCM
))
48 if (num_vector_ctors_registered
>= VECTOR_CTORS_N_STATIC_ALLOC
)
49 /* need to increase VECTOR_CTORS_N_STATIC_ALLOC, buster */
53 vector_ctors
[num_vector_ctors_registered
].tag
= type
;
54 vector_ctors
[num_vector_ctors_registered
].ctor
= ctor
;
55 num_vector_ctors_registered
++;
59 SCM_DEFINE (scm_make_generalized_vector
, "make-generalized-vector", 2, 1, 0,
60 (SCM type
, SCM len
, SCM fill
),
61 "Make a generalized vector")
62 #define FUNC_NAME s_scm_make_generalized_vector
65 for (i
= 0; i
< num_vector_ctors_registered
; i
++)
66 if (scm_is_eq (vector_ctors
[i
].tag
, type
))
67 return vector_ctors
[i
].ctor(len
, fill
);
68 scm_wrong_type_arg_msg (FUNC_NAME
, SCM_ARG1
, type
, "array type");
73 scm_is_generalized_vector (SCM obj
)
76 if (scm_is_array (obj
))
79 scm_array_get_handle (obj
, &h
);
80 ret
= scm_array_handle_rank (&h
) == 1;
81 scm_array_handle_release (&h
);
86 #define SCM_VALIDATE_VECTOR_WITH_HANDLE(pos, val, handle) \
87 scm_generalized_vector_get_handle (val, handle)
91 scm_generalized_vector_get_handle (SCM vec
, scm_t_array_handle
*h
)
93 scm_array_get_handle (vec
, h
);
94 if (scm_array_handle_rank (h
) != 1)
96 scm_array_handle_release (h
);
97 scm_wrong_type_arg_msg (NULL
, 0, vec
, "vector");
102 scm_c_generalized_vector_length (SCM v
)
104 scm_t_array_handle h
;
106 scm_generalized_vector_get_handle (v
, &h
);
107 ret
= h
.dims
[0].ubnd
- h
.dims
[0].lbnd
+ 1;
108 scm_array_handle_release (&h
);
113 scm_c_generalized_vector_ref (SCM v
, size_t idx
)
115 scm_t_array_handle h
;
118 scm_generalized_vector_get_handle (v
, &h
);
119 pos
= h
.base
+ h
.dims
[0].lbnd
+ idx
* h
.dims
[0].inc
;
120 ret
= h
.impl
->vref (&h
, pos
);
121 scm_array_handle_release (&h
);
126 scm_c_generalized_vector_set_x (SCM v
, size_t idx
, SCM val
)
128 scm_t_array_handle h
;
130 scm_generalized_vector_get_handle (v
, &h
);
131 pos
= h
.base
+ h
.dims
[0].lbnd
+ idx
* h
.dims
[0].inc
;
132 h
.impl
->vset (&h
, pos
, val
);
133 scm_array_handle_release (&h
);
137 scm_init_generalized_vectors ()
139 #include "libguile/generalized-vectors.x"