Skip to content
Snippets Groups Projects
Commit 22e2f130 authored by theo.rossmann's avatar theo.rossmann :violin:
Browse files

vector project

parent c4b5d372
No related branches found
No related tags found
No related merge requests found
Makefile 0 → 100644
CC=gcc -Wall -Wextra -g -fsanitize=address -fsanitize=leak
Vecteur: Vecteur.o main.o
$(CC) Vecteur.o main.o -o run -lm
Vecteur.o: Vecteur.c Vecteur.h
gcc -Wall -Wextra -c Vecteur.c
main.o: main.c
gcc -Wall -Wextra -c main.c
clean:
rm -f *.o
\ No newline at end of file
Vecteur.c 0 → 100644
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include "Vecteur.h"
#define VECTOR_INIT_CAPACITY 4
vector creat_vector()
{
vector vec;
vec.capacity = VECTOR_INIT_CAPACITY;
vec.length = 0;
vec.content = malloc(VECTOR_INIT_CAPACITY * sizeof(type));
if(vec.content == NULL)
{
EXIT_FAILURE;
}
return vec;
}
int vector_length(vector v)
{
return v.length;
}
void vector_push(vector *v, type element)
{
if (v->length == v->capacity)
{
v->content = realloc( v->content, 2*VECTOR_INIT_CAPACITY * sizeof(type) );
v->capacity *= 2;
}
v->content[v->capacity-1] = element;
v->length++;
}
vector vector_pop(vector *v)
{
if (v->length < (0.25*v->capacity))
{
v->content = realloc( v->content, 0.5*v->capacity * sizeof(type) );
v->capacity *= 0.5;
}
if(v->length != 0)
{
v->content[v->length - 1] = 0;
v->length--;
}
return *v;
}
void vector_set(vector *v, int index, type element)
{
if(0< index && index < v->length)
{
v->content[index] = element;
}
else
{
EXIT_FAILURE;
}
}
type vector_get(vector *v, int index)
{
if(index < v->length)
{
return v->content[index];
}
else
{
EXIT_FAILURE;
}
}
type vector_remove(vector *v, int index)
{
type valsuppr;
valsuppr = v->content[index];
for (int i = index; i< v->length-1; i++)
{
v->content[i] = v->content[i+1];
}
v->length--;
return valsuppr;
}
void vector_insert(vector *v, type element, int index)
{
if(index<v->length)
{
for(int i = v->length; i > index; i--)
{
v->content[i] = v->content[i+1];
}
v->content[index] = element;
v->length++;
}
}
void vector_empty(vector *v)
{
v->capacity = VECTOR_INIT_CAPACITY;
v->length = 0;
free(v->content);
v->content = malloc(VECTOR_INIT_CAPACITY * sizeof(type));
}
void vector_free(vector *v)
{
free(v->content);
}
void vector_print(vector *v, void (*print)(type))
{
for (int i = 0 ; i < v->length; i++){
print(v->content[i]);
}
printf("\n");
}
vector vector_map(vector *v, type (*f)(type))
{
vector vec = creat_vector();
for(int i = 0; i< v->length; i++)
{
vector_push(&vec, f(v->content[i]));
}
return vec;
}
bool lower_than_five(type a)
{
return (a < 5);
}
vector vector_filter(vector *v, bool (*f)(type))
{
vector vec = creat_vector();
for(int i = 0; i< v->length; i++)
{
if(f(v->content[i]))
{
vector_push(&vec, v->content[i]);
}
}
return vec;
}
type add(type lhs, type rhs) {
return lhs + rhs;
}
type vector_reduce(vector *v, type neutral, type (*f)(type, type))
{
type red = neutral;
for (int i = 0 ; i < v->length; i++){
red = f(v->content[i], red);
}
return red;
}
\ No newline at end of file
#ifndef _VECTEUR_H_
#define _VECTEUR_H_
#include <stdbool.h>
#define VECTOR_INIT_CAPACITY 4
typedef int type;
typedef void (*print)(type);
typedef type (*f)(type);
typedef struct _vector {
type *content; // actual content of the vector
int capacity; // capacity allocated
int length; // actual length
} vector;
vector creat_vector();
int vector_length(vector v);
void vector_push(vector *v, type element);
vector vector_pop(vector *v);
void vector_set(vector *v, int index, type element);
type vector_get(vector *v, int index);
type 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));
bool lower_than_five(type a);
vector vector_filter(vector *v, bool (*f)(type));
type add(type lhs, type rhs);
type vector_reduce(vector *v, type neutral, type (*f)(type, type));
#endif
\ No newline at end of file
Vecteur.o 0 → 100644
File added
main.c 0 → 100644
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include "Vecteur.h"
#define VECTOR_INIT_CAPACITY 4
int main()
{
vector vec = creat_vector();
vector_push(&vec, 1);
vector_push(&vec, 2);
}
\ No newline at end of file
main.o 0 → 100644
File added
run 0 → 100755
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment