npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
OrderedPointND.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ORDEREDPOINTND_HH_
2 #define NPSTAT_ORDEREDPOINTND_HH_
3 
4 /*!
5 // \file OrderedPointND.hh
6 //
7 // \brief Multidimensional points which can be sorted according to
8 // multiple sorting criteria
9 //
10 // Author: I. Volobouev
11 //
12 // September 2010
13 */
14 
15 #include <vector>
16 
18 
19 namespace npstat {
20  template <typename Point> class OrderedPointND;
21 
22  template <typename Point>
23  bool operator==(const OrderedPointND<Point>& l,
24  const OrderedPointND<Point>& r);
25 
26  template <typename Point>
27  bool operator!=(const OrderedPointND<Point>& l,
28  const OrderedPointND<Point>& r);
29 
30  /**
31  // Multivariate point which can remember its sequence number according to
32  // multiple sorting criteria. These points can be used, for example, to
33  // build composite distributions (i.e., modeled by copula and marginals).
34  //
35  // Template parameter class Point must be subscriptable. It must also
36  // publish "value_type" typedef (std::array is a good candidate).
37  */
38  template <class Point>
40  {
41  public:
42  typedef typename Point::value_type value_type;
43  enum {dim_size = PointDimensionality<Point>::dim_size};
44 
45  /** Default constructor will invoke the default Point constructor */
47 
48  /** Constructor from the underlying point */
49  OrderedPointND(const Point& point);
50 
51  /** Costructor from a data array for the underlying point */
52  template <typename Num2>
53  OrderedPointND(const Num2* data, unsigned dataLen);
54 
55  /**
56  // Costructor from a flat data array and a set of indices
57  // into this array
58  */
59  template <typename Num2>
60  OrderedPointND(const Num2* data, const unsigned* indices,
61  unsigned lenIndices);
62 
63  /**
64  // Constructor from the underlying point and its numbers in
65  // several sorted sequences
66  */
67  OrderedPointND(const Point& point, const unsigned long *number,
68  unsigned lenNumber);
69  //@{
70  /** Examine object properties */
71  inline const Point& point() const {return point_;}
72  inline const value_type* coords() const {return &point_[0];}
73  inline const unsigned long* number() const {return number_;}
74  inline unsigned dim() const {return dim_size;}
75  inline unsigned size() const {return dim_size;}
76  //@}
77 
78  /** Non-const subscripting */
79  inline value_type& operator[](unsigned i) {return point_[i];}
80 
81  /** Const subscripting */
82  inline const value_type& operator[](unsigned i) const
83  {return point_[i];}
84 
85  /** Set point number according to sorting criterion i */
86  void setNumber(unsigned i, unsigned long value);
87 
88  /** Get point number according to sorting criterion i */
89  unsigned long getNumber(unsigned i) const;
90 
91  friend bool operator==<>(const OrderedPointND&, const OrderedPointND&);
92  friend bool operator!=<>(const OrderedPointND&, const OrderedPointND&);
93 
94  private:
95  void clearNumber();
96 
97  Point point_;
98  unsigned long number_[dim_size];
99  };
100 
101  /**
102  // Fill a vector of ordered points using unordered points
103  // (but do not order them yet). Note that the output vector
104  // is _not_ cleared before filling. Class Point1 should be
105  // subscriptable.
106  //
107  // Parameters "dimsToUse" and "nDimsToUse" specify how
108  // dimensions of Point2 should be constructed out of
109  // those in Point1.
110  */
111  template <class Point1, class Point2>
112  void fillOrderedPoints(const std::vector<Point1>& data,
113  const unsigned* dimsToUse, unsigned nDimsToUse,
114  std::vector<OrderedPointND<Point2> >* out);
115 
116  /**
117  // Fill a vector of ordered points using flat data.
118  // Flat data is divided into chunks "stride" elements
119  // long. Then numbers are picked up from these chunks
120  // according to "dimsToUse" and "nDimsToUse" arguments.
121  */
122  template <typename Num2, class Point>
123  void fillOrderedFromFlat(const std::vector<Num2>& data, unsigned stride,
124  const unsigned* dimsToUse, unsigned nDimsToUse,
125  std::vector<OrderedPointND<Point> >* out);
126 
127  /** Fill a vector of points using flat data */
128  template <typename Num2, class Point>
129  void fillPointsFromFlat(const std::vector<Num2>& data, unsigned stride,
130  const unsigned* dimsToUse, unsigned nDimsToUse,
131  std::vector<Point>* out);
132 
133  /** Fill a flat vector from another flat vector */
134  template <typename Num1, typename Num2>
135  void fillFlatFromFlat(const std::vector<Num1>& data, unsigned stride,
136  const unsigned* dimsToUse, unsigned nDimsToUse,
137  std::vector<Num2>* out);
138 
139 #ifdef SWIG
140  template <class Point>
141  inline std::vector<OrderedPointND<Point> >*
142  makeOrderedPoints(const std::vector<Point>& data)
143  {
144  std::vector<OrderedPointND<Point> >* opoints =
145  new std::vector<OrderedPointND<Point> >();
146  const unsigned long sz = data.size();
147  opoints->reserve(sz);
148  for (unsigned long i=0; i<sz; ++i)
149  opoints->emplace_back(data[i]);
150  return opoints;
151  }
152 
153  template <typename Num2, class Point>
154  inline std::vector<OrderedPointND<Point> >*
155  makeOrderedFromFlat(const std::vector<Num2>& data, unsigned stride,
156  const unsigned* dimsToUse, unsigned nDimsToUse)
157  {
158  std::vector<OrderedPointND<Point> >* result =
159  new std::vector<OrderedPointND<Point> >();
160  fillOrderedFromFlat(data, stride, dimsToUse, nDimsToUse, result);
161  return result;
162  }
163 
164  template <typename Num2, class Point>
165  inline std::vector<Point>*
166  makePointsFromFlat(const std::vector<Num2>& data, unsigned stride,
167  const unsigned* dimsToUse, unsigned nDimsToUse)
168  {
169  std::vector<Point>* result = new std::vector<Point>();
170  fillPointsFromFlat(data, stride, dimsToUse, nDimsToUse, result);
171  return result;
172  }
173 
174  template <class Vec>
175  inline Vec*
176  makeFlatFromFlat(const Vec& data, unsigned stride,
177  const unsigned* dimsToUse, unsigned nDimsToUse)
178  {
179  Vec* result = new Vec();
180  fillFlatFromFlat(data, stride, dimsToUse, nDimsToUse, result);
181  return result;
182  }
183 #endif // SWIG
184 }
185 
186 #include "npstat/stat/OrderedPointND.icc"
187 
188 #endif // NPSTAT_ORDEREDPOINTND_HH_
bool operator==(const npstat::BoxND< Numeric > &l, const npstat::BoxND< Numeric > &r)
Compile-time dimensionality detector for classes like std::array.
Definition: OrderedPointND.hh:40
unsigned long getNumber(unsigned i) const
void setNumber(unsigned i, unsigned long value)
OrderedPointND(const Point &point, const unsigned long *number, unsigned lenNumber)
const value_type & operator[](unsigned i) const
Definition: OrderedPointND.hh:82
value_type & operator[](unsigned i)
Definition: OrderedPointND.hh:79
OrderedPointND(const Num2 *data, const unsigned *indices, unsigned lenIndices)
const Point & point() const
Definition: OrderedPointND.hh:71
OrderedPointND(const Num2 *data, unsigned dataLen)
OrderedPointND(const Point &point)
Definition: AbsArrayProjector.hh:14
void fillOrderedFromFlat(const std::vector< Num2 > &data, unsigned stride, const unsigned *dimsToUse, unsigned nDimsToUse, std::vector< OrderedPointND< Point > > *out)
void fillFlatFromFlat(const std::vector< Num1 > &data, unsigned stride, const unsigned *dimsToUse, unsigned nDimsToUse, std::vector< Num2 > *out)
void fillOrderedPoints(const std::vector< Point1 > &data, const unsigned *dimsToUse, unsigned nDimsToUse, std::vector< OrderedPointND< Point2 > > *out)
void fillPointsFromFlat(const std::vector< Num2 > &data, unsigned stride, const unsigned *dimsToUse, unsigned nDimsToUse, std::vector< Point > *out)
Definition: PointDimensionality.hh:20