forked from xtaci/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.h
More file actions
45 lines (41 loc) · 1.19 KB
/
sol.h
File metadata and controls
45 lines (41 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*******************************************************************************
* DANIEL'S ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* SELF ORGANIZED LINKED-LIST
*
* Features:
* 1. Implementation of MTF (move-to-front)
* 2. Implementation of MAO (move-ahead-one position)
* 3. Based on double linked list
*
* http://en.wikipedia.org/wiki/Self-organizing_list
*
******************************************************************************/
#ifndef __SOL_H__
#define __SOL_H__
#include "double_linked_list.h"
namespace alg {
/**
* Move a node to the front
*/
static inline void list_mtf(struct list_head *entry, struct list_head *head) {
if (entry->prev == head) return;
__list_del(entry->prev, entry->next);
__list_add(entry, head, head->next);
}
/**
* Move a node ahead one position
*/
static inline void list_mao(struct list_head *entry, struct list_head * head) {
// if the entry in the 1st position
if (entry->prev == head) return;
struct list_head * prev = entry->prev;
__list_del(entry->prev, entry->next);
__list_add(entry, prev->prev, prev);
}
}
#endif //