Jpp
 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 rewindable object iterator.
20  *
21  * Method hasNext normally returns true, unless
22  * - there are no available objects; or
23  * - given sampler rejects every object.
24  *
25  * Note that the internal sampler is a copy of the specified sampler and not a reference thereof.
26  *
27  * This class implements the JObjectIterator interface.
28  */
29  template<class T, template<class> class JSampler_t = JSampler>
31  public JObjectIterator<T>
32  {
33  public:
34 
36 
37  /**
38  * Constructor.
39  *
40  * \param input input
41  */
43  in (input),
44  has_next(false)
45  {}
46 
47 
48  /**
49  * Constructor.
50  *
51  * \param input input
52  * \param sampler sampler
53  */
55  const JSampler_t<T>& sampler) :
56  in (input),
57  has_next(false)
58  {
59  this->sampler = sampler;
60  }
61 
62 
63  /**
64  * Check availability of next element.
65  *
66  * \return true if the iteration has more elements; else false
67  */
68  virtual bool hasNext()
69  {
70  while (!has_next) {
71 
72  if (!in.hasNext()) {
73  in.rewind();
74  }
75 
76  if (in.hasNext()) {
77 
78  ps.set(in.next());
79 
80  skip_type ns = sampler(*ps);
81 
82  if (ns == 0) {
83 
84  has_next = true;
85 
86  } else {
87 
88  while ((ns -= in.skip(ns)) != 0) {
89  in.rewind();
90  }
91  }
92 
93  } else {
94 
95  // nothing to sample
96 
97  break;
98  }
99  }
100 
101  return has_next;
102  }
103 
104 
105  /**
106  * Get next element.
107  *
108  * \return pointer to element
109  */
110  virtual const pointer_type& next()
111  {
112  if (!has_next) {
113  ps.reset(NULL);
114  }
115 
116  has_next = false;
117 
118  return ps;
119  }
120 
121 
122  /**
123  * Get sampler.
124  *
125  * \return sampler
126  */
127  const JSampler_t<T>& getSampler() const
128  {
129  return sampler;
130  }
131 
132 
133  /**
134  * Get sampler.
135  *
136  * \return sampler
137  */
138  JSampler_t<T>& getSampler()
139  {
140  return sampler;
141  }
142 
143  protected:
145  JSampler_t<T> sampler;
147  bool has_next;
148  };
149 }
150 
151 #endif
virtual const pointer_type & next()
Get next element.
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.
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.
virtual void reset()
Reset pointer.
Definition: JPointer.hh:84
JObjectIterator< T >::pointer_type pointer_type
Auxiliary class to sample objects from a rewindable object iterator.
virtual bool hasNext()
Check availability of next element.
const JSampler_t< T > & getSampler() const
Get sampler.
JSampler_t< T > sampler
virtual void set(JClass_t *p)
Set pointer.
Definition: JPointer.hh:75