-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbts.c
executable file
·101 lines (85 loc) · 2.05 KB
/
bts.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* bts.c
*
* Implementation of backtrack stack.
*/
#include <assert.h>
#include <stdlib.h>
#include "bts.h"
typedef struct _bnode bnode_t;
struct _bnode {
state_t state;
bnode_t* next;
};
struct _bts {
bnode_t* top;
};
#define Assign(STATE) \
STATE.index = ind; \
STATE.str = str; \
STATE.matches = mat; \
STATE.recursive = rec; \
STATE.inner = inn; \
STATE.nbr = nbr; \
STATE.nest = NULL
/************************public functions****************************/
void bts_push(bts_t* obj, int ind, char* str, uint32_t mat,
bool rec, bts_t* inn, int nbr) {
assert(obj);
assert(str);
if (!obj->top) {
obj->top = malloc(sizeof(bnode_t));
assert(obj->top);
obj->top->next = NULL;
Assign(obj->top->state);
} else {
bnode_t* new_node = malloc(sizeof(bnode_t));
assert(new_node);
new_node->next = obj->top;
Assign(new_node->state);
obj->top = new_node;
}
}
state_t* bts_top(bts_t* obj) {
assert(obj);
assert(obj->top);
return &(obj->top->state);
}
void bts_set_top(bts_t* obj, int index, uint32_t matches,
range_t* nest) {
assert(obj);
assert(obj->top);
obj->top->state.index = index;
obj->top->state.matches = matches;
obj->top->state.nest = nest;
}
void bts_pop(bts_t* obj) {
assert(obj);
assert(obj->top);
bnode_t* old_top = obj->top;
obj->top = obj->top->next;
free(old_top);
}
bool bts_empty(bts_t* obj) {
assert(obj);
return !obj->top;
}
bts_t* bts_new() {
bts_t* obj = malloc(sizeof(bts_t));
assert(obj);
obj->top = NULL;
return obj;
}
void bts_free(bts_t* obj) {
if (obj) {
while (obj->top) {
if (obj->top->state.recursive) {
bts_free(obj->top->state.inner);
if (obj->top->state.nest)
range_free(obj->top->state.nest);
}
bts_pop(obj);
}
free(obj);
}
}
/********************************************************************/