-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeriv.cpp
92 lines (74 loc) · 1.76 KB
/
Deriv.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
90
91
92
/**
* @file: Deriv.cpp
* Files Deriv.* contain an example of how one could expand class Graph,
* deriving a new class from it.
* In this file we'll create class EGraph, representing extended graph. Instead
* of ordinary nodes (class Node) and edges (class Edge) it will be consisted
* of nodes and edges with additional fields: ENode and EEdge.
*
* This file contains implementations.
*/
#include "Deriv.h"
/**
*/
void EGraph::FreeNode(pNode p)
{
assert(p != NULL);
// We should cast p to pENode, because otherwise ~Node will be called instead of ~ENode.
// It will lead to memory leakages.
delete (pENode)p;
}
void EGraph::FreeEdge(pEdge p)
{
assert(p != NULL);
delete (pEEdge)p;
}
pENode EGraph::AddNode()
{
pENode new_node = new ENode(this);
return new_node;
}
pEEdge EGraph::AddEdge(pNode from, pNode to)
{
pEEdge new_edge = new EEdge((pENode)from,(pENode)to);
return new_edge;
}
void ENode::Dump()
{
Node::Dump();
printf(" X,Y: %d,%d\n",x,y);
}
void EEdge::Dump()
{
printf("Edge %d(w = %d): %d-->%d\n", id(), w, from()->id(), to()->id());
}
/**
* Create a EGraph object and call some of its methods.
*/
void Derivation_Example()
{
printf("\nDerivation test started..\n");
EGraph g;
int len = 10;
pENode *p = new pENode[len];
// Creating a new extended graph
for(int i = 0; i < len; i++) {
p[i] = (pENode)g.AddNode();
}
for(int i = 0; i < len-1; i++) {
g.AddEdge(p[i], p[i+1]);
}
for(int i = 2; i < len; i+=3) {
g.AddEdge(p[i], p[i-2]);
}
g.Dump();
// Performing some transformations
g.DeleteNode(p[5]);
g.DeleteNode(p[7]);
g.DeleteEdge(p[3],p[4]);
g.Dump();
// Clean up
g.Destroy();
delete []p;
printf("Derivation test passed!\n");
}