File tree 4 files changed +16
-17
lines changed
4 files changed +16
-17
lines changed Original file line number Diff line number Diff line change @@ -35,11 +35,8 @@ ChatLogic::~ChatLogic()
35
35
// delete chatbot instance
36
36
delete _chatBot;
37
37
38
- // delete all edges
39
- for (auto it = std::begin (_edges); it != std::end (_edges); ++it)
40
- {
41
- delete *it;
42
- }
38
+ // No need to manually delete edges now.
39
+ // The child nodes' unique_ptr<GraphEdge> will handle that.
43
40
44
41
// //
45
42
// // EOF STUDENT CODE
@@ -161,16 +158,16 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename)
161
158
auto childNode = std::find_if (_nodes.begin (), _nodes.end (), [&childToken](std::unique_ptr<GraphNode> &node) { return node->GetID () == std::stoi (childToken->second ); });
162
159
163
160
// create new edge
164
- GraphEdge * edge = new GraphEdge (id);
165
- _edges.push_back (edge);
161
+ auto edge = std::make_unique< GraphEdge> (id);
162
+ // _edges.push_back(edge);
166
163
edge->SetChildNode (childNode->get ());
167
164
edge->SetParentNode (parentNode->get ());
168
165
// find all keywords for current node
169
166
AddAllTokensToElement (" KEYWORD" , tokens, *edge);
170
167
171
168
// store reference in child node and parent node
172
- (*childNode)->AddEdgeToParentNode (edge);
173
- (*parentNode)->AddEdgeToChildNode (edge);
169
+ (*childNode)->AddEdgeToParentNode (edge. get () );
170
+ (*parentNode)->AddEdgeToChildNode (std::move ( edge) );
174
171
}
175
172
176
173
// //
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ class ChatLogic
19
19
20
20
// data handles (owned)
21
21
std::vector<std::unique_ptr<GraphNode>> _nodes;
22
- std::vector<GraphEdge *> _edges;
22
+ // std::vector<GraphEdge *> _edges;
23
23
24
24
// //
25
25
// // EOF STUDENT CODE
Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ GraphNode::~GraphNode()
11
11
// // STUDENT CODE
12
12
// //
13
13
14
+ // We do NOT delete _parentEdges here, since they are non‐owning!
15
+ // The unique_ptr in _childEdges will automatically free the GraphEdges.
14
16
15
17
// //
16
18
// // EOF STUDENT CODE
@@ -26,9 +28,9 @@ void GraphNode::AddEdgeToParentNode(GraphEdge *edge)
26
28
_parentEdges.push_back (edge);
27
29
}
28
30
29
- void GraphNode::AddEdgeToChildNode (GraphEdge * edge)
31
+ void GraphNode::AddEdgeToChildNode (std::unique_ptr< GraphEdge> edge)
30
32
{
31
- _childEdges.push_back (edge);
33
+ _childEdges.push_back (std::move ( edge) );
32
34
}
33
35
34
36
// // STUDENT CODE
@@ -52,7 +54,7 @@ GraphEdge *GraphNode::GetChildEdgeAtIndex(int index)
52
54
// // STUDENT CODE
53
55
// //
54
56
55
- return _childEdges[index ];
57
+ return _childEdges[index ]. get () ;
56
58
57
59
// //
58
60
// // EOF STUDENT CODE
Original file line number Diff line number Diff line change 4
4
#include < vector>
5
5
#include < string>
6
6
#include " chatbot.h"
7
-
7
+ # include < memory >
8
8
9
9
// forward declarations
10
10
class GraphEdge ;
@@ -16,7 +16,7 @@ class GraphNode
16
16
// //
17
17
18
18
// data handles (owned)
19
- std::vector<GraphEdge * > _childEdges; // edges to subsequent nodes
19
+ std::vector<std::unique_ptr< GraphEdge> > _childEdges; // edges to subsequent nodes
20
20
21
21
// data handles (not owned)
22
22
std::vector<GraphEdge *> _parentEdges; // edges to preceding nodes
@@ -37,14 +37,14 @@ class GraphNode
37
37
// getter / setter
38
38
int GetID () { return _id; }
39
39
int GetNumberOfChildEdges () { return _childEdges.size (); }
40
+ int GetNumberOfParents () { return _parentEdges.size (); }
40
41
GraphEdge *GetChildEdgeAtIndex (int index);
41
42
std::vector<std::string> GetAnswers () { return _answers; }
42
- int GetNumberOfParents () { return _parentEdges.size (); }
43
43
44
44
// proprietary functions
45
45
void AddToken (std::string token); // add answers to list
46
46
void AddEdgeToParentNode (GraphEdge *edge);
47
- void AddEdgeToChildNode (GraphEdge * edge);
47
+ void AddEdgeToChildNode (std::unique_ptr< GraphEdge> edge);
48
48
49
49
// // STUDENT CODE
50
50
// //
You can’t perform that action at this time.
0 commit comments