Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List utility #31

Merged
merged 9 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/util/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "list.h"

#include <stdlib.h> // memory

#include <util/out.h>

typedef struct ListBlock {
void* * array;
int size, full;
struct ListBlock* next;
} ListBlock;

struct List {
ListBlock *head, *tail;
int blocksize;
};

List* create_list(int blocksize) {
List* l = malloc(sizeof(List));
if (!l) {
PRINT_ERROR("internal: failed to allocate memory");
return NULL;
}
l->blocksize = blocksize;
l->head = l->tail = NULL;
}

int destroy_list(List* l) {
// Free all blocks.
ListBlock *lb = l->head, *next;
while (lb) {
next = lb->next;
free(lb->array);
lb = next;
}
free(l);
return 0;
}

void* lget_element(List* l, int index) {
int i = index;
ListBlock* lb = l->head;
if (index < 0) {
PRINT_ERROR("internal: list index was negative (%d)", index);
return NULL;
}
// We see if the index is in range of the current block length. If not,
// repeat with the next block.
while (i >= lb->size) {
if (lb) {
lb = lb->next;
i -= lb->size;
} else {
PRINT_ERROR("internal: list index %d out of bounds", index);
return NULL;
}
}
// Now the index is in range and we are at the list block we wish to access.
if (i >= lb->full) {
PRINT_ERROR("internal: list index %d out of bounds", index);
return NULL;
}
return lb->array[i];
}
32 changes: 32 additions & 0 deletions src/util/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Utilities for variable-length lists, so we don't always have to use fixed-
* length arrays.
* The two extreme approaches are linked lists, which are fast to resize but
* slow to access (and for that matter, bad for memory management), and fixed-
* size arrays, which are fast to access but slow to resize. We can go for an
* intermediate approach with a linked list *of* fixed-size arrays to compromise
* and get the middlest of both worlds.
*/

#pragma once

struct List;
typedef
struct List
List;

// Make and destroy lists.
struct List* create_list(int blocksize);
int destroy_list(List* l);

// Access the n-th element of a list.
void* lget_element(List* l, int index);
// Add new element. Returns success (0) or failure (-1).
int ladd_element(List* l, void* element);
// Set n-th element.
int lset_element(List* l, int index, void* value);
// Iterate over elements. This takes a highly customizable approach. We pass in
// a function which takes a void* pointer and returns an int, and we pass this
// function over every void* element in the list. We return the sum of all
// return values thus accumulated.
int literate(List* l, int (*fn)(void*));
2 changes: 2 additions & 0 deletions src/util/out.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#pragma once

#include <stdio.h> // stderr

// All of the color codes.
#define CODE_RED 31
#define CODE_YELLOW 33
Expand Down
Loading