Skip to content
Snippets Groups Projects
Commit 3a0bd826 authored by Alec's avatar Alec
Browse files

kek

parent 401211f2
No related branches found
No related tags found
No related merge requests found
TP6 0 → 100755
File added
main.c 0 → 100644
#include "vector.h"
int main()
{
return 0;
}
\ No newline at end of file
makefile 0 → 100644
CC=gcc
FLAGS= -Wall -Werror
CFLAGS= -g -std=c99
LDFLAGS= -fsanitize=address -fsanitize=leak
main: main.o vector.o
$(CC) main.o vector.o -o TP6 $(CFLAGS) $(LDFLAGS)
main.o: main.c
$(CC) $(FLAGS) $(CFLAGS) $(LDFLAGS) -c $^
vector.o: vector.c vector.h
$(CC) $(FLAGS) $(CFLAGS) $(LDFLAGS) -c $<
clean:
rm -f *.o main
rebuild: clean TP6
\ No newline at end of file
vector.c 0 → 100644
/*
* Author : Alec Schmidt
* Date : 22.02.2022
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include "vector.h"
#define VECTOR_INIT_CAPACITY 4
typedef struct _vector {
type *data; // actual content of the vector
int capacity; // capacity allocated
int length; // actual length
} vector;
vector vector_create()
{
vector v;
v.capacity = VECTOR_INIT_CAPACITY;
v.length = 0;
v.data = malloc(VECTOR_INIT_CAPACITY * sizeof(type));
return v;
}
int vector_lenght(vector *v)
{
return v->length;
}
void vector_push(vector *v, type item)
{
if (v->length == v->capacity)
{
v->capacity *=2;
v->data = realloc(v->data, v->capacity * sizeof(type));
assert(v->data!=NULL);
}
v->data[v->length] = item;
v->length++;
}
void vector_pop(vector *v, type item)
{
assert(v->length>0);
if (v->length-1 == v->capacity/2)
{
v->capacity /=2;
v->data = realloc(v->data, v->capacity * sizeof(type));
assert(v->data!=NULL);
}
v->length--;
}
void vector_set(vector *v, int index, type element)
{
assert(index <= v->length && index > 0);
v->data[index] = element;
}
type vector_get(vector *v, int index)
{
assert(index <= v->length && index > 0);
return v->data[index];
}
void vector_remove(vector *v, int index)
{
assert(index <= v->length && index > 0);
for (int i = index; i < v->length; i++)
{
v->data[i] = v->data[i+1];
}
v->length--;
}
void vector_insert(vector *v, type element, int index)
{
assert(index <= v->length && index > 0);
if (v->capacity == v->length)
{
v->capacity *=2;
v->data = realloc(v->data, v->capacity * sizeof(type));
assert(v->data!=NULL);
}
v->length++;
for (size_t i = v->length-1; i > index; i--)
{
v->data[i] = v->data[i-1];
}
v->data[index] = element;
}
void vector_empty(vector *v)
{
free(v->data);
vector v2 = vector_create();
v = &v2;
}
void vector_free(vector *v)
{
free(v->data);
free(v);
}
void vector_print(vector *v, void (*print)(type))
{
printf("[");
for (int i = 0; i < v->length; i++)
{
print(v->data[i]);
printf(",");
}
printf("]");
}
vector vector_map(vector *v, type (*f)(type))
{
vector v2 = vector_create();
for (int i = 0; i < v->length; i++)
{
vector_push(&v2, f(v->data[i]));
}
return v2;
}
vector vector_filter(vector *v, bool (*f)(type))
{
vector v2 = vector_create();
for (int i = 0; i < v->length; i++)
{
if (f(v->data[i]))
{
vector_push(&v2, v->data[i]);
}
}
return v2;
}
type vector_reduce(vector *v, type neutral, type (*f)(type, type))
{
type result = neutral;
for (int i = 0; i < v->length; i++)
{
f(neutral, v->data[i]);
}
return result;
}
\ No newline at end of file
vector.h 0 → 100644
#ifndef _VECTOR_H_
#define _VECTOR_H_
typedef int type;
typedef struct _vector vector;
vector vector_create();
int vector_lenght(vector *v);
void vector_push(vector *v, type item);
void vector_pop(vector *v, type item);
void vector_set(vector *v, int index, type element);
type vector_get(vector *v, int index);
void vector_remove(vector *v, int index);
void vector_insert(vector *v, type element, int index);
void vector_empty(vector *v);
void vector_free(vector *v);
void vector_print(vector *v, void (*print)(type));
vector vector_map(vector *v, type (*f)(type));
vector vector_filter(vector *v, bool (*f)(type));
type vector_reduce(vector *v, type neutral, type (*f)(type, type));
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment