2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
#include "picrin/vector.h"
|
2014-09-12 07:51:36 -04:00
|
|
|
#include "picrin/string.h"
|
2014-08-25 00:38:09 -04:00
|
|
|
#include "picrin/pair.h"
|
|
|
|
|
|
|
|
struct pic_vector *
|
2014-09-12 06:55:32 -04:00
|
|
|
pic_make_vec(pic_state *pic, size_t len)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_vector *vec;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
vec = (struct pic_vector *)pic_obj_alloc(pic, sizeof(struct pic_vector), PIC_TT_VECTOR);
|
|
|
|
vec->len = len;
|
|
|
|
vec->data = (pic_value *)pic_alloc(pic, sizeof(pic_value) * len);
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
vec->data[i] = pic_none_value();
|
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pic_vector *
|
2014-09-12 06:55:32 -04:00
|
|
|
pic_make_vec_from_list(pic_state *pic, pic_value data)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_vector *vec;
|
2014-09-26 03:47:23 -04:00
|
|
|
int len, i;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
len = pic_length(pic, data);
|
|
|
|
|
2014-09-26 03:47:23 -04:00
|
|
|
vec = pic_make_vec(pic, (size_t)len);
|
2014-08-25 00:38:09 -04:00
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
vec->data[i] = pic_car(pic, data);
|
|
|
|
data = pic_cdr(pic, data);
|
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
return pic_bool_value(pic_vec_p(v));
|
|
|
|
}
|
|
|
|
|
2014-09-12 07:55:34 -04:00
|
|
|
static pic_value
|
|
|
|
pic_vec_vector(pic_state *pic)
|
|
|
|
{
|
2014-09-26 04:04:20 -04:00
|
|
|
int argc, i;
|
2014-09-12 07:55:34 -04:00
|
|
|
pic_value *argv;
|
|
|
|
pic_vec *vec;
|
|
|
|
|
|
|
|
pic_get_args(pic, "*", &argc, &argv);
|
|
|
|
|
2014-09-26 04:13:17 -04:00
|
|
|
vec = pic_make_vec(pic, (size_t)argc);
|
2014-09-12 07:55:34 -04:00
|
|
|
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
vec->data[i] = argv[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_obj_value(vec);
|
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
static pic_value
|
|
|
|
pic_vec_make_vector(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
int n, k;
|
|
|
|
size_t i;
|
|
|
|
struct pic_vector *vec;
|
|
|
|
|
|
|
|
n = pic_get_args(pic, "i|o", &k, &v);
|
|
|
|
|
2014-09-26 03:47:23 -04:00
|
|
|
if (k < 0) {
|
|
|
|
pic_errorf(pic, "make-vector: vector length must be non-negative");
|
|
|
|
}
|
|
|
|
|
|
|
|
vec = pic_make_vec(pic, (size_t)k);
|
2014-08-25 00:38:09 -04:00
|
|
|
if (n == 2) {
|
|
|
|
for (i = 0; i < (size_t)k; ++i) {
|
|
|
|
vec->data[i] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pic_obj_value(vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_length(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_vector *v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "v", &v);
|
|
|
|
|
2014-09-26 03:47:23 -04:00
|
|
|
return pic_int_value((int)v->len);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_ref(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_vector *v;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
pic_get_args(pic, "vi", &v, &k);
|
|
|
|
|
|
|
|
if (k < 0 || v->len <= (size_t)k) {
|
2014-09-16 10:43:15 -04:00
|
|
|
pic_errorf(pic, "vector-ref: index out of range");
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
return v->data[k];
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_set(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_vector *v;
|
|
|
|
int k;
|
|
|
|
pic_value o;
|
|
|
|
|
|
|
|
pic_get_args(pic, "vio", &v, &k, &o);
|
|
|
|
|
|
|
|
if (k < 0 || v->len <= (size_t)k) {
|
2014-09-16 10:43:15 -04:00
|
|
|
pic_errorf(pic, "vector-set!: index out of range");
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
v->data[k] = o;
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_copy_i(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_vec *to, *from;
|
|
|
|
int n, at, start, end;
|
|
|
|
|
|
|
|
n = pic_get_args(pic, "viv|ii", &to, &at, &from, &start, &end);
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 3:
|
|
|
|
start = 0;
|
|
|
|
case 4:
|
2014-09-26 03:47:23 -04:00
|
|
|
end = (int)from->len;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (to == from && (start <= at && at < end)) {
|
|
|
|
/* copy in reversed order */
|
|
|
|
at += end - start;
|
|
|
|
while (start < end) {
|
|
|
|
to->data[--at] = from->data[--end];
|
|
|
|
}
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (start < end) {
|
|
|
|
to->data[at++] = from->data[start++];
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_copy(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_vec *vec, *to;
|
|
|
|
int n, start, end, i = 0;
|
|
|
|
|
|
|
|
n = pic_get_args(pic, "v|ii", &vec, &start, &end);
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 1:
|
|
|
|
start = 0;
|
|
|
|
case 2:
|
2014-09-26 03:47:23 -04:00
|
|
|
end = (int)vec->len;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-26 03:47:23 -04:00
|
|
|
if (end - start < 0) {
|
|
|
|
pic_errorf(pic, "vector-copy: end index must not be less than start index");
|
|
|
|
}
|
|
|
|
|
|
|
|
to = pic_make_vec(pic, (size_t)(end - start));
|
2014-08-25 00:38:09 -04:00
|
|
|
while (start < end) {
|
|
|
|
to->data[i++] = vec->data[start++];
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_obj_value(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_append(pic_state *pic)
|
|
|
|
{
|
2014-09-26 04:04:20 -04:00
|
|
|
int argc, i;
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_value *argv;
|
2014-09-26 04:04:20 -04:00
|
|
|
size_t j, len;
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_vec *vec;
|
|
|
|
|
|
|
|
pic_get_args(pic, "*", &argc, &argv);
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
pic_assert_type(pic, argv[i], vec);
|
|
|
|
len += pic_vec_ptr(argv[i])->len;
|
|
|
|
}
|
|
|
|
|
2014-09-12 06:55:32 -04:00
|
|
|
vec = pic_make_vec(pic, len);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
for (j = 0; j < pic_vec_ptr(argv[i])->len; ++j) {
|
|
|
|
vec->data[len + j] = pic_vec_ptr(argv[i])->data[j];
|
|
|
|
}
|
|
|
|
len += pic_vec_ptr(argv[i])->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_obj_value(vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_fill_i(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_vec *vec;
|
|
|
|
pic_value obj;
|
|
|
|
int n, start, end;
|
|
|
|
|
|
|
|
n = pic_get_args(pic, "vo|ii", &vec, &obj, &start, &end);
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 2:
|
|
|
|
start = 0;
|
|
|
|
case 3:
|
2014-09-26 03:47:23 -04:00
|
|
|
end = (int)vec->len;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (start < end) {
|
|
|
|
vec->data[start++] = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
2014-09-19 05:16:28 -04:00
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_map(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *proc;
|
2014-09-26 04:36:00 -04:00
|
|
|
int argc;
|
|
|
|
size_t i, len, j;
|
2014-09-19 05:16:28 -04:00
|
|
|
pic_value *argv, vals;
|
|
|
|
pic_vec *vec;
|
|
|
|
|
|
|
|
pic_get_args(pic, "l*", &proc, &argc, &argv);
|
|
|
|
|
2014-09-26 04:13:17 -04:00
|
|
|
len = INT_MAX;
|
2014-09-26 04:36:00 -04:00
|
|
|
for (i = 0; i < (size_t)argc; ++i) {
|
2014-09-19 05:16:28 -04:00
|
|
|
pic_assert_type(pic, argv[i], vec);
|
|
|
|
|
|
|
|
len = len < pic_vec_ptr(argv[i])->len
|
|
|
|
? len
|
|
|
|
: pic_vec_ptr(argv[i])->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec = pic_make_vec(pic, len);
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
vals = pic_nil_value();
|
2014-09-26 04:36:00 -04:00
|
|
|
for (j = 0; j < (size_t)argc; ++j) {
|
2014-09-19 05:16:28 -04:00
|
|
|
pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals);
|
|
|
|
}
|
|
|
|
vec->data[i] = pic_apply(pic, proc, vals);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_obj_value(vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_for_each(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *proc;
|
2014-09-26 04:36:00 -04:00
|
|
|
int argc;
|
|
|
|
size_t i, len, j;
|
2014-09-19 05:16:28 -04:00
|
|
|
pic_value *argv, vals;
|
|
|
|
|
|
|
|
pic_get_args(pic, "l*", &proc, &argc, &argv);
|
|
|
|
|
2014-09-26 04:13:17 -04:00
|
|
|
len = INT_MAX;
|
2014-09-26 04:36:00 -04:00
|
|
|
for (i = 0; i < (size_t)argc; ++i) {
|
2014-09-19 05:16:28 -04:00
|
|
|
pic_assert_type(pic, argv[i], vec);
|
|
|
|
|
|
|
|
len = len < pic_vec_ptr(argv[i])->len
|
|
|
|
? len
|
|
|
|
: pic_vec_ptr(argv[i])->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
vals = pic_nil_value();
|
2014-09-26 04:36:00 -04:00
|
|
|
for (j = 0; j < (size_t)argc; ++j) {
|
2014-09-19 05:16:28 -04:00
|
|
|
pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals);
|
|
|
|
}
|
|
|
|
pic_apply(pic, proc, vals);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
static pic_value
|
|
|
|
pic_vec_list_to_vector(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_vector *vec;
|
|
|
|
pic_value list, e, *data;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &list);
|
|
|
|
|
2014-09-26 03:47:23 -04:00
|
|
|
vec = pic_make_vec(pic, (size_t)pic_length(pic, list));
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
data = vec->data;
|
|
|
|
|
|
|
|
pic_for_each (e, list) {
|
|
|
|
*data++ = e;
|
|
|
|
}
|
|
|
|
return pic_obj_value(vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_to_list(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_vector *vec;
|
|
|
|
pic_value list;
|
|
|
|
int n, start, end, i;
|
|
|
|
|
|
|
|
n = pic_get_args(pic, "v|ii", &vec, &start, &end);
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 1:
|
|
|
|
start = 0;
|
|
|
|
case 2:
|
2014-09-26 03:47:23 -04:00
|
|
|
end = (int)vec->len;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
list = pic_nil_value();
|
|
|
|
|
|
|
|
for (i = start; i < end; ++i) {
|
|
|
|
pic_push(pic, vec->data[i], list);
|
|
|
|
}
|
|
|
|
return pic_reverse(pic, list);
|
|
|
|
}
|
|
|
|
|
2014-09-12 07:51:36 -04:00
|
|
|
static pic_value
|
|
|
|
pic_vec_vector_to_string(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_vec *vec;
|
|
|
|
char *buf;
|
|
|
|
int n, start, end, i;
|
|
|
|
pic_str *str;
|
|
|
|
|
|
|
|
n = pic_get_args(pic, "v|ii", &vec, &start, &end);
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 1:
|
|
|
|
start = 0;
|
|
|
|
case 2:
|
2014-09-26 03:47:23 -04:00
|
|
|
end = (int)vec->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end - start < 0) {
|
|
|
|
pic_errorf(pic, "vector->string: end index must not be less than start index");
|
2014-09-12 07:51:36 -04:00
|
|
|
}
|
|
|
|
|
2014-09-26 03:47:23 -04:00
|
|
|
buf = pic_alloc(pic, (size_t)(end - start));
|
2014-09-12 07:51:36 -04:00
|
|
|
|
|
|
|
for (i = start; i < end; ++i) {
|
|
|
|
pic_assert_type(pic, vec->data[i], char);
|
|
|
|
|
|
|
|
buf[i - start] = pic_char(vec->data[i]);
|
|
|
|
}
|
|
|
|
|
2014-09-26 03:47:23 -04:00
|
|
|
str = pic_make_str(pic, buf, (size_t)(end - start));
|
2014-09-12 07:51:36 -04:00
|
|
|
pic_free(pic, buf);
|
|
|
|
|
|
|
|
return pic_obj_value(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_vec_string_to_vector(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_str *str;
|
2014-09-26 03:47:23 -04:00
|
|
|
int n, start, end;
|
|
|
|
size_t i;
|
2014-09-12 07:51:36 -04:00
|
|
|
pic_vec *vec;
|
|
|
|
|
|
|
|
n = pic_get_args(pic, "s|ii", &str, &start, &end);
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 1:
|
|
|
|
start = 0;
|
|
|
|
case 2:
|
2014-09-26 03:47:23 -04:00
|
|
|
end = (int)pic_strlen(str);
|
2014-09-12 07:51:36 -04:00
|
|
|
}
|
|
|
|
|
2014-09-26 03:47:23 -04:00
|
|
|
if (start < 0) {
|
|
|
|
pic_errorf(pic, "string->vector: index must non-negative");
|
|
|
|
}
|
|
|
|
if (end - start < 0) {
|
|
|
|
pic_errorf(pic, "string->vector: end index must not be less than start index");
|
|
|
|
}
|
2014-09-12 07:51:36 -04:00
|
|
|
|
2014-09-26 03:47:23 -04:00
|
|
|
vec = pic_make_vec(pic, (size_t)(end - start));
|
|
|
|
|
|
|
|
for (i = 0; i < (size_t)(end - start); ++i) {
|
|
|
|
vec->data[i] = pic_char_value(pic_str_ref(pic, str, i + (size_t)start));
|
2014-09-12 07:51:36 -04:00
|
|
|
}
|
|
|
|
return pic_obj_value(vec);
|
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
void
|
|
|
|
pic_init_vector(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_defun(pic, "vector?", pic_vec_vector_p);
|
2014-09-12 07:55:34 -04:00
|
|
|
pic_defun(pic, "vector", pic_vec_vector);
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_defun(pic, "make-vector", pic_vec_make_vector);
|
|
|
|
pic_defun(pic, "vector-length", pic_vec_vector_length);
|
|
|
|
pic_defun(pic, "vector-ref", pic_vec_vector_ref);
|
|
|
|
pic_defun(pic, "vector-set!", pic_vec_vector_set);
|
|
|
|
pic_defun(pic, "vector-copy!", pic_vec_vector_copy_i);
|
|
|
|
pic_defun(pic, "vector-copy", pic_vec_vector_copy);
|
|
|
|
pic_defun(pic, "vector-append", pic_vec_vector_append);
|
|
|
|
pic_defun(pic, "vector-fill!", pic_vec_vector_fill_i);
|
2014-09-19 05:16:28 -04:00
|
|
|
pic_defun(pic, "vector-map", pic_vec_vector_map);
|
|
|
|
pic_defun(pic, "vector-for-each", pic_vec_vector_for_each);
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_defun(pic, "list->vector", pic_vec_list_to_vector);
|
|
|
|
pic_defun(pic, "vector->list", pic_vec_vector_to_list);
|
2014-09-12 07:51:36 -04:00
|
|
|
pic_defun(pic, "string->vector", pic_vec_string_to_vector);
|
|
|
|
pic_defun(pic, "vector->string", pic_vec_vector_to_string);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|