Skip to content
Snippets Groups Projects
Commit 4109643a authored by Daniel Rodriguez's avatar Daniel Rodriguez
Browse files

fini les fonctions 'basiques'

parent 96733b32
No related branches found
No related tags found
No related merge requests found
*.o
main
\ No newline at end of file
Makefile 0 → 100644
compile=gcc -Wall -Wextra -fsanitize=address
execName=main
libraryName=vector
rerun: clean run
run: build
./$(execName)
rebuild: clean build
build: $(execName).o $(libraryName).o
$(compile) $^ -o $(execName) -lm
$(execName).o: $(execName).c
$(compile) -c $<
$(libraryName).o: $(libraryName).c $(libraryName).h
$(compile) -c $<
clean:
rm *.o $(execName)
\ No newline at end of file
main.c 0 → 100644
#include "vector.h"
#include <stdio.h>
void print_int(int elem){
printf("%d",elem);
}
int main(){
vector* v = vector_create();
vector_push(v, 1);
vector_push(v, 2);
vector_push(v, 3);
vector_push(v, 4);
vector_push(v, 5);
vector_print(v, print_int);
vector_pop(v);
vector_print(v, print_int);
vector_insert(v, 10, 2);
vector_print(v, print_int);
vector_remove(v, 1);
vector_print(v, print_int);
vector_remove(v, 0);
vector_print(v, print_int);
vector_empty(v);
vector_print(v, print_int);
vector_free(v);
return EXIT_SUCCESS;
}
\ No newline at end of file
vector.c 0 → 100644
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "vector.h"
#define VECTOR_INIT_CAPACITY 4
struct _vector {
type *content; // actual content of the vector
int capacity; // capacity allocated
int length; // actual length
};
//typedef struct _vector vector;
static void handle_bounds(vector* v, int index){
if(index < 0 || index >= v->length){
assert(false);
}
}
vector* vector_create(){
vector* v = malloc(sizeof(*v));
v->capacity = VECTOR_INIT_CAPACITY;
v->length = 0;
v->content = malloc(v->capacity * sizeof(type));
return v;
}
int vector_length(vector* v){
return v->length;
}
void vector_push(vector* v, type element){
if(v->length >= v->capacity){
v->capacity *= 2;
v->content = realloc(v->content, v->capacity * sizeof(type));
}
v->content[v->length] = element;
v->length += 1;
}
void vector_pop(vector* v){
v->length -= 1;
if(v->length < 0){
assert(false);
}
if(v->length < (v->capacity/4)){
v->capacity /= 2;
v->content = realloc(v->content, v->capacity * sizeof(type));
}
}
void vector_set(vector* v, int index, type element){
handle_bounds(v, index);
v->content[index] = element;
}
type vector_get(vector* v, int index){
handle_bounds(v, index);
return v->content[index];
}
void vector_remove(vector* v, int index){
handle_bounds(v, index);
for(int i = index; i < v->length-1; i++){
v->content[i] = v->content[i+1];
}
vector_pop(v);
}
void vector_insert(vector *v, type element, int index){
handle_bounds(v, index);
vector_push(v,0);
for(int i = v->length-1; i > index; i--){
v->content[i] = v->content[i-1];
}
v->content[index] = element;
}
void vector_empty(vector *v){
v->length = 0;
v->capacity = VECTOR_INIT_CAPACITY;
v->content = realloc(v->content, v->capacity * sizeof(type));
}
void vector_free(vector* v){
free(v->content);
free(v);
}
void vector_print(vector* v, void (*print)(type)){
printf("[");
for(int i = 0; i < v->length-1; i++){
print(v->content[i]);
printf(", ");
}
if(v->length >= 1){
print(v->content[v->length-1]);
}
printf("]\n");
}
\ No newline at end of file
vector.h 0 → 100644
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include <stdlib.h>
#include <stdbool.h>
typedef int type;
struct _vector;
typedef struct _vector vector;
vector* vector_create();
int vector_length(vector* v);
void vector_push(vector* v, type element);
void vector_pop(vector* v);
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));
vector* 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