-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlist.cpp
89 lines (76 loc) · 2.65 KB
/
dlist.cpp
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
#include "visualize.h"
#include "dlist.h"
#include <cairomm/context.h>
#include <iostream>
#define ARROW_OFFSET 10
DList::DList() {
tail = NULL;
}
DList::~DList() {
}
void DList::arrange_nodes() {
DListNode * n = dynamic_cast <DListNode *> (head);
int xTmp = list_x;
int yTmp = list_y;
while (n != NULL) {
// check that if the node has neighbors that they point to n correctly
if ((!n->next || !n->next->prev || n->next->prev == n) && ((!n->prev && n == dynamic_cast <DListNode *> (head)) || (n->prev && (!n->prev->next || n->prev->next == n)))) {
xTmp += ListNode::padding;
n->x = xTmp;
n->y = yTmp;
n = n->next;
}
}
tail_x = xTmp + ListNode::padding;
}
void DList::draw_connected(const Cairo::RefPtr<Cairo::Context> & cr) {
DListNode * n = dynamic_cast <DListNode *> (head);
while (n != NULL) {
// check that if the node has neighbors that they point to n correctly
if ((!n->next || !n->next->prev || n->next->prev == n) && ((!n->prev && n == dynamic_cast <DListNode *> (head)) || (n->prev && (!n->prev->next || n->prev->next == n)))) {
// print the body of the node
n->draw(cr);
n->printed = true;
draw_arrows(cr, n);
n = n->next;
}
}
}
void DList::draw_disconnected(const Cairo::RefPtr<Cairo::Context> & cr) {
int xTmp = out_x;
int yTmp = out_y;
for (int i = 0; i < currentNodes; i++) {
if(!nodes[i]->printed) {
nodes[i]->y = yTmp;
nodes[i]->x = xTmp;
nodes[i]->draw(cr);
draw_arrows(cr, dynamic_cast <DListNode *> (nodes[i]));
xTmp += 2 * ListNode::padding;
}
}
}
void DList::draw_arrows(const Cairo::RefPtr<Cairo::Context> & cr, DListNode * node) {
// add the previous arrow
if (node->prev != NULL) {
draw_arrow_helper(cr, node->x + ARROW_OFFSET, node->y + (5 * ListNode::field_h) / 2, node->prev->x + ARROW_OFFSET + ListNode::field_w, node->prev->y + (5 * ListNode::field_h) / 2);
} else {
std::cout << "test" << std::endl;
draw_null_arrow(cr, node->x + ARROW_OFFSET, node->y + (5 * ListNode::field_h) / 2, false);
}
// add the next arrow
if (node->next != NULL) {
draw_arrow_helper(cr, node->x + ListNode::field_w - ARROW_OFFSET, node->y + (3 * ListNode::field_h) / 2, node->next->x - ARROW_OFFSET, node->next->y + (3 * ListNode::field_h) / 2);
} else {
draw_null_arrow(cr, node->x + ListNode::field_w - ARROW_OFFSET, node->y + (3 * ListNode::field_h) / 2, true);
}
}
DListNode::DListNode(List * dlist, int data) : ListNode(dlist, data) {
this->prev = NULL;
this->numFields = 3;
}
void DList::draw_labels(const Cairo::RefPtr<Cairo::Context> & cr) {
draw_label_helper(cr, head, "head", list_x, list_y, RIGHT);
draw_label_helper(cr, tail, "tail", tail_x, list_y, LEFT);
}
DListNode::~DListNode() {
}