mirror of
				https://github.com/theoludwig/libcproject.git
				synced 2025-05-21 23:21:15 +02:00 
			
		
		
		
	@@ -1,25 +1,30 @@
 | 
				
			|||||||
#include "array_list.h"
 | 
					#include "array_list.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct array_list* array_list_initialization() {
 | 
					struct array_list* array_list_initialization() {
 | 
				
			||||||
 | 
					  return array_list_initialization_with_capacity(ARRAY_LIST_INITIAL_CAPACITY);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct array_list* array_list_initialization_with_capacity(size_t capacity) {
 | 
				
			||||||
  struct array_list* list = malloc(sizeof(struct array_list));
 | 
					  struct array_list* list = malloc(sizeof(struct array_list));
 | 
				
			||||||
  if (list == NULL) {
 | 
					  if (list == NULL) {
 | 
				
			||||||
    perror("Error (array_list_initialization)");
 | 
					    perror("Error (array_list_initialization)");
 | 
				
			||||||
    exit(EXIT_FAILURE);
 | 
					    exit(EXIT_FAILURE);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  list->data = malloc(sizeof(void*) * ARRAY_LIST_INITIAL_CAPACITY);
 | 
					  list->size = 0;
 | 
				
			||||||
 | 
					  list->capacity = capacity;
 | 
				
			||||||
 | 
					  list->capacity_step = capacity;
 | 
				
			||||||
 | 
					  list->data = malloc(sizeof(void*) * list->capacity);
 | 
				
			||||||
  if (list->data == NULL) {
 | 
					  if (list->data == NULL) {
 | 
				
			||||||
    perror("Error (array_list_initialization)");
 | 
					    perror("Error (array_list_initialization)");
 | 
				
			||||||
    exit(EXIT_FAILURE);
 | 
					    exit(EXIT_FAILURE);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  list->size = 0;
 | 
					 | 
				
			||||||
  list->capacity = ARRAY_LIST_INITIAL_CAPACITY;
 | 
					 | 
				
			||||||
  return list;
 | 
					  return list;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void array_list_add(struct array_list* list, void* element) {
 | 
					void array_list_add(struct array_list* list, void* element) {
 | 
				
			||||||
  if (list->size >= list->capacity) {
 | 
					  if (list->size >= list->capacity) {
 | 
				
			||||||
    size_t previous_capacity = list->capacity;
 | 
					    size_t previous_capacity = list->capacity;
 | 
				
			||||||
    list->capacity += ARRAY_LIST_INITIAL_CAPACITY;
 | 
					    list->capacity += list->capacity_step;
 | 
				
			||||||
    list->data = realloc(list->data, sizeof(void*) * list->capacity);
 | 
					    list->data = realloc(list->data, sizeof(void*) * list->capacity);
 | 
				
			||||||
    if (list->data == NULL) {
 | 
					    if (list->data == NULL) {
 | 
				
			||||||
      perror("Error (array_list_add)");
 | 
					      perror("Error (array_list_add)");
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,6 +18,7 @@ struct array_list {
 | 
				
			|||||||
  void** data;
 | 
					  void** data;
 | 
				
			||||||
  size_t size;
 | 
					  size_t size;
 | 
				
			||||||
  size_t capacity;
 | 
					  size_t capacity;
 | 
				
			||||||
 | 
					  size_t capacity_step;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@@ -26,6 +27,12 @@ struct array_list {
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
struct array_list* array_list_initialization();
 | 
					struct array_list* array_list_initialization();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @brief Initializes a new array list with a capacity.
 | 
				
			||||||
 | 
					 * @since v5.0.0
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					struct array_list* array_list_initialization_with_capacity(size_t capacity);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @brief Adds an element to the end of the array list.
 | 
					 * @brief Adds an element to the end of the array list.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user