Ramen as a space-filling curve

by Brian Enigma on August 29, 2010 9:28am

in Dear Diary,Food

Although it has stared me in the face for years and should have been plainly obvi­ous, last night I had a rev­e­la­tion that the noo­dles in Top Ramen are “wrong.”  Actual ramen from a ramen house has straight noo­dles (and good broth that doesn’t come from a pow­der, but that’s beside this point).  Ramen noo­dles are, more or less, a close rel­a­tive to spaghetti noo­dles.  Top Ramen, Cup Noo­dle, and the other lesser-known instant noo­dles are all wiggly.

I remem­ber from the Ignite Port­land talk on Cup Noo­dle that the Cup Noo­dle engi­neers were under a num­ber of con­straints regard­ing the freeze-drying of the broth and the com­po­si­tion of the noo­dles.  I do not remem­ber any­thing in the talk about the wig­gly packed-in shape of the noo­dles, but I can see that eas­ily fit­ting into their design require­ments.  Instant soup with straight spaghetti-style noo­dles would take more space and would be more brit­tle dur­ing ship­ping.  The wig­gly brick of solid noo­dles is a sort of three-dimensional space-filling curve that packs the most noo­dles into the small­est space and has the added bonus of being much more solid, rigid, and resilient to ship­ping damage.

And that’s how my morn­ing dis­ap­peared — by explor­ing frac­tal curves on Wikipedia.  See also: Peano SpaceDragon Curve, Gosper Curve, Moore Curve, Sier­piński curve, and Z-Order Curve.

{ 0 comments }

& Operator Overloading">Boost C++ & Operator Overloading

by Brian Enigma on August 28, 2010 1:39pm

in Code,Work

As you may or may not know already, I recently started a new job.  The new com­pany makes a lot more heavy-duty use of C++ than the pre­vi­ous one did.  Among the C++ code is the 3rd party Boost C++ library.  I had periph­er­ally heard of Boost before, but had never looked too deeply at it.  It is a set of stan­dard cross-platform C++ classes that per­form a vari­ety of use­ful oper­a­tions.  You can think of it like an addi­tion to the Stan­dard Tem­plate Library (STL).  In fact, they are try­ing to get some of their code accepted as future C++ standards.

There are two classes in Boost that are used fre­quently in our code: for­mat and path.  I like them and find them use­ful, but a small detail of their imple­men­ta­tion just rubs me the wrong way.  Before I get into that detail, let me first dis­cuss their util­ity.  Pro­gram­mers often have to for­mat strings.  In C, the way to do this is sim­i­lar to:

sprintf(buffer, "The answer to %s is %d", "your question", 42);

You had bet­ter hope that “buffer” is big enough to hold the answer, though.  You could dynam­i­cally allo­cate the buffer (and remem­ber to free it later).  You can use snprintf() and worry about the null-terminator if the buffer is too small.  But any way you do it, it’s going to take a few lines of code, might not be mem­ory effi­cient, and prob­a­bly isn’t as clean as it should be.

The reg­u­lar C++ way of per­form­ing this oper­a­tion uses a class to worry about the details of buffer size, string length, and releas­ing the buffer.  For example:

std::ostringstream buffer << "The answer to " << "your question" << " is " << 42;

This sort of works, but gets ugly if you have lots of vari­ables inter­spersed with sta­tic text.  It also kills any efforts at internationalization.

The Boost way of doing this is more like the C way, but with­out the buffer size worries:

std::string buffer = str(format("The answer to %s is %d") % "your question" % 42);

It is even less crash-prone than the C sprintf().  If you were to feed it only one argu­ment, but have two or more place­hold­ers in your for­mat string, Boost would be fine, but sprintf() may choke, include garbage, or crash.  With Boost, you can even get more fancy (with bet­ter i18n com­pli­ance) by using posi­tional arguments:

std::string buffer = str(format("The answer to %2% is %1%") % 42 % "your question");

The per­cents act sort of like the Python for­mat­ting operator:

buffer = “The answer to %s is %d” % {“your ques­tion”, 42};

The Boost object for work­ing with paths uses a sim­i­lar trick, only instead of the per­cent oper­a­tor, it’s the slash oper­a­tor.  You can do some­thing like this:

path myPath = "/home/brian";
myPath = myPath / "Pictures";
myPath /= "photo.jpg";
myPath.replace_extension("png");

And in the end myPath.string() holds the text “/home/brian/pictures/photo.png”.  It gets the slashes and back­slashes cor­rect for Unix ver­sus Win­dows and has all sorts OS-agnostic helper func­tions for cre­at­ing paths, delet­ing files, and whatnot.

As much as I like these classes and enjoy their ease of use, there is one minor thing that rubs me the wrong way, as I men­tioned above.  Specif­i­cally: the “clever” way they’ve over­loaded the per­cent and slash oper­a­tors.  I was taught long, long ago that although you can over­load stan­dard oper­a­tors in C++ it’s a bad, bad thing to be too clever in chang­ing their seman­tic mean­ing.  The oper­a­tor+() func­tion should always per­form some sort of addi­tion (be it sum­ma­tion or con­cate­na­tion).  Sim­i­larly, operator/() should divide or do some­thing akin to divi­sion.  Oper­a­tor%() should be mod­u­lus (the remain­der after divid­ing).  You can over­load them to do some­thing spe­cial for your class related to the orig­i­nal mean­ing (adding com­plex num­bers, cross-multiplying matri­ces), but you shouldn’t go too out-of-bounds with their meaning.

I could have sworn that I read this in the Bjarne Strous­trup C++ book or one of the other top-rated C++ books, but skim­ming my dig­i­tal copies to find a spe­cific ref­er­ence quo­ta­tion to cite turned up nothing.

Do I find the for­mat and path classes incred­i­bly use­ful?  Yes!  Do I find their oper­a­tor over­load­ing a minor annoy­ance?  Yes!  Is it annoy­ing enough to pre­vent me from using those classes?  No!  I find the Boost imple­men­ta­tion of the slash and per­cent a lit­tle too out­side the box from what I’m used to, but I’ll go ahead and keep using them and will prob­a­bly get used to it in time.

{ 0 comments }

Silver-plated Letterpress

August 9, 2010

I have only linked to Etsy prod­ucts a few times on this blog, but each time I do I feel that I need a boil­er­plate caveat about my views on Etsy — or more specif­i­cally, the prod­ucts being sold there.  I mainly see Etsy through the lens of Regretsy, which goes to great lengths to […]

2 comments Read the full article →

Psychological or physiological?

August 6, 2010

As I men­tioned the other day, I have restarted the 100 Pushups and 200 Situps pro­grams.  Some­thing I noticed, both this time around and last time, is that my pos­ture has improved a bit.  I sit a lit­tle straighter.  I stand a lit­tle taller.  This feels like it is a result of the exer­cise, but […]

0 comments Read the full article →

The Home Depot Absinthe Fountain

August 3, 2010

In the past few weeks I have men­tioned my “ghetto, Home Depot absinthe foun­tain” to a num­ber of friends.  Given the inter­net mantra of “pics or it didn’t hap­pen” and my dif­fi­culty of describ­ing all of the lit­tle details in words, let me start out with a pho­to­graph: The project arose from my desire to […]

0 comments Read the full article →

Hundreds of situps and pushups

July 30, 2010

About six months ago I started doing the One Hun­dred Pushups and Two Hun­dred Situps train­ing plans.  (The situp name is a bit of a mis­nomer, in that they’re really curl-ups or crunches since actual “situps” — the type most of us learned as kids — are not all that great for your body.)  Although […]

1 comment Read the full article →

Money vs. Culture

July 28, 2010

There is a great guy named Mer­lin Mann whose work I have been appre­ci­at­ing for years.  He is pretty much a time and pro­duc­tiv­ity guru, although I am not sure you’d be able to catch him using the word “guru” to describe him­self in any way.  He invented the Hip­ster PDA and Inbox Zero.  Earlier […]

0 comments Read the full article →

My installed iPad apps. Let me show you them.

July 26, 2010

In the last two days, three peo­ple have asked me about iPad apps.  I used to occa­sion­ally post what apps I was run­ning on the iPhone (the last being in 2008), but have never done this with the iPad.  So in the inter­est of shar­ing, let me walk you through my apps. My dock apps consist […]

0 comments Read the full article →

Pioneer Courthouse, 1875

July 21, 2010

There is a local blog I fol­low, Vin­tage Port­land, that digs through the city archives and posts vin­tage pho­tos of Port­land every few days.  Last month, they posted a photo of Pio­neer Cour­t­house from 1875, with a school in the back­ground that is now PDX’s liv­ing room, a.k.a. Pio­neer Cour­t­house Square.  I really enjoyed the […]

0 comments Read the full article →

Radio Shack: “Is this stuff any good?”

July 20, 2010

My last two posts (skip­ping over the one regard­ing comics) men­tioned Radio Shack in one form or another, so I thought I would com­plete the tril­ogy with this one.  As you may recall read­ing, I worked at Radio Shack in the early 90s.  It was min­i­mum wage plus poor com­mis­sion, so basi­cally ended up being […]

0 comments Read the full article →

The Twitterverse answers my comic book question

July 19, 2010

As a kid, I never got into comic books all that much.  I sus­pect that the only comics I ever read were the free Whiz Kids comics from Radio Shack.  Seri­ously.  When the League of Extra­or­di­nary Gen­tle­men movie was on the hori­zon, I picked up the graphic novel upon which it was based.  I love […]

9 comments Read the full article →

The six degrees of Amber Alerts

July 13, 2010

This is Dave.  Back in the early 90s I ran into Dave when I was work­ing at Radio Shack.  He came in to buy a tone dialer and a crys­tal of a spe­cific fre­quency and I imme­di­ately knew what sort of shenani­gans he was up to because I had down­loaded and read the same text files with […]

2 comments Read the full article →