diff options
author | Tavian Barnes <tavianator@gmail.com> | 2010-04-29 16:53:52 -0600 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2010-04-30 09:52:34 -0600 |
commit | 5fa471d6842b46ca7c20ebc454065cdaf7488bdb (patch) | |
tree | 7a56e30232165fbffd8bbf993373548263205f27 /libdimension/dimension/array.h | |
parent | aeaa9a4e80244a5c66b1afe7e6ee1bcf48e70766 (diff) | |
download | dimension-5fa471d6842b46ca7c20ebc454065cdaf7488bdb.tar.xz |
Use dmnsn_assert() for array and list range checks.
Diffstat (limited to 'libdimension/dimension/array.h')
-rw-r--r-- | libdimension/dimension/array.h | 24 |
1 files changed, 5 insertions, 19 deletions
diff --git a/libdimension/dimension/array.h b/libdimension/dimension/array.h index e383ece..e3dc7b4 100644 --- a/libdimension/dimension/array.h +++ b/libdimension/dimension/array.h @@ -84,10 +84,7 @@ dmnsn_array_resize(dmnsn_array *array, size_t length) DMNSN_INLINE void dmnsn_array_get(const dmnsn_array *array, size_t i, void *obj) { - if (i >= dmnsn_array_size(array)) { - /* Range check failed */ - dmnsn_error(DMNSN_SEVERITY_HIGH, "Array index out of bounds."); - } + dmnsn_assert(i < dmnsn_array_size(array), "Array index out of bounds."); memcpy(obj, (char *)array->ptr + array->obj_size*i, array->obj_size); } @@ -107,10 +104,7 @@ dmnsn_array_set(dmnsn_array *array, size_t i, const void *obj) DMNSN_INLINE void * dmnsn_array_at(const dmnsn_array *array, size_t i) { - if (i >= dmnsn_array_size(array)) { - /* Resize if i is out of range */ - dmnsn_error(DMNSN_SEVERITY_HIGH, "Array index out of bounds."); - } + dmnsn_assert(i < dmnsn_array_size(array), "Array index out of bounds."); return (char *)array->ptr + array->obj_size*i; } @@ -126,11 +120,7 @@ DMNSN_INLINE void dmnsn_array_pop(dmnsn_array *array, void *obj) { size_t size = dmnsn_array_size(array); - if (size <= 0) { - /* Range check failed */ - dmnsn_error(DMNSN_SEVERITY_HIGH, "Array is empty."); - } - + dmnsn_assert(size > 0, "Array is empty."); dmnsn_array_get(array, size - 1, obj); /* Copy the object */ dmnsn_array_resize(array, size - 1); /* Shrink the array */ } @@ -157,15 +147,11 @@ DMNSN_INLINE void dmnsn_array_remove(dmnsn_array *array, size_t i) { size_t size = dmnsn_array_size(array); - if (i >= size) { - /* Range check failed */ - dmnsn_error(DMNSN_SEVERITY_HIGH, "Array index out of bounds."); - } - + dmnsn_assert(i < size, "Array index out of bounds."); /* Move the array elements after `i' 1 to the left */ memmove((char *)array->ptr + array->obj_size*i, (char *)array->ptr + array->obj_size*(i + 1), - array->obj_size*(size - i)); + array->obj_size*(size - i - 1)); /* Decrease the size by 1 */ dmnsn_array_resize(array, size - 1); } |