@@ -36,53 +36,63 @@ void NodeGroupList::Enumerate (const std::function<bool (const NodeGroupPtr&)>&
3636
3737bool 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
6868void 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
8392void 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}
0 commit comments