UniqueChars class

| | No TrackBacks

Adapted from [research!rsc]

public class UniqueChars<T>
{
	private int n;
	private T dense[((unsigned long)((T)-1)) + 1];
	private T sparse[((unsigned long)((T)-1)) + 1];

	public UniqueChars() { clearSet(); }
	public void clearSet() { n = 0; }

	public void addMember(T val)
	{
		dense[n] = val;
		sparse[val] = n++;
	}
	public isMember(T val)
	{
		const T index = sparse[val];
		return index < n && dense[index] == val;
	}
	public void removeMember(T val)
	{
		if (isMember(val))
		{
			const T old_val = dense[--n];
			const T new_index = sparse[val]
			dense[new_index] = old_val;
			sparse[old_val] = new_index;
		}
	}
};

void findUniqueChars(const unsigned char *str)
{
	UniqueChars<unsigned char> s;
	unsigned char c;
	while (0 != (c = *str++))
	{
		if (! s.isMember(c))
		{
			s.addMember(c);
			putc (c, stdout);
		}
	}
}

No TrackBacks

TrackBack URL: http://www.iwebthereforeiam.com/cgi-bin/mt/mt-tb.cgi/25

Leave a comment

Verification (needed to reduce spam):

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.32-en

About this Entry

This page contains a single entry by Hugh Brown published on April 4, 2008 8:59 AM.

SQL was the previous entry in this blog.

Wine search engines/online vendors is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.