picrin/extlib/benz/vector.c

430 lines
8.1 KiB
C
Raw Normal View History

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-27 05:10:14 -04:00
size_t len, i;
2014-08-25 00:38:09 -04:00
len = pic_length(pic, data);
2014-09-27 05:10:14 -04:00
vec = pic_make_vec(pic, 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-27 07:17:02 -04:00
size_t argc, i;
2014-09-12 07:55:34 -04:00
pic_value *argv;
pic_vec *vec;
pic_get_args(pic, "*", &argc, &argv);
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;
2014-09-27 05:10:14 -04:00
int n;
size_t k, i;
2014-08-25 00:38:09 -04:00
struct pic_vector *vec;
2014-09-27 05:10:14 -04:00
n = pic_get_args(pic, "k|o", &k, &v);
2014-08-25 00:38:09 -04:00
2014-09-27 05:10:14 -04:00
vec = pic_make_vec(pic, k);
2014-08-25 00:38:09 -04:00
if (n == 2) {
2014-09-27 05:10:14 -04:00
for (i = 0; i < k; ++i) {
2014-08-25 00:38:09 -04:00
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-27 06:40:10 -04:00
return pic_size_value(v->len);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_vec_vector_ref(pic_state *pic)
{
struct pic_vector *v;
2014-09-27 05:10:14 -04:00
size_t k;
2014-08-25 00:38:09 -04:00
2014-09-27 05:10:14 -04:00
pic_get_args(pic, "vk", &v, &k);
2014-08-25 00:38:09 -04:00
2014-09-27 05:10:14 -04:00
if (v->len <= 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;
2014-09-27 05:10:14 -04:00
size_t k;
2014-08-25 00:38:09 -04:00
pic_value o;
2014-09-27 05:10:14 -04:00
pic_get_args(pic, "vko", &v, &k, &o);
2014-08-25 00:38:09 -04:00
2014-09-27 05:10:14 -04:00
if (v->len <= 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;
2014-09-27 05:10:14 -04:00
int n;
size_t at, start, end;
2014-08-25 00:38:09 -04:00
2014-09-27 05:10:14 -04:00
n = pic_get_args(pic, "vkv|kk", &to, &at, &from, &start, &end);
2014-08-25 00:38:09 -04:00
switch (n) {
case 3:
start = 0;
case 4:
2014-09-27 05:10:14 -04:00
end = 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;
2014-09-27 05:10:14 -04:00
int n;
size_t start, end, i = 0;
2014-08-25 00:38:09 -04:00
2014-09-27 05:10:14 -04:00
n = pic_get_args(pic, "v|kk", &vec, &start, &end);
2014-08-25 00:38:09 -04:00
switch (n) {
case 1:
start = 0;
case 2:
2014-09-27 05:10:14 -04:00
end = vec->len;
2014-08-25 00:38:09 -04:00
}
2014-09-27 05:10:14 -04:00
if (end < start) {
pic_errorf(pic, "vector-copy: end index must not be less than start index");
}
2014-09-27 05:10:14 -04:00
to = pic_make_vec(pic, 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)
{
pic_value *argv;
2014-09-27 07:17:02 -04:00
size_t argc, i, 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;
2014-09-27 05:10:14 -04:00
int n;
size_t start, end;
2014-08-25 00:38:09 -04:00
2014-09-27 05:10:14 -04:00
n = pic_get_args(pic, "vo|kk", &vec, &obj, &start, &end);
2014-08-25 00:38:09 -04:00
switch (n) {
case 2:
start = 0;
case 3:
2014-09-27 05:10:14 -04:00
end = vec->len;
2014-08-25 00:38:09 -04:00
}
while (start < end) {
vec->data[start++] = obj;
}
return pic_none_value();
}
static pic_value
pic_vec_vector_map(pic_state *pic)
{
struct pic_proc *proc;
2014-09-27 07:17:02 -04:00
size_t argc, i, len, j;
pic_value *argv, vals;
pic_vec *vec;
pic_get_args(pic, "l*", &proc, &argc, &argv);
len = INT_MAX;
2014-09-27 07:17:02 -04:00
for (i = 0; i < argc; ++i) {
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-27 07:17:02 -04:00
for (j = 0; j < argc; ++j) {
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-27 07:17:02 -04:00
size_t argc, i, len, j;
pic_value *argv, vals;
pic_get_args(pic, "l*", &proc, &argc, &argv);
len = INT_MAX;
2014-09-27 07:17:02 -04:00
for (i = 0; i < argc; ++i) {
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-27 07:17:02 -04:00
for (j = 0; j < argc; ++j) {
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;
2015-01-22 05:28:31 -05:00
pic_value list, e, it, *data;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "o", &list);
2014-09-27 05:10:14 -04:00
vec = pic_make_vec(pic, pic_length(pic, list));
2014-08-25 00:38:09 -04:00
data = vec->data;
2015-01-22 05:28:31 -05:00
pic_for_each (e, list, it) {
2014-08-25 00:38:09 -04:00
*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;
2014-09-27 05:10:14 -04:00
int n;
size_t start, end, i;
2014-08-25 00:38:09 -04:00
2014-09-27 05:10:14 -04:00
n = pic_get_args(pic, "v|kk", &vec, &start, &end);
2014-08-25 00:38:09 -04:00
switch (n) {
case 1:
start = 0;
case 2:
2014-09-27 05:10:14 -04:00
end = 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;
2014-09-27 05:10:14 -04:00
int n;
size_t start, end, i;
2014-09-12 07:51:36 -04:00
pic_str *str;
2014-09-27 05:10:14 -04:00
n = pic_get_args(pic, "v|kk", &vec, &start, &end);
2014-09-12 07:51:36 -04:00
switch (n) {
case 1:
start = 0;
case 2:
2014-09-27 05:10:14 -04:00
end = vec->len;
}
2014-09-27 05:10:14 -04:00
if (end < start) {
pic_errorf(pic, "vector->string: end index must not be less than start index");
2014-09-12 07:51:36 -04:00
}
2014-09-27 05:10:14 -04:00
buf = pic_alloc(pic, 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-27 05:10:14 -04:00
str = pic_make_str(pic, buf, 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-27 05:10:14 -04:00
int n;
size_t start, end;
size_t i;
2014-09-12 07:51:36 -04:00
pic_vec *vec;
2014-09-27 05:10:14 -04:00
n = pic_get_args(pic, "s|kk", &str, &start, &end);
2014-09-12 07:51:36 -04:00
switch (n) {
case 1:
start = 0;
case 2:
end = pic_str_len(str);
2014-09-12 07:51:36 -04:00
}
2014-09-27 05:10:14 -04:00
if (end < start) {
pic_errorf(pic, "string->vector: end index must not be less than start index");
}
2014-09-12 07:51:36 -04:00
2014-09-27 05:10:14 -04:00
vec = pic_make_vec(pic, end - start);
2014-09-27 05:10:14 -04:00
for (i = 0; i < end - start; ++i) {
vec->data[i] = pic_char_value(pic_str_ref(pic, str, i + 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);
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
}