forked from wcatykid/Graph-Based-Molecular-Synthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleFragmentGraph.h
114 lines (89 loc) · 2.87 KB
/
SimpleFragmentGraph.h
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* This file is part of esynth.
*
* esynth is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* esynth is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with esynth. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _SIMPLE_FRAGMENT_GRAPH_GUARD
#define _SIMPLE_FRAGMENT_GRAPH_GUARD 1
#include <cstring>
#include <sstream>
//
// Wrapper around a simple list of integers.
// Those integers represent the edges that define the connections in a molecule.
//
class SimpleFragmentGraph
{
public:
SimpleFragmentGraph() : edgeIndices(0), sz(0) {}
~SimpleFragmentGraph()
{
if (edgeIndices) delete[] edgeIndices;
}
SimpleFragmentGraph* copyAndAppend(short edgeID) const
{
//
// Create the new Simple representation
//
SimpleFragmentGraph* theCopy = new SimpleFragmentGraph();
theCopy->edgeIndices = new short[this->sz + 1];
//
// Copy the old edges to the new edges; insert new edge into the ordered list
//
memcpy(theCopy->edgeIndices, this->edgeIndices, this->sz * sizeof(short));
int index;
for (index = sz; index > 0 && theCopy->edgeIndices[index - 1] > edgeID; index--)
{
theCopy->edgeIndices[index] = theCopy->edgeIndices[index - 1];
}
theCopy->edgeIndices[index] = edgeID;
// Update the fact that we have 1 more edge in this list.
theCopy->sz = this->sz + 1;
return theCopy;
}
//
// Graph Isomorphism using a simple comparison of the edges of the fragment graphs.
//
bool IsIsomorphicTo(SimpleFragmentGraph* const that) const
{
if (this->sz != that->sz) return false;
short* first = this->edgeIndices;
short* second = that->edgeIndices;
int counter;
for (counter = 0; counter < this->sz && *first == *second; counter++)
{
first++;
second++;
}
return counter == this->sz;
}
std::string toString() const
{
std::ostringstream oss;
oss << "(#" << sz << "): ";
for (int index = 0; index < sz; index++)
{
oss << edgeIndices[index] << ' ';
}
return oss.str();
}
friend std::ostream& operator<< (std::ostream& os, const SimpleFragmentGraph& fg)
{
os << fg.toString();
return os;
}
private:
short* edgeIndices;
unsigned short int sz;
};
#endif