ct_common  1.0.1
Common library for combinatorial testing
src/ct_common/common/tuple.cpp
Go to the documentation of this file.
00001 //===----- ct_common/common/tuple.cpp ---------------------------*- C++ -*-===//
00002 //
00003 //                      The ct_common Library
00004 //
00005 // This file is distributed under the MIT license. See LICENSE for details.
00006 //
00007 //===----------------------------------------------------------------------===//
00008 //
00009 // This file contains the function definitions of class Tuple
00010 //
00011 //===----------------------------------------------------------------------===//
00012 
00013 #include <algorithm>
00014 #include <ct_common/common/tuple.h>
00015 #include <ct_common/common/paramspec.h>
00016 
00017 using namespace ct::common;
00018 
00019 Tuple::Tuple(void)
00020     : Assignment(), std::vector<PVPair>() {
00021 }
00022 
00023 Tuple::Tuple(const Tuple &from)
00024     : Assignment(from), std::vector<PVPair>(from) {
00025 }
00026 
00027 Tuple::Tuple(const std::vector<PVPair> &from)
00028     : Assignment(), std::vector<PVPair>(from) {
00029 }
00030 
00031 Tuple &Tuple::operator = (const Tuple &right) {
00032   Assignment::operator =(right);
00033   std::vector<PVPair>::operator =(right);
00034   return *this;
00035 }
00036 
00037 Tuple::~Tuple(void) {
00038 }
00039 
00040 std::vector<std::size_t> Tuple::get_rel_pids(void) const {
00041   std::vector<std::size_t> tmp_return;
00042   for (std::size_t i = 0; i < this->size(); ++i) {
00043     tmp_return.push_back((*this)[i].pid_);
00044   }
00045   return tmp_return;
00046 }
00047 
00048 bool Tuple::operator < (const Tuple &right) const {
00049   if (this->size() < right.size()) {
00050     return true;
00051   }
00052   if (this->size() == right.size()) {
00053     for (std::size_t i = 0; i < this->size(); ++i) {
00054       if ((*this)[i] < right[i]) {
00055         return true;
00056       }
00057       if (right[i] < (*this)[i]) {
00058         return false;
00059       }
00060     }
00061   }
00062   return false;
00063 }
00064 
00065 bool Tuple::operator == (const Tuple &right) const {
00066   if (this->size() != right.size()) {
00067     return false;
00068   }
00069   for (std::size_t i = 0; i < this->size(); ++i) {
00070     if ((*this)[i] != right[i]) {
00071       return false;
00072     }
00073   }
00074   return true;
00075 }
00076 
00077 void Tuple::Sort(void) {
00078   std::sort(this->begin(), this->end());
00079 }
00080 
00081 bool Tuple::IsSubAssignmentOf(const Assignment &assignment) const {
00082   for (std::size_t i = 0; i < this->size(); ++i) {
00083     PVPair pvpair = (*this)[i];
00084     if (!assignment.IsContainParam(pvpair.pid_) || assignment.GetValue(pvpair.pid_) != pvpair.vid_) {
00085       return false;
00086     }
00087   }
00088   return true;
00089 }
00090 
00091 const PVPair *Tuple::Search(std::size_t pid) const {
00092   if (this->size() == 0 || pid == PID_BOUND) {
00093     return 0;
00094   }
00095   std::size_t low = 0;
00096   std::size_t high = this->size() - 1;
00097   while (low+1 < high) {
00098     std::size_t mid = low + (high - low) / 2;
00099     if ((*this)[mid].pid_ > pid) {
00100       high = mid;
00101     } else {
00102       low = mid;
00103     }
00104   }
00105   if ((*this)[low].pid_ == pid) {
00106     return &(*this)[low];
00107   }
00108   if ((*this)[high].pid_ == pid) {
00109     return &(*this)[high];
00110   }
00111   return 0;
00112 }
00113 
00114 bool Tuple::to_the_next_tuple(
00115     std::vector<boost::shared_ptr<ParamSpec> > param_specs) {
00116   if (this->size() == 0) {
00117     return false;
00118   }
00119   this->back().vid_++;
00120   for (std::size_t i = this->size()-1; i>0; i--) {
00121     if ((*this)[i].vid_ >= param_specs[(*this)[i].pid_]->get_level()) {
00122       (*this)[i].vid_ = 0;
00123       (*this)[i-1].vid_++;
00124     } else {
00125       break;
00126     }
00127   }
00128   if ((*this)[0].vid_ >= param_specs[(*this)[0].pid_]->get_level()) {
00129     (*this)[0].vid_ = 0;
00130     return false;
00131   }
00132   return true;
00133 }
00134 
00135 bool Tuple::to_the_next_tuple_with_ivld(
00136     std::vector<boost::shared_ptr<ParamSpec> > param_specs) {
00137   if (this->size() == 0) {
00138     return false;
00139   }
00140   this->back().vid_++;
00141   for (std::size_t i = this->size()-1; i>0; i--) {
00142     if ((*this)[i].vid_ >= param_specs[(*this)[i].pid_]->get_level()+1) {
00143       (*this)[i].vid_ = 0;
00144       (*this)[i-1].vid_++;
00145     } else {
00146       break;
00147     }
00148   }
00149   if ((*this)[0].vid_ >= param_specs[(*this)[0].pid_]->get_level()+1) {
00150     (*this)[0].vid_ = 0;
00151     return false;
00152   }
00153   return true;
00154 }
00155 
00156 bool Tuple::IsContainParam(std::size_t pid) const {
00157   return (this->Search(pid) != 0);
00158 }
00159 
00160 std::size_t Tuple::GetValue(std::size_t pid) const {
00161   const PVPair *pvpair = this->Search(pid);
00162   if (pvpair != 0) {
00163     return pvpair->vid_;
00164   }
00165   return VID_BOUND;
00166 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines