3
3
#include <stdbool.h>
4
4
#include <string.h>
5
5
6
+
6
7
#define container_of (ptr , type , member ) \
7
8
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
8
9
9
10
#define list_entry (ptr , type , member ) \
10
11
container_of(ptr, type, member)
11
12
12
- #define hlist_for_each (pos , head ) \
13
- for (pos = ( head)->first; pos; pos = pos->next)
14
-
15
- struct hlist_node ;
13
+ #define list_for_each_entry (pos , head , member ) \
14
+ for (pos = list_entry(( head)->next, typeof(* pos), member); \
15
+ &(pos)->member != (head); \
16
+ pos = list_entry((pos)->member.next, typeof(*pos), member))
16
17
17
- struct hlist_head {
18
- struct hlist_node * first ;
18
+ struct list_head {
19
+ struct list_head * next , * prev ;
19
20
};
20
21
21
- struct hlist_node {
22
- struct hlist_node * next , * * pprev ;
22
+ struct word_node {
23
+ char * word ;
24
+ int index ;
25
+ struct list_head link ;
23
26
};
24
27
25
- static inline void INIT_HLIST_HEAD (struct hlist_head * h ) {
26
- h -> first = NULL ;
28
+ static inline void INIT_LIST_HEAD (struct list_head * list )
29
+ {
30
+ list -> next = list -> prev = list ;
27
31
}
28
32
29
- static inline int hlist_empty (struct hlist_head * h ) {
30
- return !h -> first ;
33
+ static inline int list_empty (const struct list_head * head )
34
+ {
35
+ return (head -> next == head );
31
36
}
32
37
33
- static inline void hlist_add_head (struct hlist_node * n , struct hlist_head * h )
38
+ static inline void __list_add (struct list_head * new , struct list_head * prev , struct list_head * next )
34
39
{
35
- if (h -> first != NULL ) {
36
- h -> first -> pprev = & n -> next ;
37
- }
38
- n -> next = h -> first ;
39
- n -> pprev = & h -> first ;
40
- h -> first = n ;
40
+ next -> prev = new ;
41
+ new -> next = next ;
42
+ new -> prev = prev ;
43
+ prev -> next = new ;
41
44
}
42
45
43
- struct word_node {
44
- struct hlist_node node ;
45
- char * word ;
46
- int index ;
47
- };
46
+ static inline void list_add (struct list_head * _new , struct list_head * head )
47
+ {
48
+ __list_add (_new , head , head -> next );
49
+ }
50
+
51
+ static inline void list_add_tail (struct list_head * _new , struct list_head * head )
52
+ {
53
+ __list_add (_new , head -> prev , head );
54
+ }
55
+
56
+ static inline void __list_del (struct list_head * entry )
57
+ {
58
+ entry -> next -> prev = entry -> prev ;
59
+ entry -> prev -> next = entry -> next ;
60
+ }
61
+
62
+ static inline void list_del (struct list_head * entry )
63
+ {
64
+ __list_del (entry );
65
+ entry -> next = entry -> prev = NULL ;
66
+ }
48
67
49
68
static inline int BKDRHash (char * s , size_t size )
50
69
{
@@ -56,26 +75,23 @@ static inline int BKDRHash(char *s, size_t size)
56
75
return hash % size ;
57
76
}
58
77
59
- static int find (char * word , struct hlist_head * heads , int size )
78
+ static int find (char * word , struct list_head * heads , int size )
60
79
{
80
+ struct word_node * wn ;
61
81
int hash = BKDRHash (word , size );
62
- struct hlist_node * pos ;
63
- hlist_for_each (pos , & heads [hash ]) {
64
- struct word_node * wn = list_entry (pos , struct word_node , node );
82
+ list_for_each_entry (wn , & heads [hash ], link ) {
65
83
if (!strcmp (wn -> word , word )) {
66
84
return wn -> index ;
67
85
}
68
86
}
69
87
return -1 ;
70
88
}
71
89
72
- static void add (char * * words , int index , struct hlist_head * heads , int size , int * freqs )
90
+ static void add (char * * words , int index , struct list_head * heads , int size , int * freqs )
73
91
{
74
- int hash = BKDRHash (words [index ], size );
75
- struct hlist_node * pos ;
76
92
struct word_node * wn ;
77
- hlist_for_each ( pos , & heads [ hash ]) {
78
- wn = list_entry ( pos , struct word_node , node );
93
+ int hash = BKDRHash ( words [ index ], size );
94
+ list_for_each_entry ( wn , & heads [ hash ], link ) {
79
95
if (!strcmp (wn -> word , words [index ])) {
80
96
freqs [wn -> index ]++ ;
81
97
return ;
@@ -84,7 +100,7 @@ static void add(char **words, int index, struct hlist_head *heads, int size, int
84
100
wn = malloc (sizeof (* wn ));
85
101
wn -> word = words [index ];
86
102
wn -> index = index ;
87
- hlist_add_head (& wn -> node , & heads [hash ]);
103
+ list_add (& wn -> link , & heads [hash ]);
88
104
freqs [wn -> index ]++ ;
89
105
}
90
106
@@ -101,9 +117,9 @@ static int *findSubstring(char *s, char **words, int wordsSize, int *returnSize)
101
117
102
118
int i , j , cap = 10000 , count = 0 ;
103
119
int hash_size = wordsSize ;
104
- struct hlist_head * heads = malloc (hash_size * sizeof (* heads ));
120
+ struct list_head * heads = malloc (hash_size * sizeof (* heads ));
105
121
for (i = 0 ; i < hash_size ; i ++ ) {
106
- INIT_HLIST_HEAD (& heads [i ]);
122
+ INIT_LIST_HEAD (& heads [i ]);
107
123
}
108
124
109
125
int * freqs = malloc (wordsSize * sizeof (int ));
0 commit comments