#include #include #include "track_list.h" #define TRACK_LIST_START_CAPACITY 1 struct track_list { int size; int capacity; int *starts; int *ends; }; track_list *track_list_create() { track_list *l = malloc(sizeof(track_list)); if (l != NULL) { l->size = 0; l->capacity = TRACK_LIST_START_CAPACITY; l->starts = malloc(sizeof(int) * l->capacity); l->ends = malloc(sizeof(int) * l->capacity); if (l->starts == NULL || l->ends == NULL) { free(l->starts); free(l->ends); free(l); return NULL; } } return l; } void track_list_destroy(track_list *l) { if (l != NULL) { free(l->starts); l->starts = NULL; free(l->ends); l->ends = NULL; free(l); } } int track_list_size(const track_list *l) { if (l == NULL) { return 0; } else { return l->size; } } bool track_list_add(track_list *l, int start, int end) { if (l == NULL) { return false; } if (l->size > 0 && start < l->ends[l->size - 1]) { return false; } if (start > end) { return false; } if (l->size == l->capacity) { int *bigger_starts = malloc(sizeof(int) * l->capacity * 2); int *bigger_ends = malloc(sizeof(int) * l->capacity * 2); if (bigger_starts != NULL && bigger_ends != NULL) { for (int i = 0; i < l->capacity; i++) { bigger_starts[i] = l->starts[i]; bigger_ends[i] = l->ends[i]; } free(l->starts); free(l->ends); l->starts = bigger_starts; l->ends = bigger_ends; l->capacity *= 2; } else { free(bigger_starts); free(bigger_ends); } } if (l->size < l->capacity) { l->starts[l->size] = start; l->ends[l->size] = end; l->size++; return true; } else { return false; } } bool track_list_get(const track_list *l, int i, int *start, int *end) { if (l == NULL || i < 0 || i >= l->size) { return false; } if (start != NULL) { *start = l->starts[i]; } if (end != NULL) { *end = l->ends[i]; } return true; }