Skip to content

Commit

Permalink
started adding Subset structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad1991 committed Jul 9, 2024
1 parent cff7f62 commit 61a5570
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 18 deletions.
9 changes: 4 additions & 5 deletions .development/cApproach/Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
# Define the compiler and the flags
CC = gcc
CFLAGS = -Wall -g

# Define the source files and object files
SRCS = $(wildcard *.c)
SRCS = main.c SubsetManager.c VectorManager.c
OBJS = $(SRCS:.c=.o)

# Define the target executable
TARGET = main

# Rule to build the target executable
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS)

# Rule to build object files
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

SubsetManager.o: VectorManager.h
VectorManager.o: VectorManager.h

# Clean up
clean:
rm -f $(OBJS) $(TARGET)
Expand Down
134 changes: 134 additions & 0 deletions .development/cApproach/SubsetManager.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include "SubsetManager.h"
#include "VectorManager.h"

SubsetManager create_sm() {
SubsetManager sm;
sm.logicals_subsets = NULL;
sm.integers_subsets = NULL;
sm.numerics_subsets = NULL;
sm.i_vectors = 0;
sm.l_vectors = 0;
sm.n_vectors = 0;
return sm;
}

void free_sm(SubsetManager *sm) {
if (sm->numerics_subsets)
free(sm->numerics_subsets);
if (sm->integers_subsets)
free(sm->integers_subsets);
if (sm->logicals_subsets)
free(sm->logicals_subsets);
}

// TODO: pass size_t** indices as this is individual for each vector
void add_numerics_subsets(size_t *vec_indices, size_t n, VectorManager *vm,
size_t *indices, size_t size_indices,
SubsetManager *sm) {
if (n == 0)
return;

SubsetNumeric *vectors = (SubsetNumeric *)malloc(n * sizeof(SubsetNumeric));
if (!vectors) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
sm->numerics_subsets = vectors;
sm->n_vectors = n;

for (size_t i = 0; i < n; i++) {
sm->numerics_subsets[i].vec = vm->numerics[vec_indices[i]];
sm->numerics_subsets[i].indices = indices;
sm->numerics_subsets[i].size_indices = size_indices;
}
}

void add_integers_subsets(size_t *vec_indices, size_t n, VectorManager *vm,
size_t *indices, size_t size_indices,
SubsetManager *sm) {
if (n == 0)
return;

SubsetInteger *vectors = (SubsetInteger *)malloc(n * sizeof(SubsetInteger));
if (!vectors) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
sm->integers_subsets = vectors;
sm->n_vectors = n;

for (size_t i = 0; i < n; i++) {
sm->integers_subsets[i].vec = vm->integers[vec_indices[i]];
sm->integers_subsets[i].indices = indices;
sm->integers_subsets[i].size_indices = size_indices;
}
}

void add_logicals_subsets(size_t *vec_indices, size_t n, VectorManager *vm,
size_t *indices, size_t size_indices,
SubsetManager *sm) {
if (n == 0)
return;

SubsetLogical *vectors = (SubsetLogical *)malloc(n * sizeof(SubsetLogical));
if (!vectors) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
sm->logicals_subsets = vectors;
sm->n_vectors = n;

for (size_t i = 0; i < n; i++) {
sm->logicals_subsets[i].vec = vm->logicals[vec_indices[i]];
sm->logicals_subsets[i].indices = indices;
sm->logicals_subsets[i].size_indices = size_indices;
}
}

double get_num_subset(size_t index, size_t vec_index, SubsetManager *sm) {
// TODO: check that index is within indices
return sm->numerics_subsets[vec_index]
.vec
->data[sm->numerics_subsets[vec_index]
.indices[index % sm->numerics_subsets[vec_index].vec->size]];
}

double *set_num_subset(size_t index, size_t vec_index, SubsetManager *sm) {
return &(
sm->numerics_subsets[vec_index]
.vec->data[sm->numerics_subsets[vec_index].indices
[index % sm->numerics_subsets[vec_index].vec->size] %
sm->numerics_subsets[vec_index].size_indices]);
}

int get_int_subset(size_t index, size_t vec_index, SubsetManager *sm) {
return sm->integers_subsets[vec_index]
.vec
->data[sm->integers_subsets[vec_index]
.indices[index % sm->integers_subsets[vec_index].vec->size] %
sm->integers_subsets[vec_index].size_indices];
}

int *set_int_subset(size_t index, size_t vec_index, SubsetManager *sm) {
return &(
sm->integers_subsets[vec_index]
.vec->data[sm->integers_subsets[vec_index].indices
[index % sm->integers_subsets[vec_index].vec->size] %
sm->integers_subsets[vec_index].size_indices]);
}

bool get_log_subset(size_t index, size_t vec_index, SubsetManager *sm) {
return sm->logicals_subsets[vec_index]
.vec
->data[sm->logicals_subsets[vec_index]
.indices[index % sm->logicals_subsets[vec_index].vec->size] %
sm->logicals_subsets[vec_index].size_indices];
}

bool *set_log_subset(size_t index, size_t vec_index, SubsetManager *sm) {
return &(
sm->logicals_subsets[vec_index]
.vec->data[sm->logicals_subsets[vec_index].indices
[index % sm->logicals_subsets[vec_index].vec->size] %
sm->logicals_subsets[vec_index].size_indices]);
}
52 changes: 52 additions & 0 deletions .development/cApproach/SubsetManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef SUBSET_MANAGER_H
#define SUBSET_MANAGER_H

#include "VectorManager.h"

typedef struct {
Numeric *vec;
size_t *indices;
size_t size_indices;
} SubsetNumeric;

typedef struct {
Integer *vec;
size_t *indices;
size_t size_indices;
} SubsetInteger;

typedef struct {
Logical *vec;
size_t *indices;
size_t size_indices;
} SubsetLogical;

typedef struct {
SubsetLogical *logicals_subsets;
SubsetInteger *integers_subsets;
SubsetNumeric *numerics_subsets;
size_t l_vectors;
size_t i_vectors;
size_t n_vectors;
} SubsetManager;

SubsetManager create_sm();
void free_sm(SubsetManager *sm);
void add_numerics_subsets(size_t *vec_indices, size_t n, VectorManager *vm,
size_t *indices, size_t size_indices,
SubsetManager *sm);

void add_integers_subsets(size_t *vec_indices, size_t n, VectorManager *vm,
size_t *indices, size_t size_indices,
SubsetManager *sm);
void add_logicals_subsets(size_t *vec_indices, size_t n, VectorManager *vm,
size_t *indices, size_t size_indices,
SubsetManager *sm);

double get_num_subset(size_t index, size_t vec_index, SubsetManager *sm);
double *set_num_subset(size_t index, size_t vec_index, SubsetManager *sm);
int get_int_subset(size_t index, size_t vec_index, SubsetManager *sm);
int *set_int_subset(size_t index, size_t vec_index, SubsetManager *sm);
bool get_log_subset(size_t index, size_t vec_index, SubsetManager *sm);
bool *set_log_subset(size_t index, size_t vec_index, SubsetManager *sm);
#endif
3 changes: 3 additions & 0 deletions .development/cApproach/VectorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ typedef struct {
} VectorManager;

VectorManager create_vm();
void save_free_num(Numeric *v);
void save_free_int(Integer *v);
void save_free_log(Logical *v);
void free_and_exit(VectorManager *vm, const char *message);
void free_vm(VectorManager *vm);
size_t determine_size(VectorManager *vm, int *num_vars, int *types_vars, int n);
Expand Down
Binary file modified .development/cApproach/main
Binary file not shown.
30 changes: 17 additions & 13 deletions .development/cApproach/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#include "SubsetManager.h"
#include "VectorManager.h"

// User would write v3 = v1 + v2 + 3.3
void EXPR1(VectorManager *vm) {

SubsetManager sm = create_sm();
size_t subsetted_vecs[] = {0, 1};
size_t n = 2;
size_t indices[] = {0, 3};
size_t size_indices = 2;
add_numerics_subsets(subsetted_vecs, n, vm, indices, size_indices, &sm);
printf("\n");
for (size_t i = 0; i < size_indices; i++) {
printf("%f\n", get_num_subset(i, 0, &sm));
}
printf("\n");

int vars_in_expr[] = {0, 1};
int types_of_vars[] = {2, 2};
int nvars = 2;
Expand All @@ -12,29 +26,19 @@ void EXPR1(VectorManager *vm) {
vm->tempNum->data[i] =
get_num(i, 0, vm) + get_num(i, 1, vm) + get_scalar_num(2, vm);
}
if (s > vm->numerics[var_left]->size) { // TODO: implement resize functions
if (s > vm->numerics[var_left]->size) {
alloc_numeric(var_left, s, vm);
}

for (size_t i = 0; i < s; i++) {
// TODO: add break for set_scalar_xy after first assignment
*set_num(i, var_left, vm) = vm->tempNum->data[i];
}
free_sm(&sm);
}

// TODO:
// [ ] define structs SubsetNumeric, SubsetInteger and SubsetLogical
// [ ] create getter and setter for those structs
// [ ] identify in R when variable is subsetted
// [ ] first create symbol table
// [ ] traverse again and find subsetted variables.
// Store corresponding subsets for each expression
// - the variable which is subsetted
// - the expression defining the indices of the subset
// * possible expressions to define indices:
// * int, double, Numeric, Integer, Logical,
// SubsetNumeric, SubsetInteger and SubsetLogical
// but not a calculation

int main() {
VectorManager vm = create_vm();
add_numerics(3, &vm);
Expand Down

0 comments on commit 61a5570

Please sign in to comment.