template < class T > T	trapzd( T (* func) (T), T a, T b, int n )
{
/*
		Funkcija raèuna n-ti korak proširenog trapezoidalnog pravila
		
		n - broj toèaka koji se koristi kod inteprolacije unutar intervala
*/
	T					x=0, tnm, sum, del;
	static T	s;
	int				it, j;

	if( n == 1 )
		return (s=0.5*(b-a)*(FUNC_INT(a)+FUNC_INT(b)));
	else
	{
		for( it=1,j=1; j<n-1; j++ )
			it <<=1;

		tnm = it;
		del = (b-a)/tnm;
		x = a+0.5*del;
		
		for( sum=0.0, j=1; j<=it; j++, x+=del )
			sum +=FUNC_INT(x);

		s=0.5 * (s+(b-a)*sum/tnm);
		return s;
	}
}
template < class T > T midpnt( T (*func)(T), T a, T b, int n )
{
	T		x, tnm, sum, del, ddel;
	static T	s;
	int		it, j;

	if( n == 1 )
		return ( s=(b-a) * FUNC_INT(0.5*(a+b)));
	else
	{
		for( it=1, j=1; j<n-1; j++ )
			it += 3;

		tnm = it;
		del = (b-a) / (3.0 * tnm);
		ddel = del + del;
		x = a + 0.5*del;
		sum = 0.0;
		for( j=1; j<=it; j++ )
		{
			sum += FUNC_INT(x);
			x += ddel;
			sum += FUNC_INT(x);
			x += del;
		}
		s = ( s+ (b-a) * sum / tnm ) / 3.0;
		
		return s;
	}
}
Beispiel #3
0
static int ipaddr_const_any(Value *vret, Value *v, RefNode *node)
{
    RefSockAddr *rsa = NULL;

    switch (FUNC_INT(node)) {
    case AF_INET:
        rsa = fs->buf_new(cls_ipaddr, sizeof(RefSockAddr) + sizeof(struct sockaddr_in));
        rsa->len = sizeof(struct sockaddr_in);
        {
            struct sockaddr_in *addr_in = (struct sockaddr_in*)rsa->addr;
            addr_in->sin_family = AF_INET;
        }
        break;
    case AF_INET6:
        rsa = fs->buf_new(cls_ipaddr, sizeof(RefSockAddr) + sizeof(struct sockaddr_in6));
        rsa->len = sizeof(struct sockaddr_in6);
        {
            struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6*)rsa->addr;
            addr_in6->sin6_family = AF_INET6;
        }
        break;
    default:
        break;
    }

    if (rsa != NULL) {
        rsa->mask_bits = 0;
        *vret = vp_Value(rsa);
    }
    return TRUE;
}
Beispiel #4
0
static int textio_gets(Value *vret, Value *v, RefNode *node)
{
    Ref *ref = Value_ref(*v);
    int trim = FUNC_INT(node);

    if (!textio_gets_sub(vret, ref, trim)) {
        return FALSE;
    }
    return TRUE;
}
Beispiel #5
0
/**
 * 検索して見つかった場合/見つからなかった場合は
 * 1.true/falseを返す
 * 2.index/nullを返す
 */
int map_index_of(Value *vret, Value *v, RefNode *node)
{
    RefMap *rm = Value_vp(*v);
    int ret_index = FUNC_INT(node);
    Value v1 = v[1];
    RefNode *type = Value_type(v1);
    int i;

    RefNode *fn_eq = Hash_get_p(&type->u.c.h, fs->symbol_stock[T_EQ]);
    if (fn_eq == NULL) {
        throw_error_select(THROW_NO_MEMBER_EXISTS__NODE_REFSTR, type, fs->symbol_stock[T_EQ]);
        return FALSE;
    }

    rm->lock_count++;
    for (i = 0; i < rm->entry_num; i++) {
        HashValueEntry *ep = rm->entry[i];
        for (; ep != NULL; ep = ep->next) {
            Value va = ep->val;

            if (Value_type(va) == type) {
                if (type == fs->cls_str) {
                    if (refstr_eq(Value_vp(v1), Value_vp(va))) {
                        break;
                    }
                } else {
                    Value_push("vv", v1, va);
                    if (!call_function(fn_eq, 1)) {
                        goto ERROR_END;
                    }
                    fg->stk_top--;
                    if (Value_bool(*fg->stk_top)) {
                        unref(*fg->stk_top);
                        if (ret_index) {
                            *vret = Value_cp(ep->key);
                        } else {
                            *vret = VALUE_TRUE;
                        }
                        return TRUE;
                    }
                }
            }
        }
    }
    if (!ret_index) {
        *vret = VALUE_FALSE;
    }

    rm->lock_count--;
    return TRUE;
ERROR_END:
    rm->lock_count--;
    return FALSE;
}
Beispiel #6
0
static int stdout_gets(Value *vret, Value *v, RefNode *node)
{
    int trim = FUNC_INT(node);

    if (fg->v_ctextio == VALUE_NULL) {
        v_ctextio_init();
    }
    if (!textio_gets_sub(vret, Value_vp(fg->v_ctextio), trim)) {
        return FALSE;
    }
    return TRUE;
}
Beispiel #7
0
static int matrix_size(Value *vret, Value *v, RefNode *node)
{
    const RefMatrix *mat = Value_vp(*v);
    int ret;
    if (FUNC_INT(node)) {
        ret = mat->cols;
    } else {
        ret = mat->rows;
    }
    *vret = int32_Value(ret);
    return TRUE;
}
Beispiel #8
0
int map_iterator(Value *vret, Value *v, RefNode *node)
{
    RefMap *rm = Value_vp(*v);
    int type = FUNC_INT(node);
    Ref *r = ref_new(cls_mapiter);

    rm->lock_count++;
    r->v[INDEX_MAPITER_VAL] = Value_cp(*v);
    r->v[INDEX_MAPITER_TYPE] = int32_Value(type);
    r->v[INDEX_MAPITER_PTR] = ptr_Value(NULL);
    r->v[INDEX_MAPITER_IDX] = int32_Value(0);
    *vret = vp_Value(r);

    return TRUE;
}
Beispiel #9
0
/**
 * printf("format", ...)
 * printf(locale, "format", ...)
 */
static int textio_printf(Value *vret, Value *v, RefNode *node)
{
    int str = FUNC_INT(node);
    RefNode *v1_type = Value_type(v[1]);
    RefStr *s_fmt;
    Ref *r_loc = NULL;
    int start_arg;

    if (v1_type == fs->cls_str) {
        s_fmt = Value_vp(v[1]);
        start_arg = 2;
    } else if (v1_type == fs->cls_locale) {
        if (fg->stk_top > v + 2) {
            RefNode *v2_type = Value_type(v[2]);
            if (v2_type != fs->cls_str) {
                throw_error_select(THROW_ARGMENT_TYPE__NODE_NODE_INT, fs->cls_str, v2_type, 2);
                return FALSE;
            }
        } else {
            throw_errorf(fs->mod_lang, "ArgumentError", "2 or more arguments excepted (1 given)");
            return FALSE;
        }
        r_loc = Value_ref(v[1]);
        s_fmt = Value_vp(v[2]);
        start_arg = 3;
    } else {
        throw_error_select(THROW_ARGMENT_TYPE2__NODE_NODE_NODE_INT, fs->cls_str, fs->cls_locale, v1_type, 1);
        return FALSE;
    }

    if (str) {
        // sprintf
        StrBuf buf;
        StrBuf_init(&buf, 0);
        if (!textio_printf_sub(VALUE_NULL, &buf, s_fmt, start_arg, r_loc)) {
            return FALSE;
        }
        *vret = cstr_Value(fs->cls_str, buf.p, buf.size);
        StrBuf_close(&buf);
    } else {
        if (!textio_printf_sub(*v, NULL, s_fmt, start_arg, r_loc)) {
            return FALSE;
        }
    }

    return TRUE;
}
Beispiel #10
0
static int conn_query(Value *vret, Value *v, RefNode *node)
{
    Ref *r = Value_ref(*v);
    sqlite3 *conn = Value_ptr(r->v[INDEX_SQLITE_CONN]);

    sqlite3_stmt *stmt = NULL;
    if (!conn_prepare_sub(&stmt, conn, v)) {
        return FALSE;
    }

    {
        RefCursor *rc = fs->buf_new(cls_cursor, sizeof(RefCursor));
        *vret = vp_Value(rc);
        rc->connect = fs->Value_cp(*v);
        rc->stmt = stmt;
        rc->is_map = FUNC_INT(node);
    }

    return TRUE;
}
Beispiel #11
0
/**
 * 各要素をx倍する
 */
static int vector_muldiv(Value *vret, Value *v, RefNode *node)
{
    const RefVector *v1 = Value_vp(v[0]);
    double d = fs->Value_float(v[1]);
    int i;
    RefVector *vec;

    if (FUNC_INT(node)) {
        d = 1.0 / d;
    }

    vec = fs->buf_new(cls_vector, sizeof(RefVector) + sizeof(double) * v1->size);
    *vret = vp_Value(vec);
    vec->size = v1->size;

    for (i = 0; i < v1->size; i++) {
        vec->d[i] = v1->d[i] * d;
    }

    return TRUE;
}
Beispiel #12
0
static int vector_to_matrix(Value *vret, Value *v, RefNode *node)
{
    int i;
    const RefVector *vec = Value_vp(*v);
    RefMatrix *mat = fs->buf_new(cls_matrix, sizeof(RefMatrix) + sizeof(double) * vec->size);
    *vret = vp_Value(mat);

    if (FUNC_INT(node)) {
        // 縦
        mat->cols = 1;
        mat->rows = vec->size;
    } else {
        // 横
        mat->cols = vec->size;
        mat->rows = 1;
    }
    for (i = 0; i < vec->size; i++) {
        mat->d[i] = vec->d[i];
    }

    return TRUE;
}
Beispiel #13
0
static int stdout_print(Value *vret, Value *v, RefNode *node)
{
    int new_line = FUNC_INT(node);
    Value *varg = v + 1;

    if (fg->v_ctextio == VALUE_NULL) {
        v_ctextio_init();
    }
    *v = Value_cp(fg->v_ctextio);

    while (varg < fg->stk_top) {
        if (!textio_print_sub(*v, NULL, *varg, NULL)) {
            return FALSE;
        }
        varg++;
    }
    if (new_line && !stream_write_data(fg->v_cio, "\n", 1)) {
        return FALSE;
    }

    return TRUE;
}
Beispiel #14
0
static int matrix_to_vector(Value *vret, Value *v, RefNode *node)
{
    const RefMatrix *mat = Value_vp(*v);
    RefVector *vec;
    int idx = fs->Value_int64(v[1], NULL);
    int i;

    if (FUNC_INT(node)) {
        if (idx < 0) {
            idx += mat->cols;
        }
        if (idx < 0 || idx >= mat->cols) {
            fs->throw_error_select(THROW_INVALID_INDEX__VAL_INT, &v[1], mat->cols);
            return FALSE;
        }
        vec = fs->buf_new(cls_vector, sizeof(RefVector) + sizeof(double) * mat->rows);
        *vret = vp_Value(vec);
        vec->size = mat->rows;
        for (i = 0; i < mat->rows; i++) {
            vec->d[i] = mat->d[i * mat->cols + idx];
        }
    } else {
        if (idx < 0) {
            idx += mat->rows;
        }
        if (idx < 0 || idx >= mat->rows) {
            fs->throw_error_select(THROW_INVALID_INDEX__VAL_INT, &v[1], mat->rows);
            return FALSE;
        }
        vec = fs->buf_new(cls_vector, sizeof(RefVector) + sizeof(double) * mat->cols);
        *vret = vp_Value(vec);
        vec->size = mat->cols;
        for (i = 0; i < mat->cols; i++) {
            vec->d[i] = mat->d[idx * mat->cols + i];
        }
    }
    return TRUE;
}
Beispiel #15
0
/**
 * 0 : 要素数
 * 4 : 要素(key, value, key, value, ...)
 */
static int map_marshal_write(Value *vret, Value *v, RefNode *node)
{
    Value dumper = v[1];
    Value w = Value_ref(dumper)->v[INDEX_MARSHALDUMPER_SRC];

    RefMap *rm = Value_vp(*v);
    int is_map = FUNC_INT(node);
    int i;

    rm->lock_count++;
    if (!stream_write_uint32(w, rm->count)) {
        goto ERROR_END;
    }
    for (i = 0; i < rm->entry_num; i++) {
        HashValueEntry *ep = rm->entry[i];
        for (; ep != NULL; ep = ep->next) {
            Value_push("vv", dumper, ep->key);
            if (!call_member_func(fs->str_write, 1, TRUE)) {
                goto ERROR_END;
            }
            Value_pop();

            if (is_map) {
                Value_push("vv", dumper, ep->val);
                if (!call_member_func(fs->str_write, 1, TRUE)) {
                    goto ERROR_END;
                }
                Value_pop();
            }
        }
    }
    rm->lock_count--;
    return TRUE;

ERROR_END:
    rm->lock_count--;
    return FALSE;
}
Beispiel #16
0
static int vector_addsub(Value *vret, Value *v, RefNode *node)
{
    double factor = (FUNC_INT(node) ? -1.0 : 1.0);
    const RefVector *v1 = Value_vp(v[0]);
    const RefVector *v2 = Value_vp(v[1]);
    int size, i;
    RefVector *vec;

    if (v1->size != v2->size) {
        fs->throw_errorf(fs->mod_lang, "ValueError", "Vector size mismatch");
        return FALSE;
    }
    size = v1->size;
    vec = fs->buf_new(cls_vector, sizeof(RefVector) + sizeof(double) * size);
    *vret = vp_Value(vec);
    vec->size = size;

    for (i = 0; i < size; i++) {
        vec->d[i] = v1->d[i] + v2->d[i] * factor;
    }

    return TRUE;
}
Beispiel #17
0
static int textio_print(Value *vret, Value *v, RefNode *node)
{
    Ref *ref = Value_ref(*v);
    Value *varg = v + 1;
    int new_line = FUNC_INT(node);
    Value stream = ref->v[INDEX_TEXTIO_STREAM];
    RefNode *s_type = Value_type(stream);
    StrBuf *sb = NULL;

    if (s_type == fs->cls_bytesio) {
        sb = bytesio_get_strbuf(stream);
    }
    while (varg < fg->stk_top) {
        if (!textio_print_sub(*v, sb, *varg, NULL)) {
            return FALSE;
        }
        varg++;
    }

    if (new_line) {
        RefTextIO *tio = Value_vp(ref->v[INDEX_TEXTIO_TEXTIO]);
        if (tio == NULL || tio->cs->ascii) {
            if (sb != NULL) {
                if (!StrBuf_add_c(sb, '\n')) {
                    return FALSE;
                }
            } else if (!stream_write_data(stream, "\n", 1)) {
                return FALSE;
            }
        } else {
            if (!stream_write_sub_s(stream, sb, "\n", 1, tio)) {
                return FALSE;
            }
        }
    }
    return TRUE;
}
Beispiel #18
0
static int matrix_addsub(Value *vret, Value *v, RefNode *node)
{
    double factor = (FUNC_INT(node) ? -1.0 : 1.0);
    const RefMatrix *m1 = Value_vp(v[0]);
    const RefMatrix *m2 = Value_vp(v[1]);
    int size, i;
    RefMatrix *mat;

    if (m1->rows != m2->rows || m1->cols != m2->cols) {
        fs->throw_errorf(fs->mod_lang, "ValueError", "Matrix size mismatch");
        return FALSE;
    }
    size = m1->rows * m1->cols;
    mat = fs->buf_new(cls_matrix, sizeof(RefMatrix) + sizeof(double) * size);
    *vret = vp_Value(mat);
    mat->rows = m1->rows;
    mat->cols = m1->cols;

    for (i = 0; i < size; i++) {
        mat->d[i] = m1->d[i] + m2->d[i] * factor;
    }

    return TRUE;
}
Beispiel #19
0
static int map_marshal_read(Value *vret, Value *v, RefNode *node)
{
    Value dumper = v[1];
    Value r = Value_ref(dumper)->v[INDEX_MARSHALDUMPER_SRC];
    int is_map = FUNC_INT(node);
    RefMap *rm;
    uint32_t size;
    int i;

    if (!stream_read_uint32(r, &size)) {
        return FALSE;
    }
    if (size > 0xffffff) {
        throw_errorf(fs->mod_lang, "ValueError", "Invalid size number");
        return FALSE;
    }
    if (size * sizeof(HashValueEntry) > fs->max_alloc) {
        throw_error_select(THROW_MAX_ALLOC_OVER__INT, fs->max_alloc);
        return FALSE;
    }
    rm = refmap_new(size);
    if (!is_map) {
        rm->rh.type = fs->cls_set;
    }
    *vret = vp_Value(rm);

    for (i = 0; i < size; i++) {
        Value key;
        Value_push("v", dumper);
        if (!call_member_func(fs->str_read, 0, TRUE)) {
            return FALSE;
        }
        fg->stk_top--;
        key = *fg->stk_top;
        if (is_map) {
            Value val;
            HashValueEntry *ve;

            Value_push("v", dumper);
            if (!call_member_func(fs->str_read, 0, TRUE)) {
                return FALSE;
            }
            fg->stk_top--;
            val = *fg->stk_top;

            ve = refmap_add(rm, key, TRUE, FALSE);
            if (ve == NULL) {
                unref(key);
                unref(val);
                return FALSE;
            }
            ve->val = val;
        } else {
            // Set
            if (refmap_add(rm, key, TRUE, FALSE) == NULL) {
                unref(key);
                return FALSE;
            }
        }
        unref(key);
    }

    return TRUE;
}