Skip to content

Commit

Permalink
cii: Add ring interface
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Dec 6, 2023
1 parent ea939c6 commit 8b1dab0
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cii/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
103 changes: 103 additions & 0 deletions cii/include/cii/ring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) 2023 Xu Shaohua <[email protected]>. 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 <stddef.h>

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_
5 changes: 5 additions & 0 deletions cii/src/ring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) 2023 Xu Shaohua <[email protected]>. 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"

0 comments on commit 8b1dab0

Please sign in to comment.