mirror of
https://github.com/torje/C_crash_course.git
synced 2025-05-28 11:04:26 +02:00
I did stuff. Hopefully stuff is still in order
This commit is contained in:
parent
23be0aa838
commit
215f0c3f60
13
containers/Makefile
Normal file
13
containers/Makefile
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
array_unittests.o:array_unittests.c array.h
|
||||
buffer_unittests.o:buffer_unittests.c buffer.h
|
||||
array.o: array.c array.h
|
||||
buffer.o: buffer.c buffer.h
|
||||
array_units: array_unittests.o array.o buffer.o
|
||||
$(CC) -o $@ $^
|
||||
buffer_units: buffer_unittests.o buffer.o
|
||||
$(CC) -o $@ $^
|
||||
stack.o: stack.c stack.h
|
||||
stack_unittests.o: stack_unittests.c stack.h
|
||||
stack_units: stack_unittests.o stack.o buffer.o
|
||||
$(CC) -o $@ $^
|
70
containers/array.c
Normal file
70
containers/array.c
Normal file
@ -0,0 +1,70 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "array.h"
|
||||
|
||||
struct Array{
|
||||
struct Buffer * buffer;
|
||||
size_t data_size;
|
||||
size_t used;
|
||||
};
|
||||
|
||||
struct Array *Array_create( size_t data_size){
|
||||
struct Array * array = calloc( sizeof( struct Array ), 1);
|
||||
array -> data_size = data_size;
|
||||
array -> used = 0;
|
||||
array -> buffer = Buffer_create();
|
||||
return array;
|
||||
}
|
||||
|
||||
void Array_finalize(struct Array ** array){
|
||||
Buffer_clear((*array)->buffer);
|
||||
free((*array)->buffer);
|
||||
(*array)->data_size = 0;
|
||||
(*array)->used = 0;
|
||||
free( *array);
|
||||
*array=NULL;
|
||||
}
|
||||
|
||||
void Array_alloc_from_zero( struct Array * array, size_t size){
|
||||
Buffer_alloc_from_zero(array->buffer, size*array->data_size);
|
||||
}
|
||||
|
||||
int Array_resize(struct Array * array, size_t size){
|
||||
return Buffer_resize( array->buffer, size*array->data_size);
|
||||
}
|
||||
|
||||
|
||||
void *Array_at(struct Array *array, size_t pos){
|
||||
return Buffer_at( array->buffer, pos*array->data_size);
|
||||
}
|
||||
|
||||
size_t Array_size(struct Array *array){
|
||||
return array ->used;
|
||||
}
|
||||
|
||||
size_t Array_capacity(struct Array *array){
|
||||
return Buffer_size( array->buffer)/array->data_size ;
|
||||
}
|
||||
|
||||
void * Array_push_back(struct Array * array, void * data ){
|
||||
struct Buffer *buffer = array -> buffer;
|
||||
if ( array -> used >= Array_capacity( array ) ){
|
||||
size_t cursize = Buffer_size(buffer);
|
||||
size_t newsize = 0 ;
|
||||
if ( cursize== 0 ){
|
||||
newsize = 64*array -> data_size;
|
||||
}else{
|
||||
newsize = cursize *2;
|
||||
}
|
||||
Buffer_resize(buffer, newsize);
|
||||
void *pos = Buffer_at(buffer, array->used * array->data_size );
|
||||
memmove( pos, data, array->data_size );
|
||||
array->used ++;
|
||||
return pos;
|
||||
}else{
|
||||
void *pos = Buffer_at(buffer, array->used * array->data_size );
|
||||
memmove( pos, data, array->data_size );
|
||||
array->used ++;
|
||||
return pos;
|
||||
}
|
||||
}
|
12
containers/array.h
Normal file
12
containers/array.h
Normal file
@ -0,0 +1,12 @@
|
||||
#include "buffer.h"
|
||||
|
||||
struct Array;
|
||||
|
||||
struct Array * Array_create(size_t data_size);
|
||||
void Array_finalize(struct Array ** array);
|
||||
void Array_alloc_from_zero(struct Array * array, size_t size);
|
||||
int Array_resize(struct Array * array, size_t size);
|
||||
void *Array_at(struct Array *array, size_t pos);
|
||||
size_t Array_size(struct Array *array);
|
||||
size_t Array_capacity(struct Array *array);
|
||||
void * Array_push_back(struct Array * array, void * data );
|
21
containers/array_unittests.c
Normal file
21
containers/array_unittests.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
#include "array.h"
|
||||
|
||||
int main(){
|
||||
struct Array * array = Array_create( sizeof(int));
|
||||
for( int i = 0 ; i < 8 ; i++){
|
||||
int a = i;
|
||||
Array_push_back( array, &i);
|
||||
}
|
||||
printf("size: %lu -- ", Array_size(array));
|
||||
printf("capacity: %lu\n", Array_capacity(array));
|
||||
for ( int i = 0 ; i < Array_size(array) ; i++){
|
||||
//printf("%d ", *(int*)Array_at(array,i));
|
||||
//printf("%p \n", Array_at(array,i));
|
||||
}
|
||||
printf("size: %lu -- ", Array_size(array));
|
||||
printf("capacity: %lu\n", Array_capacity(array));
|
||||
Array_finalize ( &array);
|
||||
|
||||
return 0;
|
||||
}
|
47
containers/buffer.c
Normal file
47
containers/buffer.c
Normal file
@ -0,0 +1,47 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "buffer.h"
|
||||
|
||||
struct Buffer{
|
||||
void * start;
|
||||
void * end;
|
||||
//size_t size;
|
||||
};
|
||||
struct Buffer * Buffer_create(){
|
||||
struct Buffer * buffer = calloc(1, sizeof(struct Buffer) );
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void Buffer_clear(struct Buffer * buffer){
|
||||
free( buffer->start );
|
||||
buffer->start = 0x0;
|
||||
buffer->end = 0x0;
|
||||
}
|
||||
|
||||
void Buffer_alloc_from_zero(struct Buffer * buffer, size_t size){
|
||||
buffer -> start = calloc(1, size);
|
||||
buffer -> end = buffer -> start + size;
|
||||
}
|
||||
|
||||
int Buffer_resize(struct Buffer * buffer, size_t size){
|
||||
void * new = realloc(buffer -> start, size);
|
||||
if ( !new ){
|
||||
printf("Buffer_resize(): buffer->start %p , buffer->size %zu \n",buffer->start,size);
|
||||
return 1;
|
||||
}else{
|
||||
//printf("OMG; YES!\n");
|
||||
buffer -> start = new;
|
||||
buffer -> end = buffer->start + size;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void *Buffer_at(struct Buffer *buffer, size_t pos){
|
||||
void * ret = buffer -> start + pos;
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t Buffer_size(struct Buffer *buffer){
|
||||
size_t size = buffer->end - buffer ->start;
|
||||
return size;
|
||||
}
|
10
containers/buffer.h
Normal file
10
containers/buffer.h
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stddef.h>
|
||||
|
||||
struct Buffer;
|
||||
|
||||
struct Buffer * Buffer_create(void);
|
||||
void Buffer_clear(struct Buffer * buffer);
|
||||
void Buffer_alloc_from_zero(struct Buffer * buffer, size_t size);
|
||||
int Buffer_resize(struct Buffer * buffer, size_t size);
|
||||
void *Buffer_at(struct Buffer *buffer, size_t pos);
|
||||
size_t Buffer_size(struct Buffer *buffer);
|
19
containers/buffer_unittests.c
Normal file
19
containers/buffer_unittests.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include "buffer.h"
|
||||
|
||||
int main(){
|
||||
struct Buffer * buffer = Buffer_create();
|
||||
Buffer_alloc_from_zero(buffer, 1024);
|
||||
Buffer_resize(buffer, 8196);
|
||||
Buffer_resize(buffer, 4196);
|
||||
for ( int i = 0 ; i < Buffer_size(buffer) ; i++){
|
||||
char * val = Buffer_at( buffer, i);
|
||||
*val = 'l';
|
||||
}
|
||||
for ( int i = 0 ; i < Buffer_size(buffer) ; i++){
|
||||
char * val = Buffer_at( buffer, i);
|
||||
printf("%c", *val);
|
||||
}
|
||||
Buffer_clear(buffer);
|
||||
Buffer_clear(buffer);
|
||||
}
|
69
containers/stack.c
Normal file
69
containers/stack.c
Normal file
@ -0,0 +1,69 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "stack.h"
|
||||
struct Stack{
|
||||
struct Buffer *data;
|
||||
size_t used;
|
||||
size_t membersize;
|
||||
};
|
||||
|
||||
size_t Stack_bytes_used( struct Stack * stack){
|
||||
return stack->used*stack->membersize;
|
||||
}
|
||||
size_t Stack_bytes_cap( struct Stack * stack){
|
||||
return Buffer_size(stack->data);
|
||||
}
|
||||
|
||||
struct Stack * Stack_init( size_t membersize){
|
||||
struct Stack * stack = calloc(1, sizeof(struct Stack));
|
||||
stack ->data = Buffer_create();
|
||||
Buffer_alloc_from_zero(stack->data,membersize);
|
||||
stack->used = 0;
|
||||
stack->membersize = membersize;
|
||||
return stack;
|
||||
}
|
||||
|
||||
void * Stack_push_back_simple(struct Stack * stack, void * data ){
|
||||
memmove( Buffer_at( stack->data, stack->used), data, stack->membersize);
|
||||
stack->used++;
|
||||
return Buffer_at( stack->data, stack->used-1);
|
||||
}
|
||||
|
||||
void * Stack_push_back(struct Stack * stack, void * data ){
|
||||
if ( Stack_bytes_used(stack) +stack->membersize > Stack_bytes_cap(stack)){
|
||||
return Stack_push_back_simple(stack, data);
|
||||
}else{
|
||||
size_t newsize = stack->used*2 ? stack->used*2:1 ;
|
||||
Stack_resize(stack, newsize);
|
||||
return Stack_push_back_simple(stack, data);
|
||||
}
|
||||
}
|
||||
int Stack_resize(struct Stack * stack, size_t size){
|
||||
if ( size < stack->used ){
|
||||
Buffer_resize( stack->data, size*stack->membersize);
|
||||
stack->used = size;
|
||||
return size;
|
||||
}else{
|
||||
Buffer_resize(stack->data, size*stack->membersize);
|
||||
return size;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void * Stack_pop_back(struct Stack * array, void * data );
|
||||
void Stack_finalize(struct Stack ** stack){
|
||||
Buffer_clear((*stack) -> data);
|
||||
(*stack) -> data = NULL;
|
||||
(*stack) -> used = 0;
|
||||
(*stack) -> membersize = 0;
|
||||
free(*stack);
|
||||
*stack = NULL;
|
||||
|
||||
}
|
||||
size_t Stack_size(struct Stack *array);
|
||||
|
||||
//void Stack_alloc_from_zero(struct Stack * array, size_t size);
|
||||
//void *Stack_at(struct Array *array, size_t pos);
|
||||
//size_t Stack_capacity(struct Array *array);
|
||||
|
16
containers/stack.h
Normal file
16
containers/stack.h
Normal file
@ -0,0 +1,16 @@
|
||||
#include "buffer.h"
|
||||
|
||||
struct Stack;
|
||||
|
||||
struct Stack * Stack_init( size_t membersize);
|
||||
|
||||
void * Stack_push_back(struct Stack * stack, void * data );
|
||||
void * Stack_pop_back(struct Stack * stack, void * data );
|
||||
void Stack_clear(struct Stack * stack);
|
||||
size_t Stack_size(struct Stack *stack);
|
||||
|
||||
void Stack_alloc_from_zero(struct Stack * stack, size_t size);
|
||||
int Stack_resize(struct Stack * stack, size_t size);
|
||||
//void *Stack_at(struct Stack *stack, size_t pos);
|
||||
//size_t Stack_capacity(struct Stack *stack);
|
||||
|
11
containers/stack_unittests.c
Normal file
11
containers/stack_unittests.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include "stack.h"
|
||||
|
||||
int main(){
|
||||
struct Stack * stack = Stack_init(sizeof(size_t));
|
||||
for ( size_t i = 9; i < 10; i++){
|
||||
Stack_push_back(stack, &i);
|
||||
//printf("%s\n", "ypdeli");
|
||||
}
|
||||
Stack_finalize(&stack);
|
||||
}
|
25
containers/utils.c
Normal file
25
containers/utils.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
int floormod (int dividend, int divisor);
|
||||
int ceildiv(int dividend, int divisor);
|
||||
int floordiv(int dividend, int divisor);
|
||||
|
||||
int ceildiv(int dividend, int divisor){
|
||||
if (dividend > 0 ){
|
||||
return (dividend -1)/divisor+1;
|
||||
}else{
|
||||
return (dividend )/divisor;
|
||||
}
|
||||
}
|
||||
|
||||
int floordiv(int dividend, int divisor){
|
||||
if (dividend > 0 ){
|
||||
return (dividend )/divisor;
|
||||
}else{
|
||||
return (dividend +1-divisor)/divisor;
|
||||
}
|
||||
}
|
||||
|
||||
int floormod (int dividend, int divisor){
|
||||
return dividend - divisor * floordiv(dividend, divisor);
|
||||
}
|
3
containers/utils.h
Normal file
3
containers/utils.h
Normal file
@ -0,0 +1,3 @@
|
||||
int floormod (int dividend, int divisor);
|
||||
int ceildiv(int dividend, int divisor);
|
||||
int floordiv(int dividend, int divisor);
|
Loading…
x
Reference in New Issue
Block a user