summaryrefslogtreecommitdiffstats
path: root/libdimension/dimension/array.h
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension/dimension/array.h')
-rw-r--r--libdimension/dimension/array.h22
1 files changed, 21 insertions, 1 deletions
diff --git a/libdimension/dimension/array.h b/libdimension/dimension/array.h
index e3dc7b4..afd5e16 100644
--- a/libdimension/dimension/array.h
+++ b/libdimension/dimension/array.h
@@ -100,7 +100,21 @@ dmnsn_array_set(dmnsn_array *array, size_t i, const void *obj)
memcpy((char *)array->ptr + array->obj_size*i, obj, array->obj_size);
}
-/* Element access */
+/* First element */
+DMNSN_INLINE void *
+dmnsn_array_first(const dmnsn_array *array)
+{
+ return array->ptr;
+}
+
+/* Last element */
+DMNSN_INLINE void *
+dmnsn_array_last(const dmnsn_array *array)
+{
+ return (char *)array->ptr + array->obj_size*(array->length - 1);
+}
+
+/* Arbitrary element access */
DMNSN_INLINE void *
dmnsn_array_at(const dmnsn_array *array, size_t i)
{
@@ -156,4 +170,10 @@ dmnsn_array_remove(dmnsn_array *array, size_t i)
dmnsn_array_resize(array, size - 1);
}
+/* Macro to shorten array iteration */
+#define DMNSN_ARRAY_FOREACH(type, i, array) \
+ for (type i = dmnsn_array_first(array); \
+ i <= (type)dmnsn_array_last(array); \
+ ++i)
+
#endif /* DIMENSION_ARRAY_H */