1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
6 | *
|
---|
7 | *
|
---|
8 | * This file is part of MoleCuilder.
|
---|
9 | *
|
---|
10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * SubsetMap.cpp
|
---|
26 | *
|
---|
27 | * Created on: Jul 3, 2012
|
---|
28 | * Author: heber
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | // include config.h
|
---|
33 | #ifdef HAVE_CONFIG_H
|
---|
34 | #include <config.h>
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #include "CodePatterns/MemDebug.hpp"
|
---|
38 |
|
---|
39 | #include "SubsetMap.hpp"
|
---|
40 |
|
---|
41 | #include <boost/bind.hpp>
|
---|
42 | #include <boost/foreach.hpp>
|
---|
43 | #include <boost/lambda/lambda.hpp>
|
---|
44 | #include <algorithm>
|
---|
45 | #include <bitset>
|
---|
46 | #include <set>
|
---|
47 |
|
---|
48 | #include "CodePatterns/Assert.hpp"
|
---|
49 | #include "CodePatterns/Log.hpp"
|
---|
50 |
|
---|
51 | SubsetMap::SubsetMap(IndexSetContainer &_container) :
|
---|
52 | tempset(new IndexSet())
|
---|
53 | {
|
---|
54 | /// go through all IndexSets
|
---|
55 | const IndexSetContainer::Container_t &container = _container.getContainer();
|
---|
56 | for (IndexSetContainer::Container_t::const_iterator iter = container.begin();
|
---|
57 | iter != container.end(); ++iter) {
|
---|
58 | // check whether its the super set
|
---|
59 | if (*iter == _container.getSuperSet()) {
|
---|
60 | // place all other sets as its subsets
|
---|
61 | std::vector<IndexSet> returnsets;
|
---|
62 | returnsets.reserve(container.size());
|
---|
63 | for (IndexSetContainer::Container_t::const_iterator insertiter = container.begin();
|
---|
64 | insertiter != container.end(); ++insertiter)
|
---|
65 | if (iter != insertiter) {
|
---|
66 | LOG(2, "INFO: Current subset is " << **insertiter << " for super set.");
|
---|
67 | ASSERT( _container.getSuperSet()->contains(**insertiter),
|
---|
68 | "SubsetMap::SubsetMap() - "+toString(**insertiter)+" is not contained in super set "
|
---|
69 | +toString(*_container.getSuperSet())+".");
|
---|
70 | returnsets.push_back(**insertiter);
|
---|
71 | }
|
---|
72 | Lookup.insert( std::make_pair(*iter, IndexSetContainer::ptr(new IndexSetContainer(returnsets))) );
|
---|
73 | } else {
|
---|
74 | gatherSubsets(*iter);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | void SubsetMap::gatherSubsets(const IndexSet_ptr &ptr) {
|
---|
80 | // get the sets size and the size of the power sets
|
---|
81 | const size_t SetSize = ptr->size();
|
---|
82 | LOG(2, "DEBUG: Current set to gather all subsets for is " << *ptr << " with size " << SetSize << ".");
|
---|
83 | const size_t NoPowerSubsets = getNoPowerSets(SetSize);
|
---|
84 | LOG(2, "DEBUG: There are " << NoPowerSubsets << " sets to check for containment.");
|
---|
85 | std::vector<IndexSet> returnsets;
|
---|
86 | // sets is the number of possible subsets to check: NotContained means still
|
---|
87 | // needs check or is not contained; Contained means is contained, or has
|
---|
88 | // already been treated via a super(sub)set.
|
---|
89 | typedef std::vector<enum ContainedState> StateOfSubsets;
|
---|
90 | StateOfSubsets sets( NoPowerSubsets, NotContained);
|
---|
91 | // we have to traverse backwards, i.e. from greatest subsets to smallest to
|
---|
92 | // make the best possible use of the subset knowledge we already have for these
|
---|
93 | // subsets of the subset.
|
---|
94 | // WARNING: last set is the set itself which must not be in IndexSetContainer due to
|
---|
95 | // cyclic structure which shared_ptr cannot resolve.
|
---|
96 | StateOfSubsets::reverse_iterator iter = sets.rbegin()+1;
|
---|
97 | for (;iter != sets.rend(); iter = std::find(iter+1, sets.rend(), NotContained)) {
|
---|
98 | // get IndexSet (get index via distance)
|
---|
99 | // NOTE: reverse iterator naturally give reverse distance, hence flip both arguments
|
---|
100 | // also, reverse iterator is off by one, see ASSERT
|
---|
101 | const size_t index = std::distance(iter, sets.rend())-1;
|
---|
102 | ASSERT( sets.at(index) == *iter,
|
---|
103 | "SubsetMap::gatherSubsets() - distance from begin to iterator is not correct.");
|
---|
104 | *tempset = getSubset(*ptr, index );
|
---|
105 | LOG(3, "DEBUG: Current subset is " << *tempset << " at " << index << ".");
|
---|
106 | // check whether IndexSet is contained and known
|
---|
107 | if (ptr->contains(*tempset)) {
|
---|
108 | // mark as contained
|
---|
109 | *iter = Contained;
|
---|
110 | LOG(4, "DEBUG: Marking subset as Contained.");
|
---|
111 | if (Lookup.count(tempset)) {
|
---|
112 | LOG(3, "DEBUG: Subset is present in Lookup, adding.");
|
---|
113 | returnsets.push_back(*tempset);
|
---|
114 | // if so, also add its contained sets and mark them out
|
---|
115 | const IndexSetContainer::Container_t &container = getSubsets(tempset)->getContainer();
|
---|
116 | if (container.size()) {
|
---|
117 | LOG(3, "DEBUG: Subset is also present in Lookup, adding all its subsets");
|
---|
118 | for(IndexSetContainer::Container_t::const_iterator containeriter = container.begin();
|
---|
119 | containeriter != container.end();
|
---|
120 | ++containeriter) {
|
---|
121 | const size_t subindex = getPowerSetIndex(ptr, **containeriter);
|
---|
122 | LOG(5, "INFO: Current subset of subset is " << **containeriter << " at " << subindex << ".");
|
---|
123 | if (sets[subindex] != Contained) {
|
---|
124 | LOG(6, "DEBUG: Setting power subset to Contained.");
|
---|
125 | sets[subindex] = Contained;
|
---|
126 | returnsets.push_back(**containeriter);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | LOG(2, "DEBUG: Adding " << returnsets.size() << " sets to Lookup for set " << *ptr << ".");
|
---|
134 | Lookup.insert( std::make_pair(ptr, IndexSetContainer::ptr(new IndexSetContainer(returnsets))) );
|
---|
135 | }
|
---|
136 |
|
---|
137 | const size_t SubsetMap::getPowerSetIndex(const IndexSet_ptr &ptr, const IndexSet &subset) {
|
---|
138 | ASSERT( (ptr->size() < MAX_SETSIZE) && (subset.size() < MAX_SETSIZE),
|
---|
139 | "SubsetMap::getPowerSetIndex() - power sets of subsets must not be bigger than "
|
---|
140 | +toString((int)MAX_SETSIZE)+".");
|
---|
141 | ASSERT( ptr->contains(subset),
|
---|
142 | "SubsetMap::getPowerSetIndex() - "+(toString(*ptr))+" does not contain "
|
---|
143 | +toString(subset)+".");
|
---|
144 | std::bitset<MAX_SETSIZE> bits(0);
|
---|
145 | // go through each and search for indices present in both
|
---|
146 | IndexSet::iterator indexiter = ptr->begin();
|
---|
147 | IndexSet::iterator subindexiter = subset.begin();
|
---|
148 | for (; subindexiter != subset.end();) {
|
---|
149 | if (*indexiter == *subindexiter) {
|
---|
150 | // if matching, set bit and advance both iterators
|
---|
151 | bits.set( (size_t)std::distance(ptr->begin(), indexiter) );
|
---|
152 | ++indexiter;
|
---|
153 | ++subindexiter;
|
---|
154 | // if not, advance the iterator lacking behind
|
---|
155 | } else if ( *indexiter < *subindexiter) {
|
---|
156 | ++indexiter;
|
---|
157 | } else {
|
---|
158 | ++subindexiter;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return bits.to_ulong();
|
---|
162 | }
|
---|
163 |
|
---|
164 | IndexSet SubsetMap::getSubset(const IndexSet &_set, size_t PowerSetNo) {
|
---|
165 | ASSERT( _set.size() < MAX_SETSIZE,
|
---|
166 | "SubsetMap::getSubset() - power sets of subsets must not be bigger than "
|
---|
167 | +toString((int)MAX_SETSIZE)+".");
|
---|
168 | ASSERT((PowerSetNo >= 0) && (PowerSetNo < getNoPowerSets(_set.size())),
|
---|
169 | "SubsetMap::getSubset() - Power set index "+toString(PowerSetNo)+" is out of range.");
|
---|
170 | std::bitset<MAX_SETSIZE> bits(PowerSetNo);
|
---|
171 | // place index into container with random access
|
---|
172 | std::vector<IndexSet::value_type> indices(_set.begin(), _set.end());
|
---|
173 | IndexSet indexset;
|
---|
174 | // go through each bit and insert if 1, not if 0
|
---|
175 | for (size_t index=0; index < indices.size(); ++index)
|
---|
176 | if (bits[index])
|
---|
177 | indexset.insert(indices[index]);
|
---|
178 | return indexset;
|
---|
179 | }
|
---|
180 |
|
---|
181 | size_t SubsetMap::getMaximumSetLevel() const
|
---|
182 | {
|
---|
183 | if (Lookup.empty())
|
---|
184 | return 0;
|
---|
185 | // last one is super set
|
---|
186 | Lookup_t::const_iterator iter = --Lookup.end();
|
---|
187 | return iter->first->size();
|
---|
188 | }
|
---|