Skip to content

Commit 4b99a02

Browse files
committed
Refactor node group handling. Grouped node list is not owned by the group, it is the responsibility of NodeManager from now.
1 parent 0dbb285 commit 4b99a02

19 files changed

Lines changed: 212 additions & 243 deletions

Sources/NodeEngine/NE_NodeGroup.cpp

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,20 @@ NodeGroup::NodeGroup ()
1111

1212
}
1313

14-
NodeGroup::NodeGroup (const NodeCollection& nodes) :
15-
nodes (nodes)
16-
{
17-
18-
}
19-
2014
NodeGroup::~NodeGroup ()
2115
{
2216

2317
}
2418

25-
bool NodeGroup::IsEmpty () const
26-
{
27-
return nodes.IsEmpty ();
28-
}
29-
30-
bool NodeGroup::ContainsNode (const NodeId& nodeId) const
31-
{
32-
return nodes.Contains (nodeId);
33-
}
34-
35-
bool NodeGroup::AddNode (const NodeId& nodeId)
36-
{
37-
if (nodes.Contains (nodeId)) {
38-
return false;
39-
}
40-
nodes.Insert (nodeId);
41-
return true;
42-
}
43-
44-
bool NodeGroup::DeleteNode (const NodeId& nodeId)
45-
{
46-
if (!nodes.Contains (nodeId)) {
47-
return false;
48-
}
49-
nodes.Erase (nodeId);
50-
return true;
51-
}
52-
53-
const NodeCollection& NodeGroup::GetNodes () const
54-
{
55-
return nodes;
56-
}
57-
5819
Stream::Status NodeGroup::Read (InputStream& inputStream)
5920
{
6021
ObjectHeader header (inputStream);
61-
nodes.Read (inputStream);
6222
return inputStream.GetStatus ();
6323
}
6424

6525
Stream::Status NodeGroup::Write (OutputStream& outputStream) const
6626
{
6727
ObjectHeader header (outputStream, serializationInfo);
68-
nodes.Write (outputStream);
6928
return outputStream.GetStatus ();
7029
}
7130

@@ -86,15 +45,4 @@ NodeGroupPtr NodeGroup::Clone (const NodeGroupConstPtr& node)
8645
return result;
8746
}
8847

89-
bool NodeGroup::IsEqual (const NodeGroupConstPtr& aNode, const NodeGroupConstPtr& bNode)
90-
{
91-
MemoryOutputStream aStream;
92-
MemoryOutputStream bStream;
93-
94-
aNode->Write (aStream);
95-
bNode->Write (bStream);
96-
97-
return aStream.GetBuffer () == bStream.GetBuffer ();
98-
}
99-
10048
}

Sources/NodeEngine/NE_NodeGroup.hpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,12 @@ class NodeGroup : public DynamicSerializable
1717

1818
public:
1919
NodeGroup ();
20-
NodeGroup (const NodeCollection& nodes);
2120
~NodeGroup ();
2221

23-
bool IsEmpty () const;
24-
bool ContainsNode (const NodeId& nodeId) const;
25-
26-
bool AddNode (const NodeId& nodeId);
27-
bool DeleteNode (const NodeId& nodeId);
28-
const NodeCollection& GetNodes () const;
29-
3022
virtual Stream::Status Read (InputStream& inputStream) override;
3123
virtual Stream::Status Write (OutputStream& outputStream) const override;
3224

33-
static NodeGroupPtr Clone (const NodeGroupConstPtr& group);
34-
static bool IsEqual (const NodeGroupConstPtr& aGroup, const NodeGroupConstPtr& bGroup);
35-
36-
protected:
37-
NodeCollection nodes;
25+
static NodeGroupPtr Clone (const NodeGroupConstPtr& node);
3826
};
3927

4028
}

Sources/NodeEngine/NE_NodeGroupList.cpp

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,53 +36,63 @@ void NodeGroupList::Enumerate (const std::function<bool (const NodeGroupPtr&)>&
3636

3737
bool NodeGroupList::AddGroup (const NodeGroupPtr& group)
3838
{
39-
NodeCollection nodes = group->GetNodes ();
40-
nodes.Enumerate ([&] (const NodeId& nodeId) {
41-
RemoveNodeFromGroup (nodeId);
42-
nodeToGroup.insert ({ nodeId, group });
43-
return true;
44-
});
45-
4639
groups.push_back (group);
40+
groupToNodes.insert ({ group, NodeCollection () });
4741
return true;
4842
}
4943

50-
void NodeGroupList::DeleteGroup (const NodeGroupPtr& group)
44+
void NodeGroupList::DeleteGroup (const NodeGroupConstPtr& group)
5145
{
52-
NodeCollection nodes = group->GetNodes ();
46+
const NodeCollection& nodes = GetGroupNodes (group);
5347
nodes.Enumerate ([&] (const NodeId& nodeId) {
54-
RemoveNodeFromGroup (nodeId);
48+
nodeToGroup.erase (nodeId);
5549
return true;
5650
});
51+
auto foundInGroups = std::find (groups.begin (), groups.end (), group);
52+
groups.erase (foundInGroups);
53+
groupToNodes.erase (group);
5754
}
5855

59-
NodeGroupPtr NodeGroupList::GetGroup (const NodeId& nodeId)
56+
void NodeGroupList::AddNodeToGroup (const NodeGroupPtr& group, const NodeId& nodeId)
6057
{
61-
auto found = nodeToGroup.find (nodeId);
62-
if (found == nodeToGroup.end ()) {
63-
return nullptr;
64-
}
65-
return found->second;
58+
RemoveNodeFromGroup (nodeId);
59+
groupToNodes[group].Insert (nodeId);
60+
nodeToGroup.insert ({ nodeId, group });
61+
}
62+
63+
const NodeCollection& NodeGroupList::GetGroupNodes (const NodeGroupConstPtr& group) const
64+
{
65+
return groupToNodes.at (group);
6666
}
6767

6868
void NodeGroupList::RemoveNodeFromGroup (const NodeId& nodeId)
6969
{
70-
NodeGroupPtr group = GetGroup (nodeId);
70+
NodeGroupConstPtr group = GetNodeGroup (nodeId);
7171
if (group == nullptr) {
7272
return;
7373
}
74-
group->DeleteNode (nodeId);
75-
if (group->IsEmpty ()) {
76-
auto foundInGroups = std::find (groups.begin (), groups.end (), group);
77-
DBGASSERT (foundInGroups != groups.end ());
78-
groups.erase (foundInGroups);
79-
}
74+
75+
NodeCollection& nodes = groupToNodes[group];
76+
nodes.Erase (nodeId);
8077
nodeToGroup.erase (nodeId);
78+
if (nodes.IsEmpty ()) {
79+
DeleteGroup (group);
80+
}
81+
}
82+
83+
NodeGroupConstPtr NodeGroupList::GetNodeGroup (const NodeId& nodeId) const
84+
{
85+
auto found = nodeToGroup.find (nodeId);
86+
if (found == nodeToGroup.end ()) {
87+
return nullptr;
88+
}
89+
return found->second;
8190
}
8291

8392
void NodeGroupList::Clear ()
8493
{
8594
groups.clear ();
95+
groupToNodes.clear ();
8696
nodeToGroup.clear ();
8797
}
8898

@@ -94,10 +104,11 @@ Stream::Status NodeGroupList::Read (InputStream& inputStream)
94104
inputStream.Read (groupCount);
95105
for (size_t i = 0; i < groupCount; i++) {
96106
NodeGroupPtr group (ReadDynamicObject<NodeGroup> (inputStream));
97-
const NodeCollection& nodes = group->GetNodes ();
98-
groups.push_back (group);
107+
NodeCollection nodes;
108+
nodes.Read (inputStream);
109+
AddGroup (group);
99110
nodes.Enumerate ([&] (const NodeId& nodeId) {
100-
nodeToGroup.insert ({ nodeId, group });
111+
AddNodeToGroup (group, nodeId);
101112
return true;
102113
});
103114
}
@@ -108,8 +119,10 @@ Stream::Status NodeGroupList::Write (OutputStream& outputStream) const
108119
{
109120
ObjectHeader header (outputStream, serializationInfo);
110121
outputStream.Write (groups.size ());
111-
for (const NodeGroupPtr& group : groups) {
122+
for (const NodeGroupConstPtr& group : groups) {
112123
WriteDynamicObject (outputStream, group.get ());
124+
const NodeCollection& nodes = groupToNodes.at (group);
125+
nodes.Write (outputStream);
113126
}
114127
return outputStream.GetStatus ();
115128
}

Sources/NodeEngine/NE_NodeGroupList.hpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,26 @@ class NodeGroupList
1717
NodeGroupList ();
1818
~NodeGroupList ();
1919

20-
void Enumerate (const std::function<bool (const NodeGroupConstPtr&)>& processor) const;
21-
void Enumerate (const std::function<bool (const NodeGroupPtr&)>& processor);
20+
void Enumerate (const std::function<bool (const NodeGroupConstPtr&)>& processor) const;
21+
void Enumerate (const std::function<bool (const NodeGroupPtr&)>& processor);
2222

23-
bool AddGroup (const NodeGroupPtr& group);
24-
void DeleteGroup (const NodeGroupPtr& group);
25-
NodeGroupPtr GetGroup (const NodeId& nodeId);
26-
void RemoveNodeFromGroup (const NodeId& nodeId);
27-
void Clear ();
23+
bool AddGroup (const NodeGroupPtr& group);
24+
void DeleteGroup (const NodeGroupConstPtr& group);
2825

29-
Stream::Status Read (InputStream& inputStream);
30-
Stream::Status Write (OutputStream& outputStream) const;
26+
void AddNodeToGroup (const NodeGroupPtr& group, const NodeId& nodeId);
27+
void RemoveNodeFromGroup (const NodeId& nodeId);
28+
const NodeCollection& GetGroupNodes (const NodeGroupConstPtr& group) const;
29+
NodeGroupConstPtr GetNodeGroup (const NodeId& nodeId) const;
30+
31+
void Clear ();
32+
33+
Stream::Status Read (InputStream& inputStream);
34+
Stream::Status Write (OutputStream& outputStream) const;
3135

3236
private:
33-
std::vector<NodeGroupPtr> groups;
34-
std::unordered_map<NodeId, NodeGroupPtr> nodeToGroup;
37+
std::vector<NodeGroupPtr> groups;
38+
std::unordered_map<NodeGroupConstPtr, NodeCollection> groupToNodes;
39+
std::unordered_map<NodeId, NodeGroupConstPtr> nodeToGroup;
3540
};
3641

3742
}

Sources/NodeEngine/NE_NodeManager.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,19 +425,25 @@ void NodeManager::DeleteNodeGroup (const NodeGroupPtr& group)
425425
return nodeGroupList.DeleteGroup (group);
426426
}
427427

428-
void NodeManager::DeleteAllNodeGroups ()
428+
void NodeManager::AddNodeToGroup (const NodeGroupPtr& group, const NodeId& nodeId)
429429
{
430-
nodeGroupList.Clear ();
430+
DBGASSERT (ContainsNode (nodeId));
431+
nodeGroupList.AddNodeToGroup (group, nodeId);
431432
}
432433

433-
NodeGroupPtr NodeManager::GetNodeGroup (const NodeId& nodeId)
434+
void NodeManager::RemoveNodeFromGroup (const NodeId& nodeId)
434435
{
435-
return nodeGroupList.GetGroup (nodeId);
436+
nodeGroupList.RemoveNodeFromGroup (nodeId);
436437
}
437438

438-
void NodeManager::RemoveNodeFromGroup (const NodeId& nodeId)
439+
NodeGroupConstPtr NodeManager::GetNodeGroup (const NodeId& nodeId) const
439440
{
440-
nodeGroupList.RemoveNodeFromGroup (nodeId);
441+
return nodeGroupList.GetNodeGroup (nodeId);
442+
}
443+
444+
const NodeCollection& NodeManager::GetGroupNodes (const NodeGroupConstPtr& group) const
445+
{
446+
return nodeGroupList.GetGroupNodes (group);
441447
}
442448

443449
void NodeManager::EnumerateNodeGroups (const std::function<bool (const NodeGroupConstPtr&)>& processor) const
@@ -450,6 +456,11 @@ void NodeManager::EnumerateNodeGroups (const std::function<bool (const NodeGroup
450456
nodeGroupList.Enumerate (processor);
451457
}
452458

459+
void NodeManager::DeleteAllNodeGroups ()
460+
{
461+
nodeGroupList.Clear ();
462+
}
463+
453464
bool NodeManager::IsCalculationEnabled () const
454465
{
455466
return updateMode == UpdateMode::Automatic || isForceCalculate;

0 commit comments

Comments
 (0)