rcx

library of miscellaneous bits of C code
git clone git://git.rr3.xyz/rcx
Log | Files | Refs | README | LICENSE

commit 2c036d36487e5d9b49e4560a4d1ea0c43f787431
parent 2d7c0bf2ccc361d6072b7038db4fee5f06eac6ad
Author: Robert Russell <robertrussell.72001@gmail.com>
Date:   Wed,  7 Sep 2022 20:18:25 -0700

Add peek method to vectors

If we have push and pop, we ought to have peek as well, so that
vectors can be used as stacks easily.

Diffstat:
Minc/rcx/vector.h | 6+++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/inc/rcx/vector.h b/inc/rcx/vector.h @@ -33,7 +33,8 @@ R_VECTOR_STATIC UNUSED int R_VECTOR_METHOD(reserve,##__VA_ARGS__)(T **v, usize n R_VECTOR_STATIC UNUSED int R_VECTOR_METHOD(ins,##__VA_ARGS__)(T **v, usize i, T e); \ R_VECTOR_STATIC UNUSED int R_VECTOR_METHOD(push,##__VA_ARGS__)(T **v, T e); \ R_VECTOR_STATIC UNUSED T R_VECTOR_METHOD(del,##__VA_ARGS__)(T **v, usize i); \ -R_VECTOR_STATIC UNUSED T R_VECTOR_METHOD(pop,##__VA_ARGS__)(T **v); +R_VECTOR_STATIC UNUSED T R_VECTOR_METHOD(pop,##__VA_ARGS__)(T **v); \ +R_VECTOR_STATIC UNUSED T R_VECTOR_METHOD(peek,##__VA_ARGS__)(T **v); #define R_VECTOR_DEFINE(T, ...)\ void R_VECTOR_METHOD(free,##__VA_ARGS__)(T **v) { \ @@ -105,6 +106,9 @@ T R_VECTOR_METHOD(del,##__VA_ARGS__)(T **v, usize i) { \ } \ T R_VECTOR_METHOD(pop,##__VA_ARGS__)(T **v) { \ return R_VECTOR_METHOD(del,##__VA_ARGS__)(v, R_VECTOR_HDR_(*v)->len - 1); \ +} \ +T R_VECTOR_METHOD(peek,##__VA_ARGS__)(T **v) { \ + return (*v)[R_VECTOR_HDR_(*v)->len - 1]; \ } /* TODO?