PPL  0.12.1
Parma_Polyhedra_Library::Bit_Matrix Class Reference

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 m_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 remove_trailing_rows (dimension_type n)
 Removes the last n rows.
void remove_trailing_columns (dimension_type n)
 Removes the last n columns.
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)
void swap (Bit_Matrix &x, Bit_Matrix &y)
 Swaps x with y.
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.
bool operator!= (const Bit_Matrix &x, const Bit_Matrix &y)
void swap (Bit_Matrix &x, Bit_Matrix &y)

Detailed Description

A matrix of bits.

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


Constructor & Destructor Documentation

Default constructor.

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

  : rows(),
    row_size(0) {
}

Construct a bit matrix with n_rows rows and n_columns columns.

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

  : rows(n_rows),
    row_size(n_columns) {
}

Copy constructor.

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

  : rows(y.rows),
    row_size(y.row_size) {
}

Destructor.

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

                        {
}

Member Function Documentation

Adds row to *this.

Parameters:
rowThe row whose implementation will be recycled.

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

Definition at line 57 of file Bit_Matrix.cc.

References Parma_Polyhedra_Library::compute_capacity(), PPL_ASSERT, and Parma_Polyhedra_Library::swap().

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

                                            {
  const dimension_type new_rows_size = rows.size() + 1;
  if (rows.capacity() < new_rows_size) {
    // Reallocation will take place.
    std::vector<Bit_Row> new_rows;
    new_rows.reserve(compute_capacity(new_rows_size, max_num_rows()));
    new_rows.insert(new_rows.end(), new_rows_size, Bit_Row());
    // Put the new row in place.
    dimension_type i = new_rows_size-1;
    new_rows[i].m_swap(row);
    // Steal the old rows.
    while (i-- > 0)
      new_rows[i].m_swap(rows[i]);
    // Put the new rows into place.
    using std::swap;
    swap(rows, new_rows);
  }
  else
    // Reallocation will NOT take place: append an empty row
    // and swap it with the new row.
    rows.insert(rows.end(), Bit_Row())->m_swap(row);
  PPL_ASSERT(OK());
}

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

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

Writes to s an ASCII representation of *this.

Definition at line 146 of file Bit_Matrix.cc.

References Parma_Polyhedra_Library::Implementation::BD_Shapes::separator.

                                             {
  const Bit_Matrix& x = *this;
  const char separator = ' ';
  s << num_rows() << separator << 'x' << separator
    << num_columns() << "\n";
  for (dimension_type i = 0; i < num_rows(); ++i) {
    for (dimension_type j = 0; j < num_columns(); ++j)
      s << x[i][j] << separator;
    s << "\n";
  }
}

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 161 of file Bit_Matrix.cc.

References clear(), and PPL_ASSERT.

                                       {
  Bit_Matrix& x = *this;
  dimension_type nrows;
  dimension_type ncols;
  std::string str;
  if (!(s >> nrows))
    return false;
  if (!(s >> str) || str != "x")
    return false;
  if (!(s >> ncols))
    return false;
  resize(nrows, ncols);

  for (dimension_type i = 0; i < num_rows(); ++i)
    for (dimension_type j = 0; j < num_columns(); ++j) {
      int bit;
      if (!(s >> bit))
        return false;
      if (bit != 0)
        x[i].set(j);
      else
        x[i].clear(j);
    }

  // Check invariants.
  PPL_ASSERT(OK());
  return true;
}

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

Definition at line 227 of file Bit_Matrix.cc.

Referenced by sorted_contains().

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

Clears the matrix deallocating all its rows.

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

References row_size, rows, and swap().

Referenced by ascii_load(), 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().

                  {
  // Clear `rows' and minimize its capacity.
  std::vector<Bit_Row> tmp;
  using std::swap;
  swap(tmp, rows);
  row_size = 0;
}

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

Definition at line 191 of file Bit_Matrix.cc.

References Parma_Polyhedra_Library::external_memory_in_bytes().

Referenced by total_memory_in_bytes().

                                              {
  memory_size_type n = rows.capacity() * sizeof(Dense_Row);
  for (dimension_type i = num_rows(); i-- > 0; )
    n += rows[i].external_memory_in_bytes();
  return n;
}

Swaps *this with y.

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

References row_size, rows, and swap().

Referenced by Parma_Polyhedra_Library::Polyhedron::conversion(), and swap().

                                {
  using std::swap;
  swap(row_size, y.row_size);
  swap(rows, y.rows);
}

Returns the maximum number of rows of a Bit_Matrix.

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

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

Checks if all the invariants are satisfied.

Definition at line 199 of file Bit_Matrix.cc.

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

Referenced by operator=(), remove_trailing_columns(), and remove_trailing_rows().

                        {
#ifndef NDEBUG
  using std::endl;
  using std::cerr;
#endif

  const Bit_Matrix& x = *this;
  for (dimension_type i = num_rows(); i-- > 0; ) {
    const Bit_Row& row = x[i];
    if (!row.OK())
      return false;
    else if (row.last() != C_Integer<unsigned long>::max
             && row.last() >= row_size) {
#ifndef NDEBUG
      cerr << "Bit_Matrix[" << i << "] is a row with too many bits!"
           << endl
           << "(row_size == " << row_size
           << ", row.last() == " << row.last() << ")"
           << endl;
#endif
      return false;
    }
  }
  return true;
}
PPL::Bit_Matrix & Parma_Polyhedra_Library::Bit_Matrix::operator= ( const Bit_Matrix y)

Assignment operator.

Definition at line 35 of file Bit_Matrix.cc.

References OK(), PPL_ASSERT, row_size, and rows.

                                           {
  rows = y.rows;
  row_size = y.row_size;
  PPL_ASSERT(OK());
  return *this;
}
Bit_Row & Parma_Polyhedra_Library::Bit_Matrix::operator[] ( dimension_type  k)
inline

Subscript operator.

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

References PPL_ASSERT, and rows.

                                             {
  PPL_ASSERT(k < rows.size());
  return rows[k];
}
const Bit_Row & Parma_Polyhedra_Library::Bit_Matrix::operator[] ( dimension_type  k) const
inline

Constant subscript operator.

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

References PPL_ASSERT, and rows.

                                                   {
  PPL_ASSERT(k < rows.size());
  return rows[k];
}

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

Removes the last n columns.

The last n columns of the matrix are all made of zeros. If such an assumption is not met, the behavior is undefined.

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

References OK(), PPL_ASSERT, and row_size.

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

                                                          {
  // The number of columns to be erased cannot be greater
  // than the actual number of the columns of the matrix.
  PPL_ASSERT(n <= row_size);
  row_size -= n;
  PPL_ASSERT(OK());
}

Removes the last n rows.

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

References OK(), PPL_ASSERT, 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().

                                                       {
  // The number of rows to be erased cannot be greater
  // than the actual number of the rows of the matrix.
  PPL_ASSERT(n <= rows.size());
  if (n != 0)
    rows.resize(rows.size() - n);
  PPL_ASSERT(OK());
}

Resizes the matrix copying the old contents.

Definition at line 109 of file Bit_Matrix.cc.

References Parma_Polyhedra_Library::compute_capacity(), PPL_ASSERT, and Parma_Polyhedra_Library::swap().

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

                                                     {
  PPL_ASSERT(OK());
  const dimension_type old_num_rows = num_rows();
  if (new_n_columns < row_size) {
    const dimension_type num_preserved_rows
      = std::min(old_num_rows, new_n_rows);
    Bit_Matrix& x = *this;
    for (dimension_type i = num_preserved_rows; i-- > 0; )
      x[i].clear_from(new_n_columns);
  }
  row_size = new_n_columns;
  if (new_n_rows > old_num_rows) {
    if (rows.capacity() < new_n_rows) {
      // Reallocation will take place.
      std::vector<Bit_Row> new_rows;
      new_rows.reserve(compute_capacity(new_n_rows, max_num_rows()));
      new_rows.insert(new_rows.end(), new_n_rows, Bit_Row());
      // Steal the old rows.
      for (dimension_type i = old_num_rows; i-- > 0; )
        new_rows[i].m_swap(rows[i]);
      // Put the new vector into place.
      using std::swap;
      swap(rows, new_rows);
    }
    else
      // Reallocation will NOT take place.
      rows.insert(rows.end(), new_n_rows - old_num_rows, Bit_Row());
  }
  else if (new_n_rows < old_num_rows)
    // Drop some rows.
    rows.resize(new_n_rows);

  PPL_ASSERT(OK());
}

Sorts the rows and removes duplicates.

Definition at line 43 of file Bit_Matrix.cc.

References PPL_ASSERT, Parma_Polyhedra_Library::Implementation::swapping_sort(), and Parma_Polyhedra_Library::Implementation::swapping_unique().

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

                         {
  typedef std::vector<Bit_Row>::iterator Iter;
  // Sorting without removing duplicates.
  Iter first = rows.begin();
  Iter last = rows.end();
  Implementation::swapping_sort(first, last, Bit_Row_Less_Than());
  // Moving all the duplicate elements at the end of the vector.
  Iter new_last = Implementation::swapping_unique(first, last);
  // Removing duplicates.
  rows.erase(new_last, last);
  PPL_ASSERT(OK());
}

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

Returns:
true if row belongs to *this, false otherwise.
Parameters:
rowThe 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 129 of file Bit_Matrix.inlines.hh.

References check_sorted(), PPL_ASSERT, and rows.

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

                                                    {
  PPL_ASSERT(check_sorted());
  return std::binary_search(rows.begin(), rows.end(), row,
                            Bit_Row_Less_Than());
}

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

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

References external_memory_in_bytes().

                                        {
  return sizeof(*this) + external_memory_in_bytes();
}

Transposes the matrix.

Definition at line 82 of file Bit_Matrix.cc.

References PPL_ASSERT.

                         {
  const Bit_Matrix& x = *this;
  const dimension_type nrows = num_rows();
  const dimension_type ncols = num_columns();
  Bit_Matrix tmp(ncols, nrows);
  for (dimension_type i = nrows; i-- > 0; )
    for (unsigned long j = x[i].last();
         j != C_Integer<unsigned long>::max; j = x[i].prev(j))
      tmp[j].set(i);
  m_swap(tmp);
  PPL_ASSERT(OK());
}

Friends And Related Function Documentation

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

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

                                                     {
  return !(x == y);
}
bool operator!= ( const Bit_Matrix x,
const Bit_Matrix y 
)
related

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

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

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

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

Definition at line 238 of file Bit_Matrix.cc.

References num_columns(), and num_rows().

                                                        {
  const dimension_type x_num_rows = x.num_rows();
  if (x_num_rows != y.num_rows()
      || x.num_columns() != y.num_columns())
    return false;
  for (dimension_type i = x_num_rows; i-- > 0; )
    if (x[i] != y[i])
      return false;
  return true;
}
void swap ( Bit_Matrix x,
Bit_Matrix y 
)
related

Swaps x with y.

Referenced by clear(), and m_swap().

void swap ( Bit_Matrix x,
Bit_Matrix y 
)
related

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

References m_swap().

                                   {
  x.m_swap(y);
}

Member Data Documentation

Size of the initialized part of each row.

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

Referenced by clear(), m_swap(), num_columns(), operator=(), and remove_trailing_columns().


The documentation for this class was generated from the following files: