Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Irfan Hamid http://www.enst.fr/~hamid

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Bit field class

// A bit field is an important data structure in computer science, major uses include memory allocators and Bloom filters.
// The code is in the form of a header file and an implementation file.

<bitfield.h>
// This class is a code snippet, it can be freely used and distributed.
// Author: irfan[dot]hamid[at]gmail[dot]com
//
// Bit fields are a widely used mechanism in computing applications.
// Typical applications include memory allocators and Bloom filters. This class
// provides a generic bit field implementation for an arbitrary number of bits.
//
// Implementation notes:
// The use of C++ allows the clean separation of the data structure from the
// interface. The bit field is implemented as a vector of unsigned int that is
// allocated at object creation.
//
// field: The vector that represents the bit field itself
// bit_count: The total number of bits in the bit field

#ifndef __BITFIELD_H__
#define __BITFIELD_H__
#include <math.h>
#define SZ_UINT sizeof(unsigned int)

class Bitfield
{
protected:
	unsigned int *field;
	unsigned int bit_count;

public:
	Bitfield (int bc);
	~Bitfield ();

	int set (int bit);
	int get (int bit);
	int reset (int bit);
};

#endif
</bitfield.h>

<bitfield.cpp>
#include <stdlib.h>
#include "bitfield.h"

// The constructor takes an int param which gives the number of bits in this
// bit field.
Bitfield::Bitfield (int bc)
{
	bit_count = bc;

	// E.g, ceil(257/32*8) = 65, 64 ints fully used, last one partially used
	field = (unsigned int*) malloc(((int) ceil(bc/SZ_UINT*8)));
}

Bitfield::~Bitfield ()
{
	if (field)
		free(field);
}

// This function sets the corresponding bit in the field equal to 1.
//
// Returns: 0 on success, -1 on error.
int Bitfield::set (int bit)
{
	// Sanity check
	if (bit >= bit_count || !field)
		return -1;

	// The correct index into the vector will be given by bit/SZ_UNIT. The
	// index into the correct vector element is given by bit%SZ_UNIT, to
	// achieve this, simply left shift 0x01 the appropriate number of times.
	field[bit/SZ_UINT] |= (0x00000001 << (bit%(SZ_UINT*8)-1));
	return 0;
}

// This function sets the corresponding bit in the field equal to 0.
//
// Returns: 0 on success, -1 on error.
int Bitfield::reset (int bit)
{
	if (bit >= bit_count || !field)
		return -1;
	field[bit/SZ_UINT] &= ~(0x00000001 << (bit%(SZ_UINT*8)-1));
	return 0;
}

// This function returns the value of the corresponding bit.
//
// Returns: The value of the bit, if the field is initialized and bit index is
// within bounds, -1 otherwise.
int Bitfield::get (int bit)
{
	if (bit >= bit_count || !field)
		return -1;
	return (field[bit/SZ_UINT] & (0x00000001 << (bit%(SZ_UINT*8)-1)) ? 1 : 0);
}
</bitfield.cpp>

Fast anagram determination

// This function will determine whether the two C strings provided are anagrams or not. Only alphabetical strings are considered.

/******************************************************************************
 * This function is a code snippet. It can be freely used and distributed.
 * Author: irfan[dot]hamid[at]gmail[at]com
 *
 * This function takes two ASCII strings in C syntax (null-terminated
 * character arrays) and determines whether they are anagrams or not.
 *
 * Implementation notes:
 * The function contains a histogram that stores the frequency of each 
 * character it encounters in the first string. For each character of the 
 * second string, it decrements the corresponding entry in the histogram. If 
 * before every decrement, the value of the histogram bucket reaches zero, 
 * then the two strings are not anagrams, as there is some character that has
 * more occurrences in the second string than in the first string.
 *
 * If either of the two strings contains a non-alphabetic character, the
 * anagram property is considered to be violated.
 *
 * Remarks:
 * It is an efficient implementation because:
 *   o It is in-place, the original strings are not copied, no alloc or copy
 *   o Does not clobber the original strings
 *   o It is a linear time implementation
 *
 * Returns:
 * True if the strings are anagrams, false otherwise.
******************************************************************************/

bool is_anagram (char w1[], char w2[])
{
	unsigned int i, sz;

	/* The histogram */
	int freqtbl[26];

	/* Sanity check */
	if ((sz = strlen(w1)) != strlen(w2))
		return false;

	/* Initialize the histogram */
	bzero(freqtbl, 26*sizeof(int));

	/* Read the first string, incrementing the corresponding histogram entry */
	for (i = 0; i < sz; i++) {
		if (w1[i] >= 'A' && w1[i] <= 'Z')
			freqtbl[w1[i]-'A']++;
		else if (w1[i] >= 'a' && w1[i] <= 'z')
			freqtbl[w1[i]-'a']++;
		else
			return false;
	}

	/* Read the second string, decrementing the corresponding histogram entry */
	for (i = 0; i < sz; i++) {
		if (w2[i] >= 'A' && w2[i] <= 'Z') {
			if (freqtbl[w2[i]-'A'] == 0)
				return false;
			freqtbl[w2[i]-'A']--;
		} else if (w2[i] >= 'a' && w2[i] <= 'z') {
			if (freqtbl[w2[i]-'a'] == 0)
				return false;
			freqtbl[w2[i]-'a']--;
		} else {
			return false;
		}
	}

	return true;
}
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS