28 time_series( ) : m_start_date_int( 0 ), m_end_date_int(0) { };
34 virtual const observation &get_observation( date_int_type d )
const = 0;
40 virtual void set_observation( date_int_type d,
const Date & ) = 0;
41 virtual void set_observation(
date d,
const Date & ) = 0;
43 virtual void set_observation( date_int_type d,
const observation & val ) = 0;
48 set_observation(
date( d.value() ), val );
54 void set_observation( date_int_type d,
float f )
55 { set_observation(d,
Float(f) ); }
56 void set_observation(
date d,
float f )
57 { set_observation(d,
Float(f) ); }
58 void set_observation(
const Date &d,
float f )
59 { set_observation(d,
Float(f) ); }
62 void set_observation( date_int_type d,
double f )
63 { set_observation(d,
Double(f) ); }
64 void set_observation(
date d,
double f )
65 { set_observation(d,
Double(f) ); }
66 void set_observation(
const Date &d,
double f )
67 { set_observation(d,
Double(f) ); }
71 void set_observation( date_int_type d,
const std::string &f )
72 { set_observation(d,
String(f) ); }
74 void set_observation(
date d,
const std::string &f )
75 { set_observation(d,
String(f) ); }
77 void set_observation(
const Date &d,
const std::string &f )
78 { set_observation(d,
String(f) ); }
82 void set_observation( date_int_type d,
const char *f )
83 { set_observation(d,
String(f) ); }
85 void set_observation(
date d,
const char *f )
86 { set_observation(d,
String(f) ); }
88 void set_observation(
const Date &d,
const char *f )
89 { set_observation(d,
String(f) ); }
92 void set_observation( date_int_type d,
int f )
93 { set_observation(d,
Int(f) ); }
94 void set_observation(
date d,
int f )
95 { set_observation(d,
Int(f) ); }
96 void set_observation(
const Date &d,
int f )
97 { set_observation(d,
Int(f) ); }
100 void set_observation( date_int_type d,
bool f )
101 { set_observation(d,
Bool(f) ); }
102 void set_observation(
date d,
bool f )
103 { set_observation(d,
Bool(f) ); }
104 void set_observation(
const Date &d,
bool f )
105 { set_observation(d,
Bool(f) ); }
108 void set_observation( date_int_type d,
110 { set_observation(d,
Int(f) ); }
112 { set_observation(d,
Int(f) ); }
114 { set_observation(d,
Int(f) ); }
117 virtual const observation & operator[ ]( date_int_type d )
const = 0;
118 virtual observation & operator[ ]( date_int_type d ) = 0;
122 virtual std::ostream &print( std::ostream & )
const = 0;
125 virtual inline date_int_type get_first_date_int()
const {
return m_start_date_int; }
126 virtual inline date_int_type get_last_date_int()
const {
return m_end_date_int; }
127 inline date get_first_date()
const {
return get_calendar()( get_first_date_int() ); }
128 inline date get_last_date()
const {
return get_calendar()( get_last_date_int()); }
134 virtual const void *data()
const {
return NULL; }
139 date_int_type set_last_date_int(
const date_int_type d );
140 date_int_type set_first_date_int(
const date_int_type d );
142 date_int_type m_start_date_int;
143 date_int_type m_end_date_int;
308 public time_series,
public std::vector<typename T::value_type>
311 typedef T value_type;
312 typedef typename std::vector<typename T::value_type > vector_type;
314 typedef typename vector_type::pointer pointer;
315 typedef typename vector_type::reference reference;
316 typedef typename vector_type::difference_type difference_type;
317 typedef std::random_access_iterator_tag iterator_category;
319 typedef typename vector_type::iterator iterator;
320 typedef typename vector_type::const_iterator const_iterator;
323 vector_type(), m_align( OFFSET ), m_calendar( cal ),
324 m_first_date( 0 ), m_last_date( 0 ), m_min_first_date(0)
326 m_calendar_offset = cal( cal.get_first_date() );
329 template <
typename Iter>
331 vector_type( begin, end ), m_align( OFFSET ), m_calendar(cal),
332 m_first_date( start ), m_last_date( start + ( end - begin ) - 1 ),
333 m_min_first_date(start)
335 m_calendar_offset = cal( cal.get_first_date() );
337 template <
typename IteratorT>
339 vector_type( begin, end ), m_align( OFFSET ), m_calendar(cal),
340 m_first_date( cal( start ) ), m_last_date( cal( start ) + ( end - begin ) - 1 ),
341 m_min_first_date( m_first_date )
343 m_calendar_offset = cal( cal.get_first_date() );
358 get_observation( date_int_type d )
const
361 return m_return_value = *( real_begin() + index_of(d) );
366 return Int::HAS_NO_DATA;
373 set_observation_( date_int_type d,
const T &v )
380 if ( v.flag() == tom::value_type::no_data() )
387 else if ( get_first_date_int() == get_last_date_int() && d == get_first_date_int() )
395 else if ( ! ( d > get_first_date_int() && d < get_last_date_int() ) )
398 if ( d == get_last_date_int() )
402 else if ( d == get_first_date_int() )
417 else if ( d > get_last_date_int() )
420 assign_nd( get_last_date_int() +1, d );
424 else if ( d < get_first_date_int() )
427 if ( get_alignment() == OFFSET )
430 set_alignment( NATURAL );
432 extend( get_last_date_int() );
437 assign_nd( d, get_first_date_int() -1 );
441 *(real_begin() + index_of( d )) = v.value();
445 set_observation( date_int_type d,
const observation &val )
447 set_observation_( d, T(val) );
451 virtual void set_observation( date_int_type d,
const Date &v )
453 set_observation_( d, T(v) );
455 virtual void set_observation(
date d,
const Date &v )
457 set_observation_( get_calendar()(d), T(v) );
461 void set_observation(
const date_int_type d,
const typename T::value_type & val );
462 void set_observation(
const date d,
const typename T::value_type & val );
466 virtual const observation & operator[ ](
const date_int_type d )
const ;
467 virtual observation & operator[ ](
const date_int_type d );
471 virtual std::ostream &print( std::ostream & )
const;
475 get_calendar()
const {
return m_calendar; }
478 get_first_date_int()
const {
return m_first_date; }
481 get_last_date_int()
const {
return m_last_date; }
487 if ( get_last_date_int() == 0 )
492 in_range( date_int_type d )
const
494 if ( get_first_date_int() == 0 )
496 if ( d >= get_first_date_int() && d <= get_last_date_int() )
500 virtual const void *data()
const
503 return reinterpret_cast<const void *
>( (&this->vector_type::front()) + index_of( get_first_date_int() ) );
507 enum Alignment { OFFSET, NATURAL };
511 date_int_type m_calendar_offset;
512 date_int_type m_first_date;
513 date_int_type m_last_date;
514 date_int_type m_min_first_date;
516 mutable value_type m_return_value;
517 iterator real_begin() {
return this->vector_type::begin(); }
518 iterator real_end() {
return this->vector_type::end(); }
519 const_iterator real_begin()
const {
return this->vector_type::begin(); }
520 const_iterator real_end()
const {
return this->vector_type::end(); }
523 set_first_date( date_int_type d )
525 m_min_first_date = (d < m_min_first_date || m_min_first_date == 0 ) ?
526 d : m_min_first_date;
531 set_last_date( date_int_type d ) { m_last_date = d; }
534 get_alignment()
const {
return m_align; }
536 set_alignment( Alignment a ) { m_align = a; }
543 if ( get_alignment() == OFFSET )
545 return m_min_first_date;
547 return m_calendar_offset;
551 index_of( date_int_type d )
const
553 return d - get_offset();
557 extend( date_int_type d )
559 if ( index_of(d) + 1 > this->size() )
560 this->resize( index_of( d ) + 1 );
564 assign_nd( date_int_type start, date_int_type end )
566 iterator b = real_begin() + index_of( start );
567 iterator e = real_begin() + index_of( end ) + 1;
571 std::fill( b, e, T::no_data::value );
577 unwind_end( date_int_type d )
579 while ( (--d) >= get_first_date_int() )
582 if ( tmp.category() != tom::value_types::TOM_VALUE_NO_DATA )
588 set_last_date( get_first_date_int() );
591 unwind_start( date_int_type d )
595 while ( (++d) <= get_last_date_int() )
597 if (
scalar_type( *(real_begin() + index_of(d) ) ).category() != tom::value_types::TOM_VALUE_NO_DATA )
603 set_first_date( get_last_date_int() );
610 size_t diff = get_last_date_int() - get_first_date_int() + 1;
611 std::copy_backward( real_begin(), real_begin() + diff,
612 real_begin() + this->vector_type::size() );