Skip to content

Commit

Permalink
Add other types (except dict)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeldit committed Oct 25, 2024
1 parent e2ea80c commit 011ec9b
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 9 deletions.
23 changes: 20 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main(int argc, char *argv[])
return 1;
}

Array a = init_array(5);
Array a = init_array(12);

String s1 = { .str = "Test", .length = 4 };
String s2 = { .str = "Hello", .length = 5 };
Expand All @@ -19,17 +19,34 @@ int main(int argc, char *argv[])
arr_add_str(&a, s1);
arr_add_str(&a, s2);
arr_add_int(&a, 5);
arr_add_null(&a);
arr_add_null(&a);
arr_add_bool(&a, 1);
arr_add_bool(&a, 0);
arr_add_double(&a, 0.5);
arr_add_double(&a, 55.789);

for (int i = 0; i < a.size; ++i)
{
ArrayLink al = array_get(&a, i);
switch (al.type)
{
case T_STR:
printf("%s\n", al.str.str ? al.str.str : "");
printf("%s\n", al.strv.str ? al.strv.str : "");
break;
case T_INT:
printf("%d\n", al.integer);
printf("%d\n", al.integerv);
break;
case T_DOUBLE:
printf("%f\n", al.doublev);
break;
case T_BOOL:
printf("%s\n", al.boolv ? "true" : "false");
break;
case T_NULL:
printf("null\n");
break;
case T_ARR:
break;
}
}
Expand Down
238 changes: 236 additions & 2 deletions src/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,144 @@ void arr_add_int(Array *a, int value)
a->indexes_types[a->insert_idx++] = T_INT;
}

void arr_add_double(Array *a, double value)
{
if (!a)
{
return;
}

if (!a->doubles_head)
{
DoubleArrLink *l = calloc(1, sizeof(DoubleArrLink));
if (!l)
{
return;
}
l->value = value;
l->index = a->insert_idx;
a->doubles_head = l;
a->doubles_tail = l;
}
else
{
// Goes to the last element of the linked list and adds the new element
DoubleArrLink *nl = calloc(1, sizeof(DoubleArrLink));
if (!nl)
{
return;
}
nl->value = value;
nl->index = a->insert_idx;
a->doubles_tail->next = nl;
a->doubles_tail = nl;
}
a->indexes_types[a->insert_idx++] = T_DOUBLE;
}

void arr_add_bool(Array *a, char value)
{
if (!a)
{
return;
}

if (!a->booleans_head)
{
BoolArrLink *l = calloc(1, sizeof(BoolArrLink));
if (!l)
{
return;
}
l->value = value;
l->index = a->insert_idx;
a->booleans_head = l;
a->booleans_tail = l;
}
else
{
// Goes to the last element of the linked list and adds the new element
BoolArrLink *nl = calloc(1, sizeof(BoolArrLink));
if (!nl)
{
return;
}
nl->value = value;
nl->index = a->insert_idx;
a->booleans_tail->next = nl;
a->booleans_tail = nl;
}
a->indexes_types[a->insert_idx++] = T_BOOL;
}

void arr_add_null(Array *a)
{
if (!a)
{
return;
}

if (!a->nulls_head)
{
NullArrLink *l = calloc(1, sizeof(NullArrLink));
if (!l)
{
return;
}
l->index = a->insert_idx;
a->nulls_head = l;
a->nulls_tail = l;
}
else
{
// Goes to the last element of the linked list and adds the new element
NullArrLink *nl = calloc(1, sizeof(NullArrLink));
if (!nl)
{
return;
}
nl->index = a->insert_idx;
a->nulls_tail->next = nl;
a->nulls_tail = nl;
}
a->indexes_types[a->insert_idx++] = T_NULL;
}

void arr_add_array(Array *a, Array *value)
{
if (!a || !value)
{
return;
}

if (!a->arrays_head)
{
ArrArrLink *l = calloc(1, sizeof(ArrArrLink));
if (!l)
{
return;
}
l->value = value;
l->index = a->insert_idx;
a->arrays_head = l;
a->arrays_tail = l;
}
else
{
// Goes to the last element of the linked list and adds the new element
ArrArrLink *nl = calloc(1, sizeof(ArrArrLink));
if (!nl)
{
return;
}
nl->value = value;
nl->index = a->insert_idx;
a->arrays_tail->next = nl;
a->arrays_tail = nl;
}
a->indexes_types[a->insert_idx++] = T_ARR;
}

ArrayLink array_get(Array *a, int index)
{
if (!a)
Expand All @@ -87,6 +225,10 @@ ArrayLink array_get(Array *a, int index)

StrArrLink *sl = 0;
IntArrLink *il = 0;
DoubleArrLink *dl = 0;
BoolArrLink *bl = 0;
NullArrLink *nl = 0;
ArrArrLink *al = 0;
switch (a->indexes_types[index])
{
case T_STR:
Expand All @@ -103,7 +245,7 @@ ArrayLink array_get(Array *a, int index)
}
sl = sl->next;
}
return (ArrayLink){ .type = T_STR, .str = sl->value };
return (ArrayLink){ .type = T_STR, .strv = sl->value };
case T_INT:
il = a->integers_head;
if (!il)
Expand All @@ -118,7 +260,67 @@ ArrayLink array_get(Array *a, int index)
}
il = il->next;
}
return (ArrayLink){ .type = T_INT, .integer = il->value };
return (ArrayLink){ .type = T_INT, .integerv = il->value };
case T_DOUBLE:
dl = a->doubles_head;
if (!dl)
{
return (ArrayLink){ .type = T_ERROR };
}
while (dl->next)
{
if (dl->index == index)
{
break;
}
dl = dl->next;
}
return (ArrayLink){ .type = T_DOUBLE, .doublev = dl->value };
case T_BOOL:
bl = a->booleans_head;
if (!bl)
{
return (ArrayLink){ .type = T_ERROR };
}
while (bl->next)
{
if (bl->index == index)
{
break;
}
bl = bl->next;
}
return (ArrayLink){ .type = T_BOOL, .boolv = bl->value };
case T_NULL:
nl = a->nulls_head;
if (!nl)
{
return (ArrayLink){ .type = T_ERROR };
}
while (nl->next)
{
if (nl->index == index)
{
break;
}
nl = nl->next;
}
return (ArrayLink){ .type = T_NULL };
case T_ARR:
al = a->arrays_head;
if (!al)
{
return (ArrayLink){ .type = T_ERROR };
}
while (al->next)
{
if (al->index == index)
{
break;
}
al = al->next;
}
return (ArrayLink){ .type = T_ARR, .arrayv = al->value };
}
return (ArrayLink){ .type = T_ERROR };
}
Expand All @@ -142,4 +344,36 @@ void destroy_array(Array *a)
il = il->next;
free(tmp);
}

DoubleArrLink *dl = a->doubles_head;
while (dl)
{
DoubleArrLink *tmp = dl;
dl = dl->next;
free(tmp);
}

BoolArrLink *bl = a->booleans_head;
while (bl)
{
BoolArrLink *tmp = bl;
bl = bl->next;
free(tmp);
}

NullArrLink *nl = a->nulls_head;
while (nl)
{
NullArrLink *tmp = nl;
nl = nl->next;
free(tmp);
}

ArrArrLink *al = a->arrays_head;
while (al)
{
ArrArrLink *tmp = al;
al = al->next;
free(tmp);
}
}
Loading

0 comments on commit 011ec9b

Please sign in to comment.