PLearn 0.1
Calendar.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Calendar.h
00004 //
00005 // Copyright (C) 2004-2005 ApSTAT Technologies Inc.
00006 // 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************      
00036  * $Id: Calendar.h 8184 2007-10-15 20:09:46Z nouiz $ 
00037  ******************************************************* */
00038 
00039 // Authors: Jean-Sébastien Senécal
00040 
00044 #ifndef Calendar_H
00045 #define Calendar_H
00046 
00047 #include <plearn/base/Object.h>
00048 #include <plearn/base/PDate.h>
00049 #include "PRange.h"
00050 
00051 // C++ stdlib
00052 #include <map>
00053 #include <limits.h>
00054 
00055 namespace PLearn {
00056 using namespace std;
00057 
00058 class Calendar;
00059 typedef PP<Calendar> PCalendar;
00060 
00062 typedef double JTime;
00063 
00065 typedef int    CTime;
00066 
00068 typedef TVec<JTime> JTimeVec;
00069 
00071 typedef TVec<CTime> CTimeVec;
00072 
00074 const JTime MAX_TIME   = DBL_MAX;
00075 
00077 const JTime MIN_TIME   = -DBL_MAX;
00078 
00080 const JTime SMALL_TIME = 1./(24*60*60*1000);
00081 
00083 const JTime EPS_TIME   = 1e-9; // Epsilon is found as follows:
00084                                // - reserve 20 bits for the day
00085                                // - 52-20=32 bits for intra-day:
00086                                //   ==> 1/2^32 ~ 2.33e-10 < 1e-9;
00087 
00089 const CTime MAX_CTIME   = INT_MAX;
00090 
00092 const CTime MIN_CTIME   = INT_MIN;
00093 
00095 typedef PRange<CTime> CTimeRange;
00096 
00097 
00122 class Calendar : public Object
00123 {
00124 private:
00125     typedef Object inherited;
00126 
00127 protected:
00128     // Internal use.
00129 
00131     mutable JTime last_julian_time_;
00132 
00134     mutable CTime last_calendar_time_;
00135 
00137     mutable bool last_use_lower_bound;
00138 
00143     map< Calendar*, TVec<int> > active_resamplings;
00144 
00146     static map<string, PP<Calendar> > global_calendars;
00147   
00148 public:
00150     JTimeVec timestamps_;
00151 
00159     Vec dwim_timestamps_;
00160 
00161 public:
00163     Calendar();
00164   
00166     Calendar(const JTimeVec& timestamps);
00167 
00177     static PCalendar makeCalendar(const Vec& dates);
00178 
00179 private:
00181     void build_();
00182 
00183 protected:
00185     static void declareOptions(OptionList& ol);
00186   
00188     static void declareMethods(RemoteMethodMap& rmm);
00189 
00190 public:
00191     PLEARN_DECLARE_OBJECT(Calendar);
00192 
00194     virtual void build();
00195 
00197     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00198 
00199 
00200     //#####  Calendar-Specific Functions  #####################################
00201   
00203     bool isEmpty() const { return timestamps_.isEmpty(); }
00204 
00206     const JTimeVec& getTimeStamps() const { return timestamps_; }
00207   
00209     bool operator==(const Calendar& cal)
00210     { return (timestamps_ == cal.getTimeStamps()); }
00211 
00213     bool operator!=(const Calendar& cal)
00214     { return (timestamps_ != cal.getTimeStamps()); }
00215   
00217     int size() const { return timestamps_.length(); }
00218 
00220     inline JTime getTime(CTime calendar_time) const;
00221     inline JTime operator[](CTime calendar_time) const;
00222 
00230     CTime getCalendarTime(JTime julian_time, bool use_lower_bound = true) const;
00231 
00237     CTime getLastDayOfMonth(const PDate& day_of_the_month) const;
00238     
00244     bool containsTime(JTime julian_time, CTime *calendar_time = 0) const;
00245 
00248     JTime calendarTimeOnOrAfter(JTime julian_time) const;
00249 
00252     JTime calendarTimeOnOrBefore(JTime julian_time) const;  
00253   
00260     PCalendar clamp(JTime lower, JTime upper);
00261 
00262 
00263     //#####  Resamplings  ######################################################
00264 
00266     void setResampling(Calendar* other_cal, const TVec<int>& resampling)
00267     { active_resamplings[other_cal] = resampling; }
00268 
00271     TVec<int> getResampling(Calendar* other_cal)
00272     { return active_resamplings[other_cal]; }
00273 
00274   
00275     //#####  Static Calendar Conversion  ######################################
00276 
00278     static CTime convertCalendarTime(const Calendar& source_calendar,
00279                                      const Calendar& dest_calendar,
00280                                      CTime source_time,
00281                                      bool use_lower_bound = true);
00282 
00284     static PCalendar unite(const TVec<PCalendar>& calendars);
00285 
00287     static PCalendar intersect(const TVec<PCalendar>& calendars);
00288 
00291     static PCalendar calendarDiff(const Calendar* cal, const JTimeVec& to_remove);
00292 
00293 
00294     //#####  Global Static Calendars  ##########################################
00295 
00297     static void setGlobalCalendar(const string& calendar_name,
00298                                   PCalendar calendar);
00299 
00302     static const Calendar* getGlobalCalendar(const string& calendar_name);
00303 
00304 
00305     //#####  Remote-Callables  ################################################
00306     
00307     void remote_setGlobalCalendar(string calendar_name, Vec calendar_dates);
00308     JTimeVec remote_getGlobalCalendar(string calendar_name);
00309 };
00310 
00311 // Declares a few other classes and functions related to this class
00312 DECLARE_OBJECT_PTR(Calendar);
00313 
00314 JTime Calendar::getTime(CTime calendar_time) const
00315 {
00316     if (calendar_time < 0 || calendar_time >= timestamps_.length())
00317         PLERROR("In Calendar::getTime(), argument calendar_time = %d out of bounds.", calendar_time);
00318     return timestamps_[calendar_time];
00319 }
00320 
00321 JTime Calendar::operator[](CTime calendar_time) const
00322 {
00323     return getTime(calendar_time);
00324 }
00325 
00326 } // end of namespace PLearn
00327 
00328 #endif
00329 
00330 
00331 /*
00332   Local Variables:
00333   mode:c++
00334   c-basic-offset:4
00335   c-file-style:"stroustrup"
00336   c-file-offsets:((innamespace . 0)(inline-open . 0))
00337   indent-tabs-mode:nil
00338   fill-column:79
00339   End:
00340 */
00341 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines