diff --git a/array.c b/array.c new file mode 100644 index 0000000..1540431 --- /dev/null +++ b/array.c @@ -0,0 +1,164 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 05 +*/ + +#include +#include + +#include "raise.h" +#include "array.h" +#include "new.h" + +typedef struct { + Container base; + Class *_type; + size_t _size; + Object **_tab; +} ArrayClass; + +typedef struct { + Iterator base; + ArrayClass *_array; + size_t _idx; +} ArrayIteratorClass; + +static void ArrayIterator_ctor( + ArrayIteratorClass *this, + va_list *args) +{ + this->_array = va_arg(*args, ArrayClass *); + this->_idx = va_arg(*args, int); +} + +static bool ArrayIterator_eq( + ArrayIteratorClass *this, + ArrayIteratorClass *other) +{ + return (this->_idx == other->_idx); +} + +static bool ArrayIterator_gt( + ArrayIteratorClass *this, + ArrayIteratorClass *other) +{ + return (this->_idx > other->_idx); +} + +static bool ArrayIterator_lt( + ArrayIteratorClass *this, + ArrayIteratorClass *other) +{ + return (this->_idx < other->_idx); +} + +static void ArrayIterator_incr(ArrayIteratorClass *this) +{ + this->_idx += 1; +} + +static Object *ArrayIterator_getval(ArrayIteratorClass *this) +{ + if (this->_idx >= this->_array->_size) + raise("Out of range"); + return (this->_array->_tab[this->_idx]); +} + +/* Fill this function for exercice 05 */ +static void ArrayIterator_setval(ArrayIteratorClass *this, ...) +{ +} + +static const ArrayIteratorClass ArrayIteratorDescr = { + { /* Iterator struct */ + { /* Class struct */ + .__size__ = sizeof(ArrayIteratorClass), + .__name__ = "ArrayIterator", + .__ctor__ = (ctor_t)&ArrayIterator_ctor, + .__dtor__ = NULL, + .__str__ = NULL, + .__add__ = NULL, + .__sub__ = NULL, + .__mul__ = NULL, + .__div__ = NULL, + .__eq__ = (binary_comparator_t)&ArrayIterator_eq, + .__gt__ = (binary_comparator_t)&ArrayIterator_gt, + .__lt__ = (binary_comparator_t)&ArrayIterator_lt, + }, + .__incr__ = (incr_t)&ArrayIterator_incr, + .__getval__ = (getval_t)&ArrayIterator_getval, + .__setval__ = (setval_t)&ArrayIterator_setval, + }, + ._array = NULL, + ._idx = 0 +}; + +static const Class *ArrayIterator = (const Class *)&ArrayIteratorDescr; + +/* Fill this function for exercice 05 */ +static void Array_ctor(ArrayClass *this, va_list *args) +{ +} + +static void Array_dtor(ArrayClass *this) +{ + for (unsigned int i = 0; i < this->_size; i++) + delete(this->_tab[i]); + free(this->_tab); +} + +static size_t Array_len(ArrayClass *this) +{ + return (this->_size); +} + +static Iterator *Array_begin(ArrayClass *this) +{ + return (new(ArrayIterator, this, 0)); +} + +static Iterator *Array_end(ArrayClass *this) +{ + return (new(ArrayIterator, this, this->_size)); +} + +/* Fill this function for exercice 05 */ +static Object *Array_getitem(ArrayClass *this, ...) +{ +} + +/* Fill this function for exercice 05 */ +static void Array_setitem(ArrayClass *this, ...) +{ +} + +static const ArrayClass _descr = { + { /* Container struct */ + { /* Class struct */ + .__size__ = sizeof(ArrayClass), + .__name__ = "Array", + .__ctor__ = (ctor_t)&Array_ctor, + .__dtor__ = (dtor_t)&Array_dtor, + .__str__ = NULL, + .__add__ = NULL, + .__sub__ = NULL, + .__mul__ = NULL, + .__div__ = NULL, + .__eq__ = NULL, + .__gt__ = NULL, + .__lt__ = NULL, + }, + .__len__ = (len_t)&Array_len, + .__begin__ = (iter_t)&Array_begin, + .__end__ = (iter_t)&Array_end, + .__getitem__ = (getitem_t)&Array_getitem, + .__setitem__ = (setitem_t)&Array_setitem, + }, + ._type = NULL, + ._size = 0, + ._tab = NULL +}; + +const Class *Array = (const Class *)&_descr; diff --git a/array.h b/array.h new file mode 100644 index 0000000..9cc0033 --- /dev/null +++ b/array.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 05 +*/ + +#pragma once + +#include "container.h" + +extern const Class *Array; diff --git a/char.h b/char.h new file mode 100644 index 0000000..1ddb2f7 --- /dev/null +++ b/char.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 04 +*/ + +#pragma once + +#include "object.h" + +extern const Class *Char; diff --git a/container.h b/container.h new file mode 100644 index 0000000..7e5b08b --- /dev/null +++ b/container.h @@ -0,0 +1,34 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 05 +*/ + +#pragma once + +#include "object.h" +#include "iterator.h" +#include "raise.h" + +typedef struct Container_s Container; + +typedef Iterator *(*iter_t)(Container *this); +typedef size_t (*len_t)(Container *this); +typedef Object *(*getitem_t)(Container *this, ...); +typedef void (*setitem_t)(Container *this, ...); + +struct Container_s { + Class base; + len_t __len__; + iter_t __begin__; + iter_t __end__; + getitem_t __getitem__; + setitem_t __setitem__; +}; + +#define len(c) ((Container *)c)->__len__(c) +#define begin(c) ((Container *)c)->__begin__(c) +#define end(c) ((Container *)c)->__end__(c) +#define getitem(c, ...) ((Container *)c)->__getitem__(c, __VA_ARGS__) +#define setitem(c, ...) ((Container *)c)->__setitem__(c, __VA_ARGS__) diff --git a/ecsls.toml b/ecsls.toml new file mode 100644 index 0000000..04134d1 --- /dev/null +++ b/ecsls.toml @@ -0,0 +1,2 @@ +[reports] +merge = "multiplier" diff --git a/ex01.c b/ex01.c new file mode 100644 index 0000000..5c502f6 --- /dev/null +++ b/ex01.c @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 01 +*/ + +#include "new.h" +#include "player.h" + +int main(void) +{ + Object *player = new(Player); + + delete(player); + return (0); +} diff --git a/ex02.c b/ex02.c new file mode 100644 index 0000000..8d8336f --- /dev/null +++ b/ex02.c @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercise 02 +*/ + +#include "new.h" +#include "point.h" +#include "vertex.h" +#include + +int main(void) +{ + Object *point = new(Point, 42, -42); + Object *vertex = new(Vertex, 0, 1, 2); + + printf("point = %s\\n", str(point)); + printf("vertex = %s\\n", str(vertex)); + delete(point); + delete(vertex); + return (0); +} diff --git a/ex03.c b/ex03.c new file mode 100644 index 0000000..f64a2a3 --- /dev/null +++ b/ex03.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 03 +*/ + +#include "new.h" +#include "point.h" +#include "vertex.h" +#include + +int main(void) +{ + Object *p1 = new(Point, 12, 13); + Object *p2 = new(Point, 2, 2); + Object *v1 = new(Vertex, 1, 2, 3); + Object *v2 = new(Vertex, 4, 5, 6); + + printf("%s + %s = %s\n", str(p1), str(p2), str(addition(p1, p2))); + printf("%s - %s = %s\n", str(p1), str(p2), str(subtraction(p1, p2))); + printf("%s + %s = %s\n", str(v1), str(v2), str(addition(v1, v2))); + printf("%s - %s = %s\n", str(v1), str(v2), str(subtraction(v1, v2))); + return (0); +} diff --git a/ex05.c b/ex05.c new file mode 100644 index 0000000..4e58732 --- /dev/null +++ b/ex05.c @@ -0,0 +1,30 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 05 +*/ + +#include "array.h" +#include "int.h" +#include "new.h" +#include + +int main(void) +{ + Object *array = new(Array, 10, Int, 0); + Object *it = begin(array); + Object *it_end = end(array); + + printf("array size: %zu\n", len(array)); + setitem(array, 5, 12); + setitem(array, 6, 13); + while (lt(it, it_end)) { + printf("%s\n", str(getval(it))); + incr(it); + } + delete(it); + delete(it_end); + delete(array); + return (0); +} diff --git a/float.h b/float.h new file mode 100644 index 0000000..11ddd6d --- /dev/null +++ b/float.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 04 +*/ + +#pragma once + +#include "object.h" + +extern const Class *Float; diff --git a/int.h b/int.h new file mode 100644 index 0000000..6ef99df --- /dev/null +++ b/int.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 04 +*/ + +#pragma once + +#include "object.h" + +extern const Class *Int; diff --git a/iterator.h b/iterator.h new file mode 100644 index 0000000..6833bf4 --- /dev/null +++ b/iterator.h @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 05 +*/ + +#pragma once + +#include "object.h" +#include "raise.h" + +typedef struct Iterator_s Iterator; + +typedef void (*incr_t)(Iterator *this); +typedef Object *(*getval_t)(Iterator *this); +typedef void (*setval_t)(Iterator *this, ...); + +struct Iterator_s { + Class base; + incr_t __incr__; + getval_t __getval__; + setval_t __setval__; +}; + +#define incr(it) ((Iterator *)it)->__incr__(it) +#define getval(it) ((Iterator *)it)->__getval__(it) +#define setval(it, ...) ((Iterator *)it)->__setval__(it, __VA_ARGS__) diff --git a/new.h b/new.h new file mode 100644 index 0000000..a9048e0 --- /dev/null +++ b/new.h @@ -0,0 +1,15 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 01 +*/ + +#pragma once + +#include "object.h" +#include + +Object *new(const Class *class, ...); +Object *va_new(const Class *class, va_list* ap); +void delete(Object *ptr); diff --git a/object.h b/object.h new file mode 100644 index 0000000..43f5092 --- /dev/null +++ b/object.h @@ -0,0 +1,45 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exerice 01 +*/ + +#pragma once + +#include "raise.h" +#include +#include +#include +#include + +typedef void Object; +typedef void (*ctor_t)(Object *this, va_list *args); +typedef void (*dtor_t)(Object *this); +typedef char *(*to_string_t)(Object *this); +typedef Object *(*binary_operator_t)(const Object *this, const Object *other); +typedef bool (*binary_comparator_t)(const Object *this, const Object *other); + +typedef struct { + const size_t __size__; // ex01 + const char *__name__; // ex01 + ctor_t __ctor__; // ex01 + dtor_t __dtor__; // ex01 + to_string_t __str__; // ex02 + binary_operator_t __add__; // ex03 + binary_operator_t __sub__; // ex03 + binary_operator_t __mul__; // ex04 + binary_operator_t __div__; // ex04 + binary_comparator_t __eq__; // ex04 + binary_comparator_t __gt__; // ex04 + binary_comparator_t __lt__; // ex04 +} Class; + +#define str(o) (((Class *)o)->__str__ != NULL ? ((Class *)o)->__str__(o) : strdup(((Class *)o)->__name__)) +#define addition(a, b) ((Class *)(a))->__add__((a), (b)) +#define subtraction(a, b) ((Class *)(a))->__sub__((a), (b)) +#define multiplication(a, b) ((Class *)(a))->__mul__((a), (b)) +#define division(a, b) ((Class *)(a))->__div__((a), (b)) +#define eq(a, b) ((Class *)(a))->__eq__((a), (b)) +#define gt(a, b) ((Class *)(a))->__gt__((a), (b)) +#define lt(a, b) ((Class *)(a))->__lt__((a), (b)) diff --git a/player.c b/player.c new file mode 100644 index 0000000..79069e5 --- /dev/null +++ b/player.c @@ -0,0 +1,53 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 01 +*/ + +#include "player.h" +#include +#include +#include + +typedef struct { + Class base; + char *identifier; + int power; +} PlayerClass; + +// You need to initialize internal resources at the start of the function +static void Player_ctor(PlayerClass *this, va_list *args) +{ + this->identifier = strdup("Kreog"); + this->power = rand() % 42; + printf("Player()\n"); +} + +// You need to release internal resources at the start of the function +static void Player_dtor(PlayerClass *this) +{ + free(this->identifier); + printf("~Player()\n"); +} + +static const PlayerClass _description = { + { /* Class struct */ + .__size__ = sizeof(PlayerClass), + .__name__ = "Player", + .__ctor__ = (ctor_t)&Player_ctor, + .__dtor__ = (dtor_t)&Player_dtor, + .__str__ = NULL, + .__add__ = NULL, + .__sub__ = NULL, + .__mul__ = NULL, + .__div__ = NULL, + .__eq__ = NULL, + .__gt__ = NULL, + .__lt__ = NULL + }, + .identifier = NULL, + .power = -1 +}; + +const Class *Player = (const Class *)&_description; diff --git a/player.h b/player.h new file mode 100644 index 0000000..b223d78 --- /dev/null +++ b/player.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 01 +*/ + +#pragma once + +# include "object.h" + +extern const Class *Player; diff --git a/point.c b/point.c new file mode 100644 index 0000000..03d4d71 --- /dev/null +++ b/point.c @@ -0,0 +1,47 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 02 +*/ + +#include "point.h" +#include + +typedef struct { + Class base; + int x, y; +} PointClass; + +/* Fill this function for exercice 02 */ +static void Point_ctor(PointClass *this, va_list *args) +{ +} + +/* Fill this function for exercice 02 */ +static void Point_dtor(PointClass *this) +{ +} + +// Create additional functions here + +static const PointClass _description = { + { /* Class struct */ + .__size__ = sizeof(PointClass), + .__name__ = "Point", + .__ctor__ = (ctor_t)&Point_ctor, + .__dtor__ = (dtor_t)&Point_dtor, + .__str__ = NULL, /* Implement this method for exercice 02 */ + .__add__ = NULL, /* Implement this method for exercice 03 */ + .__sub__ = NULL, /* Implement this method for exercice 03 */ + .__mul__ = NULL, + .__div__ = NULL, + .__eq__ = NULL, + .__gt__ = NULL, + .__lt__ = NULL + }, + .x = 0, + .y = 0 +}; + +const Class *Point = (const Class *)&_description; diff --git a/point.h b/point.h new file mode 100644 index 0000000..8e91a27 --- /dev/null +++ b/point.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 02 +*/ + +#pragma once + +#include "object.h" + +extern const Class *Point; diff --git a/raise.h b/raise.h new file mode 100644 index 0000000..c9a56fc --- /dev/null +++ b/raise.h @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 01 +*/ + +#pragma once + +#include +#include + +#define raise(msg) \ +do { \ + fprintf(stderr, "%s: %u: %s\n", __FILE__, __LINE__, msg); \ + abort(); \ +} while (0) diff --git a/vertex.h b/vertex.h new file mode 100644 index 0000000..c6701fb --- /dev/null +++ b/vertex.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2021 +** Paradigms Seminar +** File description: +** Exercice 02 +*/ + +#pragma once + +#include "object.h" + +extern const Class *Vertex;