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