| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

ContextRange.cc

Go to the documentation of this file.
00001 
00002 // 
00003 //
00004 // ContextRange
00005 //
00006 // ContextRange is delimits/identifies the allowed "context" values
00007 // associated with the associated DBI returned information
00008 //
00009 // Author:  N. Tagg March 2007,
00010 // Adopted from VldRange by R. Hatcher 2000.05.03
00011 //
00013 
00014 #include "Context/ContextRange.h"
00015 #include <cassert>
00016 #include <cctype>
00017 #include <string>
00018 #include <algorithm>
00019 #include <cstdio>
00020 
00021 //_____________________________________________________________________________
00022 ContextRange::ContextRange()
00023     : mSiteMask(0)
00024     , mSimMask(0)
00025     , mTimeStart(0,0)
00026     , mTimeEnd(0,0)
00027 {
00028     // Default constructor
00029 }
00030 
00031 //_____________________________________________________________________________
00032 ContextRange::ContextRange(const int siteMask, const int simMask, 
00033                            const TimeStamp &tstart,
00034                            const TimeStamp &tend)
00035                          
00036     : mSiteMask(siteMask), mSimMask(simMask), 
00037       mTimeStart(tstart), mTimeEnd(tend)
00038 {
00039     // normal constructor
00040 }
00041 
00042 //_____________________________________________________________________________
00043 ContextRange::~ContextRange()
00044 {
00045     // delete all the owned sub-objects
00046 
00047 }
00048 
00049 //_____________________________________________________________________________
00050 std::string ContextRange::AsString(const char* option) const
00051 {
00052     // Return the ContextRange as a string
00053     //
00054     // Result is a pointer to a statically allocated string.
00055     // User should copy this into their own buffer before calling
00056     // this method again.
00057     //
00058     // option "a": give site/simflag masks as alpha chars
00059     // option "c": compact (single line)
00060     // option "s": drop nsec part of times
00061 
00062     std::string opt = option;
00063     std::transform(opt.begin(),opt.end(),opt.begin(),::tolower);
00064 
00065     bool opt_a = (opt.find("a") != std::string::npos);
00066     bool opt_c = (opt.find("c") != std::string::npos);
00067     bool opt_s = (opt.find("s") != std::string::npos);
00068 
00069     static std::string out;
00070     out="|";
00071 
00072     if (opt_a) 
00073         out += Site::StringFromMask(mSiteMask);
00074     else {
00075         out += "site ";
00076         char buffer[1024] = {0};
00077         snprintf(buffer,1024,"%#4.4x",mSiteMask);
00078         out += buffer;
00079     }
00080 
00081     out += "|";
00082    
00083     if (opt_a) 
00084         out += SimFlag::StringFromMask(mSimMask);
00085     else {
00086         out += "sim ";
00087         char buffer[1024] = {0};
00088         snprintf(buffer,1024,"%#4.4x",mSiteMask);
00089         out += buffer;
00090     }
00091 
00092     // TimeStamp::AsString returns pointer to statically allocated string
00093     // one needs to copy this before calling it again in same function call
00094     static char timeopt[4] = "c  ";
00095     timeopt[0] = (opt_s ? 's' : 'c');
00096     if(opt_c)    out += "|";
00097     else         out += "\n\t";
00098 
00099     out += mTimeStart.AsString(timeopt);
00100 
00101     if(opt_c)    out += "|";
00102     else         out += "\n\t";
00103 
00104     out += mTimeEnd.AsString(timeopt);
00105    
00106     return out;
00107 }
00108 
00109 //_____________________________________________________________________________
00110 bool ContextRange::IsCompatible(const Context &cx) const
00111 {
00112     // compare Context with this ContextRange to see if the
00113     // the tagged set is compatible
00114 
00115     int site     = (int)cx.GetSite();
00116     int simflag  = (int)cx.GetSimFlag();
00117 
00118     // account for case where both Context and ContextRange
00119     // are using "kUnknown" which has no bits set
00120     if ( ! (site & mSiteMask) &&
00121          (site      != Site::kUnknown || 
00122           mSiteMask != Site::kUnknown    ) ) return false;
00123     if ( ! (simflag  & mSimMask) &&
00124          (simflag  != SimFlag::kUnknown || 
00125           mSimMask != SimFlag::kUnknown   ) ) return false;
00126 
00127     // the start time is taken as inclusive, but the end time is exclusive
00128 
00129     if ( cx.GetTimeStamp() <  mTimeStart ) return false;
00130     if ( cx.GetTimeStamp() >= mTimeEnd   ) return false;
00131 
00132     return true;
00133 }
00134 
00135 //_____________________________________________________________________________
00136 bool ContextRange::IsCompatible(const Context *cx) const
00137 {
00138     // compare Context with this ContextRange to see if the
00139     // the tagged set is compatible
00140     assert(cx);
00141     return IsCompatible(*cx);
00142 }
00143 
00144 
00145 //_____________________________________________________________________________
00146 void ContextRange::TrimTo(const ContextRange& cx)
00147 {
00148     // Trim this range to the intersection (ie. more restricted)
00149     // limits of it's initial value and that of the argument
00150 
00151     mSiteMask &= cx.mSiteMask;
00152     mSimMask      &= cx.mSimMask;
00153     if (mTimeStart < cx.mTimeStart) mTimeStart = cx.mTimeStart;
00154     if (mTimeEnd   > cx.mTimeEnd  ) mTimeEnd   = cx.mTimeEnd;
00155 }
00156 
00157 //_____________________________________________________________________________
00158 bool operator==(const ContextRange &lhs, const ContextRange &rhs)
00159 { return lhs.mTimeStart  == rhs.mTimeStart; }
00160 
00161 bool operator!=(const ContextRange &lhs, const ContextRange &rhs)
00162 { return lhs.mTimeStart  != rhs.mTimeStart; }
00163 
00164 bool operator<(const ContextRange &lhs, const ContextRange &rhs)
00165 { return lhs.mTimeStart  < rhs.mTimeStart; }
00166 
00167 bool operator<=(const ContextRange &lhs, const ContextRange &rhs)
00168 { return lhs.mTimeStart  < rhs.mTimeStart; }
00169 
00170 bool operator>(const ContextRange &lhs, const ContextRange &rhs)
00171 { return lhs.mTimeStart  > rhs.mTimeStart; }
00172 
00173 bool operator>=(const ContextRange &lhs, const ContextRange &rhs)
00174 { return lhs.mTimeStart  > rhs.mTimeStart; }
00175 
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:15:31 2011 for Context by doxygen 1.4.7