Changeset 1619 for trunk/lib


Ignore:
Timestamp:
Oct 31, 2011, 3:48:52 AM (8 years ago)
Author:
ksherdy
Message:

Refactored bit block iterator class to contain forward/reverse iterators based and built on base scanner classes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/bitblock_iterator.hpp

    r1611 r1619  
    1111
    1212#include "bitblock.hpp"
     13#include "bitblock_scan.hpp"
    1314
    1415template<class bitblock_t, class scanblock_t>
    15 class BitBlockIteratorBase: public iterator<input_iterator_tag, int>
     16class ForwardIterator //: public iterator<input_iterator_tag, int>
    1617{
    1718public:
    18         // equal position and stream
    19         bool operator==(const BitBlockIteratorBase& iter)
     19        ForwardIterator() {}
     20
     21        ForwardIterator(bitblock_t * s): scanner(s)
    2022        {
    21                 return((strm = iter.strm) && (crt_pos == iter.crt_pos));
     23                scanner.scan_to_next();
    2224        }
    2325
    24         // not equal position and stream
    25         bool operator!=(const BitBlockIteratorBase& iter)
     26        // equal position and stream
     27        bool operator==(const ForwardIterator& iter)
    2628        {
    27                 return((strm != iter.strm) && (crt_pos != iter.crt_pos));
     29                return ((scanner.get_strm() = iter.scanner.get_strm()) && (scanner.get_pos() == iter.scanner.get_pos));
     30        }
     31
     32        // not equal .get_pos()ition and stream
     33        bool operator!=(const ForwardIterator& iter)
     34        {
     35                return ( (scanner.get_strm() != iter.scanner.get_strm()) && (scanner.get_pos() != iter.scanner.get_pos()));
    2836        }
    2937
     
    3139        IDISA_ALWAYS_INLINE int operator*()
    3240        {
    33                 return crt_pos;
    34         }
    35 
    36 protected:
    37         // default constructor defines past-the-end of bit stream semantics, crt_pos == -1
    38         BitBlockIteratorBase(): strm(NULL), crt_pos(-1), crt_blk(-1), scan_blk(-1){}
    39         BitBlockIteratorBase(bitblock_t * s, uint32_t pos, uint32_t blk):  strm(s), crt_pos(pos), crt_blk(blk), scan_blk(*((scanblock_t *)s)) {}
    40 
    41         bitblock_t * strm;
    42         uint32_t crt_pos;
    43         uint32_t crt_blk;
    44         scanblock_t scan_blk;
    45 };
    46 
    47 class BitBlockForwardIterator : public BitBlockIteratorBase<BitBlock, unsigned long>
    48 {
    49 public:
    50         BitBlockForwardIterator(): BitBlockIteratorBase<BitBlock, unsigned long>() {}
    51 
    52         BitBlockForwardIterator(BitBlock * b): BitBlockIteratorBase<BitBlock, unsigned long>(b,0,0)
    53         {
    54                 scan_forward();
     41                return scanner.get_pos();
    5542        }
    5643
    5744        // pre-increment
    58         IDISA_ALWAYS_INLINE BitBlockForwardIterator& operator++()
     45        IDISA_ALWAYS_INLINE ForwardIterator& operator++()
    5946        {
    60                 scan_forward();
     47                scanner.scan_to_next();
    6148                return(*this);
    6249        }
    6350
    6451        // post-increment
    65         BitBlockForwardIterator& operator++(int)
     52        ForwardIterator& operator++(int)
    6653        {
    67                 BitBlockForwardIterator temp(*this);
     54                ForwardIterator temp(*this);
    6855                ++(*this);
    6956                return(temp);
     
    7158
    7259private:
     60        ForwardScanner<bitblock_t, scanblock_t> scanner;
     61};
    7362
    74         IDISA_ALWAYS_INLINE void scan_forward() {
    75                 while (crt_blk < (sizeof(BitBlock)/sizeof(unsigned long))){
    76                         if(scan_blk > 0){
    77                                 crt_pos = cfzl(scan_blk) + (crt_blk * (sizeof(unsigned long)*8));
    78                                 scan_blk = scan_blk & (scan_blk-1);  // clear rightmost bit
    79                                 return;
    80                         }
     63class BitBlockForwardIterator: public ForwardIterator<BitBlock, unsigned long> {
     64public:
     65        BitBlockForwardIterator(){}
     66        BitBlockForwardIterator(BitBlock * s): ForwardIterator<BitBlock, unsigned long>(s){}
     67};
    8168
    82                         crt_blk++;
    83                         scan_blk = *((unsigned long *)strm + crt_blk);
    84                 };
    85 
    86                 crt_pos = -1;
    87                 return;
     69template<class bitblock_t, class scanblock_t>
     70class ReverseIterator
     71{
     72public:
     73        ReverseIterator() {}
     74        ReverseIterator(BitBlock * s): scanner(s)
     75        {
     76                scanner.scan_to_next();
    8877        }
    8978
    90 };
     79        // equal position and stream
     80        bool operator==(const ReverseIterator& iter)
     81        {
     82                return ((scanner.get_strm() = iter.scanner.get_strm()) && (scanner.get_pos() == iter.scanner.get_pos));
     83        }
    9184
    92 class BitBlockReverseIterator : public BitBlockIteratorBase<BitBlock, unsigned long>
    93 {
    94 public:
    95         BitBlockReverseIterator(): BitBlockIteratorBase<BitBlock, unsigned long>() {}
     85        // not equal .get_pos()ition and stream
     86        bool operator!=(const ReverseIterator& iter)
     87        {
     88                return ((scanner.get_strm() != iter.scanner.get_strm()) && (scanner.get_pos() != iter.scanner.get_pos()));
     89        }
    9690
    97         BitBlockReverseIterator(BitBlock * b): BitBlockIteratorBase<BitBlock, unsigned long>(b,0,sizeof(BitBlock)/sizeof(unsigned long))
     91        // Returns absolute position.
     92        IDISA_ALWAYS_INLINE int operator*()
    9893        {
    99                 scan_reverse();
     94                return scanner.get_pos();
    10095        }
    10196
    10297        // pre-increment
    103         IDISA_ALWAYS_INLINE BitBlockReverseIterator& operator--()
     98        IDISA_ALWAYS_INLINE ReverseIterator& operator--()
    10499        {
    105                 scan_reverse();
     100                scanner.scan_to_next();
    106101                return(*this);
    107102        }
    108103
    109104        // post-increment
    110         BitBlockReverseIterator& operator--(int)
     105        ReverseIterator& operator--(int)
    111106        {
    112                 BitBlockReverseIterator temp(*this);
     107                ReverseIterator temp(*this);
    113108                --(*this);
    114109                return(temp);
     
    116111
    117112private:
    118 
    119         IDISA_ALWAYS_INLINE void scan_reverse() {
    120                 while (crt_blk > 0){
    121                         if(scan_blk > 0){
    122                                 crt_pos = ((sizeof(unsigned long)*8) - cbzl(scan_blk)) + (crt_blk * (sizeof(unsigned long)*8));
    123                                 scan_blk = scan_blk & (crt_pos-1);  // clear leftmost bit
    124                                 return;
    125                         }
    126 
    127                         crt_blk--;
    128                         scan_blk = *((unsigned long *)strm + crt_blk);
    129                 };
    130 
    131                 crt_pos = -1;
    132                 return;
    133         }
    134 
     113        ReverseScanner<bitblock_t, scanblock_t> scanner;
    135114};
    136115
Note: See TracChangeset for help on using the changeset viewer.