I know how to make and sell software online, and I can share my tips
with you.
Email
|
Twitter
|
LinkedIn
|
Comics
|
All articles
The simple and obvious way to walk through a graph
Posted 14 years ago
At some point in your programming career you may have to go through a graph of items and process them all exactly once. If you keep following neighbours, the path might loop back on itself, so you need to keep track of which ones have been processed already.
function process_node( node )
{
if ( node.processed ) {
return;
}
node.processed = true;
var i;
for( i = 0; i < node.neighbours.length; i++ ) {
process_node( node.neighbours[i] );
}
// code to process the node goes here. It is executed only
// once per node.
}
The code works, but it only works once! The next time you try it, all the nodes will already be marked as processed, so nothing will happen.
Here's a neat trick that elegantly solves the problem. Instead of using a boolean flag in each node, use a generation count.
var CurrentGeneration = 0;
function process_node( node )
{
if ( node.generation == CurrentGeneration ) {
return;
}
node.generation = CurrentGeneration;
var i;
for( i = 0; i < node.neighbours.length; i++ ) {
process_node( node.neighbours[i] );
}
// code to process the node goes here. It is executed only
// once per node.
}
function process_all_nodes( root )
{
CurrentGeneration += 1;
process_node( root );
}
It's simple and obvious right? So why didn't I think of it in eight years?
Cell Phones on Airplanes
Much ink has been spilled about the use of cell phones on airplanes. Here's the truth, which will be disappointing to conspiracy theorists: Cell phone signals most definately have an effect on other electronic equipment. Read on for more.
Why Perforce is more scalable than Git
Branching on Perforce is kind of like performing open heart surgery. But here's why git can't hope to compete with it.
An instant rhyming dictionary for any web site
Sometimes your API has to be simple enough for non-technical people to use it. Find out how to include a rhyming dictionary on your web page just by copying and pasting.
Finding Bieber: On removing duplicates from a set of documents
Using a locality sensitive hash, you can mark duplicates in millions of items in no time.
What does your phone number spell?
Here, I explain a technique for figuring out which words are in which phone numbers. Full C source code is included.
Detecting C++ memory leaks
It's fairly simple to redefine malloc() and free() to your own functions, to track the file and line number of memory leaks.
When a reporter mangles your elevator pitch
If a reporter asks you about your new startup company, be careful what you say.
Copy a cairo surface to the windows clipboard
I just spent several hours debugging clipboard copy of a DIB image. I could copy from my application, and paste into Paint. I could paste into Word. But if I pasted into WordPad, nothing showed up. If I pasted into GIMP, it crashed.
Creating portable binaries on Linux
Distributing applications on Linux is hard. Sure, with modern package management,
installing software is easy. But if you are
distributing an application, you probably need one Windows version, plus umpteen different versions for Linux. In this article, we'll create a dummy application that targets the following operating systems, which are commonly used in business environments...