Parma_Polyhedra_Library::Bit_Matrix Class Reference
[C++ Language Interface]

A matrix of bits. More...

#include <Bit_Matrix.defs.hh>

List of all members.

Classes

struct  Bit_Row_Less_Than
 Ordering predicate (used when implementing the sort algorithm). More...

Public Member Functions

 Bit_Matrix ()
 Default constructor.
 Bit_Matrix (dimension_type n_rows, dimension_type n_columns)
 Construct a bit matrix with n_rows rows and n_columns columns.
 Bit_Matrix (const Bit_Matrix &y)
 Copy constructor.
 ~Bit_Matrix ()
 Destructor.
Bit_Matrixoperator= (const Bit_Matrix &y)
 Assignment operator.
void swap (Bit_Matrix &y)
 Swaps *this with y.
Bit_Rowoperator[] (dimension_type k)
 Subscript operator.
const Bit_Rowoperator[] (dimension_type k) const
 Constant subscript operator.
void clear ()
 Clears the matrix deallocating all its rows.
void transpose ()
 Transposes the matrix.
void transpose_assign (const Bit_Matrix &y)
 Makes *this a transposed copy of y.
dimension_type num_columns () const
 Returns the number of columns of *this.
dimension_type num_rows () const
 Returns the number of rows of *this.
void sort_rows ()
 Sorts the rows and removes duplicates.
bool sorted_contains (const Bit_Row &row) const
 Looks for row in *this, which is assumed to be sorted.
void add_recycled_row (Bit_Row &row)
 Adds row to *this.
void rows_erase_to_end (dimension_type first_to_erase)
 Erases the rows from the first_to_erase -th to the last one.
void columns_erase_to_end (dimension_type first_to_erase)
 Erases the columns from the first_to_erase -th to the last one.
void resize (dimension_type new_n_rows, dimension_type new_n_columns)
 Resizes the matrix copying the old contents.
bool OK () const
 Checks if all the invariants are satisfied.
void ascii_dump () const
 Writes to std::cerr an ASCII representation of *this.
void ascii_dump (std::ostream &s) const
 Writes to s an ASCII representation of *this.
void print () const
 Prints *this to std::cerr using operator<<.
bool ascii_load (std::istream &s)
 Loads from s an ASCII representation (as produced by ascii_dump(std::ostream&) const) and sets *this accordingly. Returns true if successful, false otherwise.
memory_size_type total_memory_in_bytes () const
 Returns the total size in bytes of the memory occupied by *this.
memory_size_type external_memory_in_bytes () const
 Returns the size in bytes of the memory managed by *this.
bool check_sorted () const
 Checks whether *this is sorted. It does NOT check for duplicates.

Static Public Member Functions

static dimension_type max_num_rows ()
 Returns the maximum number of rows of a Bit_Matrix.

Private Attributes

std::vector< Bit_Rowrows
 Contains the rows of the matrix.
dimension_type row_size
 Size of the initialized part of each row.

Friends

void Parma_Polyhedra_Library::Linear_System::sort_and_remove_with_sat (Bit_Matrix &sat)

Related Functions

(Note that these are not member functions.)



bool operator== (const Bit_Matrix &x, const Bit_Matrix &y)
 Returns true if and only if x and y are equal.
bool operator!= (const Bit_Matrix &x, const Bit_Matrix &y)
 Returns true if and only if x and y are not equal.
void swap (Parma_Polyhedra_Library::Bit_Matrix &x, Parma_Polyhedra_Library::Bit_Matrix &y)
 Specializes std::swap.

Detailed Description

A matrix of bits.

Definition at line 37 of file Bit_Matrix.defs.hh.


Constructor & Destructor Documentation

Parma_Polyhedra_Library::Bit_Matrix::Bit_Matrix (  )  [inline]

Default constructor.

Definition at line 33 of file Bit_Matrix.inlines.hh.

00034   : rows(),
00035     row_size(0) {
00036 }

Parma_Polyhedra_Library::Bit_Matrix::Bit_Matrix ( dimension_type  n_rows,
dimension_type  n_columns 
) [inline]

Construct a bit matrix with n_rows rows and n_columns columns.

Definition at line 44 of file Bit_Matrix.inlines.hh.

00046   : rows(n_rows),
00047     row_size(n_columns) {
00048 }

Parma_Polyhedra_Library::Bit_Matrix::Bit_Matrix ( const Bit_Matrix y  )  [inline]

Copy constructor.

Definition at line 51 of file Bit_Matrix.inlines.hh.

00052   : rows(y.rows),
00053     row_size(y.row_size) {
00054 }

Parma_Polyhedra_Library::Bit_Matrix::~Bit_Matrix (  )  [inline]

Destructor.

Definition at line 57 of file Bit_Matrix.inlines.hh.

00057                         {
00058 }


Member Function Documentation

void Parma_Polyhedra_Library::Bit_Matrix::add_recycled_row ( Bit_Row row  ) 

Adds row to *this.

Parameters:
row The row whose implementation will be recycled.

The only thing that can be done with row upon return is destruction.

Definition at line 67 of file Bit_Matrix.cc.

References Parma_Polyhedra_Library::compute_capacity(), max_num_rows(), OK(), rows, and swap().

Referenced by Parma_Polyhedra_Library::Polyhedron::conversion().

00067                                             {
00068   const dimension_type new_rows_size = rows.size() + 1;
00069   if (rows.capacity() < new_rows_size) {
00070     // Reallocation will take place.
00071     std::vector<Bit_Row> new_rows;
00072     new_rows.reserve(compute_capacity(new_rows_size, max_num_rows()));
00073     new_rows.insert(new_rows.end(), new_rows_size, Bit_Row());
00074     // Put the new row in place.
00075     dimension_type i = new_rows_size-1;
00076     new_rows[i].swap(row);
00077     // Steal the old rows.
00078     while (i-- > 0)
00079       new_rows[i].swap(rows[i]);
00080     // Put the new rows into place.
00081     std::swap(rows, new_rows);
00082   }
00083   else
00084     // Reallocation will NOT take place: append an empty row
00085     // and swap it with the new row.
00086     rows.insert(rows.end(), Bit_Row())->swap(row);
00087   PPL_ASSERT(OK());
00088 }

void Parma_Polyhedra_Library::Bit_Matrix::ascii_dump ( std::ostream &  s  )  const

Writes to s an ASCII representation of *this.

Definition at line 152 of file Bit_Matrix.cc.

References num_columns(), and num_rows().

00152                                              {
00153   const Bit_Matrix& x = *this;
00154   const char separator = ' ';
00155   s << num_rows() << separator << 'x' << separator
00156     << num_columns() << "\n";
00157   for (dimension_type i = 0; i < num_rows(); ++i) {
00158     for (dimension_type j = 0; j < num_columns(); ++j)
00159       s << x[i][j] << separator;
00160     s << "\n";
00161   }
00162 }

void Parma_Polyhedra_Library::Bit_Matrix::ascii_dump (  )  const

Writes to std::cerr an ASCII representation of *this.

Referenced by Parma_Polyhedra_Library::Polyhedron::ascii_dump(), and Parma_Polyhedra_Library::BD_Shape< T >::ascii_dump().

bool Parma_Polyhedra_Library::Bit_Matrix::ascii_load ( std::istream &  s  ) 

Loads from s an ASCII representation (as produced by ascii_dump(std::ostream&) const) and sets *this accordingly. Returns true if successful, false otherwise.

Definition at line 167 of file Bit_Matrix.cc.

References clear().

00167                                        {
00168   Bit_Matrix& x = *this;
00169   dimension_type nrows;
00170   dimension_type ncols;
00171   std::string str;
00172   if (!(s >> nrows))
00173     return false;
00174   if (!(s >> str) || str != "x")
00175     return false;
00176   if (!(s >> ncols))
00177     return false;
00178   resize(nrows, ncols);
00179 
00180   for (dimension_type i = 0; i < num_rows(); ++i)
00181     for (dimension_type j = 0; j < num_columns(); ++j) {
00182       int bit;
00183       if (!(s >> bit))
00184         return false;
00185       if (bit)
00186         x[i].set(j);
00187       else
00188         x[i].clear(j);
00189     }
00190 
00191   // Check invariants.
00192   PPL_ASSERT(OK());
00193   return true;
00194 }

bool Parma_Polyhedra_Library::Bit_Matrix::check_sorted (  )  const

Checks whether *this is sorted. It does NOT check for duplicates.

Definition at line 232 of file Bit_Matrix.cc.

References num_rows().

Referenced by sorted_contains().

00232                                   {
00233   const Bit_Matrix& x = *this;
00234   for (dimension_type i = num_rows(); i-- > 1; )
00235     if (compare(x[i-1], x[i]) > 0)
00236       return false;
00237   return true;
00238 }

void Parma_Polyhedra_Library::Bit_Matrix::clear (  )  [inline]

Clears the matrix deallocating all its rows.

Definition at line 108 of file Bit_Matrix.inlines.hh.

References row_size, rows, and swap().

Referenced by ascii_load(), Parma_Polyhedra_Library::Polyhedron::set_empty(), Parma_Polyhedra_Library::BD_Shape< T >::shortest_path_reduction_assign(), Parma_Polyhedra_Library::Polyhedron::update_sat_c(), and Parma_Polyhedra_Library::Polyhedron::update_sat_g().

00108                   {
00109   // Clear `rows' and minimize its capacity.
00110   std::vector<Bit_Row>().swap(rows);
00111   row_size = 0;
00112 }

void Parma_Polyhedra_Library::Bit_Matrix::columns_erase_to_end ( dimension_type  first_to_erase  )  [inline]

Erases the columns from the first_to_erase -th to the last one.

Definition at line 71 of file Bit_Matrix.inlines.hh.

References OK(), and row_size.

Referenced by Parma_Polyhedra_Library::Polyhedron::conversion().

00071                                                                     {
00072   // The first column to be erased cannot be greater
00073   // than the actual number of the columns of the matrix.
00074   PPL_ASSERT(first_to_erase <= row_size);
00075   row_size = first_to_erase;
00076   PPL_ASSERT(OK());
00077 }

PPL::memory_size_type Parma_Polyhedra_Library::Bit_Matrix::external_memory_in_bytes (  )  const

Returns the size in bytes of the memory managed by *this.

Definition at line 197 of file Bit_Matrix.cc.

References num_rows(), and rows.

Referenced by Parma_Polyhedra_Library::Polyhedron::external_memory_in_bytes(), Parma_Polyhedra_Library::BD_Shape< T >::external_memory_in_bytes(), and total_memory_in_bytes().

00197                                               {
00198   memory_size_type n = rows.capacity() * sizeof(Row);
00199   for (dimension_type i = num_rows(); i-- > 0; )
00200     n += rows[i].external_memory_in_bytes();
00201   return n;
00202 }

dimension_type Parma_Polyhedra_Library::Bit_Matrix::max_num_rows (  )  [inline, static]

Returns the maximum number of rows of a Bit_Matrix.

Definition at line 39 of file Bit_Matrix.inlines.hh.

Referenced by add_recycled_row(), and resize().

00039                          {
00040   return std::vector<Bit_Row>().max_size();
00041 }

dimension_type Parma_Polyhedra_Library::Bit_Matrix::num_columns (  )  const [inline]
dimension_type Parma_Polyhedra_Library::Bit_Matrix::num_rows (  )  const [inline]
bool Parma_Polyhedra_Library::Bit_Matrix::OK (  )  const

Checks if all the invariants are satisfied.

Definition at line 205 of file Bit_Matrix.cc.

References Parma_Polyhedra_Library::Bit_Row::last(), num_rows(), Parma_Polyhedra_Library::Bit_Row::OK(), and row_size.

Referenced by add_recycled_row(), columns_erase_to_end(), Parma_Polyhedra_Library::Polyhedron::OK(), operator=(), resize(), rows_erase_to_end(), sort_rows(), transpose(), and transpose_assign().

00205                         {
00206 #ifndef NDEBUG
00207   using std::endl;
00208   using std::cerr;
00209 #endif
00210 
00211   const Bit_Matrix& x = *this;
00212   for (dimension_type i = num_rows(); i-- > 1; ) {
00213     const Bit_Row& row = x[i];
00214     if (!row.OK())
00215       return false;
00216     else if (row.last() != ULONG_MAX && row.last() >= row_size) {
00217 #ifndef NDEBUG
00218       cerr << "Bit_Matrix[" << i << "] is a row with too many bits!"
00219            << endl
00220            << "(row_size == " << row_size
00221            << ", row.last() == " << row.last() << ")"
00222            << endl;
00223 #endif
00224       return false;
00225     }
00226   }
00227   return true;
00228 }

PPL::Bit_Matrix & Parma_Polyhedra_Library::Bit_Matrix::operator= ( const Bit_Matrix y  ) 

Assignment operator.

Definition at line 37 of file Bit_Matrix.cc.

References OK(), row_size, and rows.

00037                                            {
00038   rows = y.rows;
00039   row_size = y.row_size;
00040   PPL_ASSERT(OK());
00041   return *this;
00042 }

const Bit_Row & Parma_Polyhedra_Library::Bit_Matrix::operator[] ( dimension_type  k  )  const [inline]

Constant subscript operator.

Definition at line 92 of file Bit_Matrix.inlines.hh.

References rows.

00092                                                    {
00093   PPL_ASSERT(k < rows.size());
00094   return rows[k];
00095 }

Bit_Row & Parma_Polyhedra_Library::Bit_Matrix::operator[] ( dimension_type  k  )  [inline]

Subscript operator.

Definition at line 86 of file Bit_Matrix.inlines.hh.

References rows.

00086                                              {
00087   PPL_ASSERT(k < rows.size());
00088   return rows[k];
00089 }

void Parma_Polyhedra_Library::Bit_Matrix::print (  )  const

Prints *this to std::cerr using operator<<.

void Parma_Polyhedra_Library::Bit_Matrix::resize ( dimension_type  new_n_rows,
dimension_type  new_n_columns 
)

Resizes the matrix copying the old contents.

Definition at line 116 of file Bit_Matrix.cc.

References Parma_Polyhedra_Library::compute_capacity(), max_num_rows(), num_rows(), OK(), row_size, rows, and swap().

Referenced by Parma_Polyhedra_Library::Polyhedron::add_and_minimize(), Parma_Polyhedra_Library::Polyhedron::add_space_dimensions(), Parma_Polyhedra_Library::Polyhedron::concatenate_assign(), Parma_Polyhedra_Library::Polyhedron::update_sat_c(), and Parma_Polyhedra_Library::Polyhedron::update_sat_g().

00117                                                      {
00118   PPL_ASSERT(OK());
00119   const dimension_type old_num_rows = num_rows();
00120   if (new_n_columns < row_size) {
00121     const dimension_type num_preserved_rows
00122       = std::min(old_num_rows, new_n_rows);
00123     Bit_Matrix& x = *this;
00124     for (dimension_type i = num_preserved_rows; i-- > 0; )
00125       x[i].clear_from(new_n_columns);
00126   }
00127   row_size = new_n_columns;
00128   if (new_n_rows > old_num_rows) {
00129     if (rows.capacity() < new_n_rows) {
00130       // Reallocation will take place.
00131       std::vector<Bit_Row> new_rows;
00132       new_rows.reserve(compute_capacity(new_n_rows, max_num_rows()));
00133       new_rows.insert(new_rows.end(), new_n_rows, Bit_Row());
00134       // Steal the old rows.
00135       for (dimension_type i = old_num_rows; i-- > 0; )
00136         new_rows[i].swap(rows[i]);
00137       // Put the new vector into place.
00138       std::swap(rows, new_rows);
00139     }
00140     else
00141       // Reallocation will NOT take place.
00142       rows.insert(rows.end(), new_n_rows - old_num_rows, Bit_Row());
00143   }
00144   else if (new_n_rows < old_num_rows)
00145     // Drop some rows.
00146     rows.erase(rows.begin() + new_n_rows, rows.end());
00147 
00148   PPL_ASSERT(OK());
00149 }

void Parma_Polyhedra_Library::Bit_Matrix::rows_erase_to_end ( dimension_type  first_to_erase  )  [inline]

Erases the rows from the first_to_erase -th to the last one.

Definition at line 61 of file Bit_Matrix.inlines.hh.

References OK(), and rows.

Referenced by Parma_Polyhedra_Library::Polyhedron::conversion(), Parma_Polyhedra_Library::Polyhedron::select_H79_constraints(), Parma_Polyhedra_Library::Polyhedron::simplify(), and Parma_Polyhedra_Library::Linear_System::sort_and_remove_with_sat().

00061                                                                  {
00062   // The first row to be erased cannot be greater
00063   // than the actual number of the rows of the matrix.
00064   PPL_ASSERT(first_to_erase <= rows.size());
00065   if (first_to_erase < rows.size())
00066     rows.erase(rows.begin() + first_to_erase, rows.end());
00067   PPL_ASSERT(OK());
00068 }

void Parma_Polyhedra_Library::Bit_Matrix::sort_rows (  ) 

Sorts the rows and removes duplicates.

Definition at line 45 of file Bit_Matrix.cc.

References OK(), and rows.

Referenced by Parma_Polyhedra_Library::Polyhedron::select_H79_constraints().

00045                          {
00046   const dimension_type num_elems = rows.size();
00047   if (num_elems < 2)
00048     return;
00049 
00050   // Build the function objects implementing indirect sort comparison,
00051   // indirect unique comparison and indirect swap operation.
00052   typedef std::vector<Bit_Row> Cont;
00053   Indirect_Sort_Compare<Cont, Bit_Row_Less_Than> sort_cmp(rows);
00054   Indirect_Unique_Compare<Cont> unique_cmp(rows);
00055   Indirect_Swapper<Cont> swapper(rows);
00056 
00057   const dimension_type num_duplicates
00058     = indirect_sort_and_unique(num_elems, sort_cmp, unique_cmp, swapper);
00059 
00060   if (num_duplicates > 0)
00061     rows.erase(rows.end() - num_duplicates, rows.end());
00062 
00063   PPL_ASSERT(OK());
00064 }

bool Parma_Polyhedra_Library::Bit_Matrix::sorted_contains ( const Bit_Row row  )  const [inline]

Looks for row in *this, which is assumed to be sorted.

Returns:
true if row belongs to *this, false otherwise.
Parameters:
row The row that will be searched for in the matrix.

Given a sorted bit matrix (this ensures better efficiency), tells whether it contains the given row.

Definition at line 126 of file Bit_Matrix.inlines.hh.

References check_sorted(), and rows.

Referenced by Parma_Polyhedra_Library::Polyhedron::select_H79_constraints().

00126                                                     {
00127   PPL_ASSERT(check_sorted());
00128   return std::binary_search(rows.begin(), rows.end(), row,
00129                             Bit_Row_Less_Than());
00130 }

void Parma_Polyhedra_Library::Bit_Matrix::swap ( Bit_Matrix y  )  [inline]

Swaps *this with y.

Definition at line 80 of file Bit_Matrix.inlines.hh.

References row_size, and rows.

Referenced by add_recycled_row(), clear(), Parma_Polyhedra_Library::Polyhedron::conversion(), resize(), swap(), transpose(), and transpose_assign().

00080                               {
00081   std::swap(row_size, y.row_size);
00082   std::swap(rows, y.rows);
00083 }

memory_size_type Parma_Polyhedra_Library::Bit_Matrix::total_memory_in_bytes (  )  const [inline]

Returns the total size in bytes of the memory occupied by *this.

Definition at line 115 of file Bit_Matrix.inlines.hh.

References external_memory_in_bytes().

00115                                         {
00116   return sizeof(*this) + external_memory_in_bytes();
00117 }

void Parma_Polyhedra_Library::Bit_Matrix::transpose (  ) 

Transposes the matrix.

Definition at line 91 of file Bit_Matrix.cc.

References num_columns(), num_rows(), OK(), and swap().

00091                          {
00092   const Bit_Matrix& x = *this;
00093   const dimension_type nrows = num_rows();
00094   const dimension_type ncols = num_columns();
00095   Bit_Matrix tmp(ncols, nrows);
00096   for (dimension_type i = nrows; i-- > 0; )
00097     for (unsigned long j = x[i].last(); j != ULONG_MAX; j = x[i].prev(j))
00098       tmp[j].set(i);
00099   swap(tmp);
00100   PPL_ASSERT(OK());
00101 }

void Parma_Polyhedra_Library::Bit_Matrix::transpose_assign ( const Bit_Matrix y  ) 

Friends And Related Function Documentation

bool operator!= ( const Bit_Matrix x,
const Bit_Matrix y 
) [related]

Returns true if and only if x and y are not equal.

Definition at line 134 of file Bit_Matrix.inlines.hh.

00134                                                      {
00135   return !(x == y);
00136 }

bool operator== ( const Bit_Matrix x,
const Bit_Matrix y 
) [related]

Returns true if and only if x and y are equal.

Definition at line 243 of file Bit_Matrix.cc.

References num_columns(), and num_rows().

00243                                                         {
00244   const dimension_type x_num_rows = x.num_rows();
00245   if (x_num_rows != y.num_rows()
00246       || x.num_columns() != y.num_columns())
00247     return false;
00248   for (dimension_type i = x_num_rows; i-- > 0; )
00249     if (x[i] != y[i])
00250       return false;
00251   return true;
00252 }

void Parma_Polyhedra_Library::Linear_System::sort_and_remove_with_sat ( Bit_Matrix sat  )  [friend]

Specializes std::swap.

Definition at line 145 of file Bit_Matrix.inlines.hh.

References swap().

00146                                            {
00147   x.swap(y);
00148 }


Member Data Documentation

Size of the initialized part of each row.

Definition at line 143 of file Bit_Matrix.defs.hh.

Referenced by clear(), columns_erase_to_end(), num_columns(), OK(), operator=(), resize(), and swap().


The documentation for this class was generated from the following files:
Generated on Sun Feb 27 16:20:23 2011 for PPL by  doxygen 1.6.3