FMT 0.9.8
Forest management tools for forest planning
Loading...
Searching...
No Matches
FMTmask.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 FMTMASK_H_INCLUDED
9#define FMTMASK_H_INCLUDED
10
11#ifndef BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS
12#define BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS
13#endif
14
15#include <boost/dynamic_bitset.hpp>
16#include <boost/unordered_map.hpp>
17#include <boost/algorithm/string.hpp>
18#include <boost/algorithm/string/join.hpp>
19#include <boost/functional/hash.hpp>
20#include <boost/serialization/serialization.hpp>
21#include <boost/serialization/split_free.hpp>
22#include <boost/serialization/nvp.hpp>
23#include <boost/serialization/export.hpp>
24#include <boost/serialization/string.hpp>
25#include <boost/functional/hash.hpp>
26#include "FMTexception.hpp"
27#include <vector>
28#include <string>
29
30
34namespace boost {
35 namespace serialization {
36
37 template <typename Ar, typename Block, typename Alloc>
38 void save(Ar& ar, dynamic_bitset<Block, Alloc> const& bs, unsigned) {
39 size_t num_bits = bs.size();
40 std::vector<Block> blocks(bs.num_blocks());
41 to_block_range(bs, blocks.begin());
42
43 ar & BOOST_SERIALIZATION_NVP(num_bits);
44 ar & BOOST_SERIALIZATION_NVP(blocks);
45 }
46
47 template <typename Ar, typename Block, typename Alloc>
48 void load(Ar& ar, dynamic_bitset<Block, Alloc>& bs, unsigned) {
49 size_t num_bits;
50 std::vector<Block> blocks;
51 ar & BOOST_SERIALIZATION_NVP(num_bits);
52 ar & BOOST_SERIALIZATION_NVP(blocks);
53
54 bs.resize(num_bits);
55 from_block_range(blocks.begin(), blocks.end(), bs);
56 bs.resize(num_bits);
57 }
58
59 template <typename Ar, typename Block, typename Alloc>
60 void serialize(Ar& ar, dynamic_bitset<Block, Alloc>& bs, unsigned version) {
61 split_free(ar, bs, version);
62 }
63
64 }
65}
69#if (BOOST_VERSION / 100 % 1000) < 71
70namespace boost {
71 template <typename Block, typename Alloc>
72 std::size_t hash_value(const boost::dynamic_bitset<Block, Alloc>& bs)
73 {
74 std::size_t hash = hash_value(bs.m_num_bits);
75 boost::hash_combine(hash, bs.m_bits);
76 return hash;
77 }
78}
79#endif
80
81namespace Core
82{
83
84class FMTtheme;
85class FMTmaskfilter;
86// DocString: FMTmask
96 {
97 friend class FMTtheme;
98 // DocString: FMTmask::serialize
102 friend class boost::serialization::access;
103 template<class Archive>
104 void serialize(Archive& ar, const unsigned int version)
105 {
106 ar & BOOST_SERIALIZATION_NVP(data);
107 ar & BOOST_SERIALIZATION_NVP(name);
108 }
109 // DocString: FMTmask::subset
114 boost::dynamic_bitset<> subset(const FMTtheme& theme) const;
115 // DocString: FMTmask::setsubset
119 void setsubset(const FMTtheme& theme,const boost::dynamic_bitset<>& subset);
120 // DocString: FMTmask::name
122 std::string name;
123 // DocString: FMTmask::data
125 boost::dynamic_bitset<> data;
126 public:
127 // DocString: swap()
131 void swap(FMTmask& rhs);
132 // DocString: FMTmask()
137 // DocString: FMTmask(const FMTmask&)
141 FMTmask(const FMTmask& rhs);
142 // DocString: ~FMTmask()
146 virtual ~FMTmask()=default;
147 // DocString: FMTmask(const boost::dynamic_bitset<>&)
151 FMTmask(const boost::dynamic_bitset<>& bits);
152 // DocString: FMTmask::operator bool
156 explicit operator bool() const;
157 // DocString: FMTmask(const std::string&,const boost::dynamic_bitset<>&)
161 FMTmask(const std::string& mask,const boost::dynamic_bitset<>& bits);
162 // DocString: FMTmask(const std::string&,const std::vector<FMTtheme>&)
166 FMTmask(const std::string& mask,const std::vector<FMTtheme>& themes);
167 // DocString: FMTmask(const std::vector<std::string>&,const std::vector<FMTtheme>&)
172 FMTmask(const std::vector<std::string>& values,const std::vector<FMTtheme>& themes);
173 // DocString: FMTmask(const std::vector<FMTtheme>&,const std::vector<FMTtheme>&)
178 FMTmask(const std::vector<FMTtheme>& themes);
179 // DocString: FMTmask::clear
183 void clear();
184 // DocString: FMTmask::size
188 size_t size() const;
189 // DocString: FMTmask::count
193 size_t count() const;
194 // DocString: FMTmask::get
198 std::string get(const std::vector<FMTtheme>& themes) const;
199 // DocString: FMTmask::get
203 std::string get(const FMTtheme& theme) const;
204 // DocString: FMTmask::getsubsetcount
208 size_t getsubsetcount(const FMTtheme& theme) const;
209 // DocString: FMTmask::set
213 void set(const std::vector<FMTtheme>& themes,const std::string& value);
214 // DocString: FMTmask::set
218 void set(const FMTtheme& theme, const std::string& value);
219 // DocString: FMTmask::getstaticthemes
223 std::vector<FMTtheme>getstaticthemes(const std::vector<FMTtheme>& themes) const;
224 // DocString: FMTmask::getselectedthemes
228 std::vector<FMTtheme> getselectedthemes(const std::vector<FMTtheme>& themes) const;
229 // DocString: FMTmask::isnotthemessubset
233 bool isnotthemessubset(const FMTmask& rhs, const std::vector<FMTtheme>& themes) const;
234 // DocString: FMTmask::removeaggregates
239 FMTmask removeaggregates(const std::vector<FMTtheme>& themes,bool questionmarkonly=false) const;
240 // DocString: FMTmask::empty
244 bool empty() const;
245 // DocString: FMTmask::update
249 void update(const std::vector<FMTtheme>& themes);
250 // DocString: FMTmask::decompose
255 std::vector<FMTmask>decompose(const FMTtheme &theme) const;
256 // DocString: FMTmask::append
260 void append(const boost::dynamic_bitset<> &bits);
261 // DocString: FMTmask::binarizedappend
265 template <class typetobinarize>
266 void binarizedappend(const typetobinarize& element)
267 {
268 size_t location = data.size();
269 data.resize(data.size() + (sizeof(typetobinarize) * 8));
270 const char* charelements = reinterpret_cast<const char*>(&element);
271 for (size_t charit = 0; charit < sizeof(typetobinarize); ++charit)
272 {
273 const char value = charelements[charit];
274 for (int i = 7; i >= 0; --i)
275 {
276 data[location] = ((value & (1 << i)));
277 ++location;
278 }
279 }
280 }
281 // DocString: FMTmask::getunion
285 FMTmask getunion(const FMTmask& rhs) const;
286 // DocString: FMTmask::getintersect
290 FMTmask getintersect(const FMTmask& rhs) const;
291 // DocString: FMTmask::getbitsetintersect
296 boost::dynamic_bitset<> getbitsetintersect(const FMTmask& rhs) const;
297 // DocString: FMTmask::operator=
301 FMTmask& operator = (const FMTmask& rhs);
302 // DocString: FMTmask::operator!=
306 bool operator != (const FMTmask& rhs) const;
307 // DocString: FMTmask::operator==
311 bool operator == (const FMTmask& rhs) const;
312 // DocString: FMTmask::operator<
316 bool operator < (const FMTmask& rhs) const;
317 // DocString: FMTmask::resume
321 FMTmask resume(const boost::dynamic_bitset<>& rhs) const;
322 // DocString: FMTmask::resume
326 FMTmask resume(const std::vector<size_t>& indexes) const;
327 // DocString: FMTmask::hash
331 inline size_t hash() const
332 {
333 return boost::hash<boost::dynamic_bitset<>>()(data);
334 }
335 // DocString: FMTmask::getbitsstring
339 std::string getbitsstring() const;
340 // DocString: FMTmask::operator std::string
344 inline operator std::string() const
345 {
346 return name;
347 }
348 // DocString: FMTmask::issubsetof
352 inline bool issubsetof(const boost::dynamic_bitset<>& rhs) const
353 {
354 return data.is_subset_of(rhs);
355 }
356 // DocString: FMTmask::issubsetof
360 inline bool issubsetof(const FMTmask& rhs) const
361 {
362 return data.is_subset_of(rhs.data);
363 }
364 // DocString: FMTmask::getbitsetreference
368 inline const boost::dynamic_bitset<>& getbitsetreference() const
369 {
370 return data;
371 }
372 // DocString: FMTmask::getstringreference
376 inline const std::string& getstringreference() const
377 {
378 return name;
379 }
380 // DocString: FMTmask::refine
385 FMTmask refine(const FMTmask& mask, const std::vector<FMTtheme>& themes) const;
386 // DocString: FMTmask::getpostsolvemask
390 FMTmask getpostsolvemask(const FMTmask& mask,const std::vector<FMTtheme>& themes) const;
391 // DocString: FMTmask::presolve
396 FMTmask presolve(const FMTmaskfilter& filter,const std::vector<FMTtheme>&presolvedthemes) const;
397 // DocString: FMTmask::postsolve
401 FMTmask postsolve(const FMTmaskfilter& filter, const std::vector<FMTtheme>&basethemes) const;
402 };
403
404
405template<> inline void FMTmask::binarizedappend<std::string>(const std::string& element)
406 {
407 size_t location = data.size();
408 data.resize(data.size() + (element.size() * 8));
409 const char* charelements = element.c_str();
410 for (size_t charit = 0; charit < element.size(); ++charit)
411 {
412 const char value = charelements[charit];
413 for (int i = 7; i >= 0; --i)
414 {
415 data[location] = ((value & (1 << i)));
416 ++location;
417 }
418 }
419 }
420
421template<> inline void FMTmask::binarizedappend<double>(const double& element)
422{
423 size_t location = data.size();
424 const int corrected = static_cast<int>(element*(1 / FMT_DBL_TOLERANCE));
425 data.resize(data.size() + (sizeof(int) * 8));
426 const char* charelements = reinterpret_cast<const char*>(&corrected);
427 for (size_t charit = 0; charit < sizeof(int); ++charit)
428 {
429 const char value = charelements[charit];
430 for (int i = 7; i >= 0; --i)
431 {
432 data[location] = ((value & (1 << i)));
433 ++location;
434 }
435 }
436}
437
438// DocString: FMTmaskcomparator
443 {
444 // DocString: FMTmaskcomparator::base_mask
446 FMTmask base_mask;
447 public:
448 // DocString: FMTmaskcomparator:(const FMTmask&)
452 FMTmaskcomparator(const FMTmask& lbase_mask);
453 // DocString: FMTmaskcomparator::operator()(const FMTaction&)
457 bool operator()(const FMTmask& mask) const;
458
459 };
460
461}
462
463namespace boost {
467 template <>
468 struct hash<Core::FMTmask>
469 {
470 std::size_t operator()(const Core::FMTmask& mask) const
471 {
472 return (mask.hash());
473 }
474 };
475
476}
477
478BOOST_CLASS_EXPORT_KEY(Core::FMTmask)
479
480#endif // FMTMASK_H_INCLUDED
#define FMT_DBL_TOLERANCE
Definition: FMTutility.hpp:11
#define FMTEXPORT
Definition: FMTutility.hpp:92
Definition: FMTmask.hpp:96
std::vector< FMTtheme > getstaticthemes(const std::vector< FMTtheme > &themes) const
std::string getbitsstring() const
FMTmask(const boost::dynamic_bitset<> &bits)
std::string get(const std::vector< FMTtheme > &themes) const
FMTmask(const std::vector< FMTtheme > &themes)
FMTmask(const FMTmask &rhs)
FMTmask(const std::string &mask, const boost::dynamic_bitset<> &bits)
std::string get(const FMTtheme &theme) const
FMTmask resume(const boost::dynamic_bitset<> &rhs) const
FMTmask(const std::vector< std::string > &values, const std::vector< FMTtheme > &themes)
FMTmask(const std::string &mask, const std::vector< FMTtheme > &themes)
bool issubsetof(const FMTmask &rhs) const
Definition: FMTmask.hpp:360
void set(const std::vector< FMTtheme > &themes, const std::string &value)
const boost::dynamic_bitset & getbitsetreference() const
Definition: FMTmask.hpp:368
FMTmask presolve(const FMTmaskfilter &filter, const std::vector< FMTtheme > &presolvedthemes) const
void binarizedappend(const typetobinarize &element)
Definition: FMTmask.hpp:266
void swap(FMTmask &rhs)
void set(const FMTtheme &theme, const std::string &value)
virtual ~FMTmask()=default
const std::string & getstringreference() const
Definition: FMTmask.hpp:376
FMTmask getintersect(const FMTmask &rhs) const
size_t size() const
size_t count() const
FMTmask getunion(const FMTmask &rhs) const
bool empty() const
FMTmask refine(const FMTmask &mask, const std::vector< FMTtheme > &themes) const
bool issubsetof(const boost::dynamic_bitset<> &rhs) const
Definition: FMTmask.hpp:352
void update(const std::vector< FMTtheme > &themes)
FMTmask removeaggregates(const std::vector< FMTtheme > &themes, bool questionmarkonly=false) const
FMTmask resume(const std::vector< size_t > &indexes) const
FMTmask getpostsolvemask(const FMTmask &mask, const std::vector< FMTtheme > &themes) const
boost::dynamic_bitset getbitsetintersect(const FMTmask &rhs) const
size_t getsubsetcount(const FMTtheme &theme) const
void append(const boost::dynamic_bitset<> &bits)
FMTmask postsolve(const FMTmaskfilter &filter, const std::vector< FMTtheme > &basethemes) const
std::vector< FMTtheme > getselectedthemes(const std::vector< FMTtheme > &themes) const
size_t hash() const
Definition: FMTmask.hpp:331
bool isnotthemessubset(const FMTmask &rhs, const std::vector< FMTtheme > &themes) const
std::vector< FMTmask > decompose(const FMTtheme &theme) const
Definition: FMTmask.hpp:443
bool operator()(const FMTmask &mask) const
FMTmaskcomparator(const FMTmask &lbase_mask)
Definition: FMTmaskfilter.hpp:27
Definition: FMTtheme.hpp:47
The Core namespace provides classes for simulating stands/strata growth/harvest through time.
Definition: FMTaction.hpp:31
void load(Ar &ar, dynamic_bitset< Block, Alloc > &bs, unsigned)
Definition: FMTmask.hpp:48
void serialize(Ar &ar, dynamic_bitset< Block, Alloc > &bs, unsigned version)
Definition: FMTmask.hpp:60
void save(Ar &ar, dynamic_bitset< Block, Alloc > const &bs, unsigned)
Definition: FMTmask.hpp:38
Definition: FMTaction.hpp:364
std::size_t hash_value(const boost::dynamic_bitset< Block, Alloc > &bs)
Definition: FMTmask.hpp:72
std::size_t operator()(const Core::FMTmask &mask) const
Definition: FMTmask.hpp:470