-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
122 lines (99 loc) · 2.16 KB
/
utils.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "utils.h"
#include "mm.h"
#include <stdio.h>
#include <stdint.h>
// Container for cell pointer queue
static struct cell *arr[MAZE_SIZE * MAZE_SIZE];
// Container for cell pointers stack
static struct cell *sarr[MAZE_SIZE * MAZE_SIZE];
struct cq {
int r;
int w;
int count;
};
struct cstack{
int r;
int w;
int count;
};
static struct cq cq = {.r = 0, .w = 0, .count = 0};
static struct cstack cstack = {.r = 0, .w = -1, .count = 0};
/* Queue API */
int q_isempty() {
return (cq.count == 0);
}
void q_add(struct cell *p) {
arr[cq.w] = p;
cq.w = (cq.w + 1) % (MAZE_SIZE * MAZE_SIZE);
cq.count++;
}
struct cell *q_peek() {
if (!q_isempty())
return arr[cq.r];
else
return NULL;
}
void q_pop() {
if (!q_isempty()) {
cq.r = (cq.r + 1) % (MAZE_SIZE * MAZE_SIZE);
cq.count--;
}
}
void q_reset() {
cq.w = cq.r = cq.count = 0;
}
// TODO: Make more efficient later on w/ hashing
bool q_is_processed(int nx, int ny) {
// If cell (nx, ny) is currently in the queue, skip
// processing it again
for (int i = 0; i < cq.count; i++) {
struct cell *in_queue = arr[i];
if (in_queue->x == nx && in_queue->y == ny) {
return true;
}
}
return false;
}
void q_status() {
//mvprintw(41, 0, "c %d, r %d, w %d\n", cq.count, cq.r, cq.w);
}
/* Stack API */
int stack_isempty() {
return (cstack.count == 0);
}
void stack_add(struct cell *p) {
sarr[++cstack.w] = p;
cstack.count++;
}
struct cell *stack_peek() {
if (!stack_isempty())
return sarr[cstack.w];
else
return NULL;
}
void stack_pop() {
if (!stack_isempty()) {
cstack.w--;
cstack.count--;
}
}
void stack_reset() {
cstack.r = cstack.count = 0;
cstack.w = -1;
}
bool stack_is_processed(int nx, int ny) {
// If cell (nx, ny) is currently in the stack, skip
// processing it again
for (int i = 0; i < cstack.count; i++) {
struct cell *in_stack= sarr[i];
if(in_stack!= NULL){
if (in_stack->x == nx && in_stack->y == ny) {
return true;
}
}
return false;
}
}
void stack_status() {
//mvprintw(41, 0, "c %d, r %d, w %d\n", cstack.count, cstack.r, cstack.w);
}