more work

This commit is contained in:
Emanuel Rodriguez 2023-07-30 20:27:50 -07:00
parent 1986404da5
commit 68ced98e24
3 changed files with 161 additions and 51 deletions

View File

@ -23,14 +23,10 @@ i32_ArrayList *new_arraylist(int cap) {
}
// add to end of the array
void array_append(i32_ArrayList *s, int v) {
if (s->index == s->capacity) {
printf("you attempted to insert %d, but array is at capacity cannot add mode values\n", v);
} else {
void do_append(i32_ArrayList *s, int v) {
s->data[s->index] = v;
s->index++;
}
}
// takes the address of a pointer
i32_ArrayList *resize_arraylist(i32_ArrayList *arr) {
@ -47,15 +43,15 @@ i32_ArrayList *resize_arraylist(i32_ArrayList *arr) {
return (new_arr);
}
void array_append2(i32_ArrayList **arr_ptr, int v) {
void array_append(i32_ArrayList **arr_ptr, int v) {
i32_ArrayList *arr = *arr_ptr;
if (arr->index == arr->capacity) {
i32_ArrayList *new_arr = resize_arraylist(arr);
*arr_ptr = new_arr;
array_append(*arr_ptr, v);
do_append(*arr_ptr, v);
} else {
array_append(arr, v);
do_append(arr, v);
}
}
@ -63,7 +59,7 @@ void array_append2(i32_ArrayList **arr_ptr, int v) {
i32_ArrayList *new_arraylist_from_array(int cap, int *arr) {
i32_ArrayList *out = new_arraylist(cap);
for (int i = 0; i < cap; i++) {
array_append(out, arr[i]);
do_append(out, arr[i]);
}
return (out);
@ -74,9 +70,10 @@ i32_ArrayList *new_arraylist_from_array(int cap, int *arr) {
// gotta be careful and check that the index + 1 <= capacity otherwise we are in trouble
void array_insert_at(i32_ArrayList *arr, int at_index, int32_t value) {
if (at_index == arr->index) {
array_append(arr, value);
do_append(arr, value);
}
// TODO: eh this should be much better
if (at_index + 1 > arr->capacity) {
printf("ERROR: this insert is not possible since the shift required would be over the capacity of the array\n");
printf("You requested insert at %d but array capacity is set to %d\n", at_index, arr->capacity);
@ -119,24 +116,24 @@ int main() {
print_array_list(b);
// these should all work just fine
array_append(a, 10);
array_append(&a, 10);
print_array_list(a);
array_append(a, 11);
array_append(&a, 11);
print_array_list(a);
array_append(a, 12);
array_append(&a, 12);
print_array_list(a);
array_append(a, 13);
array_append(&a, 13);
print_array_list(a);
array_append(a, 14);
array_append(&a, 14);
print_array_list(a);
// this one will error
array_append(a, 100);
array_append(&a, 100);
// so we remove one and then add
pop_from_array(a);
print_array_list(a);
array_append(a, 100);
array_append(&a, 100);
print_array_list(a);
// now we test inserting different index
@ -154,13 +151,19 @@ int main() {
// this will shift the current 3 to 4, but this causes the 100 to be removed
array_insert_at(a, 3, 123);
print_array_list(a);
array_append2(&a, 5000);
array_append(&a, 5000);
print_array_list(a);
printf("--------------------------------\n");
print_array_list(b);
array_append2(&b, 100);
array_append(&b, 100);
print_array_list(b);
int vals[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
i32_ArrayList *c = new_arraylist_from_array(10, vals);
array_append(&c, 10);
print_array_list(c);
// array_append(a, 14);
// print_array_list(a);
// pop_from_array(a);

107
c/ordered-arrays.c Normal file
View File

@ -0,0 +1,107 @@
/*
an ordered array is just a regular array except all elements must be in order,
so there a few things to note:
1. inserting must respect order
2. removing must respect order
3. serching can be made faster with binary search
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct FixedOrderedArray {
int capacity;
int index; // index next to insert at, arr[index] should be empty
int data[];
} FixedOrderedArray;
FixedOrderedArray *new_fixed_ordered_array(int capacity) {
FixedOrderedArray *arr = malloc(sizeof(FixedOrderedArray) + (sizeof(int) * capacity));
arr->capacity = capacity;
arr->index = 0;
return (arr);
}
void fixed_ordered_array_insert(FixedOrderedArray *arr, int val) {
if (arr->capacity == arr->index) {
fprintf(stderr, "ERROR: array is at capacity, cannot append value\n");
exit(1);
}
if (arr->index == 0) {
arr->data[0] = val;
arr->index++;
} else if (val >= arr->data[arr->index - 1]) {
arr->data[arr->index] = val;
arr->index++;
} else if (val <= arr->data[0]) { // its smallest to add to start
for (int i = arr->index; i > 0; i--) { // shift everything to the right
arr->data[i] = arr->data[i - 1];
}
arr->data[0] = val;
arr->index++;
} else { // look for where it belongs
for (int i = 0; i < arr->index; i++) {
if (arr->data[i] > val) { // we must shift everything to the right up to this index
for (int j = arr->index; j > i; j--) {
arr->data[j] = arr->data[j - 1];
}
arr->data[i] = val;
arr->index++;
break;
}
}
}
}
bool search_fixed_ordered_array(FixedOrderedArray *arr, int val) {
if (arr->index == 0) {
return (false);
}
if (val < arr->data[0]) {
return (false);
}
if (val > arr->data[arr->index - 1]) {
return (false);
}
for (int i = 0; i < arr->index; i++) {
if (val == arr->data[i]) {
return (true);
} else if (arr->data[i] > val) {
return (false);
}
}
return (false);
}
void print_array_list(FixedOrderedArray *arr) {
printf("[");
for (int i = 0; i < arr->index; i++) {
printf(" %d ", arr->data[i]);
}
printf("]\t<capacity: %d; index: %d>\n", arr->capacity, arr->index);
}
int main() {
FixedOrderedArray *arr = new_fixed_ordered_array(5);
fixed_ordered_array_insert(arr, 10);
print_array_list(arr);
fixed_ordered_array_insert(arr, 9);
print_array_list(arr);
fixed_ordered_array_insert(arr, 13);
print_array_list(arr);
fixed_ordered_array_insert(arr, 11);
print_array_list(arr);
fixed_ordered_array_insert(arr, 9);
print_array_list(arr);
bool result = search_fixed_ordered_array(arr, 11);
printf("the result is %d\n", result);
}