Properly modularize functions
This commit is contained in:
parent
b9abe4d28f
commit
f87049589d
10 changed files with 88 additions and 50 deletions
|
@ -1,35 +1,9 @@
|
|||
#ifndef __ORDINARY_H_
|
||||
#define __ORDINARY_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct ordinary_node {
|
||||
struct ordinary_node *prev;
|
||||
struct ordinary_node *next;
|
||||
void *val;
|
||||
};
|
||||
|
||||
struct ordinary_list {
|
||||
struct ordinary_node *head;
|
||||
struct ordinary_node *tail;
|
||||
uint32_t count;
|
||||
uint32_t limit;
|
||||
};
|
||||
|
||||
typedef uint8_t (*callback)(struct ordinary_node *node, uint32_t idx);
|
||||
|
||||
void ordinary_list_new(struct ordinary_list *list, uint32_t limit);
|
||||
void ordinary_list_delete(struct ordinary_list *list);
|
||||
|
||||
uint8_t ordinary_list_empty(struct ordinary_list *list);
|
||||
uint8_t ordinary_list_full(struct ordinary_list *list);
|
||||
struct ordinary_node *ordinary_list_at(struct ordinary_list *list, uint32_t idx);
|
||||
struct ordinary_node *ordinary_list_find(struct ordinary_list *list, callback cb);
|
||||
uint8_t ordinary_list_for(struct ordinary_list *list, callback cb);
|
||||
|
||||
struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val);
|
||||
void ordinary_list_move(struct ordinary_list *dst, struct ordinary_list *src, struct ordinary_node *node);
|
||||
struct ordinary_node *ordinary_list_pop(struct ordinary_list *list);
|
||||
void ordinary_list_drop(struct ordinary_list *list, struct ordinary_node *node);
|
||||
#include <ordinary/build.h>
|
||||
#include <ordinary/idx.h>
|
||||
#include <ordinary/inspect.h>
|
||||
#include <ordinary/maint.h>
|
||||
|
||||
#endif
|
||||
|
|
9
include/ordinary/build.h
Normal file
9
include/ordinary/build.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef __ORDINARY_BUILD_H_
|
||||
#define __ORDINARY_BUILD_H_
|
||||
|
||||
#include <ordinary/list.h>
|
||||
|
||||
void ordinary_list_new(struct ordinary_list *list, uint32_t limit);
|
||||
void ordinary_list_delete(struct ordinary_list *list);
|
||||
|
||||
#endif
|
11
include/ordinary/idx.h
Normal file
11
include/ordinary/idx.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef __ORDINARY_IDX_H_
|
||||
#define __ORDINARY_IDX_H_
|
||||
|
||||
#include <ordinary/list.h>
|
||||
|
||||
typedef uint8_t (*ordinary_cb)(struct ordinary_node *node, uint32_t idx);
|
||||
|
||||
struct ordinary_node *ordinary_list_find(struct ordinary_list *list, ordinary_cb cb);
|
||||
uint8_t ordinary_list_for(struct ordinary_list *list, ordinary_cb cb);
|
||||
|
||||
#endif
|
10
include/ordinary/inspect.h
Normal file
10
include/ordinary/inspect.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef __ORDINARY_INSPECT_H_
|
||||
#define __ORDINARY_INSPECT_H_
|
||||
|
||||
#include <ordinary/list.h>
|
||||
|
||||
uint8_t ordinary_list_empty(struct ordinary_list *list);
|
||||
uint8_t ordinary_list_full(struct ordinary_list *list);
|
||||
struct ordinary_node *ordinary_list_at(struct ordinary_list *list, uint32_t idx);
|
||||
|
||||
#endif
|
19
include/ordinary/list.h
Normal file
19
include/ordinary/list.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
#ifndef __ORDINARY_LIST_H_
|
||||
#define __ORDINARY_LIST_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct ordinary_node {
|
||||
struct ordinary_node *prev;
|
||||
struct ordinary_node *next;
|
||||
void *val;
|
||||
};
|
||||
|
||||
struct ordinary_list {
|
||||
struct ordinary_node *head;
|
||||
struct ordinary_node *tail;
|
||||
uint32_t count;
|
||||
uint32_t limit;
|
||||
};
|
||||
|
||||
#endif
|
11
include/ordinary/maint.h
Normal file
11
include/ordinary/maint.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef __ORDINARY_MAINT_H_
|
||||
#define __ORDINARY_MAINT_H_
|
||||
|
||||
#include <ordinary/list.h>
|
||||
|
||||
struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val);
|
||||
void ordinary_list_move(struct ordinary_list *dst, struct ordinary_list *src, struct ordinary_node *node);
|
||||
struct ordinary_node *ordinary_list_pop(struct ordinary_list *list);
|
||||
void ordinary_list_drop(struct ordinary_list *list, struct ordinary_node *node);
|
||||
|
||||
#endif
|
|
@ -1,4 +1,4 @@
|
|||
#include <ordinary.h>
|
||||
#include <ordinary/build.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void ordinary_list_new(struct ordinary_list *list, uint32_t limit) {
|
||||
|
|
21
src/idx.c
21
src/idx.c
|
@ -1,20 +1,7 @@
|
|||
#include <ordinary.h>
|
||||
#include <ordinary/idx.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct ordinary_node *ordinary_list_at(struct ordinary_list *list, uint32_t idx) {
|
||||
struct ordinary_node *node = list->head;
|
||||
|
||||
for(; node; node = node->next) {
|
||||
idx -= 1;
|
||||
|
||||
if(!idx) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ordinary_node *ordinary_list_find(struct ordinary_list *list, callback cb) {
|
||||
struct ordinary_node *ordinary_list_find(struct ordinary_list *list, ordinary_cb cb) {
|
||||
struct ordinary_node *node = list->head;
|
||||
uint32_t idx = 0;
|
||||
|
||||
|
@ -31,7 +18,7 @@ struct ordinary_node *ordinary_list_find(struct ordinary_list *list, callback cb
|
|||
return NULL;
|
||||
}
|
||||
|
||||
uint8_t ordinary_list_for(struct ordinary_list *list, callback cb) {
|
||||
uint8_t ordinary_list_for(struct ordinary_list *list, ordinary_cb cb) {
|
||||
struct ordinary_node *node = list->head;
|
||||
uint32_t idx = 0, res = 0;
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <ordinary.h>
|
||||
#include <ordinary/inspect.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
uint8_t ordinary_list_empty(struct ordinary_list *list) {
|
||||
return !list->head;
|
||||
|
@ -7,3 +8,17 @@ uint8_t ordinary_list_empty(struct ordinary_list *list) {
|
|||
uint8_t ordinary_list_full(struct ordinary_list *list) {
|
||||
return !list->limit || list->count == list->limit;
|
||||
}
|
||||
|
||||
struct ordinary_node *ordinary_list_at(struct ordinary_list *list, uint32_t idx) {
|
||||
struct ordinary_node *node = list->head;
|
||||
|
||||
for(; node; node = node->next) {
|
||||
idx -= 1;
|
||||
|
||||
if(!idx) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
#include <ordinary.h>
|
||||
#include <ordinary/inspect.h>
|
||||
#include <ordinary/maint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void ordinary_list_insert(struct ordinary_list *list, struct ordinary_node *node) {
|
||||
if(!list->head) {
|
||||
|
|
Loading…
Reference in a new issue