forked from wcatykid/Graph-Based-Molecular-Synthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoleculeHashHypergraph.h
123 lines (94 loc) · 3.27 KB
/
MoleculeHashHypergraph.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
115
116
117
118
119
120
121
122
123
/*
* 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 _MOLECULE_HASH_HYPERGRAPH_GUARD
#define _MOLECULE_HASH_HYPERGRAPH_GUARD 1
#include <pthread.h>
#include "LevelHashMap.h"
class MoleculeHashHypergraph
{
private:
LevelHashMap** level_maps;
unsigned int numLevels;
// disregards killed nodes counted
unsigned int overall_sz;
unsigned int num_killed;
// Lock for the important hypergraph operations: these include only operations attributed
// to structures interacting with the main structure.
pthread_mutex_t graph_lock;
// This vector links all levels together.
std::vector<MinimalMolecule*> nodes;
public:
unsigned int currentSize() const { return overall_sz - num_killed; }
unsigned int nonKilledSize() const { return overall_sz; }
MoleculeHashHypergraph(unsigned int n) : overall_sz(0), num_killed(0)
{
numLevels = n;
level_maps = new LevelHashMap*[numLevels];
for (int i = 0; i < numLevels; i++)
{
level_maps[i] = new LevelHashMap();
}
pthread_mutex_init(&graph_lock, NULL);
}
//
// Delete all hash
//
~MoleculeHashHypergraph()
{
for (int i = 0; i < numLevels; i++)
{
// we may have already deleted a level; check before deletion.
if (level_maps[i]) delete level_maps[i];
}
delete[] level_maps;
}
// Once we have completed processing of a particular, we can kill all memory
// related to them.
void killLevel(unsigned int level)
{
num_killed += level_maps[level]->size();
delete level_maps[level];
level_maps[level] = 0;
std::cerr << "Killed level " << level << std::endl;
}
//
// This function is provided ONLY for debugging purposes.
// This should NOT be called at all to prevent duplicate isomorphism checks.
//
bool contains(MinimalMolecule* const that, unsigned int level) const
{
return level_maps[level]->contains(that);
}
// We assume a call to contains has already been made (as to limit the
// number of graph isomorphisms)
std::pair<unsigned int, bool> addNode(MinimalMolecule* const that, unsigned int level)
{
std::pair<unsigned int, bool> added = level_maps[level]->add(that);
if (added.second)
{
overall_sz++;
pthread_mutex_lock(&graph_lock);
int index = nodes.size();
nodes.push_back(that);
that->uniqueIndexID = index;
pthread_mutex_unlock(&graph_lock);
return std::make_pair(index, true);
}
return added;
}
};
#endif