mirror of
https://github.com/theoludwig/libcproject.git
synced 2024-11-08 22:31:31 +01:00
parent
821c27c6a9
commit
886038a0ac
39
lib/array_list.c
Normal file
39
lib/array_list.c
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include "array_list.h"
|
||||||
|
|
||||||
|
struct array_list* array_list_initialization() {
|
||||||
|
struct array_list* list = malloc(sizeof(struct array_list));
|
||||||
|
list->data = malloc(sizeof(void*) * ARRAY_LIST_INITIAL_CAPACITY);
|
||||||
|
list->size = 0;
|
||||||
|
list->capacity = ARRAY_LIST_INITIAL_CAPACITY;
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
void array_list_add(struct array_list* list, void* element) {
|
||||||
|
if (list->size >= list->capacity) {
|
||||||
|
size_t previous_capacity = list->capacity;
|
||||||
|
list->capacity += ARRAY_LIST_INITIAL_CAPACITY;
|
||||||
|
list->data = realloc(list->data, sizeof(void*) * list->capacity);
|
||||||
|
for (size_t index = previous_capacity; index < list->capacity; index++) {
|
||||||
|
list->data[index] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list->data[list->size] = element;
|
||||||
|
list->size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void array_list_remove(struct array_list* list, size_t index) {
|
||||||
|
if (index >= list->size) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (size_t i = index + 1; i < list->size - 1; i++) {
|
||||||
|
list->data[i - 1] = list->data[i];
|
||||||
|
}
|
||||||
|
list->size--;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* array_list_get(struct array_list* list, size_t index) {
|
||||||
|
if (index >= list->size) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return list->data[index];
|
||||||
|
}
|
25
lib/array_list.h
Normal file
25
lib/array_list.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#ifndef __ARRAY_LIST__
|
||||||
|
#define __ARRAY_LIST__
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "linked_list.h"
|
||||||
|
|
||||||
|
#define ARRAY_LIST_INITIAL_CAPACITY 10
|
||||||
|
|
||||||
|
struct array_list {
|
||||||
|
void** data;
|
||||||
|
size_t size;
|
||||||
|
size_t capacity;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct array_list* array_list_initialization();
|
||||||
|
|
||||||
|
void array_list_add(struct array_list* list, void* element);
|
||||||
|
|
||||||
|
void array_list_remove(struct array_list* list, size_t index);
|
||||||
|
|
||||||
|
void* array_list_get(struct array_list* list, size_t index);
|
||||||
|
|
||||||
|
#endif
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef __LIBCPROJECT__
|
#ifndef __LIBCPROJECT__
|
||||||
#define __LIBCPROJECT__
|
#define __LIBCPROJECT__
|
||||||
|
|
||||||
|
#include "lib/array_list.h"
|
||||||
#include "lib/character.h"
|
#include "lib/character.h"
|
||||||
#include "lib/convert.h"
|
#include "lib/convert.h"
|
||||||
#include "lib/dictionary.h"
|
#include "lib/dictionary.h"
|
||||||
|
42
test/array_list_test.c
Normal file
42
test/array_list_test.c
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#include "array_list_test.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "libcproject.h"
|
||||||
|
|
||||||
|
void array_list_test() {
|
||||||
|
struct array_list *list = array_list_initialization();
|
||||||
|
assert(list->size == 0);
|
||||||
|
array_list_add(list, (void *)'a');
|
||||||
|
array_list_add(list, (void *)'b');
|
||||||
|
array_list_add(list, (void *)'c');
|
||||||
|
array_list_add(list, (void *)'d');
|
||||||
|
array_list_add(list, (void *)'e');
|
||||||
|
array_list_add(list, (void *)'f');
|
||||||
|
assert(list->size == 6);
|
||||||
|
assert(array_list_get(list, 0) == (void *)'a');
|
||||||
|
assert(array_list_get(list, 1) == (void *)'b');
|
||||||
|
assert(array_list_get(list, 2) == (void *)'c');
|
||||||
|
assert(array_list_get(list, 3) == (void *)'d');
|
||||||
|
assert(array_list_get(list, 4) == (void *)'e');
|
||||||
|
assert(array_list_get(list, 5) == (void *)'f');
|
||||||
|
array_list_add(list, (void *)'a');
|
||||||
|
assert(array_list_get(list, 6) == (void *)'a');
|
||||||
|
assert(list->size == 7);
|
||||||
|
array_list_remove(list, 6);
|
||||||
|
assert(list->size == 6);
|
||||||
|
assert(array_list_get(list, 6) == NULL);
|
||||||
|
|
||||||
|
for (size_t index = 0; index < 100; index++) {
|
||||||
|
array_list_add(list, (void *)index);
|
||||||
|
}
|
||||||
|
assert(list->size == 106);
|
||||||
|
assert(array_list_get(list, 100) == (void *)94);
|
||||||
|
assert(array_list_get(list, 101) == (void *)95);
|
||||||
|
array_list_remove(list, 100);
|
||||||
|
assert(list->size == 105);
|
||||||
|
assert(array_list_get(list, 100) == (void *)95);
|
||||||
|
}
|
6
test/array_list_test.h
Normal file
6
test/array_list_test.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#ifndef __ARRAY_LIST_TEST__
|
||||||
|
#define __ARRAY_LIST_TEST__
|
||||||
|
|
||||||
|
void array_list_test();
|
||||||
|
|
||||||
|
#endif
|
@ -1,6 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "array_list_test.h"
|
||||||
#include "character_test.h"
|
#include "character_test.h"
|
||||||
#include "convert_test.h"
|
#include "convert_test.h"
|
||||||
#include "dictionary_test.h"
|
#include "dictionary_test.h"
|
||||||
@ -11,6 +12,7 @@
|
|||||||
#include "string_test.h"
|
#include "string_test.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
array_list_test();
|
||||||
character_test();
|
character_test();
|
||||||
convert_test();
|
convert_test();
|
||||||
dictionary_test();
|
dictionary_test();
|
||||||
|
Loading…
Reference in New Issue
Block a user