00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
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
00040 }
00041
00042
00043 ContextRange::~ContextRange()
00044 {
00045
00046
00047 }
00048
00049
00050 std::string ContextRange::AsString(const char* option) const
00051 {
00052
00053
00054
00055
00056
00057
00058
00059
00060
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
00093
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
00113
00114
00115 int site = (int)cx.GetSite();
00116 int simflag = (int)cx.GetSimFlag();
00117
00118
00119
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
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
00139
00140 assert(cx);
00141 return IsCompatible(*cx);
00142 }
00143
00144
00145
00146 void ContextRange::TrimTo(const ContextRange& cx)
00147 {
00148
00149
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