00001 /************************************************************************ 00002 * Copyright (C) 2012 Eindhoven University of Technology (TU/e). * 00003 * All rights reserved. * 00004 ************************************************************************ 00005 * Redistribution and use in source and binary forms, with or without * 00006 * modification, are permitted provided that the following conditions * 00007 * are met: * 00008 * * 00009 * 1. Redistributions of source code must retain the above * 00010 * copyright notice, this list of conditions and the following * 00011 * disclaimer. * 00012 * * 00013 * 2. Redistributions in binary form must reproduce the above * 00014 * copyright notice, this list of conditions and the following * 00015 * disclaimer in the documentation and/or other materials * 00016 * provided with the distribution. * 00017 * * 00018 * THIS SOFTWARE IS PROVIDED BY TU/e "AS IS" AND ANY EXPRESS OR * 00019 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * 00020 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * 00021 * ARE DISCLAIMED. IN NO EVENT SHALL TU/e OR CONTRIBUTORS BE LIABLE * 00022 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * 00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * 00024 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * 00025 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * 00026 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 00027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * 00028 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * 00029 * DAMAGE. * 00030 * * 00031 * The views and conclusions contained in the software and * 00032 * documentation are those of the authors and should not be * 00033 * interpreted as representing official policies, either expressed or * 00034 * implied, of TU/e. * 00035 ************************************************************************/ 00036 00037 #include "problib/pdfs/Hybrid.h" 00038 00039 using namespace pbl; 00040 00041 Hybrid::Hybrid() : PDF(-1, PDF::HYBRID), ptr_(0) { 00042 } 00043 00044 Hybrid::Hybrid(const Hybrid& orig) : PDF(orig), ptr_(orig.ptr_) { 00045 if (ptr_) { 00046 ++ptr_->n_ptrs_; 00047 } 00048 } 00049 00050 Hybrid::~Hybrid() { 00051 if (ptr_) { 00052 --ptr_->n_ptrs_; 00053 00054 if (ptr_->n_ptrs_ == 0) { 00055 delete ptr_; 00056 } 00057 } 00058 } 00059 00060 Hybrid& Hybrid::operator=(const Hybrid& other) { 00061 if (this != &other) { 00062 if (ptr_) { 00063 --ptr_->n_ptrs_; 00064 if (ptr_->n_ptrs_ == 0) { 00065 delete ptr_; 00066 } 00067 } 00068 ptr_ = other.ptr_; 00069 ++ptr_->n_ptrs_; 00070 00071 dimensions_ = other.dimensions_; 00072 } 00073 return *this; 00074 } 00075 00076 Hybrid* Hybrid::clone() const { 00077 return new Hybrid(*this); 00078 } 00079 00080 void Hybrid::cloneStruct() { 00081 if (ptr_->n_ptrs_ > 1) { 00082 --ptr_->n_ptrs_; 00083 ptr_ = new HybridStruct(*ptr_); 00084 } 00085 } 00086 00087 double Hybrid::getLikelihood(const PDF& pdf) const { 00088 assert_msg(false, "Likelihood method not implemented. Please create a subclass of Hybrid and implement your own method."); 00089 } 00090 00091 void Hybrid::clear() { 00092 if (ptr_) { 00093 --ptr_->n_ptrs_; 00094 if (ptr_->n_ptrs_ == 0) { 00095 delete ptr_; 00096 } 00097 ptr_ = 0; 00098 } 00099 } 00100 00101 double Hybrid::getMaxDensity() const { 00102 assert_msg(false, "Cannot calculate MaxDensity of Hybrid."); 00103 return 0; 00104 } 00105 00106 void Hybrid::addPDF(const PDF& pdf, double priority) { 00107 if (dimensions_ < 0) { 00108 dimensions_ = pdf.dimensions(); 00109 } else { 00110 assert(dimensions_ == pdf.dimensions() || pdf.type() == PDF::DISCRETE); 00111 } 00112 00113 if (!ptr_) { 00114 ptr_ = new HybridStruct(); 00115 } else { 00116 cloneStruct(); 00117 } 00118 00119 ptr_->pdfs_.push_back(pdf.clone()); 00120 } 00121 00122 const std::vector<PDF*>& Hybrid::getPDFS() const { 00123 assert_msg(ptr_, "Hybrid does not contain pdfs."); 00124 return ptr_->pdfs_; 00125 } 00126 00127 std::string Hybrid::toString(const std::string& indent) const { 00128 if (!ptr_) { 00129 return "HYBRID(-)"; 00130 } 00131 00132 std::string new_indent = indent + " "; 00133 00134 std::stringstream ss; 00135 ss << "HYBRID{\n"; 00136 for (std::vector<PDF*>::const_iterator it_pdf = ptr_->pdfs_.begin(); it_pdf != ptr_->pdfs_.end(); ++it_pdf) { 00137 ss << new_indent << (*it_pdf)->toString(new_indent) << "\n"; 00138 } 00139 ss << indent << "}"; 00140 return ss.str(); 00141 }