-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cpp
More file actions
56 lines (48 loc) · 1.32 KB
/
Copy pathNode.cpp
File metadata and controls
56 lines (48 loc) · 1.32 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
46
47
48
49
50
51
52
53
54
55
56
#include "Node.h"
#include "Rule.h"
#include "SequiturGrammar.h"
#include <iostream>
Node::Node(Rule* rule, int symbol) :
rule(rule),
symbol(symbol){}
bool Node::isGuard() {
return this->symbol == -1;
}
int Node::hashCode() { // Same as craig's implementation
int combined = ((this->symbol << 16) | (this->symbol >> 16)) ^ (this->next->symbol);
return (combined * (combined + 3));
}
bool Node::digramOverlap(Node* other) {
return this->next == other || this->prev == other;
}
void Node::connect(Node* other) {
this->next = other;
other->prev = this;
}
void Node::printRule() {
Node* guard = this;
while (!guard->isGuard()) guard=guard->next;
std::cout << "RULE USAGE=" << guard->rule->usage << ", ";
std::cout << guard->rule->symbol;
std::cout << " -> ";
Node* t = guard->next;
while (!t->isGuard()) {
if (t->symbol<this->rule->grammar->M)
std::cout << (char)t->symbol<< " ";
else {
std::cout << t->symbol<< " ";
}
t = t->next;
}
std::cout << std::endl;
}
void Node::printDigram() {
if (this->symbol < this->rule->grammar->M)
std::cout << (char)this->symbol << ",";
else
std::cout << this->symbol << ",";
if (this->next->symbol < this->rule->grammar->M)
std::cout << (char)this->next->symbol << std::endl;
else
std::cout << this->next->symbol << std::endl;
}