* Added few coding style rules

* Added btree_optimize and btree_hittest() incomplete functions
  - Just a PoC of the idea..with few test
This commit is contained in:
pancake 2009-04-06 10:57:37 +00:00
parent a976816e30
commit 5116358ce4
4 changed files with 119 additions and 49 deletions

34
doc/syntax Normal file
View File

@ -0,0 +1,34 @@
Syntax coding style guidelines
==============================
I will try to not being a boring document like most of already available
coding style guidelines ;) Here'r some rules:
* Do not bypass 78 columns
* Use tabs instead of space based indentation
- The code should be smart enought to do not bypass 78 columns
using 5 space indentation.
* function opens brackets at next line
* do/for/if/while open brackets at same line
* Commas and keywords should be followed by a space. f.ex:
if (blabl)
foo(one, two);
* Do not use C99 variable declaration
- This way we reduce the number of local variables per function
and it's easier to find which variables are used, where and so on.
* Comments should be smart. Function names should be enought explicit
to not require a comment to explain what it does. If this is not
possible at all, we can still use a comment. But it is a bad idea
to relay on comment to make the code readable.
* Use 'R_API' define to mark exportable methods
VIM syntax configuration:
-------------------------
TODO:

View File

@ -19,7 +19,7 @@ R_API struct btree_node *btree_remove(struct btree_node *p, BTREE_DEL(del));
R_API void *btree_search(struct btree_node *proot, void *x, BTREE_CMP(cmp), int parent);
R_API int btree_del(struct btree_node *proot, void *x, BTREE_CMP(cmp), BTREE_DEL(del));
R_API void *btree_get(struct btree_node *proot, void *x, BTREE_CMP(cmp));
R_API void btree_insert(struct btree_node *p,struct btree_node **T, BTREE_CMP(cmp));
R_API void btree_insert(struct btree_node **T, struct btree_node *p, BTREE_CMP(cmp));
R_API void btree_add(struct btree_node **T, void *e, BTREE_CMP(cmp));
R_API void btree_cleartree(struct btree_node *proot, BTREE_DEL(del));

View File

@ -12,7 +12,7 @@
*/
struct list_head {
struct list_head *next, *prev;
struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
@ -35,10 +35,10 @@ static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
/**
@ -51,7 +51,7 @@ static inline void __list_add(struct list_head *new,
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
__list_add(new, head, head->next);
}
/**
@ -64,7 +64,7 @@ static inline void list_add(struct list_head *new, struct list_head *head)
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
__list_add(new, head->prev, head);
}
/*
@ -76,8 +76,8 @@ static inline void list_add_tail(struct list_head *new, struct list_head *head)
*/
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
next->prev = prev;
prev->next = next;
}
/**
@ -87,9 +87,9 @@ static inline void __list_del(struct list_head *prev, struct list_head *next)
*/
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = (void *) 0;
entry->prev = (void *) 0;
__list_del(entry->prev, entry->next);
entry->next = (void *) 0;
entry->prev = (void *) 0;
}
/**
@ -98,8 +98,8 @@ static inline void list_del(struct list_head *entry)
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
@ -130,20 +130,20 @@ static inline void list_move_tail(struct list_head *list, struct list_head *head
*/
static inline int list_empty(struct list_head *head)
{
return head->next == head;
return head->next == head;
}
static inline void __list_splice(struct list_head *list, struct list_head *head)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;
struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;
first->prev = head;
head->next = first;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
last->next = at;
at->prev = last;
}
/**
@ -153,8 +153,8 @@ static inline void __list_splice(struct list_head *list, struct list_head *head)
*/
static inline void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
if (!list_empty(list))
__list_splice(list, head);
}
/**
@ -167,10 +167,10 @@ static inline void list_splice(struct list_head *list, struct list_head *head)
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}
/**
@ -189,6 +189,7 @@ static inline void list_splice_init(struct list_head *list,
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop counter.

View File

@ -31,21 +31,21 @@ R_API struct btree_node *btree_remove(struct btree_node *p, BTREE_DEL(del))
return(rp);
}
R_API void *btree_search(struct btree_node *proot, void *x, BTREE_CMP(cmp), int parent)
R_API void *btree_search(struct btree_node *root, void *x, BTREE_CMP(cmp), int parent)
{
struct btree_node *p = NULL;
if (proot!=NULL) {
p = proot;
if (cmp (x, proot->data)<0)
p = btree_search (proot->left, x, cmp, parent);
else if (cmp(x, proot->data)>0)
p = btree_search (proot->right, x, cmp, parent);
if (root!=NULL) {
if (cmp (x, root->data)<0)
p = btree_search (root->left, x, cmp, parent);
else if (cmp(x, root->data)>0)
p = btree_search (root->right, x, cmp, parent);
else p = root;
}
/* node found */
if (p) {
if (parent)
return proot;
return root;
return p;
} return NULL;
}
@ -80,17 +80,15 @@ R_API void btree_cleartree(struct btree_node *proot, BTREE_DEL(del))
}
}
R_API void btree_insert(struct btree_node *p,struct btree_node **T, BTREE_CMP(cmp))
R_API void btree_insert(struct btree_node **T, struct btree_node *p, BTREE_CMP(cmp))
{
int ret = cmp (p->data,(*T)->data);
int ret = cmp (p->data, (*T)->data);
if (ret<0) {
if ((*T)->left) btree_insert (p, &(*T)->left, cmp);
else (*T)->left=p;
if ((*T)->left) btree_insert (&(*T)->left, p, cmp);
else (*T)->left = p;
} else if (ret>0) {
if ((*T)->right) btree_insert (p, &(*T)->right, cmp);
else (*T)->right=p;
}
}
if ((*T)->right) btree_insert (&(*T)->right, p, cmp);
else (*T)->right = p; } }
R_API void btree_add(struct btree_node **T, void *e, BTREE_CMP(cmp))
{
@ -100,7 +98,39 @@ R_API void btree_add(struct btree_node **T, void *e, BTREE_CMP(cmp))
p->hits = 0;
p->left = p->right = NULL;
if (*T==NULL) *T = p;
else btree_insert (p, T, cmp);
else btree_insert (T, p, cmp);
}
/* unused */
R_API int btree_empty(struct btree_node **T)
{
return !( (T && (*T) && ((*T)->right || (*T)->left)) );
}
R_API struct btree_node *btree_hittest(struct btree_node *root, struct btree_node *hn)
{
struct btree_node *p = root;
if (root != NULL) {
struct btree_node *ml = btree_hittest(root->left, root);
struct btree_node *mr = btree_hittest(root->right, root);
if (ml && ml->hits > p->hits) p = ml;
if (mr && mr->hits > p->hits) p = mr;
}
return p;
}
R_API int btree_optimize(struct btree_node **T, BTREE_CMP(cmp))
{
struct btree_node *NT, *node;
do {
node = btree_hittest(*T, NULL);
if (node) {
btree_add(&NT, node->data, cmp);
btree_del(*T, node->data, cmp, NULL);
}
} while(node);
*T = NT; /* replace one tree with the other */
return 0;
}
/*--------------------*/
@ -130,13 +160,16 @@ int mycmp(const void *a, const void *b)
int main()
{
struct btree_node *bt = NULL;
struct btree_node *n, *bt = NULL;
//btree_init(&bt);
struct mydata foo = { 10, "hello" };
struct mydata bar = { 20, "world" };
printf("EMPTY TREE: %d\n", btree_empty(&bt));
btree_add(&bt, &foo, mycmp);
btree_add(&bt, &bar, mycmp);
printf("EMPTY TREE: %d\n", btree_empty(&bt));
printf("==== go search ====\n");
/* find existent data */
@ -149,12 +182,14 @@ printf("==== go search ====\n");
p = btree_get(bt, &nop, mycmp);
shownode("result for 15: ", p);
#if 1
printf("==== go get hittest ====\n");
n = btree_hittest(bt, NULL);
shownode("hitest is: ", p);
printf("==== go remove 20 ====\n");
if (btree_del(bt, &bar, mycmp, NULL))
printf("node found and removed\n");
else printf("oops\n");
#endif
printf("==== go search ====\n");
/* find existent data */