/* This file contains the definition of the classes binArray and binMatrix. These two classes could have been simply defined as Array and Matrix. However, we chose to define new classes for two reasons: - the set of operators and comparators can be more specific to booleans, - we chose to implement each coefficient on a single bit to save memory space and to exploit C bitwise operators. September 23, 94 Creation: E. Mayoraz */ #ifndef binMatrix_h #define binMatrix_h #include "../Basics/basic.h" #include "Matrix.h" typedef unsigned char packet; typedef Array ArrayP; typedef Matrix MatrixP; extern const packetSize; extern const packet p2 [8]; #define moduloB(n) ( ((n)%8) ) #define ceilDB(n) ( (((n)+7)>>3) ) #define floorDB(n) ( ((n)>>3) ) class binMatrix; // Forward declaration class binArray : protected virtual ArrayP { public: ///////////////////////// // Arrays constructors // ///////////////////////// binArray (); virtual ~binArray (); ////////////////////// // Arrays selectors // ////////////////////// int m() const; // return number of rows int n() const; // return number of columns // subArray (without space reallocation) binArray s(int i, int m, int j, int n) const; binArray s(int i=0, int m=1, int j=0) const; // subMatrix (with space reallocation) binMatrix _(int i, int m, int j, int n) const; binMatrix _(int i=0, int m=1, int j=0) const; // subscripting boolean operator () (int i, int j) const; boolean operator () ( int j) const; char block (int i, int j) const; char block ( int j) const; // [j] return the integer representation of the 8 bits // from (8j) to (8j+7), similarly for [i,j]. // The result of block can be arbitrary for j = 0 or n()/8. ////////////////////// // Arrays modifiers // ////////////////////// binArray& operator = (const boolean b); // assign to a constant binArray& operator = (const binArray& a); // assign binArray& operator &= (const binArray& a); // and and assign binArray& operator |= (const binArray& a); // or and assign binArray& operator ^= (const binArray& a); // xor and assign // N.B. On the contrary than for class Array, the 4 arrays assignements // operators are not valid if m() != a.m() || n() != a.n(). binArray& setOn ( int j); binArray& setOn (int i, int j); binArray& setOff ( int j); binArray& setOff (int i, int j); binArray& flip ( int j); binArray& flip (int i, int j); binArray& set ( int j, boolean a); binArray& set (int i, int j, boolean a); //////////////////////// // Arrays comparators // //////////////////////// boolean operator < (const binArray& a) const; // this <= a && this != a. boolean operator <= (const binArray& a) const; // this(i,j) <= a(i,j) Vi,j. boolean operator > (const binArray& a) const; // a < this. boolean operator >= (const binArray& a) const; // a <= this. boolean operator == (const binArray& a) const; // this(i,j) == a(i,j) Vi,j. boolean operator != (const binArray& a) const; // !(this == a). int operator %= (const binArray& a) const; // !!! This last operator does not have the usual sense. !!! // It returns +1 if *this < a, 0 if *this == a, // -1 if *this > a, 2 if *this and a ar not comparable. // All the comparators are valid only if m() == a.m() && n() == a.n(). ////////////////////// // Arrays operators // ////////////////////// binMatrix operator & (const binArray& a) const; binMatrix operator | (const binArray& a) const; binMatrix operator ^ (const binArray& a) const; binMatrix operator ~ () const; // complement component-wise binMatrix andByRows () const; // bit-wise 'and' between all rows binMatrix orByRows () const; // bit-wise 'or' between all rows binMatrix xorByRows () const; // bit-wise 'xor' between all rows binMatrix andByCols () const; // bit-wise 'and' between all columns binMatrix orByCols () const; // bit-wise 'or' between all columns binMatrix xorByCols () const; // bit-wise 'xor' between all columns binMatrix t() const; // transpose /////////////////// // I/O of arrays // /////////////////// // output friend ostream& operator << (ostream& s, const binArray& a); // input friend istream& operator >> (istream& s, binArray& a); ////////////////////////////////////////////////////////////////////////////// protected: binArray (int l, int m, int n, int jOff, packet* d=0); binArray ( int m, int n, int jOff, packet* d=0); binArray ( int m, int n, packet* d=0); binArray ( int n, packet* d=0); binArray (const binArray& a); binArray& resize (const binArray& a); int offJ; int realN; }; class binMatrix : protected virtual ArrayP, public binArray, protected MatrixP { public: /////////////////////////// // Matrices constructors // /////////////////////////// binMatrix (int m, int n); binMatrix ( int n); binMatrix (int m, int n, const boolean b); binMatrix ( int n, const boolean b); binMatrix (const binMatrix& a); binMatrix (const binArray& a); binMatrix (); virtual ~binMatrix (); //////////////////////// // Matrices modifiers // //////////////////////// binMatrix& resize (const binArray& a); binMatrix& resize (int m, int n); binMatrix& resize ( int n=0); binMatrix& resize (int m, int n, const boolean b); binMatrix& resize ( int n, const boolean b); binMatrix& resize_(int m, int n); binMatrix& resize_( int n=0); //////////////////////// // Matrices operators // //////////////////////// binMatrix& operator = (const binMatrix& a); binMatrix& operator = (const binArray& a); binMatrix& operator = (const boolean a); }; #endif