using-gcc-c-hash-classes-with-strings

So even though hash_map and hash_set aren’t a part of the C++ standard, there are still implementations included with the gcc compiler. You have to include or and a little magic to get them to work properly with std::string keys.

// Example code using a hash_set with std::string keys on gcc
// Example based on hash_set code from "6.7 Using Hashed Containers"
// in O'Reilly C++ Cookbook by Stephens, Diggins, Turkanis & Cogswell  (2006)
// and gcc fix from post http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html
// Tested with g++ 4.1.2
// Compile with:
// g++ -o hash hash.cpp
// Kristi Tsukida <kristi.tsukida at gmail dot com> 30-10-2008
 
#include <iostream>
#include <string>
#include <ext/hash_set>
 
using namespace std;
// The __gnu_cxx namespace contains the hash_set since it's not standard c++
using namespace __gnu_cxx;
 
// This is the magic that will allow the usage of string keys in the hash
namespace __gnu_cxx
{
        template<> struct hash< std::string >
        {
                size_t operator()( const std::string& x ) const
                {
                        return hash< const char* >()( x.c_str() );
                }
        };
}
 
// must specify the hash<string> hash function
typedef hash_set<string, hash<string> > string_hash_set;
 
int main()
{
	string_hash_set hs;
 
	string s = "bravo";
	hs.insert(s);
	s = "alpha";
	hs.insert(s);
	s = "charlie";
	hs.insert(s);
 
	for(string_hash_set::const_iterator p = hs.begin(); p!= hs.end(); ++p)
	{
		cout << *p << endl;
	}
 
}

Add all files which aren’t currently under version control to the svn:ignore property.

for line in $(svn status | grep ^? | awk '{print $2}')
do
    pushd $(dirname $line)
    svn propset svn:ignore "$(svn propget svn:ignore)$(echo -e "\n$(basename $line)")" .  
    popd
done
svn commit
opencv-corner-detection-using-cvgoodfeaturestotrack

Here’s an example opencv application which uses cvGoodFeaturesToTrack to detect corners in a webcam video feed.

download good_features.cpp source

//
// This code displays corners found by the opencv function
// GoodFeaturesToTrack (cvGoodFeaturesToTrack)
//
// Sample webcam code taken from
//   http://www.cs.iit.edu/~agam/cs512/lect-notes/
//   opencv-intro/opencv-intro.html#SECTION00070000000000000000
//
// compile with:
// gcc `pkg-config --cflags opencv` `pkg-config --libs opencv`
// -o good_features good_features.cpp
//
// Kristi Tsukida  <kristi.tsukida@gmail.com> Aug 20, 2008
//
// Note: I commented out the corner detection using the Harris
// algorithm because my computer isn't fast enough to process
// both the Harris and the eigenvalue corners in real time.
// You can uncomment it and test for yourself.
//
 
#include <cv.h>
#include <highgui.h>
 
#include <stdio.h>
#include <sys/time.h>
 
#define VIDEO_WINDOW   "Webcam"
#define CORNER_EIG     "Eigenvalue Corner Detection"
// Disable harris processing
//#define CORNER_HARRIS  "Corner Detection (Harris)"
 
#define USEC_PER_SEC 1000000L
 
CvScalar target_color[4] = { // in BGR order
		{{   0,   0, 255,   0 }},  // red
		{{   0, 255,   0,   0 }},  // green
		{{ 255,   0,   0,   0 }},  // blue
		{{   0, 255, 255,   0 }}   // yellow
};
 
// returns the number of usecs of (t2 - t1)
long time_elapsed (struct timeval &t1, struct timeval &t2) {
 
	long sec, usec;
 
	sec = t2.tv_sec - t1.tv_sec;
	usec = t2.tv_usec - t1.tv_usec;
	if (usec < 0) {
		--sec;
		usec = usec + USEC_PER_SEC;
	}
	return sec*USEC_PER_SEC + usec;
}
 
struct timeval start_time;
struct timeval end_time;
void start_timer() {
	struct timezone tz;
	gettimeofday (&start_time, &tz);
}
long end_timer() {
	struct timezone tz;
	gettimeofday (&end_time, &tz);
	return time_elapsed(start_time, end_time);
}
 
// A Simple Camera Capture Framework
int main(int argc, char *argv[]) {
	long harris_time;
	long eig_time;
 
	CvCapture* capture = 0;
	IplImage* curr_frame = 0; // current video frame
	IplImage* gray_frame = 0; // grayscale version of current frame
	int w, h; // video frame size
	IplImage* eig_image = 0;
	IplImage* temp_image = 0;
	// Disable harris processing
	//IplImage* harris_eig_image = 0;
	//IplImage* harris_temp_image = 0;
 
	// Pick one of these capture methods:
	// You must have compiled opencv with ffmpeg enabled
	// to use a web stream!
	//capture = cvCaptureFromFile(
	//		"http://user:pw@192.168.1.101:81/img/video.mjpeg");
	//capture = cvCaptureFromAVI(
	//		"http://user:pw@192.168.1.101:81/img/video.mjpeg");
 
	// Capture from a webcam
	capture = cvCaptureFromCAM(CV_CAP_ANY);
	//capture = cvCaptureFromCAM(0); // capture from video device #0
	if ( !capture) {
		fprintf(stderr, "ERROR: capture is NULL... Exiting\n");
		//getchar();
		return -1;
	}
 
	// Create a window in which the captured images will be presented
	cvNamedWindow(VIDEO_WINDOW, 0); // allow the window to be resized
 
	cvNamedWindow(CORNER_EIG, 0); // allow the window to be resized
	cvMoveWindow(CORNER_EIG, 330, 0);
 
	// Disable harris processing
	//cvNamedWindow(CORNER_HARRIS, 0); // allow the window to be resized
	//cvMoveWindow(CORNER_HARRIS, 660, 0);
 
	// Show the image captured from the camera in the window and repeat
	while (true) {
 
		// Get one frame
		curr_frame = cvQueryFrame(capture);
		if ( !curr_frame) {
			fprintf(stderr, "ERROR: frame is null... Exiting\n");
			//getchar();
			break;
		}
		// Do not release the frame!
 
		// Get frame size
		w = curr_frame->width;
		h = curr_frame->height;
 
		// Convert the frame image to grayscale
		if( ! gray_frame ) {
			//fprintf(stderr, "Allocate gray_frame\n");
			int channels = 1;
			gray_frame = cvCreateImage(
					cvGetSize(curr_frame),
					IPL_DEPTH_8U, channels);
		}
		cvCvtColor(curr_frame, gray_frame, CV_BGR2GRAY);
 
		// ==== Allocate memory for corner arrays ====
		if ( !eig_image) {
			//fprintf(stderr, "Allocate eig_image\n");
			eig_image = cvCreateImage(cvSize(w, h),
					IPL_DEPTH_32F, 1);
		}
		if ( !temp_image) {
			//fprintf(stderr, "Allocate temp_image\n");
			temp_image = cvCreateImage(cvSize(w, h),
					IPL_DEPTH_32F, 1);
		}
// Disable harris processing
//		if ( !harris_eig_image) {
//			//fprintf(stderr, "Allocate harris_eig_image\n");
//			harris_eig_image = cvCreateImage(cvSize(w, h),
//					IPL_DEPTH_32F, 1);
//		}
//		if ( !harris_temp_image) {
//			//fprintf(stderr, "Allocate harris_temp_image\n");
//			harris_temp_image = cvCreateImage(cvSize(w, h),
//					IPL_DEPTH_32F, 1);
//		}
 
		// ==== Corner Detection: MinEigenVal method ====
		start_timer();
		const int MAX_CORNERS = 100;
		CvPoint2D32f corners[MAX_CORNERS] = {0};
		int corner_count = MAX_CORNERS;
		double quality_level = 0.1;
		double min_distance = 5;
		int eig_block_size = 3;
		int use_harris = false;
 
		cvGoodFeaturesToTrack(gray_frame,
				eig_image,                    // output
				temp_image,
				corners,
				&corner_count,
				quality_level,
				min_distance,
				NULL,
				eig_block_size,
				use_harris);
		cvScale(eig_image, eig_image, 100, 0.00);
		eig_time = end_timer();
		cvShowImage(CORNER_EIG, eig_image);
 
// Disable harris processing
//		// ==== Corner Detection: Harris method ====
//		start_timer();
////		const int MAX_CORNERS = 100;
//		CvPoint2D32f harris_corners[MAX_CORNERS] = {0};
//		int harris_corner_count = MAX_CORNERS;
//		double harris_quality_level = 0.1;
//		double harris_min_distance = 1;
//		int harris_eig_block_size = 3;
//		int harris_use_harris = true;
//
//		cvGoodFeaturesToTrack(gray_frame,
//				harris_eig_image,                    // output
//				harris_temp_image,
//				harris_corners,
//				&harris_corner_count,
//				harris_quality_level,
//				harris_min_distance,
//				NULL,
//				harris_eig_block_size,
//				harris_use_harris);
//		cvScale(harris_eig_image, harris_eig_image, 200, 0.50);
//		harris_time = end_timer();
//		cvShowImage(CORNER_HARRIS, harris_eig_image);
//
//		fprintf(stderr, "harris time: %i  eig time: %i\n", harris_time, eig_time);
 
		// ==== Draw circles around detected corners in original image
		//fprintf(stderr, "corner[0] = (%f, %f)\n", corners[0].x, corners[0].y);
		for( int i = 0; i < corner_count; i++) {
			int radius = h/25;
			cvCircle(curr_frame,
					cvPoint((int)(corners[i].x + 0.5f),(int)(corners[i].y + 0.5f)),
					radius,
					target_color[0]);
		}
		cvShowImage(VIDEO_WINDOW, curr_frame);
 
		// If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
		// remove higher bits using AND operator
		if ( (cvWaitKey(10) & 255) == 27)
			break;
	}
 
	// Release the capture device housekeeping
	cvReleaseCapture( &capture);
	cvDestroyWindow(VIDEO_WINDOW);
	cvDestroyWindow(CORNER_EIG);
// Disable harris processing
//	cvDestroyWindow(CORNER_HARRIS);
	return 0;
}
vim-case-insensitive-searches

Use “\c” anywhere in a search to ignore case (overriding your ignorecase or smartcase settings).
e.g. “/\cfoo” or “/foo\c” will match foo, Foo, fOO, FOO, etc.

Use “\C” anywhere in a search to force case matching.
e.g. “/\Cfoo” or “/foo\C” will only match foo.

You can set vim to ignore case on all your searches by running “:set ignorecase“.
If “ignorecase” is on, you can vim to ignore case on searches of only lowercase letters by running “:set smartcase“. (Searches with any capitalization or with “\C” will run a case-sensitive search.)

from Vim’s :help ignorecase and :help smartcase

wordpress-syntax-highlighting-syntaxhighlighter-plus-review

Plugin: SyntaxHighlighter Plus 0.18
Backend: Google Syntax Highlighter Javascripts
Score: 7/10

The SyntaxHighlighter Plus is another Wordpress highlighting plugin based off of the Google SyntaxHighlighter. (See my Google Syntax Highlighter Review) But while the Google Syntax Highlighter plugin simply inserts links to the javascripts, SyntaxHighlighter Plus is more integrated into Wordpress. It uses BBcode tags instead of <pre> and doesn’t require the code to be preformatted, letting Wordpress convert the special characters (sidenote: If you go back to edit the source code, you will find that the special characters have been escaped, making it difficult to edit your code.)

Unfortunately, the BBcode tags don’t allow for the configuration options to be passed to the scripts. (Maybe in a future release?)

SyntaxHighlighter Plus supports the same languages as Google Syntax Highlighter, and the formatted output looks the same. I recommend the Google Syntax Highlighter plugin if you want to use the configuration options, and this SyntaxHighlighter Plus plugin if you are too lazy to pre-escape all your special characters.

Example formatting… looks familiar

WordPress Markup: Notice the non-escaped angle brackets and [sourcecode] BBtag

wordpress-syntax-highlighting-google-syntax-highlighter-review

Plugin: Google Syntax Highlighter 1.5.1
Backend: Google Syntax Highlighter Javascripts
Score: 7/10

Google Syntax Highlighter uses Google’s Syntax Highlighter by Alex Gorbatchev.

Installation is easy, just unzip and activate. If it doesn’t work, make sure that your theme has a <?php wp_footer(); ?> tag just before the </body> tag.

Google Syntax Highlighter supports C++, C#, css, delphi, java, javascript, php, python, ruby, sql, vb, and xml (according to the Supported Languages wiki page). I’d like to see bash and perl on the list. I’m not sure how extendable the code is, or if anyone has written their own scripts for these.

Customization seems like it would be pretty easy since formatting and colors are defined in a css style sheet. There are a few configuration options you can pass to the script which control how the formatting looks. The collapse is nice, so your code starts out hidden. Unfortunately, there is no way to re-collapse the code after being expanded, but it is a nice option to have. You can also start the line numbering at a specific number.

The highlighter uses a <pre> tag to format the code, but requires that the code is preformatted, escaping special characters. This can be accomplished with postable, but I’d rather not have to deal with that. Also, since it gets parsed by javascript scripts, it lags a bit when loaded. There’s maybe half a second before the actual formatting appears.

There are some nice features, with view plain, copy to clipboard, and print links at the top of the code. Overall, a pretty nice highlighter.

Example of configuration options: collapse and showcolumns

Example highlighting
example of the Google Syntax Highlighter highlighting

Wordpress markup

wordpress-syntax-highlighting-wp-chili-review

Plugin: WP Chili 1.1
Backend: Chili 2.2
Score: 3/10

WP Chili was the #1 pick by Chris Cagle’s Wordpress Syntax Highlighting Plugins Review, so I thought I’d give it a try. I’m looking for a highlighter for c/c++ code and probably bash/perl/python scripts I’ll be posting here. My test platform is Utuntu Hardy with Firefox 3.0.1 and Opera 9.50 beta 2.

Installation was simple, just unzip into the plugins directory and activate.

Setup was a bit more complicated. I used Chris’s kindly provided css for formatting, but the formatting failed to apply. I eventually found the correct C++ tag (<code class=”cplusplus”>) in the source code of this example page. Line numbering is possible by wrapping the <code> tag in a <pre class=”ln-”> tag. I didn’t like having to preformat the code, although Elliot Swan’s postable site did make this fairly easy, it’s just another step I’d rather not do.

WP Chili seems to support c++, c#, css, delphi, html, java, javascript, lotusscript, mysql, php. It lacks support for scripting languages, and easy customizable colors (the colors are set in the javascript code, not in an easily editable css file). At least the default color scheme looks decent.

Overall, WP Chili seems like too much work to use, with only a few features and poor documentation.

Example highlighting with default colors
wp-chili syntax formatting

Wordpress markup
how to use wp-chili