#define __SFJP_ROAST__math__bit_HPP__
#include <memory.h>
+//#include "iterator.hpp"
namespace roast
{
template <typename T>
- class bit_ptr
+ class bitref
{
protected:
- enum { TypeSize = sizeof(T) };
- enum { TypeBits = TypeSize*8 };
- T* m_p;
- size_t m_index;
+ T& m_value;
+ const size_t m_bit_index;
public:
+ bitref(T& bit_in, size_t index) : m_value(bit_in), m_bit_index(index) {}
- class _bit//ref
+ void set(bool b)
{
- protected:
- T& m_bit;
- size_t m_index;
- public:
- _bit(T& bit_in, size_t index) : m_bit(bit_in), m_index(index) {}
+ m_value |= (b << m_bit_index);
+ }
+ bool get() const
+ {
+ return !(!( m_value & (1 << m_bit_index) ));
+ //return (bool)((m_value & (1 << m_bit_index) ) >> m_bit_index);
+ }
- void set(bool b)
- {
- m_bit |= (b << m_index);
- }
- bool get()
- {
- return !(!( m_bit & (1 << m_index) ));
- //return (bool)((m_bit & (1 << m_index) ) >> m_index);
- }
-
- _bit operator = (bool b){ set(b); return *this; }
- operator bool(){ return get(); }
- operator char(){ return get(); }
- operator unsigned char(){ return get(); }
- operator short(){ return get(); }
- operator unsigned short(){ return get(); }
- operator int(){ return get(); }
- operator unsigned int(){ return get(); }
- operator long(){ return get(); }
- operator unsigned long(){ return get(); }
+ bitref operator = (bool b){ set(b); return *this; }
+ operator bool() const { return get(); }
+ operator char() const { return get(); }
+ operator short() const { return get(); }
+ operator int() const { return get(); }
+ operator long() const { return get(); }
+ operator unsigned char() const { return get(); }
+ operator unsigned short() const { return get(); }
+ operator unsigned int() const { return get(); }
+ operator unsigned long() const { return get(); }
+ };
+
+ namespace adapter
+ {
+ template <typename _Base>
+ class plus_minus_operator
+ {
};
-
+ }
+
+ class bit_iterator
+ {
+ };
+
+ //////////////////////////////////////////////////////////////
+
+ //bit_iterator
+ template <typename T>
+ class bit_ptr
+ {
+ protected:
+ enum { TypeSize = sizeof(T) };
+ enum { TypeBits = TypeSize*8 };
+ T* m_p;
+ size_t m_index;
+
public:
bit_ptr(T* p, size_t index=0) : m_p(p), m_index(index) {}
-
+
bit_ptr& operator ++(int){ m_index++; return *this; }
bit_ptr& operator --(int){ m_index--; return *this; }
bit_ptr& operator ++(){ bit_ptr work=*this; m_index++; return work; }
bit_ptr& operator +(int n){ bit_ptr work=*this; work+=n; return work; }
bit_ptr& operator -(int n){ bit_ptr work=*this; work-=n; return work; }
- _bit get_bit_ref(size_t index=0)
+ bitref<T> get_bit_ref(size_t index=0)
{
index += m_index;
size_t ary_index = index / TypeBits;
size_t bit_index = index % TypeBits;
- return _bit( m_p[ary_index], bit_index );
+ return bitref<T>( m_p[ary_index], bit_index );
}
- _bit operator [](size_t index){ return get_bit_ref(index); }
- _bit operator *(){ return get_bit_ref(); }
+ bitref<T> operator [](size_t index){ return get_bit_ref(index); }
+ bitref<T> operator *(){ return get_bit_ref(); }
void set(bool b){
get_bit_ref().set(b);
return get_bit_ref().get();
}
};
-
- ////////////////////////////////////////////////////////////////////
-
+
//template <unsigned int _BitCount, typename _BaseType = unsigned int>
template <size_t _BitCount, typename _BaseType = unsigned long>
class bits
enum { BaseTypeBitCount = sizeof(_BaseType)*8; };
enum { ArraySize = (_BitCount + BaseTypeBitCount - 1) / 8 };
typedef _BaseType ArrayType[ArraySize];
+ typedef _BaseType T;
protected:
ArrayType m_array;
+
+ bit_ptr<T> get_bit_ptr(){ return bp(&m_array, index); }
public:
bits(){ memset(&m_array,0,sizeof(m_array)); }
bits(const _BaseType& in){ m_array[0] = in; }
//////////////////////////////////////////////////////
- bits operator |= (const bits& from){
+ template <size_t _BitCount, typename _BaseType>
+ bits operator |= (const bits<_BitCount,_BaseType>& from){
for(size_t i=0; i<ArraySize; i++)
m_array[i] |= from.internal_array()[i];
}
- bool operator [] (size_t index){ return get(index); }
- bool get(size_t index)
- {
- if ( index > _BitCount )
- return false;
- size_t ary_index = index / BaseTypeBitCount;
- size_t bit_index = index % BaseTypeBitCount;
- return m_array[ary_index] & (1 << bit_index);
+ template <size_t _BitCount, typename _BaseType>
+ bits operator += (const bits<_BitCount,_BaseType>& from){
+ for(size_t i=0; i<ArraySize; i++)
+ m_array[i] |= from.internal_array()[i];
}
+ bool get(size_t index){ return get_bit_ptr().get(); }
+ void set(size_t index, bool b){ return get_bit_ptr().set(b); }
+ bitref<T> operator [] (size_t index){ return get_bit_ptr().get_bit_ref(); }
+
+ //////////////////////////////////////////////////////
+
//ArrayType internal_array(){ return m_array; }
_BaseType* internal_array(){ return m_array; }
size_t size(){ return _BitCount; }