PVector_ptr Vector_mul(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); int i; if ( a.vector==NULL || b.vector==NULL || a.vector->length!=b.vector->length ) vector_operation_error(); size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) c.vector->nodes[i].data = ith(a, i) * ith(b, i); DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }
PVector_ptr Vector_add(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); int i; if ( a.vector==NULL || b.vector==NULL || a.vector->length!=b.vector->length ) vector_operation_error(); size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) c.vector->nodes[i].data = ith(a, i) + ith(b, i); // safe because we have sole ptr to c for now DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }
PVector_ptr Vector_div(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); int i; if ( a.vector==NULL || b.vector==NULL || a.vector->length!=b.vector->length ) vector_operation_error(); size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) { if (ith(b,i) == 0) { fprintf(stderr, "ZeroDivisionError: Divisor cann't be 0\n"); exit(1); } c.vector->nodes[i].data = ith(a, i) / ith(b, i); } DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }
PVector_ptr Vector_add(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); if ( a.vector==NULL || b.vector==NULL ) { null_pointer_error("Addition operator cannot be applied to NULL Vectors\n"); return NIL_VECTOR; } if ( a.vector->length!=b.vector->length ) { vector_operation_error(); return NIL_VECTOR; } int i; size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) c.vector->nodes[i].data = ith(a, i) + ith(b, i); // safe because we have sole ptr to c for now DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }
PVector_ptr Vector_mul(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); if ( a.vector==NULL || b.vector==NULL ) { null_pointer_error("Multiplication operator cannot be applied to NULL Vectors\n"); return NIL_VECTOR; } if ( a.vector->length!=b.vector->length ) { vector_operation_error(); return NIL_VECTOR; } int i; size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) c.vector->nodes[i].data = ith(a, i) * ith(b, i); DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }
int push_default_value(int i, int sp, element *stack) { switch (i) { case INT_TYPE: stack[++sp].i = DEFAULT_INT_VALUE; break; case FLOAT_TYPE: stack[++sp].f = DEFAULT_FLOAT_VALUE; break; case BOOLEAN_TYPE: stack[++sp].b = DEFAULT_BOOLEAN_VALUE; break; case STRING_TYPE: stack[++sp].s = DEFAULT_STRING_VALUE; break; case VECTOR_TYPE: stack[++sp].vptr = PVector_init(0, 0); break; default: break; } return sp; }
PVector_ptr Vector_div(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); if ( a.vector==NULL || b.vector==NULL ) { null_pointer_error("Division operator cannot be applied to NULL Vectors\n"); return NIL_VECTOR; } if ( a.vector->length!=b.vector->length ) { vector_operation_error(); return NIL_VECTOR; } int i; size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) { if (ith(b,i) == 0) { fprintf(stderr, "ZeroDivisionError: Divisor cann't be 0\n"); return NIL_VECTOR; } c.vector->nodes[i].data = ith(a, i) / ith(b, i); } DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }
PVector_ptr Vector_from_float(double value, size_t len) { PVector_ptr result = PVector_init(value, len); return result; }
PVector_ptr Vector_from_int(int value, size_t len) { PVector_ptr result = PVector_init(value, len); return result; }
PVector_ptr Vector_empty(size_t n) { PVector_ptr v = PVector_init(0.0, n); return v; }