From 8b1dab02140be91373b0a8de6c4578420743d57f Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Wed, 6 Dec 2023 17:41:22 +0800 Subject: [PATCH] cii: Add ring interface --- cii/CMakeLists.txt | 2 + cii/include/cii/ring.h | 103 +++++++++++++++++++++++++++++++++++++++++ cii/src/ring.c | 5 ++ 3 files changed, 110 insertions(+) create mode 100644 cii/include/cii/ring.h create mode 100644 cii/src/ring.c diff --git a/cii/CMakeLists.txt b/cii/CMakeLists.txt index 69b989ca..fefe36d5 100644 --- a/cii/CMakeLists.txt +++ b/cii/CMakeLists.txt @@ -14,6 +14,7 @@ set(SOURCE_FILES src/hash.c src/list.c src/mem.c + src/ring.c src/seq.c src/set.c src/table.c @@ -28,6 +29,7 @@ set(HEADER_FILES include/cii/hash.h include/cii/list.h include/cii/mem.h + include/cii/ring.h include/cii/seq.h include/cii/set.h include/cii/table.h diff --git a/cii/include/cii/ring.h b/cii/include/cii/ring.h new file mode 100644 index 00000000..82e1b246 --- /dev/null +++ b/cii/include/cii/ring.h @@ -0,0 +1,103 @@ +// Copyright (c) 2023 Xu Shaohua . All rights reserved. +// Use of this source is governed by GNU General Public License +// that can be found in the LICENSE file. + +#ifndef CII_RING_H_ +#define CII_RING_H_ + +#include + +typedef struct ring_s ring_t; + +/** + * Creates and returns an empty ring. + * + * @return + */ +extern ring_t* ring_new(void); + +/** + * Creates and returns a ring whose values are initialized to its non-null pointer + * arguments. + * + * @param x + * @return + */ +extern ring_t* ring_ring(void* x, ...); + +/** + * Deallocates and clear a ring. + * @param ring + */ +extern void ring_free(ring_t** ring); + +/** + * Returns the number of values in a ring. + * + * @param ring + * @return + */ +extern size_t ring_length(ring_t* ring); + +/** + * Get the value at |index| in a ring. + * + * It is a checked runtime error if index is out of range. + * + * @param ring + * @param index + * @return + */ +extern void* ring_get(ring_t* ring, size_t index); + +/** + * Put a new value at |index| and returns old value. + * + * @param ring + * @param index + * @param value + * @return + */ +extern void* ring_put(ring_t* ring, size_t index, void* value); + +/** + * Add |value| at anywhere in a ring and returns |value|. + * + * @param ring + * @param pos + * @param value + * @return + */ +extern void* ring_add(ring_t* ring, int pos, void* value); + +/** + * Add a value to lower end of ring. + * + * @param ring + * @param value + * @return + */ +extern void* ring_add_low(ring_t* ring, void* value); + +/** + * Add a value to higher end of ring. + * @param ring + * @param value + * @return + */ +extern void* ring_add_high(ring_t* ring, void* value); + +/** + * Remove value at |index| and returns that value. + * + * @param ring + * @param index + * @return + */ +extern void* ring_remove(ring_t* ring, size_t index); + +extern void* ring_remove_low(ring_t* ring); + +extern void* ring_remove_high(ring_t* ring); + +#endif // CII_RING_H_ diff --git a/cii/src/ring.c b/cii/src/ring.c new file mode 100644 index 00000000..62a8725c --- /dev/null +++ b/cii/src/ring.c @@ -0,0 +1,5 @@ +// Copyright (c) 2023 Xu Shaohua . All rights reserved. +// Use of this source is governed by GNU General Public License +// that can be found in the LICENSE file. + +#include "cii/ring.h" \ No newline at end of file