Jpp  18.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JObjectSampler.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JOBJECTSAMPLER__
2 #define __JLANG__JOBJECTSAMPLER__
3 
5 #include "JLang/JPointer.hh"
6 #include "JLang/JSampler.hh"
7 
8 
9 /**
10  * \author mdejong
11  */
12 
13 namespace JLANG {}
14 namespace JPP { using namespace JLANG; }
15 
16 namespace JLANG {
17 
18  /**
19  * Auxiliary class to sample objects from a JRewindableObjectIterator.
20  *
21  * This class can be used for iterations that are different than strict sequential.
22  * The first template parameter corresponds to the data type of the iteration and the second to the sampler.\n
23  * The sampling is defined by the number of objects to be skipped.
24  * The number of objects to be skipped is returned by the function object operator
25  * of the sampler which takes the actual object as input.\n
26  * The default JSampler corresponds to zero elements to be skipped,
27  * which in turn corresponds to the iteration of all objects in sequential order.
28  *
29  * The JObjectSampler class implements the JObjectIterator
30  * using a reference to a JRewindableObjectIterator object so that
31  * the sampling of objects can continue ad infinitum.
32  *
33  * Method JObjectSampler::hasNext normally returns <tt>true</tt>, unless
34  * - there are no available objects; or
35  * - given sampler rejects every object.
36  *
37  * Note that the internal sampler is a copy of the specified sampler and not a reference thereof.
38  */
39  template<class T, template<class> class JSampler_t = JSampler>
41  public JObjectIterator<T>
42  {
43  public:
44 
46 
47  /**
48  * Constructor.
49  *
50  * \param input input
51  */
53  in (input),
54  has_next(false)
55  {}
56 
57 
58  /**
59  * Constructor.
60  *
61  * \param input input
62  * \param sampler sampler
63  */
65  const JSampler_t<T>& sampler) :
66  in (input),
67  has_next(false)
68  {
69  this->sampler = sampler;
70  }
71 
72 
73  /**
74  * Check availability of next element.
75  *
76  * \return true if the iteration has more elements; else false
77  */
78  virtual bool hasNext() override
79  {
80  while (!has_next) {
81 
82  if (!in.hasNext()) {
83  in.rewind();
84  }
85 
86  if (in.hasNext()) {
87 
88  ps.set(in.next());
89 
90  skip_type ns = sampler(*ps);
91 
92  if (ns == 0) {
93 
94  has_next = true;
95 
96  } else {
97 
98  while ((ns -= in.skip(ns)) != 0) {
99  in.rewind();
100  }
101  }
102 
103  } else {
104 
105  // nothing to sample
106 
107  break;
108  }
109  }
110 
111  return has_next;
112  }
113 
114 
115  /**
116  * Get next element.
117  *
118  * \return pointer to element
119  */
120  virtual const pointer_type& next() override
121  {
122  if (!has_next) {
123  ps.reset(NULL);
124  }
125 
126  has_next = false;
127 
128  return ps;
129  }
130 
131 
132  /**
133  * Get sampler.
134  *
135  * \return sampler
136  */
137  const JSampler_t<T>& getSampler() const
138  {
139  return sampler;
140  }
141 
142 
143  /**
144  * Get sampler.
145  *
146  * \return sampler
147  */
148  JSampler_t<T>& getSampler()
149  {
150  return sampler;
151  }
152 
153  protected:
155  JSampler_t<T> sampler;
157  bool has_next;
158  };
159 }
160 
161 #endif
Template class for sampling from a JRewindableObjectIterator.
Definition: JSampler.hh:22
JObjectSampler(JRewindableObjectIterator< T > &input)
Constructor.
Interface for object iteration with rewinding.
unsigned int skip_type
Type definition for number of objects to skip.
std::vector< size_t > ns
JRewindableObjectIterator< T > & in
Interface of object iteration for a single data type.
JObjectSampler(JRewindableObjectIterator< T > &input, const JSampler_t< T > &sampler)
Constructor.
JSampler_t< T > & getSampler()
Get sampler.
JObjectIterator< T >::pointer_type pointer_type
virtual bool hasNext() override
Check availability of next element.
Auxiliary class to sample objects from a JRewindableObjectIterator.
virtual const pointer_type & next() override
Get next element.
const JSampler_t< T > & getSampler() const
Get sampler.
virtual void set(JClass_t *p) override
Set pointer.
Definition: JPointer.hh:75
JSampler_t< T > sampler
virtual void reset() override
Reset pointer.
Definition: JPointer.hh:84