Skip to content

Commit

Permalink
Add files from archive
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbu committed Jan 9, 2024
1 parent 75c637b commit c9e7091
Show file tree
Hide file tree
Showing 20 changed files with 584 additions and 0 deletions.
164 changes: 164 additions & 0 deletions array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
** EPITECH PROJECT, 2021
** Paradigms Seminar
** File description:
** Exercice 05
*/

#include <stdarg.h>
#include <stdlib.h>

#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;
12 changes: 12 additions & 0 deletions array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
** EPITECH PROJECT, 2021
** Paradigms Seminar
** File description:
** Exercice 05
*/

#pragma once

#include "container.h"

extern const Class *Array;
12 changes: 12 additions & 0 deletions char.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
** EPITECH PROJECT, 2021
** Paradigms Seminar
** File description:
** Exercice 04
*/

#pragma once

#include "object.h"

extern const Class *Char;
34 changes: 34 additions & 0 deletions container.h
Original file line number Diff line number Diff line change
@@ -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__)
2 changes: 2 additions & 0 deletions ecsls.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[reports]
merge = "multiplier"
17 changes: 17 additions & 0 deletions ex01.c
Original file line number Diff line number Diff line change
@@ -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);
}
23 changes: 23 additions & 0 deletions ex02.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
** EPITECH PROJECT, 2021
** Paradigms Seminar
** File description:
** Exercise 02
*/

#include "new.h"
#include "point.h"
#include "vertex.h"
#include <stdio.h>

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);
}
25 changes: 25 additions & 0 deletions ex03.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
** EPITECH PROJECT, 2021
** Paradigms Seminar
** File description:
** Exercice 03
*/

#include "new.h"
#include "point.h"
#include "vertex.h"
#include <stdio.h>

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);
}
30 changes: 30 additions & 0 deletions ex05.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
** EPITECH PROJECT, 2021
** Paradigms Seminar
** File description:
** Exercice 05
*/

#include "array.h"
#include "int.h"
#include "new.h"
#include <stdio.h>

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);
}
12 changes: 12 additions & 0 deletions float.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
** EPITECH PROJECT, 2021
** Paradigms Seminar
** File description:
** Exercice 04
*/

#pragma once

#include "object.h"

extern const Class *Float;
12 changes: 12 additions & 0 deletions int.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
** EPITECH PROJECT, 2021
** Paradigms Seminar
** File description:
** Exercice 04
*/

#pragma once

#include "object.h"

extern const Class *Int;
28 changes: 28 additions & 0 deletions iterator.h
Original file line number Diff line number Diff line change
@@ -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__)
15 changes: 15 additions & 0 deletions new.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
** EPITECH PROJECT, 2021
** Paradigms Seminar
** File description:
** Exercice 01
*/

#pragma once

#include "object.h"
#include <stdarg.h>

Object *new(const Class *class, ...);
Object *va_new(const Class *class, va_list* ap);
void delete(Object *ptr);
Loading

0 comments on commit c9e7091

Please sign in to comment.