/*
This program is distributed under the terms of the 'MIT license'. The text
of this licence follows...

Copyright (c) 2007 J.D.Medhurst (a.k.a. Tixy)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
@file

@brief Bit-Vector utilities header file.
*/

#ifndef BITVECTOR_H
#define BITVECTOR_H

/**
@defgroup utils_bitvector Utils - Bit-Vector

Classes for constructing and manipluating a bit-vector (array of bits).

The BitVectorBits class is a template for constructing a memory buffer to contain
a bit-vector. Bits are stored in unsigned integers, where bit index 0 is the least
significant bit of the first integer.

The concrete class which is used to manipulation the contents of a bit-vector is
BitVectorPointer. This only contains the address and size of the buffer containing
the bits, which enables the methods to work on arbitrary memory.

BitVector is a thin template which wraps up both BitVectorBits and BitVectorPointer,
providing an object contining a bit-vector and the methods to manipuate it.

@{
*/

/**
Shift value to scale bit index to index into #BitVectorBits::Buffer.
This is log2 of the number of bits in an <CODE>unsigned int</CODE>.
*/
const unsigned BitVectorIndexShift =
	sizeof(unsigned)==2 ? 4 :	// assume 2 bytes is 2^4 bits
	sizeof(unsigned)==4 ? 5 :	// assume 4 bytes is 2^5 bits
	sizeof(unsigned)==8 ? 6 :	// assume 8 bytes is 2^6 bits
	~0u;						// value which will cause later compile error

/**
Bitmask to obtain bit poision for a bit within a single entry in #BitVectorBits::Buffer.
*/
const unsigned BitVectorIndexMask = (1<<BitVectorIndexShift)-1;

// check that size of 'unsigned' is really as we specified...
ASSERT_COMPILE( (1u<<BitVectorIndexMask) == (~0u>>1)+1 );


/**
Template for a bit-vector buffer for storing S bits.

Bits are stored in unsigned integers, where bit index 0 is the least significant bit
of the first integer.

@see BitVector and BitVectorPointer.
*/
template <size_t S>
struct BitVectorBits
	{
	/**
	Buffer used to store bits.
	*/
	unsigned Buffer[(S+BitVectorIndexMask)>>BitVectorIndexShift];

	/**
	Address of #Buffer.
	*/
	inline operator unsigned*()
		{
		return Buffer;
		}
	};


/**
Template for a bit-vector buffer for storing zero bits.

@see BitVector and BitVectorPointer.
*/
template <>
struct BitVectorBits<0>
	{
	/**
	Address of Buffer, returns the null pointer.
	*/
	inline operator unsigned*()
		{
		return 0;
		}
	};


/**
Object pointing to a bit-vector (array of bits).

This is the concrete class which is used to manipulation the contents of a bit-vector.

@see BitVector and BitVectorBits.
*/
class BitVectorPointer
	{
public:
	/**
	Contructor taking a BitVectorBits object.

	@param bits		Buffer where bits are stored.
	*/
	template <size_t S>
	inline BitVectorPointer(BitVectorBits<S>& bits)
		: Bits(bits), Size(S)
		{}

	/**
	Contructor taking an arbitrary block of memory.

	@param bits		Add of memory block holding bits for bit-vector.
	@param size		Number of bits in bit-vector.
	*/
	inline BitVectorPointer(unsigned* bits, size_t size)
		: Bits(bits), Size(size)
		{}

	/**
	Empty contructor.
	*/
	inline BitVectorPointer()
		{}

	/**
	Clear all bits.
	*/
	void Reset();

	/**
	Test a single bit.

	@param index The bit to test.

	@return 1 if the bit is set, 0 if it is clear.
	*/
	int Test(unsigned index);

	/**
	Set a single bit.

	@param index The bit to set.
	*/
	void Set(unsigned index);

	/**
	Clear a single bit.

	@param index The bit to clear.
	*/
	void Clear(unsigned index);

	/**
	Test if a range of bits are all set.

	@param index The index of the first bit to test.
	@param count The number of bits to test.

	@return True if all bits are clear, false otherwise.
	*/
	int TestSet(unsigned index, size_t count);

	/**
	Test if a range of bits are all clear.

	@param index The index of the first bit to test.
	@param count The number of bits to test.

	@return True if all bits are clear, false otherwise.
	*/
	int TestClear(unsigned index, size_t count);

	/**
	Set a range of bits.

	@param index The index of the first bit to set.
	@param count The number of bits to set.
	*/
	void Set(unsigned index, size_t count);

	/**
	Clear a range of bits.

	@param index The index of the first bit to clear.
	@param count The number of bits to clear.
	*/
	void Clear(unsigned index, size_t count);

	/**
	Find an region of consecutive bits of the specified state.

	@param count	The number of required bits.
	@param state	True to find a region of set bits, false to find clear bits.

	@return The index of the first bit in the region allocated, or -1 if no region was found.
	*/
	int Find(size_t count, unsigned state);

public:
	unsigned*	Bits; ///< Pointer to array where bits are stored.
	size_t		Size; ///< Number of bits in bit-vector.
	};


/**
BitVector is a thin template which provides an object contining a bit-vector
of S bits and the methods to manipuate it.

@see BitVectorPointer and BitVectorBits.
*/
template <size_t S>
class BitVector : public BitVectorPointer, public BitVectorBits<S>
	{
public:
	/**
	Constructor which clears all bits in the bit-vector.
	*/
	inline BitVector()
		: BitVectorPointer(static_cast<BitVectorBits<S>&>(*this))
		{
		Reset();
		}
	};


/** @} */ // End of group

#endif // BITVECTOR_H
