ClockWork DB CoreAPI 1.0.48
Abstract Time Series and Storage/Management Library
Loading...
Searching...
No Matches
time_scale_converters.hpp
1/***************************************************************************
2 * time_scale_converters.hpp
3 *
4 * Sun Jul 18 13:49:19 2004
5 * Copyright 2004 Tom McCubbin
6 * tmccubbin@tomsolutions.com
7 ****************************************************************************/
8#ifndef HAVE_TOM__TIME_SCALE_CONVERTERS_HPP
9#define HAVE_TOM__TIME_SCALE_CONVERTERS_HPP
10
11#include <tom-util/defines.hpp>
12#include <tom-util/calendar.hpp>
13#include <tom-util/scalar.hpp>
14#include <tom-util/time_series.hpp>
15
16namespace tom {
17
18namespace calendars {
19
20class TOM_UTIL_API date_period_converter
21{
22public:
24 : m_from(from), m_to(to) { }
25
29 date_int_type next( date_int_type start );
32 date_int_type target( date_int_type start ) { return m_to( m_from( start ) ); }
33
34protected:
35 calendar &m_from;
36 calendar &m_to;
37};
38
39// date_int_type
40// date_period_converter::next( date_int_type start )
41// {
42// date from_start = m_from( start ); // get end date of start
43// date to_end = m_to( m_to( from_start ) ); // convert to 'to' cal end date
44// date from_end = m_from( m_from( to_end ) ); // and back to from cal date
45// // should point to one PAST the last,
46// // aka: a sentry value, or
47// // aka: consistent w/ STL end();
48// if ( from_end <= to_end )
49// from_end += date_duration(1);
50// return m_from( from_end );
51// }
52
54{
55public:
56
57 virtual ~aggregator() { }
58
59 virtual
60 const tom::observation &
61 operator()( const date_period &d, const tom::collections::time_series &ts ) const = 0;
62
63};
64
66{
67public:
68
69 aggregate_end( bool ignore_missing = true );
70 virtual ~aggregate_end();
71
72 virtual
73 const tom::observation &
74 operator()( const date_period &d, const tom::collections::time_series &ts ) const;
75
76protected:
77 bool m_ignore_missing;
78
79};
80
82{
83public:
84
85 aggregate_begin( bool ignore_missing = true );
86 virtual ~aggregate_begin();
87
88 virtual
89 const tom::observation &
90 operator()( const date_period &d, const tom::collections::time_series &ts ) const;
91
92protected:
93 bool m_ignore_missing;
94
95};
97{
98public:
99
100 aggregate_sum( bool ignore_missing = true );
101 virtual ~aggregate_sum();
102
103 virtual
104 const tom::observation &
105 operator()( const date_period &d, const tom::collections::time_series &ts ) const;
106
107protected:
108 bool m_ignore_missing;
109
110};
112{
113public:
114
115 aggregate_avg( bool ignore_missing = true );
116 virtual ~aggregate_avg();
117
118 virtual
119 const tom::observation &
120 operator()( const date_period &d, const tom::collections::time_series &ts ) const;
121
122protected:
123 bool m_ignore_missing;
124
125};
127{
128public:
129
130 aggregate_max( bool ignore_missing = true );
131 virtual ~aggregate_max();
132
133 virtual
134 const tom::observation &
135 operator()( const date_period &d, const tom::collections::time_series &ts ) const;
136
137protected:
138 bool m_ignore_missing;
139
140};
142{
143public:
144
145 aggregate_min( bool ignore_missing = true );
146 virtual ~aggregate_min();
147
148 virtual
149 const tom::observation &
150 operator()( const date_period &d, const tom::collections::time_series &ts ) const;
151
152protected:
153 bool m_ignore_missing;
154
155};
157{
158public:
159
160 class iterator;
161 friend class iterator;
162
164 {
165 public:
166 explicit iterator( time_scale_adapter &adapter, date_int_type date );
167
168 bool
169 operator==( const iterator &iter );
170
171 bool
172 operator!=( const iterator &iter );
173
174 iterator &
175 operator++();
176
178 operator++(int);
179
181 operator*() const;
182
183 const observation *
184 operator->() const;
185
186 date
187 get_date();
188
189 date_int_type
190 get_date_int();
191
192 private:
193 iterator();
194
195 time_scale_adapter &m_adapter;
196 date_int_type m_date;
197 };
199 calendar & target_cal, const aggregator &agg );
200 time_scale_adapter( calendar & target_cal, const aggregator &agg );
201 time_scale_adapter( const aggregator &agg );
202
204 begin();
205
207 end();
208
209 void
210 set_calendar( calendar &cal );
211
212 calendar &
213 get_calendar( ) const;
214
215 const aggregator &
216 get_aggregator( ) const;
217
218 void
219 set_first_date( date d );
220
221 date_int_type
222 get_first_date_int() const;
223
224 date
225 get_first_date() const;
226
227 void
228 set_last_date( date d );
229
230 date_int_type
231 get_last_date_int() const;
232
233 date
234 get_last_date() const;
235
236 void
237 set_time_series( const tom::collections::time_series & ts );
238
240 get_time_series( ) const;
241
242private:
244 calendar & m_target_cal;
245 const aggregator & m_aggregator;
246
247 date_int_type m_begin;
248 date_int_type m_end;
249};
250
251template <class aggregator_type, bool ignore_missing = true>
253{
254public:
255
256 class iterator;
257 friend class iterator;
258
260 {
261 public:
262 explicit iterator( time_scale_adapter_impl &adapter, date_int_type date ) :
263 m_adapter( adapter ), m_date( date )
264 { }
265
266 bool
267 operator==( const iterator &iter )
268 {
269 if ( m_date == iter.m_date &&
270 m_adapter.m_ts == iter.m_adapter.m_ts )
271 return true;
272
273 return false;
274 }
275 bool
276 operator!=( const iterator &iter )
277 {
278 return !( *this == iter );
279 }
280 iterator &
281 operator++()
282 {
283 ++m_date;
284 return *this;
285 }
287 operator++(int)
288 {
289 date_int_type tmp = m_date;
290 ++m_date;
291 return iterator( m_adapter, tmp );
292 }
294 operator*() const
295 {
296 if ( m_adapter.m_ts == NULL )
297 throw std::runtime_error( "No time_series set for time_scale_adapter!" );
298
299 return m_adapter.m_aggregator( m_adapter.m_target_cal.get_date_period( m_date ), *m_adapter.m_ts ).clone();
300 }
301 const observation *
302 operator->() const
303 {
304 if ( m_adapter.m_ts == NULL )
305 throw std::runtime_error( "No time_series set for time_scale_adapter!" );
306
307 return & m_adapter.m_aggregator( m_adapter.m_target_cal.get_date_period( m_date ), *m_adapter.m_ts );
308 }
309 date
310 get_date()
311 {
312 return m_adapter.get_calendar()( m_date );
313 }
314 date_int_type
315 get_date_int()
316 {
317 return m_date;
318 }
319 private:
320 iterator();
321
322 time_scale_adapter_impl &m_adapter;
323 date_int_type m_date;
324 };
326 calendar & target_cal ) :
327 m_ts( &ts ), m_target_cal( target_cal ), m_aggregator( ignore_missing )
328 {
329 m_begin = m_target_cal( m_ts->get_first_date() );
330 m_end = m_target_cal( m_ts->get_last_date() ) + 1;
331 }
332 time_scale_adapter_impl( calendar & target_cal ) :
333 m_ts( NULL ), m_target_cal( target_cal ), m_aggregator( ignore_missing )
334 {
335 m_begin = 0;
336 m_end = 0;
337 }
338 time_scale_adapter_impl( ) :
339 m_ts( NULL ), m_target_cal ( business_calendar::Instance() ),
340 m_aggregator( ignore_missing )
341 {
342 m_begin = 0;
343 m_end = 0;
344 }
345 iterator
346 begin()
347 {
348 return iterator( *this, m_begin );
349 }
350 iterator
351 end()
352 {
353 return iterator( *this, m_end );
354 }
355 void
356 set_calendar( calendar &cal )
357 {
358 m_target_cal = cal;
359 }
360 calendar &
361 get_calendar( ) const
362 {
363 return m_target_cal;
364 }
365 void
366 set_first_date( date d )
367 {
368 m_begin = m_target_cal( d );
369 }
370 date_int_type
371 get_first_date_int() const
372 {
373 return m_begin;
374 }
375 date
376 get_first_date() const
377 {
378 return m_target_cal( m_begin );
379 }
380 void
381 set_last_date( date d )
382 {
383 m_end = m_target_cal( d );
384 }
385 date_int_type
386 get_last_date_int() const
387 {
388 return m_end;
389 }
390 date
391 get_last_date() const
392 {
393 return m_target_cal( m_end );
394 }
395 void
396 set_time_series( const tom::collections::time_series & ts )
397 {
398 m_ts = &ts;
399 m_begin = m_target_cal( m_ts->get_first_date() );
400 m_end = m_target_cal( m_ts->get_last_date() ) + 1;
401 }
403 get_time_series( ) const
404 {
405 if ( m_ts == NULL )
406 throw std::runtime_error( "No time_series set for time_scale_adapter!" );
407
408 return *m_ts;
409 }
410
411private:
413 calendar & m_target_cal;
414 aggregator_type m_aggregator;
415
416 date_int_type m_begin;
417 date_int_type m_end;
418};
419
420} // end calendars namespace
421} // end tom namespace
422
423#endif
Definition time_scale_converters.hpp:112
Definition time_scale_converters.hpp:82
Definition time_scale_converters.hpp:66
Definition time_scale_converters.hpp:127
Definition time_scale_converters.hpp:142
Definition time_scale_converters.hpp:97
Definition time_scale_converters.hpp:54
Definition calendar.hpp:120
Definition time_scale_converters.hpp:21
date_int_type target(date_int_type start)
Definition time_scale_converters.hpp:32
Definition calendar.hpp:105
Definition calendar.hpp:47
Definition time_scale_converters.hpp:164
Definition time_scale_converters.hpp:260
Definition time_scale_converters.hpp:253
Definition time_scale_converters.hpp:157
Definition time_series.hpp:25
Definition observation.hpp:13