FMT 0.9.8
Forest management tools for forest planning
Loading...
Searching...
No Matches
FMToutputnodecache.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2019 Gouvernement du Québec
3
4SPDX-License-Identifier: LiLiQ-R-1.1
5License-Filename: LICENSES/EN/LiLiQ-R11unicode.txt
6*/
7
8#ifndef FMToutputnodecache_H
9#define FMToutputnodecache_H
10
11#include<map>
12#include<vector>
13#include <unordered_map>
14#include "FMToutputnode.hpp"
15#include <boost/serialization/serialization.hpp>
16#include <boost/serialization/map.hpp>
17#include <boost/serialization/vector.hpp>
18#include "FMTtheme.hpp"
19#include "FMTaction.hpp"
20#include "FMTgraph.hpp"
21
22
23namespace Graph
24{
25 template <class tvdescriptor,class titerator>
27 {
29 template<class Archive>
30 void serialize(Archive& ar, const unsigned int version)
31 {
32 ar & BOOST_SERIALIZATION_NVP(inmemorynodes);
33 //ar & BOOST_SERIALIZATION_NVP(basenode);
34 ar & BOOST_SERIALIZATION_NVP(searchtree);
35 }
36 std::vector<tvdescriptor>inmemorynodes;
37 titerator const * beginit;
38 titerator const * endit;
39 //boost::unordered_set<Core::FMTlookup<tvdescriptor, Core::FMTdevelopment>> const * basenode;
40 mutable std::map<Core::FMToutputsource,std::vector<tvdescriptor>>searchtree;
41 typedef typename std::map<Core::FMToutputsource,std::vector<tvdescriptor>>::const_iterator notecacheit;
42 const std::vector<tvdescriptor>& getcleandescriptors(const Core::FMToutputnode& targetnode,const std::vector<Core::FMTaction>& actions,
43 const std::vector<Core::FMTtheme>&themes, bool& exactnode) const
44 {
45 //bool exact = false;
46 exactnode = false;
47 typename std::map<Core::FMToutputsource, std::vector<tvdescriptor>>::const_iterator parent = this->getparentnode(targetnode, actions, exactnode);
48 //Logging::FMTlogger() << "exact? " << exactnode << "\n";
49 if (exactnode)
50 {
51 return parent->second;
52 }
53
54 //std::vector<tvdescriptor> cleaned(basenode);
55 std::vector<tvdescriptor> cleaned;
56 pushtovector(cleaned);
57 //
58 if (parent != searchtree.end())
59 {
60 cleaned = parent->second;
61 }
62 getactionrebuild(targetnode, actions, cleaned, exactnode); // should be able to find also exact!!!!!!!!
63 if (!exactnode)
64 {
65 std::vector<tvdescriptor>toremove;
66 /*size_t basenodesize = inmemorynodes.size();
67 if (basenode != nullptr)
68 {
69 basenodesize = basenode->size();
70 }*/
71 const Core::FMTmask& targetmask = targetnode.source.getmask();
72 //Core::FMTmask unionmask(themes);
73 for (typename std::map<Core::FMToutputsource, std::vector<tvdescriptor>>::const_reverse_iterator sit = searchtree.rbegin();
74 sit != searchtree.rend(); sit++)
75 {
76 const Core::FMTmask& nodemask = sit->first.getmask();
77 if (targetmask.isnotthemessubset(nodemask, themes))//deal only with mask
78 {
79 //unionmask = unionmask.getunion(nodemask);
80 toremove.insert(toremove.end(), sit->second.begin(), sit->second.end());
81 }
82 }
83 if (!toremove.empty())
84 {
85 //std::cout << "mask size of " << unionmask.size() << " " << unionmask.count() << "\n";
86 std::vector<tvdescriptor>difference;
87 std::sort(toremove.begin(), toremove.end());
88 std::set_difference(cleaned.begin(), cleaned.end(),
89 toremove.begin(), toremove.end(), std::inserter(difference, difference.begin()));
90 cleaned = difference;
91 }
92 }
93 std::pair<notecacheit, bool> returniterator;
94 returniterator = searchtree.insert(std::pair<Core::FMToutputsource, std::vector<tvdescriptor>>(targetnode.source, cleaned));
95 return (returniterator.first)->second;
96 }
97 void getactionrebuild(const Core::FMToutputnode& targetnode,
98 const std::vector<Core::FMTaction>& actions,
99 std::vector<tvdescriptor>& cleaned,
100 bool& exactnode) const
101 {
102 const std::string actionname = targetnode.source.getaction();
103 const std::vector<const Core::FMTaction*>aggregatesptr = Core::FMTactioncomparator(actionname).getallaggregates(actions, true);
104 if (!actionname.empty() && !aggregatesptr.empty()) //so it's a aggregate!
105 {
106 std::map<std::string, std::vector< notecacheit>>potentials;
107 for (const Core::FMTaction* attributeptr : aggregatesptr)
108 {
109 potentials[attributeptr->getname()] = std::vector< notecacheit>();
110 }
111 for (notecacheit sit = searchtree.begin();
112 sit != searchtree.end(); sit++)
113 {
114 if (sit->first.issubsetof(targetnode.source, actions))
115 {
116 const std::string nodeaction = sit->first.getaction();
117 potentials[nodeaction].push_back(sit);
118 }
119 }
120 for (const Core::FMTaction* attributeptr : aggregatesptr)
121 {
122 if (potentials.at(attributeptr->getname()).empty())
123 {
124 return; //not a perfect rebuilt need to be complete!!
125 }
126 }
127 typename std::vector< notecacheit>::const_iterator testting = potentials.begin()->second.begin();
128 while (testting != potentials.begin()->second.end())
129 {
130 size_t attid = 0;
131 std::vector<tvdescriptor>finalselection((*testting)->second);
132 size_t insertingdone = 1;
133 for (const auto& attribute : potentials)
134 {
135 if (attid != 0)
136 {
137 for (notecacheit it : potentials.at(attribute.first))
138 {
139 if ((*testting)->first.issamebutdifferentaction(it->first))
140 {
141 finalselection.insert(finalselection.end(), it->second.begin(), it->second.end());
142 ++insertingdone;
143 break;
144 }
145
146 }
147
148 }
149 ++attid;
150 }
151 if (insertingdone == potentials.size())
152 {
153 std::sort(finalselection.begin(), finalselection.end());
154 //Weird fix BF
155 finalselection.erase(std::unique(finalselection.begin(), finalselection.end()),finalselection.end());
156 if ((*testting)->first.issamebutdifferentaction(targetnode.source)) //we got a exact match!!!
157 {
158 exactnode = true;
159 cleaned = finalselection;
160 }
161 else {
162 std::vector<tvdescriptor>intersection;
163 std::set_intersection(cleaned.begin(), cleaned.end(),
164 finalselection.begin(), finalselection.end(), std::inserter(intersection, intersection.begin()));
165 cleaned = intersection;
166 }
167 }
168 ++testting;
169 }
170
171
172 }
173 }
174 notecacheit getparentnode(const Core::FMToutputnode& targetnode, const std::vector<Core::FMTaction>& actions, bool& exactnode) const
175 {
176 notecacheit parentit = searchtree.find(targetnode.source);
177 if (parentit != searchtree.end())
178 {
179 exactnode = true;
180 return parentit;
181 }
182 parentit = searchtree.begin();
183 exactnode = false;
184 while (parentit != searchtree.end())
185 {
186 if (targetnode.source.issubsetof(parentit->first, actions))
187 {
188 return parentit;
189 }
190 ++parentit;
191 }
192 return searchtree.end();
193 }
194 public:
199 /*FMToutputnodecache(const boost::unordered_set<Core::FMTlookup<tvdescriptor,Core::FMTdevelopment>>& initialgraph) :
200 inmemorynodes(),beginit(),endit(), searchtree()
201 {
202 //this->setinitialcache(initialgraph);
203 }*/
204 FMToutputnodecache(const std::vector<tvdescriptor>& initialnodes) :
205 inmemorynodes(initialnodes), beginit(nullptr), endit(nullptr), searchtree()
206 {
207 inmemorynodes.shrink_to_fit();
208 std::sort(inmemorynodes.begin(),inmemorynodes.end());
209 //this->setinitialcache(initialnodes);
210 }
211 FMToutputnodecache(const titerator& first, const titerator& last) :
212 inmemorynodes(), beginit(&first), endit(&last), searchtree()
213 {
214 //this->setinitialcache(initialnodes);
215 }
216 /*unsigned long long getpotentialsize() const
217 {
218 return static_cast<unsigned long long>(searchtree.size()) * static_cast<unsigned long long>(basenode->size()) * sizeof(tvdescriptor);
219 }*/
220
222 {
223 searchtree.erase(node.source);
224 }
225
226 unsigned long long removelargest()
227 {
228 size_t largestsize = 0;
229 unsigned long long removedmemory = 0;
230 notecacheit largestiterator = searchtree.end();
231 for (typename std::map<Core::FMToutputsource, std::vector<tvdescriptor>>::iterator mapit = searchtree.begin(); mapit != searchtree.end(); mapit++)
232 {
233 size_t sizeofvec = mapit->second.size();
234 if (sizeofvec > largestsize)
235 {
236 largestsize = mapit->second.size();
237 largestiterator = mapit;
238 }
239
240 }
241 if (largestiterator != searchtree.end())
242 {
243 removedmemory = largestsize * sizeof(tvdescriptor);
244 //std::vector<tvdescriptor>().swap(largestiterator->second);
245 searchtree.erase(largestiterator);
246 }
247 return removedmemory;
248 }
249 const std::vector<tvdescriptor>& getverticies(const Core::FMToutputnode& targetnode, const std::vector<Core::FMTaction>& actions,
250 const std::vector<Core::FMTtheme>&themes, bool& exactvecticies) const
251 {
252 return this->getcleandescriptors(targetnode, actions, themes, exactvecticies);
253 }
254 void setvalidverticies(const Core::FMToutputnode& targetnode,const std::vector<tvdescriptor>& verticies) const
255 {
256 searchtree[targetnode.source] = verticies;
257 searchtree[targetnode.source].shrink_to_fit();
258 }
259 void clear()
260 {
261 //basenode.clear();
262 beginit = nullptr;
263 endit = nullptr;
264 inmemorynodes.clear();
265 searchtree.clear();
266 }
267 void rebase(const titerator& beginofdevs, const titerator& endofdevs)
268 {
269 beginit = &beginofdevs;
270 endit = &endofdevs;
271 }
272 void insert(const FMToutputnodecache& rhs)
273 {
274 if (beginit==nullptr)
275 {
276 if (inmemorynodes.size() < rhs.inmemorynodes.size())
277 {
278 inmemorynodes = rhs.inmemorynodes;
279 }
280 }
281
282 searchtree.insert(rhs.searchtree.begin(), rhs.searchtree.end());
283 }
284
285 void pushtovector(std::vector<tvdescriptor>& refvecs) const
286 {
287 if (beginit!=nullptr)
288 {
289 for (titerator it = *beginit; it != *endit; ++it)
290 {
291 refvecs.push_back(*it);
292 }
293 refvecs.shrink_to_fit();
294 std::sort(refvecs.begin(), refvecs.end());
295 }
296 else {
297 refvecs = inmemorynodes;
298 }
299 }
300
301 };
302
303
304 /*template<> inline void FMToutputnodecache<FMTgraph<FMTvertexproperties,FMTedgeproperties>::FMTvertex_descriptor,FMTgraph<FMTvertexproperties,FMTedgeproperties>::FMTvertex_iterator>::pushtovector(std::vector<FMTgraph<FMTvertexproperties,FMTedgeproperties>::FMTvertex_descriptor>& refvecs) const
305 {
306 for (FMTgraph<FMTvertexproperties,FMTedgeproperties>::FMTvertex_iterator it = *beginit; it != *endit; ++it)
307 {
308 refvecs.push_back(*it);
309
310 }
311 refvecs.shrink_to_fit();
312 std::sort(refvecs.begin(), refvecs.end());
313 }
314
315 template<> inline void FMToutputnodecache<FMTgraph<FMTbasevertexproperties,FMTbaseedgeproperties>::FMTvertex_descriptor,FMTgraph<FMTbasevertexproperties,FMTbaseedgeproperties>::FMTvertex_iterator>::pushtovector(std::vector<FMTgraph<FMTbasevertexproperties,FMTbaseedgeproperties>::FMTvertex_descriptor>& refvecs) const
316 {
317 for (FMTgraph<FMTbasevertexproperties,FMTbaseedgeproperties>::FMTvertex_iterator it = *beginit; it != *endit; ++it)
318 {
319 refvecs.push_back(*it);
320
321 }
322 refvecs.shrink_to_fit();
323 std::sort(refvecs.begin(), refvecs.end());
324 }*/
325
326}
327
328#endif
Definition: FMTaction.hpp:42
Definition: FMTaction.hpp:327
std::vector< const FMTaction * > getallaggregates(const std::vector< FMTaction > &actions, bool aggregateonly=false) const
Definition: FMTmask.hpp:96
bool isnotthemessubset(const FMTmask &rhs, const std::vector< FMTtheme > &themes) const
Definition: FMToutputnode.hpp:20
FMToutputsource source
Definition: FMToutputnode.hpp:22
Definition: FMToutputsource.hpp:40
const std::string & getaction() const
Definition: FMToutputsource.hpp:102
bool issubsetof(const FMToutputsource &rhs, const std::vector< Core::FMTaction > &actions) const
const FMTmask & getmask() const
Definition: FMToutputsource.hpp:92
Definition: FMToutputnodecache.hpp:27
void insert(const FMToutputnodecache &rhs)
Definition: FMToutputnodecache.hpp:272
void setvalidverticies(const Core::FMToutputnode &targetnode, const std::vector< tvdescriptor > &verticies) const
Definition: FMToutputnodecache.hpp:254
void pushtovector(std::vector< tvdescriptor > &refvecs) const
Definition: FMToutputnodecache.hpp:285
void rebase(const titerator &beginofdevs, const titerator &endofdevs)
Definition: FMToutputnodecache.hpp:267
void clear()
Definition: FMToutputnodecache.hpp:259
FMToutputnodecache & operator=(const FMToutputnodecache &rhs)=default
FMToutputnodecache(const std::vector< tvdescriptor > &initialnodes)
Definition: FMToutputnodecache.hpp:204
FMToutputnodecache(const FMToutputnodecache &rhs)=default
friend class boost::serialization::access
Definition: FMToutputnodecache.hpp:28
void erasenode(const Core::FMToutputnode &node)
Definition: FMToutputnodecache.hpp:221
FMToutputnodecache(const titerator &first, const titerator &last)
Definition: FMToutputnodecache.hpp:211
const std::vector< tvdescriptor > & getverticies(const Core::FMToutputnode &targetnode, const std::vector< Core::FMTaction > &actions, const std::vector< Core::FMTtheme > &themes, bool &exactvecticies) const
Definition: FMToutputnodecache.hpp:249
unsigned long long removelargest()
Definition: FMToutputnodecache.hpp:226
Namespace for using/building unidirectional graphs in FMT.
Definition: FMTareaparser.hpp:31