Skip to content

Commit

Permalink
refactor(fheap): add a fast-call functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon committed Feb 9, 2024
1 parent a74098d commit 6e61abd
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 21 deletions.
27 changes: 15 additions & 12 deletions docs/Melon Developer Guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -338,46 +338,49 @@ Their definitions can be found in melon/include/mln_types.h.
d) mln_fheap_t *mln_fheap_new(struct mln_fheap_attr *attr);
It initializes and returns a fibonacci heap.

e) void mln_fheap_free(mln_fheap_t *fh);
e) mln_fheap_t *mln_fheap_new_fast(void *min_val, fheap_cmp cmp, fheap_copy copy, fheap_key_free key_free);
It initializes and returns a fibonacci heap.

f) void mln_fheap_free(mln_fheap_t *fh);
It destroys a fibonacci heap. If 'key_free' is not NULL, the key's value in every heap node will be freed by
the function 'key_free' at the same time.

f) void mln_fheap_insert(mln_fheap_t *fh, mln_fheap_node_t *fn);
g) void mln_fheap_insert(mln_fheap_t *fh, mln_fheap_node_t *fn);
This interface inserts a heap node.

g) void mln_fheap_delete(mln_fheap_t *fh, mln_fheap_node_t *node);
h) void mln_fheap_delete(mln_fheap_t *fh, mln_fheap_node_t *node);
This interface removes a heap node.

h) mln_fheap_node_t* mln_fheap_minimum(mln_fheap_t *fh);
i) mln_fheap_node_t* mln_fheap_minimum(mln_fheap_t *fh);
It retrieves a minimum heap node.
If the heap is empty, NULL will be returned.

i) mln_fheap_node_t* mln_fheap_extract_min(mln_fheap_t *fh);
j) mln_fheap_node_t* mln_fheap_extract_min(mln_fheap_t *fh);
Extract the node whose key value is minimum in the heap.
If the heap is empty, NULL will be returned.

j) int mln_fheap_decrease_key(mln_fheap_t *fh, mln_fheap_node_t *node, void *key);
k) int mln_fheap_decrease_key(mln_fheap_t *fh, mln_fheap_node_t *node, void *key);
It decreases the key's value of the specified node to the key.
The type of key must be identical with the type of keys in heap nodes.
The return value is: -1 - key error, 0 - on success.

k) mln_fheap_inline_insert(fh, fn, compare)
l) mln_fheap_inline_insert(fh, fn, compare)
Inline version of 'mln_fheap_insert'. 'compare' is the callback function 'cmp' in 'attr'.

l) mln_fheap_inline_extract_min(fh, compare)
m) mln_fheap_inline_extract_min(fh, compare)
Inline version of 'mln_fheap_insert'. 'compare' is the callback function 'cmp' in 'attr'.

m) mln_fheap_inline_decrease_key(fh, node, k, cpy, compare)
n) mln_fheap_inline_decrease_key(fh, node, k, cpy, compare)
Inline version of 'mln_fheap_decrease_key'. 'compare' is the callback function 'cmp' in 'attr'.

n) mln_fheap_inline_delete(fh, node, cpy, compare)
o) mln_fheap_inline_delete(fh, node, cpy, compare)
Inline version of 'mln_fheap_delelte'. 'compare' is the callback function 'cmp' in 'attr'.
'cpy' is the callback function 'copy' in 'attr'.

o) mln_fheap_inline_node_free(fh, fn, freer)
p) mln_fheap_inline_node_free(fh, fn, freer)
Inline version of 'mln_fheap_node_free'. 'freer' is the callback function 'key_free' in 'attr'.

p) mln_fheap_inline_free(fh, compare, freer)
q) mln_fheap_inline_free(fh, compare, freer)
Inline version of 'mln_fheap_free'. 'freer' is the callback function 'key_free' in 'attr'.

4) Red-Black Tree
Expand Down
23 changes: 23 additions & 0 deletions docs/book/cn/fheap.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ typedef void (*fheap_pool_free_handler)(void *);
#### mln_fheap_new_fast
```c
mln_fheap_t *mln_fheap_new_fast(void *min_val, fheap_cmp cmp, fheap_copy copy, fheap_key_free key_free);
typedef int (*fheap_cmp)(const void *, const void *);
typedef void (*fheap_copy)(void *, void *);
typedef void (*fheap_key_free)(void *);
```

描述:创建斐波那契堆。与`mln_fheap_new_fast`的差异在于不支持内存池且将属性作为函数参数传递。

其中:

- `min_val`是指堆结点中用户自定义数据类型的最小值,这个指针的类型与堆结点内用户自定义数据的类型一致。注意,这个指针指向的内存不应被更改,其生命周期应至少贯穿该斐波那契堆的生命周期。
- `cmp`是用于key比较大小的函数,该函数有两个参数,皆为用户自定义的key结构指针。如果参数1小于参数2,则返回`0`,否则返回`非0`
- `copy`是用来拷贝key结构的,这个回调函数会在`decrease key`时被调用,用于将原有key值改为新的key值。这个函数有两个参数,依次分别为:原有key值指针和新key值指针。
- `key_free`为key值结构释放函数,如果不需要释放,则可以置`NULL`

返回值:成功则返回`mln_fheap_t`类型指针,否则返回`NULL`



#### mln_fheap_free

```c
Expand Down
23 changes: 22 additions & 1 deletion docs/book/en/fheap.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Description: Create a Fibonacci heap.
- `pool` is a user-defined memory pool structure. If this item is not `NULL`, the Fibonacci heap structure will be allocated from this memory pool, otherwise it will be allocated from the malloc library.
- `pool_alloc` is the allocation memory function pointer of the memory pool, and this callback will be used when `pool` is not empty.
- `pool_free` is the pointer to the free memory function of the memory pool, and this callback will be used when `pool` is not empty.
- `cmp` is a function used to compare key sizes. This function has two parameters, both of which are user-defined key structure pointers. Return `0` if argument 1 is less than argument 2, otherwise return `not 0`.
- `cmp` is a function used to compare key. This function has two parameters, both of which are user-defined key structure pointers. Return `0` if argument 1 is less than argument 2, otherwise return `not 0`.
- `copy` is used to copy the key structure, this callback function will be called when `decrease key` is used to change the original key value to a new key value. This function has two parameters, respectively: the original key value pointer and the new key value pointer.
- `key_free` is the release function of the key value structure, if it does not need to be released, you can set `NULL`.
Expand All @@ -75,6 +75,27 @@ Return value: return `mln_fheap_t` type pointer if successful, otherwise return
#### mln_fheap_new_fast
```c
mln_fheap_t *mln_fheap_new_fast(void *min_val, fheap_cmp cmp, fheap_copy copy, fheap_key_free key_free);
typedef int (*fheap_cmp)(const void *, const void *);
typedef void (*fheap_copy)(void *, void *);
typedef void (*fheap_key_free)(void *);
```

Description: Create a Fibonacci heap. The difference from `mln_fheap_new_fast` is that memory pool is not supported and attributes are passed as function parameters.

- `min_val` refers to the minimum value of the user-defined data type in the heap node. The type of this pointer is consistent with the type of user-defined data in the heap node. Note that the memory pointed to by this pointer should not be changed, and its life cycle should last at least throughout the life cycle of the Fibonacci heap.
- `cmp` is a function used for key comparison. This function has two parameters, both of which are user-defined key structure pointers. If parameter 1 is less than parameter 2, return `0`, otherwise return `non-0`.
- `copy` is used to copy the key structure. This callback function will be called when `decrease key` is used to change the original key value to the new key value. This function has two parameters, which are: the original key value pointer and the new key value pointer.
- `key_free` is the key value structure release function. If it does not need to be released, it can be set to `NULL`.

Return value: Returns `mln_fheap_t` type pointer if successful, otherwise returns `NULL`



#### mln_fheap_free

```c
Expand Down
2 changes: 2 additions & 0 deletions include/mln_fheap.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ MLN_FUNC(static inline, mln_fheap_node_t *, mln_fheap_remove_child, \

extern mln_fheap_t *
mln_fheap_new(void *min_val, struct mln_fheap_attr *attr) __NONNULL1(1);
extern mln_fheap_t *
mln_fheap_new_fast(void *min_val, fheap_cmp cmp, fheap_copy copy, fheap_key_free key_free);
extern void
mln_fheap_free(mln_fheap_t *fh);
extern void
Expand Down
12 changes: 4 additions & 8 deletions src/mln_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,10 @@ MLN_FUNC(, mln_event_t *, mln_event_new, (void), (), {
goto err2;
}
/*timer heap*/
struct mln_fheap_attr fattr;
fattr.pool = NULL;
fattr.pool_alloc = NULL;
fattr.pool_free = NULL;
fattr.cmp = mln_event_fheap_timer_cmp;
fattr.copy = mln_event_fheap_timer_copy;
fattr.key_free = mln_event_desc_free;
ev->ev_timer_heap = mln_fheap_new(&fheap_min, &fattr);
ev->ev_timer_heap = mln_fheap_new_fast(&fheap_min, \
mln_event_fheap_timer_cmp, \
mln_event_fheap_timer_copy, \
mln_event_desc_free);
if (ev->ev_timer_heap == NULL) {
goto err3;
}
Expand Down
22 changes: 22 additions & 0 deletions src/mln_fheap.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ MLN_FUNC(, mln_fheap_t *, mln_fheap_new, (void *min_val, struct mln_fheap_attr *
return fh;
})

MLN_FUNC(, mln_fheap_t *, mln_fheap_new_fast, \
(void *min_val, fheap_cmp cmp, fheap_copy copy, fheap_key_free key_free), \
(min_val, cmp, copy, key_free), \
{
mln_fheap_t *fh;

fh = (mln_fheap_t *)malloc(sizeof(mln_fheap_t));
if (fh == NULL) return NULL;

fh->pool = NULL;
fh->pool_alloc = NULL;
fh->pool_free = NULL;
fh->cmp = cmp;
fh->copy = copy;
fh->key_free = key_free;
fh->min_val = min_val;
fh->min = NULL;
fh->root_list = NULL;
fh->num = 0;
return fh;
})

MLN_FUNC_VOID(, void, mln_fheap_insert, (mln_fheap_t *fh, mln_fheap_node_t *fn), (fh, fn), {
mln_fheap_inline_insert(fh, fn, NULL);
})
Expand Down

0 comments on commit 6e61abd

Please sign in to comment.