Hybrid.cpp
Go to the documentation of this file.
1 /************************************************************************
2  * Copyright (C) 2012 Eindhoven University of Technology (TU/e). *
3  * All rights reserved. *
4  ************************************************************************
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above *
10  * copyright notice, this list of conditions and the following *
11  * disclaimer. *
12  * *
13  * 2. Redistributions in binary form must reproduce the above *
14  * copyright notice, this list of conditions and the following *
15  * disclaimer in the documentation and/or other materials *
16  * provided with the distribution. *
17  * *
18  * THIS SOFTWARE IS PROVIDED BY TU/e "AS IS" AND ANY EXPRESS OR *
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
21  * ARE DISCLAIMED. IN NO EVENT SHALL TU/e OR CONTRIBUTORS BE LIABLE *
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
28  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
29  * DAMAGE. *
30  * *
31  * The views and conclusions contained in the software and *
32  * documentation are those of the authors and should not be *
33  * interpreted as representing official policies, either expressed or *
34  * implied, of TU/e. *
35  ************************************************************************/
36 
37 #include "problib/pdfs/Hybrid.h"
38 
39 using namespace pbl;
40 
41 Hybrid::Hybrid() : PDF(-1, PDF::HYBRID), ptr_(0) {
42 }
43 
44 Hybrid::Hybrid(const Hybrid& orig) : PDF(orig), ptr_(orig.ptr_) {
45  if (ptr_) {
46  ++ptr_->n_ptrs_;
47  }
48 }
49 
51  if (ptr_) {
52  --ptr_->n_ptrs_;
53 
54  if (ptr_->n_ptrs_ == 0) {
55  delete ptr_;
56  }
57  }
58 }
59 
61  if (this != &other) {
62  if (ptr_) {
63  --ptr_->n_ptrs_;
64  if (ptr_->n_ptrs_ == 0) {
65  delete ptr_;
66  }
67  }
68  ptr_ = other.ptr_;
69  ++ptr_->n_ptrs_;
70 
71  dimensions_ = other.dimensions_;
72  }
73  return *this;
74 }
75 
77  return new Hybrid(*this);
78 }
79 
81  if (ptr_->n_ptrs_ > 1) {
82  --ptr_->n_ptrs_;
83  ptr_ = new HybridStruct(*ptr_);
84  }
85 }
86 
87 double Hybrid::getLikelihood(const PDF& pdf) const {
88  assert_msg(false, "Likelihood method not implemented. Please create a subclass of Hybrid and implement your own method.");
89 }
90 
91 void Hybrid::clear() {
92  if (ptr_) {
93  --ptr_->n_ptrs_;
94  if (ptr_->n_ptrs_ == 0) {
95  delete ptr_;
96  }
97  ptr_ = 0;
98  }
99 }
100 
101 double Hybrid::getMaxDensity() const {
102  assert_msg(false, "Cannot calculate MaxDensity of Hybrid.");
103  return 0;
104 }
105 
106 void Hybrid::addPDF(const PDF& pdf, double priority) {
107  if (dimensions_ < 0) {
108  dimensions_ = pdf.dimensions();
109  } else {
110  assert(dimensions_ == pdf.dimensions() || pdf.type() == PDF::DISCRETE);
111  }
112 
113  if (!ptr_) {
114  ptr_ = new HybridStruct();
115  } else {
116  cloneStruct();
117  }
118 
119  ptr_->pdfs_.push_back(pdf.clone());
120 }
121 
122 const std::vector<PDF*>& Hybrid::getPDFS() const {
123  assert_msg(ptr_, "Hybrid does not contain pdfs.");
124  return ptr_->pdfs_;
125 }
126 
127 std::string Hybrid::toString(const std::string& indent) const {
128  if (!ptr_) {
129  return "HYBRID(-)";
130  }
131 
132  std::string new_indent = indent + " ";
133 
134  std::stringstream ss;
135  ss << "HYBRID{\n";
136  for (std::vector<PDF*>::const_iterator it_pdf = ptr_->pdfs_.begin(); it_pdf != ptr_->pdfs_.end(); ++it_pdf) {
137  ss << new_indent << (*it_pdf)->toString(new_indent) << "\n";
138  }
139  ss << indent << "}";
140  return ss.str();
141 }
int dimensions() const
Definition: PDF.cpp:52
PDFType type() const
Definition: PDF.cpp:56
int dimensions_
Definition: PDF.h:87
#define assert_msg(_Expression, _Msg)
Definition: globals.h:53
void cloneStruct()
Definition: Hybrid.cpp:80
Hybrid * clone() const
Definition: Hybrid.cpp:76
virtual ~Hybrid()
Definition: Hybrid.cpp:50
void addPDF(const PDF &pdf, double priority)
Definition: Hybrid.cpp:106
virtual PDF * clone() const =0
double getMaxDensity() const
Definition: Hybrid.cpp:101
const_iterator(const field< oT > &in_M, const bool at_end=false)
const std::vector< PDF * > & getPDFS() const
Definition: Hybrid.cpp:122
Hybrid & operator=(const Hybrid &other)
Definition: Hybrid.cpp:60
virtual double getLikelihood(const PDF &pdf) const
Definition: Hybrid.cpp:87
Definition: PDF.h:47
void clear()
Definition: Hybrid.cpp:91
HybridStruct * ptr_
Definition: Hybrid.h:65
std::vector< PDF * > pdfs_
Definition: Hybrid.h:45
std::string toString(const std::string &indent="") const
Definition: Hybrid.cpp:127


problib
Author(s): Sjoerd van den Dries, Jos Elfring
autogenerated on Fri Apr 16 2021 02:32:19