Quellcode durchsuchen

[array] Fixing Array

Vinicius Teshima vor 2 Jahren
Ursprung
Commit
ae0d5fb181
2 geänderte Dateien mit 58 neuen und 54 gelöschten Zeilen
  1. 5 5
      include/toolbox/array.h
  2. 53 49
      src/toolbox/array.c

+ 5 - 5
include/toolbox/array.h

@@ -2,7 +2,7 @@
 #define TOOLBOX_ARRAY_H
 
 #include "toolbox/vptr.h"
-#include "toolbox/return_codes.h"
+#include "toolbox/errno.h"
 
 #include <stdint.h>
 #include <stddef.h>
@@ -43,12 +43,12 @@ array_destroy(struct array **self);
 __attribute__((__pure__))
 size_t
 array_index_of(const struct array *self
-			   , const void *item);
+	       , const void *item);
 
 __attribute__((__pure__))
 _Bool
 array_contain(const struct array *self
-			  , const void *item);
+	      , const void *item);
 
 __attribute__((__pure__))
 size_t
@@ -56,11 +56,11 @@ array_count(const struct array *self, const void *item);
 
 RET_TYPE
 array_set(struct array *self, const size_t p_index
-		  , const void *item);
+	  , const void *item);
 
 RET_TYPE
 array_insert(struct array *self, const size_t p_index
-			 , const void *item);
+	     , const void *item);
 
 RET_TYPE
 array_append(struct array *self, const void *item);

+ 53 - 49
src/toolbox/array.c

@@ -1,35 +1,37 @@
 #include "toolbox/array.h"
-#include "toolbox/void_pointer.h"
-#include "toolbox/log.h"
-#include "toolbox/return_codes.h"
 
 #include <stdint.h>
 #include <stddef.h>
 #include <string.h>
 #include <stdlib.h>
 
-#define TEST_FOR_NULL(POINTER)											\
-	if ( POINTER == NULL )												\
-		{																\
-			LOG_ERROR("Null Pointer Not Expected");					\
-			toolbox_errno = RET_ERR_VALUE_NULL;							\
-			return toolbox_errno;										\
-		}
+#include "toolbox/void_pointer.h"
+#include "toolbox/log.h"
+#include "toolbox/errno.h"
 
-#define TEST_FOR_NULL_SR(POINTER, RET)									\
-	if ( POINTER == NULL )												\
-		{																\
-			LOG_ERROR("Null Pointer Not Expected");					\
-			toolbox_errno = RET_ERR_VALUE_NULL;							\
-			return RET;													\
-		}
+#define true ((unsigned char) 1L)
+#define false ((unsigned char) 0L)
+
+#define TEST_FOR_NULL(POINTER)						\
+	if ( POINTER == NULL ) {					\
+		LOG_ERROR("Null Pointer Not Expected");		\
+		toolbox_errno = RET_EVN;				\
+		return toolbox_errno;					\
+	}
+
+#define TEST_FOR_NULL_SR(POINTER, RET)					\
+	if ( POINTER == NULL ) {					\
+		LOG_ERROR("Null Pointer Not Expected");		\
+		toolbox_errno = RET_EVN;				\
+		return RET;						\
+	}
 
 struct array *
 array_create(size_t arr_size, size_t item_size) {
 	toolbox_errno = RET_OK;
 	struct array *self = calloc(1, sizeof(struct array));
 	if ( self == NULL ) {
-		toolbox_errno = RET_ERR_FAILED_MALLOC;
+		toolbox_errno = RET_EFM;
 		return NULL;
 	}
 	self->item_size = item_size;
@@ -37,7 +39,7 @@ array_create(size_t arr_size, size_t item_size) {
 	self->size = 0;
 	self->data = calloc(self->alloc_size, sizeof(void *));
 	if ( self->data == NULL ) {
-		toolbox_errno = RET_ERR_FAILED_MALLOC;
+		toolbox_errno = RET_EFM;
 		free(self);
 		return NULL;
 	}
@@ -68,7 +70,7 @@ array_destroy(struct array **self) {
 			return RET_OK;
 		}
 	}
-	toolbox_errno = RET_ERR_VALUE_NULL;
+	toolbox_errno = RET_EVN;
 	return toolbox_errno;
 }
 
@@ -76,15 +78,15 @@ size_t
 array_index_of(const struct array *self
 			   , const void *item) {
 	toolbox_errno = RET_OK;
-	TEST_FOR_NULL_SR(self, (size_t) -1);
-	TEST_FOR_NULL_SR(item, (size_t) -1);
+	TEST_FOR_NULL_SR(self, (size_t) -1)
+	TEST_FOR_NULL_SR(item, (size_t) -1)
 	for ( size_t i = 0; i < self->size; ++i ) {
 		if ( void_pointer_equal(self->data[i], self->item_size
 								, item, self->item_size) ) {
 			return i;
 		}
 	}
-	toolbox_errno = RET_ERR_ITEM_DOES_NOT_EXIST;
+	toolbox_errno = RET_EIDNE;
 	return (size_t) -1;
 }
 
@@ -95,11 +97,11 @@ array_contain(const struct array *self, const void *item) {
 	TEST_FOR_NULL_SR(item, 0);
 	for ( size_t i = 0; i < self->size; ++i ) {
 		if ( void_pointer_equal(self->data[i], self->item_size
-								, item, self->item_size) ) {
-			return 1;
+					, item, self->item_size) ) {
+			return true;
 		}
 	}
-	return 0;
+	return false;
 }
 
 size_t
@@ -124,7 +126,7 @@ array_set(struct array *self, const size_t p_index
 	TEST_FOR_NULL(self);
 	TEST_FOR_NULL(item);
 	if ( p_index >= self->size ) {
-		toolbox_errno = RET_ERR_VALUE_BIGGER_THAN_SIZE;
+		toolbox_errno = RET_EVBTS;
 		return toolbox_errno;
 	}
 	if ( self->data[p_index] != NULL ) {
@@ -135,13 +137,6 @@ array_set(struct array *self, const size_t p_index
 	return RET_OK;
 }
 
-static void
-mmemcpy(void *restrict dest, const void *restrict src, const size_t size) {
-	for ( size_t i = 0; i < size; ++i) {
-		*((uint8_t *) dest + i) = *((uint8_t *) src + i);
-	}
-}
-
 RET_TYPE
 array_insert(struct array *self, const size_t p_index
 			 , const void *item) {
@@ -149,7 +144,7 @@ array_insert(struct array *self, const size_t p_index
 	TEST_FOR_NULL(self);
 	TEST_FOR_NULL(item);
 	if ( p_index > self->size+1 ) {
-		toolbox_errno = RET_ERR_VALUE_BIGGER_THAN_SIZE;
+		toolbox_errno = RET_EVBTS;
 		return toolbox_errno;
 	}
 	++self->size;
@@ -192,7 +187,7 @@ array_delete(struct array *self, const size_t p_index) {
 	toolbox_errno = RET_OK;
 	TEST_FOR_NULL(self);
 	if ( p_index >= self->size ) {
-		toolbox_errno = RET_ERR_VALUE_BIGGER_THAN_SIZE;
+		toolbox_errno = RET_EVBTS;
 		return toolbox_errno;
 	}
 	--self->size;
@@ -213,29 +208,38 @@ array_remove(struct array *self, const void *item) {
 	toolbox_errno = RET_OK;
 	TEST_FOR_NULL(self);
 	TEST_FOR_NULL(item);
-	_Bool found = 0;
-	for ( size_t i = 0; i < self->size; ++i ) {
-		if ( found ) {
-			array_delete(self, i);
-			break;
-		} else if ( void_pointer_equal(self->data[i], self->item_size
-									   , item, self->item_size) ) {
-			found = 1;
+
+	for ( size_t z = 0; z < self->size; ++z ) {
+		if ( void_pointer_equal(self->data[z], self->item_size
+					, item, self->item_size) ) {
+			array_delete(self, z);
+			goto exit_found;
 		}
 	}
-	if ( ! found ) {
-		toolbox_errno = RET_ERR_ITEM_DOES_NOT_EXIST;
-		return toolbox_errno;
-	}
+
+	toolbox_errno = RET_EIDNE;
+	return RET_EIDNE;
+
+exit_found:
 	return RET_OK;
 }
 
 RET_TYPE
 array_for_each(struct array *self, void (*for_each)(void *item)) {
-	TEST_FOR_NULL(self);
 	toolbox_errno = RET_OK;
+
+	TEST_FOR_NULL(self);
+	TEST_FOR_NULL(for_each);
+
 	for ( size_t i = 0; i < self->size; ++i ) {
 		for_each(self->data[i]);
 	}
+
 	return RET_OK;
 }
+
+#undef TEST_FOR_NULL_SR
+#undef TEST_FOR_NULL
+
+#undef false
+#undef true