Kalanand's March 2014 Log
March 11th
Swapping two numbers in place
Suppose we want to write a function to swap two numbers in place
without using any temporary variables.
Let's first try the old way (swapping the numbers using a temp variable):
void swapNumbers (int& x, int& y)
{
int temp = x; //temp holds x
int x = y; // x is now y
y = temp; // and y is now x!
}
Now, here is how I can get rid of the temp variable
void swapNumbers (int& x, int& y)
{
x = y - x;
y = y - x; // y is now equal to original x
x = x + y; // and x is now y!
}
March 17
Computing week number of the year corresponding to yyyy-mm-dd
Here is C++ code to convert a date (yyyy-mm-dd) into the
week number of the year (in the range 0--33).
DateWeekConversion.C
int GetWeekOfTheYear(int year, int month, int day) {
const char * weekday[] = { "Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
std::tm timeinfo;
timeinfo.tm_sec = timeinfo.tm_min = timeinfo.tm_hour = 1; // past midnight
timeinfo.tm_mday = day;
timeinfo.tm_mon = month-1; // month number starting with 0
timeinfo.tm_year = year - 1900;
timeinfo.tm_hour = 0;
timeinfo.tm_min = 0;
timeinfo.tm_sec = 1;
timeinfo.tm_isdst= 0; // day-light saving
std::mktime(&timeinfo);
char buffer[4];
strftime(buffer,4,"%W", &timeinfo); // '%W' = week number of the year
int week = atoi(buffer)+1; // week starts at 0
// cout << "week of the year = " << week << endl;
// cout << "the day of the week = " << weekday[timeinfo.tm_wday] << endl;
return week;
}
Reverse problem: determine the date assuming you are given year, week # and weekday(1-7)
DateWeekConversion.C
// Get the date for a given year, week and weekday(1-7)
void GetDateFromWeekNumber(int year, int week, int dayOfWeek,
int& day, int& month)
{
std::tm jan1;
jan1.tm_year = year - 1900;
jan1.tm_mon = 0; // JAN
jan1.tm_mday = 1; // 1st
jan1.tm_hour = 0;
jan1.tm_min = 0;
jan1.tm_sec = 1;
jan1.tm_isdst= 0; // day-light saving
std::time_t tim = std::mktime(&jan1);
int wday_on_Jan1 = jan1.tm_wday;
int days = (week-1)*7 - wday_on_Jan1 + dayOfWeek+1;
enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC };
int NDays[13];
NDays[0] = 0;
NDays[1] = 31;
NDays[2] = NDays[1] + ((year%4==0) ? 29 : 28);
for (int i=3; i<13; ++i) {
NDays[i] = NDays[i-1] + ( (i%2==1 || i==8) ? 31 : 30);
}
Months tm_month=JAN;
for (; tm_month <= DEC; tm_month++ ) {
if( (days - NDays[tm_month]) <= 0) break;
}
month = tm_month;
day = days - NDays[month-1];
std::tm newtime;
newtime.tm_year = year - 1900;
newtime.tm_mon = month-1;
newtime.tm_mday = day;
newtime.tm_hour = 0;
newtime.tm_min = 0;
newtime.tm_sec = 1;
newtime.tm_isdst= 0;
std::mktime(&newtime);
int new_dayOfWeek = newtime.tm_wday;
if(!(new_dayOfWeek==dayOfWeek)) {
int diff = dayOfWeek - new_dayOfWeek;
day += dayOfWeek - new_dayOfWeek;
// cout << "new_dayOfWeek = " << new_dayOfWeek
// << ", diff = " << diff
// << ", date = " << year << "-" << month << "-" << day << endl;
}
}
March 24
Specialized sorting in C++
// Suppose I have a list of 4-vectors:
// ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double> > p4;
//the vector needs to be sorted, highest pT first.
auto comp = [&](ROOT::Math::LorentzVector i, ROOT::Math::LorentzVector j)-> bool {
return (i.Pt() < j.Pt() );
};
std::sort( p4.begin(), p4.end(), comp);
std::reverse( p4.begin(), p4.end() );
Go to February's log
Last modified: Wed Mar 26 11:51:06 CST 2014