mirror of
https://github.com/janet-lang/janet
synced 2025-01-24 14:16:52 +00:00
Update license to include contributors.
Use 4 spaces for indentation.
This commit is contained in:
parent
f6a3853131
commit
1394dbbd57
2
LICENSE
2
LICENSE
@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2019 Calvin Rose
|
Copyright (c) 2019 Calvin Rose and contributors
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
this software and associated documentation files (the "Software"), to deal in
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
@ -2,32 +2,30 @@
|
|||||||
#include <janet/janet.h>
|
#include <janet/janet.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
double * data;
|
double * data;
|
||||||
size_t size;
|
size_t size;
|
||||||
} num_array;
|
} num_array;
|
||||||
|
|
||||||
static num_array * num_array_init(num_array * array,size_t size) {
|
static num_array * num_array_init(num_array * array,size_t size) {
|
||||||
array->data=(double *)calloc(size,sizeof(double));
|
array->data=(double *)calloc(size,sizeof(double));
|
||||||
array->size=size;
|
array->size=size;
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void num_array_deinit(num_array * array) {
|
static void num_array_deinit(num_array * array) {
|
||||||
free(array->data);
|
free(array->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int num_array_gc(void *p, size_t s) {
|
static int num_array_gc(void *p, size_t s) {
|
||||||
(void) s;
|
(void) s;
|
||||||
num_array * array=(num_array *)p;
|
num_array * array=(num_array *)p;
|
||||||
num_array_deinit(array);
|
num_array_deinit(array);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Janet num_array_get(void *p, Janet key);
|
Janet num_array_get(void *p, Janet key);
|
||||||
void num_array_put(void *p, Janet key, Janet value);
|
void num_array_put(void *p, Janet key, Janet value);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static const JanetAbstractType num_array_type = {
|
static const JanetAbstractType num_array_type = {
|
||||||
"numarray",
|
"numarray",
|
||||||
num_array_gc,
|
num_array_gc,
|
||||||
@ -37,91 +35,89 @@ static const JanetAbstractType num_array_type = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static Janet num_array_new(int32_t argc, Janet *argv) {
|
static Janet num_array_new(int32_t argc, Janet *argv) {
|
||||||
janet_fixarity(argc, 1);
|
janet_fixarity(argc, 1);
|
||||||
int32_t size=janet_getinteger(argv,0);
|
int32_t size=janet_getinteger(argv,0);
|
||||||
num_array * array = (num_array *)janet_abstract(&num_array_type,sizeof(num_array));
|
num_array * array = (num_array *)janet_abstract(&num_array_type,sizeof(num_array));
|
||||||
num_array_init(array,size);
|
num_array_init(array,size);
|
||||||
return janet_wrap_abstract(array);
|
return janet_wrap_abstract(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Janet num_array_scale(int32_t argc, Janet *argv) {
|
static Janet num_array_scale(int32_t argc, Janet *argv) {
|
||||||
janet_fixarity(argc, 2);
|
janet_fixarity(argc, 2);
|
||||||
num_array * array = (num_array *)janet_getabstract(argv,0,&num_array_type);
|
num_array * array = (num_array *)janet_getabstract(argv,0,&num_array_type);
|
||||||
double factor = janet_getnumber(argv,1);
|
double factor = janet_getnumber(argv,1);
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i=0;i<array->size;i++) {
|
for (i=0;i<array->size;i++) {
|
||||||
array->data[i]*=factor;
|
array->data[i]*=factor;
|
||||||
}
|
}
|
||||||
return argv[0];
|
return argv[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
static Janet num_array_sum(int32_t argc, Janet *argv) {
|
static Janet num_array_sum(int32_t argc, Janet *argv) {
|
||||||
janet_fixarity(argc, 1);
|
janet_fixarity(argc, 1);
|
||||||
num_array * array = (num_array *)janet_getabstract(argv,0,&num_array_type);
|
num_array * array = (num_array *)janet_getabstract(argv,0,&num_array_type);
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
for (size_t i=0;i<array->size;i++) sum+=array->data[i];
|
for (size_t i=0;i<array->size;i++) sum+=array->data[i];
|
||||||
return janet_wrap_number(sum);
|
return janet_wrap_number(sum);
|
||||||
}
|
}
|
||||||
|
|
||||||
void num_array_put(void *p, Janet key, Janet value) {
|
void num_array_put(void *p, Janet key, Janet value) {
|
||||||
size_t index;
|
size_t index;
|
||||||
num_array * array=(num_array *)p;
|
num_array * array=(num_array *)p;
|
||||||
if (!janet_checkint(key))
|
if (!janet_checkint(key))
|
||||||
janet_panic("expected integer key");
|
janet_panic("expected integer key");
|
||||||
if (!janet_checktype(value,JANET_NUMBER))
|
if (!janet_checktype(value,JANET_NUMBER))
|
||||||
janet_panic("expected number value");
|
janet_panic("expected number value");
|
||||||
|
|
||||||
index = (size_t)janet_unwrap_integer(key);
|
index = (size_t)janet_unwrap_integer(key);
|
||||||
if (index < array->size) {
|
if (index < array->size) {
|
||||||
array->data[index]=janet_unwrap_number(value);
|
array->data[index]=janet_unwrap_number(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const JanetReg methods[] = {
|
static const JanetReg methods[] = {
|
||||||
{"scale", num_array_scale,"(:scale numarray factor)"},
|
{"scale", num_array_scale,"(:scale numarray factor)"},
|
||||||
{"sum", num_array_sum,"(:sum numarray)"}
|
{"sum", num_array_sum,"(:sum numarray)"}
|
||||||
};
|
};
|
||||||
|
|
||||||
Janet num_array_get(void *p, Janet key) {
|
Janet num_array_get(void *p, Janet key) {
|
||||||
size_t index;
|
size_t index;
|
||||||
Janet value;
|
Janet value;
|
||||||
num_array * array=(num_array *)p;
|
num_array * array=(num_array *)p;
|
||||||
if (janet_checktype(key,JANET_KEYWORD)) {
|
if (janet_checktype(key,JANET_KEYWORD)) {
|
||||||
const uint8_t *keyw = janet_unwrap_keyword(key);
|
const uint8_t *keyw = janet_unwrap_keyword(key);
|
||||||
const size_t nm = sizeof(methods)/sizeof(JanetReg);
|
const size_t nm = sizeof(methods)/sizeof(JanetReg);
|
||||||
size_t i;
|
size_t i;
|
||||||
for ( i=0 ; i<nm ; i++ ) {
|
for ( i=0 ; i<nm ; i++ ) {
|
||||||
if (!janet_cstrcmp(keyw, methods[i].name)) {
|
if (!janet_cstrcmp(keyw, methods[i].name)) {
|
||||||
return janet_wrap_cfunction(methods[i].cfun);
|
return janet_wrap_cfunction(methods[i].cfun);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
janet_panicf("undefined method %s",keyw);
|
||||||
}
|
}
|
||||||
janet_panicf("undefined method %s",keyw);
|
if (!janet_checkint(key))
|
||||||
}
|
janet_panic("expected integer key");
|
||||||
if (!janet_checkint(key))
|
index = (size_t)janet_unwrap_integer(key);
|
||||||
janet_panic("expected integer key");
|
if (index >= array->size) {
|
||||||
index = (size_t)janet_unwrap_integer(key);
|
value = janet_wrap_nil();
|
||||||
if (index >= array->size) {
|
} else {
|
||||||
value = janet_wrap_nil();
|
value = janet_wrap_number(array->data[index]);
|
||||||
} else {
|
}
|
||||||
value = janet_wrap_number(array->data[index]);
|
return value;
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static const JanetReg cfuns[] = {
|
static const JanetReg cfuns[] = {
|
||||||
{"numarray/new", num_array_new,
|
{"numarray/new", num_array_new,
|
||||||
"(numarray/new size)\n\n"
|
"(numarray/new size)\n\n"
|
||||||
"Create new numarray"
|
"Create new numarray"
|
||||||
},
|
},
|
||||||
{"numarray/scale", num_array_scale,
|
{"numarray/scale", num_array_scale,
|
||||||
"(numarray/scale numarray factor)\n\n"
|
"(numarray/scale numarray factor)\n\n"
|
||||||
"scale numarray by factor"
|
"scale numarray by factor"
|
||||||
},
|
},
|
||||||
{NULL,NULL,NULL}
|
{NULL,NULL,NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
JANET_MODULE_ENTRY(JanetTable *env) {
|
JANET_MODULE_ENTRY(JanetTable *env) {
|
||||||
janet_cfuns(env, "numarray", cfuns);
|
janet_cfuns(env, "numarray", cfuns);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user