intarray.hh

Go to the documentation of this file.
00001 
00007 #ifndef INTARRAY_h
00008 #define INTARRAY_h
00009 #include <stdint.h>
00010 #include <limits.h>
00011 
00016 class intarray : public std::iterator<std::random_access_iterator_tag,uint64_t>{
00017   //utilities:
00018 public:
00020   uint64_t GetMax(){ return _mask; }
00021   
00022 public:
00024   intarray(int nbits, void* start = 0) : 
00025     _nbits(nbits), _start((char*)start)
00026   { 
00027     if(nbits <= CHAR_BIT) _nchars = 1;
00028     else if(nbits <=2*CHAR_BIT) _nchars = 2;
00029     else if(nbits <=4*CHAR_BIT) _nchars = 4;
00030     else _nchars = 8;
00031     _mask = 0;
00032     --_mask;
00033     _mask = (uint64_t)
00034       (_mask >> (sizeof(uint64_t)*CHAR_BIT - nbits));
00035   }
00036   intarray(const intarray& right) 
00037   {
00038     if( this != &right){
00039       _nbits = right._nbits;
00040       _start = right._start;
00041       _nchars = right._nchars;
00042       _mask = right._mask;
00043     }
00044   }
00045   intarray& operator=(const intarray& right) 
00046   {
00047     if( this != &right){
00048       _nbits = right._nbits;
00049       _start = right._start;
00050       _nchars = right._nchars;
00051       _mask = right._mask;
00052     }
00053     return *this;
00054   }
00055   intarray& operator=(void* start) 
00056   {
00057     _start = (char*)start;
00058     return *this;
00059   }
00060   
00061   ~intarray() {}
00062   
00063 
00064   //operator overloads
00065   bool operator==(void* start) const
00066   {
00067     return _start == (char*)(start);
00068   }
00069   bool operator==(const intarray& right) const
00070   {
00071     return (_start == right._start && _nbits == right._nbits);
00072   }
00073   bool operator!=(const intarray& right) const
00074   {
00075     return !operator==(right);
00076   }
00077   uint64_t operator[](int index) const
00078   {
00079     return (*((uint64_t*)(_start + index*_nchars))) & _mask;
00080   }
00081   
00082   uint64_t operator*() const 
00083   {
00084     return (*(uint64_t*)(_start)) & _mask;
00085   }
00086   /*
00087   uint64_t& operator*()
00088   {
00089     return *((uint64_t*)(_start));
00090   }
00091   */
00092   intarray& operator++() //prefix++
00093   {
00094     _start += _nchars;
00095     return *this;
00096   }
00097   intarray operator++(int) //postfix++
00098   {
00099     intarray ans = *this;
00100     _start += _nchars;
00101     return ans;
00102   }
00103   intarray& operator--() //prefix--
00104   {
00105     _start -= _nchars;
00106     return *this;
00107   }
00108   intarray operator--(int) //postfix--
00109   {
00110     intarray ans = *this;
00111     _start -= _nchars;
00112     return ans;
00113   }
00114   intarray operator+(int d) const
00115   {
00116     return intarray(_nbits, _start+_nchars*d);
00117   }
00118   intarray operator-(int d) const
00119   {
00120     return intarray(_nbits, _start-_nchars*d);
00121   }
00122   int operator-(const intarray& a) const
00123   {
00124     return  (_start-a._start)/_nchars;
00125   }
00126   intarray& operator+=(int d)
00127   {
00128     _start += _nchars*d;
00129     return *this;
00130   }
00131   intarray& operator-=(int d)
00132   {
00133     _start -=_nchars*d;
00134     return *this;
00135   }
00136 
00137 private:
00138   int _nbits;
00139   char* _start;
00140   int _nchars;
00141   uint64_t _mask;
00142 
00143 public:
00144   //typedefs for interator_traits to use:
00145   typedef int difference_type ;
00146   typedef uint64_t value_type;
00147   typedef uint64_t* pointer ;
00148   typedef uint64_t& reference ;
00149   typedef std::random_access_iterator_tag iterator_category ;
00150 };
00151 
00152 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 20 Jun 2014 for daqman by  doxygen 1.6.1