Author Archive
C++ test for within
Thursday, August 26th, 2010I found myself having to right a bunch of range checking code like this:
val = 1.0;
so I wrote this simple function.
template
const Type & within (const Type &lower, const Type &val, const Type &upper)
{
return std::min (std::max (lower, val), upper);
}
So the above code gets simplified to:
I wasn’t happy about the extra copy being created but was willing to live with it.
Then I ran across this fragment which is so similar but ended up changing the final design:
val = 5;
The difference is the above check sets an arbitrary value if it wasn’t within the range instead of just assigning one of the bounds to the value. That caused me to split it into a test, within(), to handle this case and a setwithin() to handle the earlier cases.
Splitting it also let me do a minor optimization. The typical case is that being out of bounds is rather exceptional and it’d be nice to avoid doing a copy. Accordingly, setwithin() checks if the dest has the same address as the replacement value and avoids doing the assignment.
int within (const Type & lower, const Type & val, const Type & upper)
{
if (val < lower)
return -1;
if (upper < val)
return 1;
return 0;
}
template
int setwithin (const Type & lower, const Type & val, const Type & upper, Type & dest)
{
int res = within (lower, val, upper);
switch (res)
{
case -1:
if (&lower != &dest)
dest = lower;
break;
case 0:
if (&val != &dest)
dest = val;
break;
case 1:
if (&upper != &dest)
dest = upper;
break;
}
return res;
}
Way funny way to quit!
Tuesday, August 10th, 2010This woman used a dry erase board and sent email to everyone to quit. I especially like how she reports on her boss.

Editted Aug 10.
Sadly, it’s a hoax:
“Girl quits her job on dry erase board, emails entire office (33 Photos)” is indeed a hoax, say its creators John and Leo Resig.

Federal budget overview
Monday, July 26th, 2010Here’s a great overview from Barry Ritholz showing how the Federal budget is proportioned.
Unemployment won’t recover for 157 months (2021)
Sunday, July 18th, 2010If we look at employment prior to the Great Recession compared to now there is a difference of 11.3 million jobs (from Bookings institute). Now how long is it going to take to return to that level? If you take the best job growth of the 2000′s it’ll be 157 months or 11years. That’s not until 2021!
If you take the best rate from the 1990′s it’s down to about 8 years! Here’s the chart with how many months it’ll take based on the rate with a couple note worthy rates highlighted.
iPhone4 and FaceTime
Monday, July 12th, 2010I’d been surprised about Apple emphasizing FaceTime in advertising. I don’t think it’s very compelling — I don’t know enough people with an iPhone4, I rarely have a Wi-Fi connection when I want to talk to them, and I don’t find video chats very compelling anyway. After all, how often do you video iChat or Skype?
Anyway, TechCrunch had an interesting perspective about it when they compared it to a scene from Mad Men (official site. They are getting people to make an emotional connection with the iPhone rather then the analytical one of feature X vs. feature Y.
More exonerations for Climategate researchers
Monday, July 5th, 2010Yet another report says Michael E Mann is cleared of any wrongdoing related to his climate research.
An investigative panel at Pennsylvania State University, weighing the question of whether the scientist, Michael E. Mann, had “seriously deviated from accepted practices within the academic community for proposing, conducting or reporting research or other scholarly activities,” declared that he had not.
This is the second report from Penn St. clearing him and joins two others in Britain clearing related researchers.
Senator Robert Byrd
Sunday, July 4th, 2010From Frank Rich’s column about Senator Robert Byrd.
These senators were in the tradition of Thurmond, not Byrd — indeed, they are Thurmond’s direct heirs. Like Byrd, Thurmond had been an ardent Democratic foe of the Civil Rights Act of 1964. Unlike Byrd, he left his party in disgust that year and endorsed Goldwater, jump-starting the migration of the Democrats’ racist cadre and their political toxins to the G.O.P. and setting the stage for the Republican “Southern strategy.” That strategy isn’t dead.
The NY Times had an interesting obituary about Senator Byrd including his Ku Klux Klan membership, title as “King of Pork”
Mr. Byrd’s perspective on the world changed over the years. A former member of the Ku Klux Klan, he filibustered against the 1964 Civil Rights Act only to come to back civil rights measures and Mr. Obama. A supporter of the Vietnam War, he became a fierce critic, decades later, of the war in Iraq. In 1964, the Americans for Democratic Action, the liberal lobbying group, found that his views and the group’s aligned only 16 percent of the time. In 2005, he got an A.D.A. rating of 95.
Switching keys in values in a map
Sunday, July 4th, 2010A functor that reverses a pair: first becomes second, second becomes first. Useful for switching from a map to another map (or multimap in this example) where the value becomes the key and the key the value.
std::map m;
std::multimap m2;
m["a"] = 0;
m["b"] = 1;
m["c"] = 0;
std::transform (m.begin(), m.end(), std::inserter (m2, m2.begin()),
pair_switch<std::map::value_type> ());
And the actual code:
template
struct pair_switch : public std::unary_function<
std::pair,
std::pair >
{
typedef std::pair argument_type;
typedef std::pair argument_type2;
typedef std::pair argument_type3;
typedef std::pair result_type;
result_type operator()(const argument_type3 &p) const
{
return result_type (p.second, p.first);
}
};

