CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Need 2-dimensional Bitarray

    If you do not need the second dimension bits in contiguous memory, then an "std::vector<std::bitset<500>> my_bits(1000)" should fit the bill.

    If you need everything in contiguous memory, or would like to wrap everything inside a strong type, just do the good old wrapper approach:

    Code:
    #include <bitset>
    
    template <size_t M, size_t N>
    class bitset2D
    {
    public:
      //typedefs
      typedef typename std::bitset<M*N>::reference reference;
    
      //bitset::bitset
      //See http://www.cplusplus.com/reference/stl/bitset/bitset/
      bitset2D() : m_bits(){}
    
      //bitset operators
      //See http://www.cplusplus.com/reference/stl/bitset/operators/
    
      //Bit access
      bool operator()(size_t m, size_t n) const {return m_bits[m*N + n];}
      reference operator()(size_t m, size_t n) {return m_bits[m*N + n];}
    
      //Bit operations:
      bitset2D<M, N>& reset() {m_bits.reset(); return *this;}
      bitset2D<M, N>& reset(size_t m, size_t n) {m_bits.reset(m*N + n); return *this;}
      bitset2D<M, N>& flip() {m_bits.flip(); return *this;}
      bitset2D<M, N>& flip(size_t m, size_t n) {m_bits.flip(m*N + n); return *this;}
      bitset2D<M, N>& set() {m_bits.set(); return *this;}
      bitset2D<M, N>& set(size_t m, size_t n, bool val = true) {m_bits.set(m*N + n, val); return *this;}
    
      //Bitset operations:
      unsigned long to_ulong() const {return m_bits.to_ulong();}
      template <class charT, class traits, class Allocator>
      std::basic_string<charT,traits,Allocator> to_string() const {m_bits.to_string<charT, traits, Allocator>(); return *this;}
      unsigned long count() const {return m_bits.count();}
      unsigned long size() const {return m_bits.size();}
      unsigned long test(size_t m, size_t n) const {m_bits.test(m*N + n); return *this;}
      unsigned long any() const {return m_bits.any();}
      unsigned long none() const {return m_bits.none();}
    
    private:
      std::bitset<M*N> m_bits;
    };
    This wraps most of bitset into a 2D class. I didn't do all the constructors, or any of the operators (because I'm not going to do everything myself).

    Here is some example usage.
    Code:
    #include <iostream>
    #include "bitset2D.h"
    
    int main()
    {
      bitset2D<1000,500> a;
      std::cout << a(1,1) << std::endl;
    }
    I used "a(1,1)" instead of "a[1][1]". Long story short, it is a mostly better approach. If you MUST have the a[1][1] syntax, then I recommend writing an operator[j] which returns a proxy object, itself having an operator[i] which just forwards back to (j, i). YOu can then move operator()(i, j) to protected, if you want.
    Last edited by monarch_dodra; April 5th, 2012 at 04:48 AM. Reason: little errors
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured