Jpp
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
JLANG::JPointer::set
virtual void set(JClass_t *p)
Set pointer.
Definition: JPointer.hh:75
JLANG::skip_type
unsigned int skip_type
Type definition for number of objects to skip.
Definition: JObjectIterator.hh:25
JLANG::JObjectSampler::next
virtual const pointer_type & next()
Get next element.
Definition: JObjectSampler.hh:110
JSampler.hh
JObjectIterator.hh
JLANG::JObjectSampler::JObjectSampler
JObjectSampler(JRewindableObjectIterator< T > &input, const JSampler_t< T > &sampler)
Constructor.
Definition: JObjectSampler.hh:54
JLANG::JObjectSampler::in
JRewindableObjectIterator< T > & in
Definition: JObjectSampler.hh:144
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JLANG::JPointer< T >
JLANG::JPointer::reset
virtual void reset()
Reset pointer.
Definition: JPointer.hh:84
JLANG::JObjectSampler::getSampler
JSampler_t< T > & getSampler()
Get sampler.
Definition: JObjectSampler.hh:138
JLANG::JObjectSampler::has_next
bool has_next
Definition: JObjectSampler.hh:147
JLANG::JRewindableObjectIterator
Interface for object iteration with rewinding.
Definition: JObjectIterator.hh:362
JLANG::JObjectSampler::ps
JPointer< T > ps
Definition: JObjectSampler.hh:146
JLANG::JObjectSampler::pointer_type
JObjectIterator< T >::pointer_type pointer_type
Definition: JObjectSampler.hh:35
JLANG::JSampler
Template class for sampling from a JRewindableObjectIterator.
Definition: JSampler.hh:22
JLANG::JObjectIterator
Interface of object iteration for a single data type.
Definition: JObjectIterator.hh:43
JLANG::JObjectSampler::hasNext
virtual bool hasNext()
Check availability of next element.
Definition: JObjectSampler.hh:68
JLANG::JObjectSampler::getSampler
const JSampler_t< T > & getSampler() const
Get sampler.
Definition: JObjectSampler.hh:127
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10
JPointer.hh
JLANG::JObjectSampler::JObjectSampler
JObjectSampler(JRewindableObjectIterator< T > &input)
Constructor.
Definition: JObjectSampler.hh:42
JLANG::JObjectSampler
Auxiliary class to sample objects from a rewindable object iterator.
Definition: JObjectSampler.hh:30
JLANG::JObjectSampler::sampler
JSampler_t< T > sampler
Definition: JObjectSampler.hh:145