Skip to content

Commit

Permalink
introduce filter, shape, reshape
Browse files Browse the repository at this point in the history
  • Loading branch information
ap29600 committed Sep 22, 2022
1 parent f66d803 commit 8d644ca
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/funcs/arithmetic.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,6 @@ Eval_Node func_square_root(Eval_Context *ctx, Node_Handle left, Node_Handle righ

return (Eval_Node){.type = Node_Array, .as.array = right_};
}



65 changes: 65 additions & 0 deletions src/funcs/collections.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "funcs.h"

const static u64 sizes[] = {
[Type_Float] = sizeof(f64),
[Type_Char] = sizeof(char),
[Type_Int] = sizeof(i64),
[Type_Bool] = sizeof(bool),
};

Eval_Node func_filter (Eval_Context *ctx, Node_Handle left, Node_Handle right) {
assert(ctx->nodes[left ].type == Node_Array);
assert(ctx->nodes[right].type == Node_Array);
Array *left_ = borrow_array(ctx->nodes[left ].as.array);
Array *right_ = borrow_array(ctx->nodes[right].as.array);
assert(left_->shape == right_->shape);
left_ = array_cast(left_, Type_Bool);

u64 i = 0;
for (u64 j = 0; j < left_->shape; ++j) if (((bool*)left_->data)[j]) ++i;
Array *result = make_array(NULL, i, right_->type);

u64 size = sizes[right_->type];
for(u64 j = 0, i = 0; j < right_->shape; ++j) {
if (((bool*)left_->data)[j]) {
memcpy(((u8*)result->data) + i * size, ((u8*)right_->data) + j * size, size);
++i;
}
}

release_array(left_);
release_array(right_);

return (Eval_Node){ .type = Node_Array, .as.array = result};
}

Eval_Node func_reshape (Eval_Context *ctx, Node_Handle left, Node_Handle right) {
assert(ctx->nodes[left].type == Node_Array);
assert(ctx->nodes[right].type == Node_Array);
Array *left_ = borrow_array(ctx->nodes[left ].as.array);
Array *right_ = borrow_array(ctx->nodes[right].as.array);
assert(left_->shape == 1);
left_ = array_cast(left_, Type_Int);
Array *result = make_array(NULL, ((i64*)left_->data)[0], right_->type);
u64 count = 0;
u64 size = sizes[right_->type];

while (count < result->shape) {
u64 to_copy = min(result->shape - count, right_->shape);
memcpy(((u8*)result->data) + count * size, right_->data, to_copy * size);
count += to_copy;
}

release_array(right_);
release_array(left_);
return (Eval_Node){ .type = Node_Array, .as.array = result};
}

Eval_Node func_shape (Eval_Context *ctx, Node_Handle left, Node_Handle right) {
assert(ctx->nodes[right].type == Node_Array);
Array *right_ = borrow_array(ctx->nodes[right].as.array);
Array *result = make_array(NULL, 1, Type_Int);
((i64*)result->data)[0] = right_->shape;
release_array(right_);
return (Eval_Node){ .type = Node_Array, .as.array = result};
}
2 changes: 2 additions & 0 deletions src/funcs/funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ void init_default_scope() {
scope_insert(&default_scope, (Lookup_Entry){ .name = "/", .as_monadic = func_square_root, .as_dyadic = func_divide });
scope_insert(&default_scope, (Lookup_Entry){ .name = "~", .as_monadic = func_complement, .as_dyadic = func_mismatch });
scope_insert(&default_scope, (Lookup_Entry){ .name = "=", .as_monadic = NULL, .as_dyadic = func_equal });
scope_insert(&default_scope, (Lookup_Entry){ .name = "?", .as_monadic = NULL, .as_dyadic = func_filter });
scope_insert(&default_scope, (Lookup_Entry){ .name = "$", .as_monadic = func_shape, .as_dyadic = func_reshape });
}

Lookup_Scope default_scope = {0};
4 changes: 4 additions & 0 deletions src/funcs/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ Eval_Node func_negate (Eval_Context *ctx, Node_Handle left, Node_Handle righ
Eval_Node func_complement (Eval_Context *ctx, Node_Handle left, Node_Handle right);
Eval_Node func_square_root(Eval_Context *ctx, Node_Handle left, Node_Handle right);

Eval_Node func_filter (Eval_Context *ctx, Node_Handle left, Node_Handle right);
Eval_Node func_shape (Eval_Context *ctx, Node_Handle left, Node_Handle right);
Eval_Node func_reshape (Eval_Context *ctx, Node_Handle left, Node_Handle right);

#endif // FUNCS_H

0 comments on commit 8d644ca

Please sign in to comment.