ProteoWizard
bucket1d.hpp
Go to the documentation of this file.
1 //
2 // $Id: bucket1d.hpp 5313 2013-12-17 18:06:54Z chambm $
3 //
4 //
5 // Original author: Witold Wolski <wewolski@gmail.com>
6 //
7 // Copyright : ETH Zurich
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 
23 #ifndef BUCKET1D_H
24 #define BUCKET1D_H
25 
26 #include <vector>
27 #include <stdexcept>
28 #include <boost/cstdint.hpp>
29 
31 
32 
33 namespace ralab{
34  namespace base{
35  namespace resample{
36  /*!\given breaks and data compute frequencies in bins*/
38 struct Bucket1D
39  {
40  private:
41  std::vector<double> breaks_; // boundaries
42  std::vector<uint32_t> indicator_;//Has length length(breaks_) - 1, and indicates if bin is of interest.
43  //this code is required to avoid bound checking in release.
44  //Some stupid bruker compiler settings.
45  double * begbreaks_;
46  double * endbreaks_;
47  uint32_t * indicatorptr_;
48  public:
49  /*!\brief CTor*/
51  std::vector<double> & breaks, // breaks
52  std::vector<uint32_t> & indic // indicator
53  ):breaks_(), indicator_(), begbreaks_(0), endbreaks_(0), indicatorptr_(0)
54  {
55  set( breaks , indic );
56  }
57 
58  /*!\brief CCTor*/
60  const Bucket1D & rhs
61  ):breaks_(), indicator_(), begbreaks_(0), endbreaks_(0), indicatorptr_(0)
62  {
63  this->set(rhs.breaks_,rhs.indicator_);
64  }
65 
66  private:
67  /*!\brief set the data*/
68  void set( const std::vector<double> & breaks,
69  const std::vector<uint32_t> & indic )
70  {
71  if(( breaks.size() - 1 ) != indic.size()){
72  throw std::out_of_range( "breaks.size == inic + 1 , failed!" );
73  }
74  breaks_ = breaks;
75  begbreaks_ = &breaks_[0];
76  endbreaks_ = begbreaks_ + breaks_.size();
77  indicator_ = indic;
78  indicatorptr_ = &indicator_[0];
79  }
80 
81  public:
82 
83  /*! \Assignment */
85  {
86  if (this == &rhs) // protect against invalid self-assignment
87  return *this; // See "4:"
88  this->set( rhs.breaks_, rhs.indicator_);
89  return *this;
90  }
91 
92  /*!\brief
93  The result tells you in which bucket which input should end up.
94  */
95  template<typename InputIterator>
96  void operator()(
97  InputIterator beg, //!< Check wich of these masses should be bucketed
98  InputIterator end, //!<
99  std::vector<std::pair<std::size_t, std::size_t> > & bucketPairs //indicates an successful assignment, first: index in bucket second: index in input
100  )
101  {
102  std::size_t index(0);
103  std::pair<std::size_t, bool> res;
104  for(;beg !=end; ++beg, ++index)
105  {
106  res = this->operator()(*beg);
107  if(res.second)
108  {
109  bucketPairs.push_back(std::make_pair(res.first , index ));
110  }
111  }
112  }
113 
114  /*!\brief
115 
116  the std::size_t indicates to which bucket dat belongs too.
117  The bool indicates if a new bucket is of interest
118  */
119  std::pair<std::size_t, bool> operator()(double dat)
120  {
121  double * it2 = std::lower_bound(begbreaks_,endbreaks_,dat);
122  std::size_t ub = std::distance(begbreaks_,it2);
123 
124  if(ub > 0 && ub <= indicator_.size())
125  {
126  ub = ub - 1;
127  if(*(indicatorptr_ + ub) > 0)
128  return std::make_pair(ub, true);
129  else
130  return std::make_pair(ub, false);
131  }
132  else
133  return std::make_pair(0, false);
134  }
135 
136  }; //Bucket1D
137  }//namespace resample
138  }//namespace base
139 }//namespace ralab
140 
141 #endif // BUCKET1D_H
Bucket1D(std::vector< double > &breaks, std::vector< uint32_t > &indic)
CTor.
Definition: bucket1d.hpp:50
Bucket1D(const Bucket1D &rhs)
CCTor.
Definition: bucket1d.hpp:59
std::vector< uint32_t > indicator_
Definition: bucket1d.hpp:42
std::vector< double > breaks_
Definition: bucket1d.hpp:41
void breaks(double minMass, double maxMass, TMassComparator tmassComp, std::vector< double > &breaks, bool exact=false)
Segment mass range according to Mass Compare functor could be used to histogram a dataset or to compu...
Definition: breakspec.hpp:41
Bucket1D & operator=(const Bucket1D &rhs)
Definition: bucket1d.hpp:84
std::pair< std::size_t, bool > operator()(double dat)
the std::size_t indicates to which bucket dat belongs too. The bool indicates if a new bucket is of i...
Definition: bucket1d.hpp:119
EQUISPACEINTERPOL Interpolation on a equidistantly spaced grid.
Definition: base.hpp:39
boost::uint32_t uint32_t
Definition: bucket1d.hpp:37
void operator()(InputIterator beg, InputIterator end, std::vector< std::pair< std::size_t, std::size_t > > &bucketPairs)
The result tells you in which bucket which input should end up.
Definition: bucket1d.hpp:96