Skip to content

Commit cf586a0

Browse files
committed
changed the ownership of all instances of GraphEdge in a way that each instance of graphNode exlusively owns the outgoing graph edges and holds non-owning references to incoming GraphEdges
1 parent ac5eb05 commit cf586a0

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

src/chatlogic.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@ ChatLogic::~ChatLogic()
3535
// delete chatbot instance
3636
delete _chatBot;
3737

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.
4340

4441
////
4542
//// EOF STUDENT CODE
@@ -161,16 +158,16 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename)
161158
auto childNode = std::find_if(_nodes.begin(), _nodes.end(), [&childToken](std::unique_ptr<GraphNode> &node) { return node->GetID() == std::stoi(childToken->second); });
162159

163160
// 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);
166163
edge->SetChildNode(childNode->get());
167164
edge->SetParentNode(parentNode->get());
168165
// find all keywords for current node
169166
AddAllTokensToElement("KEYWORD", tokens, *edge);
170167

171168
// 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));
174171
}
175172

176173
////

src/chatlogic.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ChatLogic
1919

2020
// data handles (owned)
2121
std::vector<std::unique_ptr<GraphNode>> _nodes;
22-
std::vector<GraphEdge *> _edges;
22+
// std::vector<GraphEdge *> _edges;
2323

2424
////
2525
//// EOF STUDENT CODE

src/graphnode.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ GraphNode::~GraphNode()
1111
//// STUDENT CODE
1212
////
1313

14+
// We do NOT delete _parentEdges here, since they are non‐owning!
15+
// The unique_ptr in _childEdges will automatically free the GraphEdges.
1416

1517
////
1618
//// EOF STUDENT CODE
@@ -26,9 +28,9 @@ void GraphNode::AddEdgeToParentNode(GraphEdge *edge)
2628
_parentEdges.push_back(edge);
2729
}
2830

29-
void GraphNode::AddEdgeToChildNode(GraphEdge *edge)
31+
void GraphNode::AddEdgeToChildNode(std::unique_ptr<GraphEdge> edge)
3032
{
31-
_childEdges.push_back(edge);
33+
_childEdges.push_back(std::move(edge));
3234
}
3335

3436
//// STUDENT CODE
@@ -52,7 +54,7 @@ GraphEdge *GraphNode::GetChildEdgeAtIndex(int index)
5254
//// STUDENT CODE
5355
////
5456

55-
return _childEdges[index];
57+
return _childEdges[index].get();
5658

5759
////
5860
//// EOF STUDENT CODE

src/graphnode.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <vector>
55
#include <string>
66
#include "chatbot.h"
7-
7+
#include <memory>
88

99
// forward declarations
1010
class GraphEdge;
@@ -16,7 +16,7 @@ class GraphNode
1616
////
1717

1818
// data handles (owned)
19-
std::vector<GraphEdge *> _childEdges; // edges to subsequent nodes
19+
std::vector<std::unique_ptr<GraphEdge>> _childEdges; // edges to subsequent nodes
2020

2121
// data handles (not owned)
2222
std::vector<GraphEdge *> _parentEdges; // edges to preceding nodes
@@ -37,14 +37,14 @@ class GraphNode
3737
// getter / setter
3838
int GetID() { return _id; }
3939
int GetNumberOfChildEdges() { return _childEdges.size(); }
40+
int GetNumberOfParents() { return _parentEdges.size(); }
4041
GraphEdge *GetChildEdgeAtIndex(int index);
4142
std::vector<std::string> GetAnswers() { return _answers; }
42-
int GetNumberOfParents() { return _parentEdges.size(); }
4343

4444
// proprietary functions
4545
void AddToken(std::string token); // add answers to list
4646
void AddEdgeToParentNode(GraphEdge *edge);
47-
void AddEdgeToChildNode(GraphEdge *edge);
47+
void AddEdgeToChildNode(std::unique_ptr<GraphEdge> edge);
4848

4949
//// STUDENT CODE
5050
////

0 commit comments

Comments
 (0)