Compare commits

...

2 Commits

Author SHA1 Message Date
c1efc67106 rename types 2026-05-05 01:30:10 -05:00
cb5111de82 add array functionality 2026-05-05 01:21:11 -05:00
5 changed files with 88 additions and 8 deletions

View File

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

View File

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

34
include/types.h Normal file
View File

@@ -0,0 +1,34 @@
#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_array_i;
lsort_array_i* lsort_create_int_array();
void lsort_array_i_append(lsort_array_i* array, int number);
int lsort_array_i_swap(lsort_array_i* array, int indexA, int indexB);
typedef struct {
size_t count;
size_t capacity;
lsort_array_i** items;
} lsort_array_i_array;
// struct lsort_int_link {
// int value;
// lsort_array_i* next;
// };
#endif

View File

@@ -1,11 +1,34 @@
#include "../include/lsort.h"
#include "stdio.h"
#include "../include/types.h"
int main(void) {
int x = 5;
int y = 2;
int test_array_i_append(lsort_array_i* array) {
printf("%d + %d = %d\n", x, y, add(x, y));
lsort_array_i_append(array, 25);
lsort_array_i_append(array, 64);
lsort_array_i_append(array, 34);
lsort_array_i_append(array, 74);
lsort_array_i_append(array, 99);
lsort_array_i_append(array, 23);
if (array->items[5] != 23) return 1;
return 0;
}
int test_array_i_swap(lsort_array_i* array) {
lsort_array_i_swap(array, 0, 5);
lsort_array_i_swap(array, 4, 2);
lsort_array_i_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_array_i array = {0};
if (test_array_i_append(&array)) return 1;
if (test_array_i_swap(&array)) return 2;
return 0;
}

21
types.c Normal file
View File

@@ -0,0 +1,21 @@
#include "include/types.h"
#include <stdlib.h>
void lsort_array_i_append(lsort_array_i* 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_array_i_swap(lsort_array_i* 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;
}