Skip to content

Commit

Permalink
Fix coding style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbu committed Jan 10, 2024
1 parent 9361aad commit ee1d8e2
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 92 deletions.
131 changes: 60 additions & 71 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,139 +8,128 @@
#include <stdarg.h>
#include <stdlib.h>

#include "object.h"
#include "raise.h"
#include "array.h"
#include "new.h"
#include "rush.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,
static void array_iter_ctor(
array_iter_class_t *this,
va_list *args)
{
this->_array = va_arg(*args, ArrayClass *);
this->_array = va_arg(*args, array_class_t *);
this->_idx = va_arg(*args, int);
}

static bool ArrayIterator_eq(
ArrayIteratorClass *this,
ArrayIteratorClass *other)
static bool array_iter_eq(
array_iter_class_t *this,
array_iter_class_t *other)
{
return (this->_idx == other->_idx);
}

static bool ArrayIterator_gt(
ArrayIteratorClass *this,
ArrayIteratorClass *other)
static bool array_iter_gt(
array_iter_class_t *this,
array_iter_class_t *other)
{
return (this->_idx > other->_idx);
}

static bool ArrayIterator_lt(
ArrayIteratorClass *this,
ArrayIteratorClass *other)
static bool array_iter_lt(
array_iter_class_t *this,
array_iter_class_t *other)
{
return (this->_idx < other->_idx);
}

static void ArrayIterator_incr(ArrayIteratorClass *this)
static void array_iter_incr(array_iter_class_t *this)
{
this->_idx += 1;
}

static Object *ArrayIterator_getval(ArrayIteratorClass *this)
static object_t *array_iter_getval(array_iter_class_t *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 void array_iter_setval(array_iter_class_t *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_ctor(array_class_t *this, va_list *args)
{
}

static void Array_dtor(ArrayClass *this)
static void array_dtor(array_class_t *this)
{
for (unsigned int i = 0; i < this->_size; i++)
delete(this->_tab[i]);
free(this->_tab);
}

static size_t Array_len(ArrayClass *this)
static size_t array_len(array_class_t *this)
{
return (this->_size);
}

static Iterator *Array_begin(ArrayClass *this)
static iterator_t *array_begin(array_class_t *this)
{
return (new(ArrayIterator, this, 0));
}

static Iterator *Array_end(ArrayClass *this)
static iterator_t *array_end(array_class_t *this)
{
return (new(ArrayIterator, this, this->_size));
}

/* Fill this function for exercice 05 */
static Object *Array_getitem(ArrayClass *this, ...)
static object_t *array_getitem(array_class_t *this, ...)
{
}

/* Fill this function for exercice 05 */
static void Array_setitem(ArrayClass *this, ...)
static void array_setitem(array_class_t *this, ...)
{
}

static const ArrayClass _descr = {
static const array_iter_class_t ArrayIteratorDescr = {
{ /* Iterator struct */
{ /* Class struct */
.__size__ = sizeof(array_iter_class_t),
.__name__ = "ArrayIterator",
.__ctor__ = (ctor_t)&array_iter_ctor,
.__dtor__ = NULL,
.__str__ = NULL,
.__add__ = NULL,
.__sub__ = NULL,
.__mul__ = NULL,
.__div__ = NULL,
.__eq__ = (binary_comparator_t)&array_iter_eq,
.__gt__ = (binary_comparator_t)&array_iter_gt,
.__lt__ = (binary_comparator_t)&array_iter_lt,
},
.__incr__ = (incr_t)&array_iter_incr,
.__getval__ = (getval_t)&array_iter_getval,
.__setval__ = (setval_t)&array_iter_setval,
},
._array = NULL,
._idx = 0
};

static const Class *ArrayIterator = (const Class *)&ArrayIteratorDescr;

static const array_class_t _descr = {
{ /* Container struct */
{ /* Class struct */
.__size__ = sizeof(ArrayClass),
.__size__ = sizeof(array_class_t),
.__name__ = "Array",
.__ctor__ = (ctor_t)&Array_ctor,
.__dtor__ = (dtor_t)&Array_dtor,
.__ctor__ = (ctor_t)&array_ctor,
.__dtor__ = (dtor_t)&array_dtor,
.__str__ = NULL,
.__add__ = NULL,
.__sub__ = NULL,
Expand All @@ -150,15 +139,15 @@ static const ArrayClass _descr = {
.__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,
.__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;
const class_t *Array = (const class_t *)&_descr;
2 changes: 1 addition & 1 deletion char.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ static const char_class_t _description = {
.v = 0
};

const Class *Char = (const Class *)&_description;
const class_t *Char = (const class_t *)&_description;
2 changes: 1 addition & 1 deletion float.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ static const float_class_t _description = {
.v = 0
};

const Class *Float = (const Class *)&_description;
const class_t *Float = (const class_t *)&_description;
2 changes: 1 addition & 1 deletion int.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ static const int_class_t _description = {
.v = 0
};

const Class *Int = (const Class *)&_description;
const class_t *Int = (const class_t *)&_description;
22 changes: 12 additions & 10 deletions new.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#include "raise.h"
#include <stdarg.h>
#include "new.h"
#include "rush.h"

Object *va_new(const Class *class, va_list *ap)
object_t *va_new(const class_t *class, va_list *ap)
{
Class *new_obj = NULL;
class_t *new_obj = NULL;

if (!class)
raise("Null pointer passed");
Expand All @@ -21,13 +22,13 @@ Object *va_new(const Class *class, va_list *ap)
memcpy(new_obj, class, class->__size__);
if (class->__ctor__)
class->__ctor__(new_obj, ap);
return (Object *)new_obj;
return (object_t *)new_obj;
}

Object *new(const Class *class, ...)
object_t *new(const class_t *class, ...)
{
va_list ap;
Object *new_obj = NULL;
object_t *new_obj = NULL;

if (!class)
raise("Null pointer passed");
Expand All @@ -37,10 +38,11 @@ Object *new(const Class *class, ...)
return new_obj;
}

void delete(Object *ptr)
void delete(object_t *ptr)
{
if ((Class *)ptr && ((Class *)ptr)->__dtor__)
((Class *)ptr)->__dtor__(ptr);
if (ptr)
free(ptr);
if (!ptr)
return;
if (((class_t *)ptr)->__dtor__)
((class_t *)ptr)->__dtor__(ptr);
free(ptr);
}
2 changes: 1 addition & 1 deletion point.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ static const point_class_t _description = {
.y = 0
};

const Class *Point = (const Class *)&_description;
const class_t *Point = (const class_t *)&_description;
31 changes: 25 additions & 6 deletions rush.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,51 @@

#define _GNU_SOURCE

#include "container.h"
#include "iterator.h"
#include "point.h"

typedef Object object_t;
typedef Iterator iterator_t;
typedef Class class_t;
typedef Container container_t;

typedef struct {
Class base;
class_t base;
int x, y;
} point_class_t;


typedef struct {
Class base;
class_t base;
int x, y, z;
} vertex_class_t;

typedef struct {
Class base;
class_t base;
int v;
} int_class_t;

typedef struct {
Class base;
class_t base;
float v;
} float_class_t;

typedef struct {
Class base;
class_t base;
char v;
} char_class_t;

typedef struct {
container_t base;
class_t *_type;
size_t _size;
object_t **_tab;
} array_class_t;

typedef struct {
iterator_t base;
array_class_t *_array;
size_t _idx;
} array_iter_class_t;

#endif /* !RUSH2_H_ */
2 changes: 1 addition & 1 deletion vertex.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ static const vertex_class_t _description = {
.z = 0,
};

const Class *Vertex = (const Class *)&_description;
const class_t *Vertex = (const class_t *)&_description;

0 comments on commit ee1d8e2

Please sign in to comment.