add array functionality

This commit is contained in:
2026-05-05 01:21:11 -05:00
parent fdc5b699bd
commit cb5111de82
5 changed files with 83 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ CC = gcc
SHELL := /bin/bash SHELL := /bin/bash
INCLUDE := include INCLUDE := include
CFLAGS = -Wall -I$(INCLUDE) CFLAGS = -Wall -I$(INCLUDE) -ggdb
BUILD_DIR := build BUILD_DIR := build
@@ -18,8 +18,10 @@ INCLUDE_INSTALL_PATH := /usr/local/include/lsort
LIB_INSTALL_PATH := /usr/local/lib LIB_INSTALL_PATH := /usr/local/lib
STATIC_BUILD_FILES := $(STATIC_BUILD_DIR)/lsort.o \ STATIC_BUILD_FILES := $(STATIC_BUILD_DIR)/lsort.o \
$(STATIC_BUILD_DIR)/types.o
DYNAMIC_BUILD_FILES := $(DYNAMIC_BUILD_DIR)/lsort.o \ DYNAMIC_BUILD_FILES := $(DYNAMIC_BUILD_DIR)/lsort.o \
$(STATIC_BUILD_DIR)/types.o
TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \ TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \

View File

@@ -1,6 +1,6 @@
#ifndef LSORT_H #ifndef LSORT_H
#define LSORT_H #define LSORT_H
int add(int x, int y);
#endif #endif

29
include/types.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef LSORT_TYPES_H
#define LSORT_TYPES_H
#include <stddef.h>
#include "string.h"
typedef struct {
const char *buf;
size_t len;
} lsort_string;
typedef struct {
size_t count;
size_t capacity;
int* items;
} lsort_int_array;
lsort_int_array* lsort_create_int_array();
void lsort_int_array_append(lsort_int_array* array, int number);
int lsort_int_array_swap(lsort_int_array* array, int indexA, int indexB);
// struct lsort_int_link {
// int value;
// lsort_int_array* next;
// };
#endif

View File

@@ -1,11 +1,34 @@
#include "../include/lsort.h" #include "../include/types.h"
#include "stdio.h"
int main(void) { int test_int_array_append(lsort_int_array* array) {
int x = 5;
int y = 2;
printf("%d + %d = %d\n", x, y, add(x, y)); lsort_int_array_append(array, 25);
lsort_int_array_append(array, 64);
lsort_int_array_append(array, 34);
lsort_int_array_append(array, 74);
lsort_int_array_append(array, 99);
lsort_int_array_append(array, 23);
if (array->items[5] != 23) return 1;
return 0;
}
int test_int_array_swap(lsort_int_array* array) {
lsort_int_array_swap(array, 0, 5);
lsort_int_array_swap(array, 4, 2);
lsort_int_array_swap(array, 2, 3);
if (array->items[0] != 23 || array->items[5] != 25 || array->items[4] != 34 || array->items[2] != 74 || array->items[3] != 99) return 1;
return 0;
}
int main(void) {
lsort_int_array array = {0};
if (test_int_array_append(&array)) return 1;
if (test_int_array_swap(&array)) return 2;
return 0; return 0;
} }

21
types.c Normal file
View File

@@ -0,0 +1,21 @@
#include "include/types.h"
#include <stdlib.h>
void lsort_int_array_append(lsort_int_array* array, int item) {
if (array->count >= array->capacity) {
if (array->capacity == 0) array->capacity = 8;
else array->capacity *= 2;
array->items = realloc(array->items, array->capacity * sizeof(*array->items));
}
array->items[array->count++] = item;
}
int lsort_int_array_swap(lsort_int_array* array, int indexA, int indexB) {
if (indexA > array->count - 1 || indexB > array->count - 1) return 1;
int temp = array->items[indexA];
array->items[indexA] = array->items[indexB];
array->items[indexB] = temp;
return 0;
}