From 7bc2fb906b41960eba4391d7dbf7526ea02d5afd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 19 Apr 2017 09:33:30 +0200 Subject: [PATCH] array: implement array_truncate --- array.c | 8 ++++++++ array.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/array.c b/array.c index 46ea9e0..8328b06 100644 --- a/array.c +++ b/array.c @@ -127,3 +127,11 @@ size_t array_length(Array *arr) { size_t array_capacity(Array *arr) { return arr->count; } + +bool array_truncate(Array *arr, size_t len) { + if (len <= arr->len) { + arr->len = len; + return true; + } + return false; +} diff --git a/array.h b/array.h index 21036a9..b369d68 100644 --- a/array.h +++ b/array.h @@ -56,5 +56,7 @@ bool array_remove(Array*, size_t idx); size_t array_length(Array*); /* return the number of elements which can be stored without enlarging the array */ size_t array_capacity(Array*); +/* remove all elements at index >= length, keep allocated memory */ +bool array_truncate(Array*, size_t length); #endif -- 2.11.4.GIT