Skip to content

Instantly share code, notes, and snippets.

@pavly-gerges
Last active August 19, 2024 15:28
Show Gist options
  • Save pavly-gerges/614ca5ade1e586952d6dfadf3c2593d5 to your computer and use it in GitHub Desktop.
Save pavly-gerges/614ca5ade1e586952d6dfadf3c2593d5 to your computer and use it in GitHub Desktop.
This is an early-access demonstrative example for the `contiguous_buffer.c` library and the List ADT from Arithmos.
#include <electrostatic/util/errno/errno.h>
#include <alloca.h>
#include <stdio.h>
#include <stdlib.h>
#include <electrostatic/algorithm/arithmos/adt/list.h>
void iterator(list *buffer, list_element *element) {
uint64_t index = 0;
buffer->function_table->indexof(buffer, element, &index);
printf("Index at %llud = %d\n", index, *((int*) element->data));
}
int main() {
list *buffer = malloc(sizeof(list));
list_element **elements = malloc(sizeof(list_element**));
list_function_table *table = malloc(sizeof(list_function_table));
buffer->type = CONTIGUOUS_BUFFER;
if (init_list_function_table(buffer, elements, table) == 0) {
printf("Init Succeeded!\n");
} else {
printf("%i\n", buffer->error.value);
}
int x = 20;
list_element ielement = {
.data = &x,
};
int y = 30;
list_element iielement = {
.data = &y,
};
printf("%llud\n", buffer->length);
printf("%llud\n", buffer->limit);
list_element **ebuffer = malloc(sizeof(void**));
ebuffer[0] = &ielement;
ebuffer[1] = &iielement;
buffer->function_table->add_all(buffer, ebuffer);
printf("Last Index = %llud\n", buffer->length);
printf("Fetch by address: %d\n", *((int*) ((list_element *) buffer->elements_memory.start_address)->data));
if (buffer->function_table->contains(buffer, &ielement) != 0) {
printf("Element Not Found, E = %d\n", buffer->error.value);
} else {
uint64_t index = 0;
buffer->function_table->indexof(buffer, &iielement, &index);
printf("Element Found at index = %llud\n", index);
}
if (buffer->function_table->remove_all(buffer, ebuffer) != 0) {
printf("Remove failed, E = %d\n", buffer->error.value);
}
if (buffer->function_table->contains_all(buffer, ebuffer) != 0) {
printf("Elements Not Found, E = %d\n", buffer->error.value);
} else {
uint64_t index = 0;
buffer->function_table->indexof(buffer, &iielement, &index);
buffer->function_table->indexof(buffer, &ielement, &index);
printf("Elements Found at index = %llud\n", index);
}
printf("Last Index = %llud\n", buffer->length);
buffer->function_table->add_all(buffer, ebuffer);
buffer->function_table->iterator(buffer, (list_info) {
.start_index = 0,
.length = buffer->length,
.rate = 1
}, &iterator);
printf("Last Index = %llud\n", buffer->length);
printf("%llud\n", buffer->limit);
free(buffer);
free(ebuffer);
return 0;
}
@pavly-gerges
Copy link
Author

Runtime Logs:

Init Succeeded!
0d
32d
Last Index = 2d
Fetch by address: 20
Element Found at index = 1d
Elements Not Found, E = 239
Last Index = 0d
Index at 0d = 20
Index at 1d = 30
Last Index = 2d
32d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment