I love bitsets...

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
dirkmausf
In need of some credit
In need of some credit
Posts: 7
Joined: Tue May 15, 2007 9:05 pm
Location: Freiburg
Contact:

I love bitsets...

Post by dirkmausf » Sat Jan 01, 2011 12:40 pm

Hi all!

This is a simple wrapper class for the std::bitset<>.

I like it and used it very often in many projects.

Its yours now!


Happy new year!

Code: Select all


#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include <bitset>

#pragma once

template<long BitCount>
class wxBitSet
{

	typedef std::bitset<BitCount> ThisSet;
public:

	bool			IsSet(long Index)					{ return m_oBitset.test(Index); }
	long			GetCount()							{ return (long)m_oBitset.count(); }
	long			GetSize()							{ return (long)m_oBitset.size(); }
	bool			IsAll()								{ return m_oBitset.any(); }
	bool			IsNone()							{ return m_oBitset.none(); }
	ThisSet			FlipAll()							{ return m_oBitset.flip(); }
	void			Clear()								{ m_oBitset.reset(); }
	bool			Flip(long Index)					{ return m_oBitset.flip(Index); }
	bool			ShiftLeft(long Index)				{ m_oBitset<<Index; }
	bool			ShiftRight(long Index)				{ m_oBitset>>Index; }
	void			SetAt(long Index, bool Val=true)	{ m_oBitset.set(Index,Val); }
	bool			Set(long lValue)					{ m_oBitset=lValue; }
	bool			SetAll()							{ return m_oBitset.Set(); }
	wxString		GetAsString()						{ return (wxString)m_oBitset.to_string(); }
	unsigned long	GetAsLong()							{ return m_oBitset.to_ulong(); }

	ThisSet&		GetSTLBitSet()						{ return m_oBitset; }

    bool& operator[](int nIndex) { return m_oBitset[nIndex]; }
	void operator=(long lValue) { m_oBitset=lValue; }
	bool operator==(wxBitSet<BitCount>& in_oBitset)				{ return (in_oBitset.GetAsLong() == m_oBitset.to_ulong()); }
	bool operator!=(wxBitSet<BitCount>& in_oBitset)				{ return (in_oBitset.GetAsLong() != m_oBitset.to_ulong()); }
	
	bool			IsEqual(wxBitSet<BitCount>& in_oBitset)			{ return (in_oBitset.GetAsLong() == m_oBitset.to_ulong()); }; 
	bool			IsNotEqual(wxBitSet<BitCount>& in_oBitset)		{ return (in_oBitset.GetAsLong() != m_oBitset.to_ulong()); };
	void			AND(wxBitSet<BitCount>& in_oBitset)				{ m_oBitset &= in_oBitset.GetSTLBitSet(); };
	void			OR(wxBitSet<BitCount>& in_oBitset)				{ m_oBitset |= in_oBitset.GetSTLBitSet(); };
	void			XOR(wxBitSet<BitCount>& in_oBitset)				{ m_oBitset ^= in_oBitset.GetSTLBitSet(); };

protected:
	ThisSet			m_oBitset;

};


typedef wxBitSet<512>		BITSET512;
typedef wxBitSet<256>		BITSET256;
typedef wxBitSet<128>		BITSET128;
typedef wxBitSet<64>		BITSET64;
typedef wxBitSet<32>		BITSET32;
typedef wxBitSet<16>		BITSET16;
typedef wxBitSet<8>			BITSET8;



Greetings,

Dirk

Post Reply