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.
Finding the top K items in a list efficiently
Do you use sort() to find the top results? Here's a simple trick that will make your software run much faster.
O(n) Delta Compression With a Suffix Array
The difference between two sequences A and B can be compactly stored using COPY/INSERT operations. The greedy algorithm for finding these operations relies on an efficient way of finding the longest matching part of A of any given position in B. This article describes how to use a suffix array to find the optimal sequence of operations in time proportional to the length of the input sequences. As a preprocessing step, we find and store the longest match in A for every position in B in two passes over the suffix array.
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.
The Curious Complexity of Being Turned On
In software, the simplest things can turn into a nightmare, especially at a large company.
Coding tips they don't teach you in school
Some time-saving shortcuts for C code that will make your coworkers scream. In Awe.
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.