// ESAF : Euso Simulation and Analysis Framework
// $Id: BoolAlgebra.cc,v 1.9 2005/05/02 10:11:10 pesce Exp $
// M. Pallavicini created Mar, 3 2004
#include "euso.hh"
#include <stdexcept>
#include <iostream>
#include "BoolAlgebra.hh"
#include "TMath.h"
ClassImp(BoolVector)
ClassImp(BoolMatrix)
//______________________________________________________________________________
BoolVector::BoolVector() : fSize(16) {
// empty ctor
Zero();
}
//______________________________________________________________________________
BoolVector::BoolVector(Int_t size) : fSize(size) {
// ctor
if ( size <= 0 || fSize>=256 )
throw runtime_error("Meaningless BoolVector");
Zero();
}
//______________________________________________________________________________
BoolVector::~BoolVector() {
// dtor
}
//______________________________________________________________________________
Bool_t BoolVector::OR() const {
// return OR of all elements
Bool_t res = fVector[0];
for(Int_t i=1; i<fSize; i++) {
res = (res || fVector[i]);
}
return res;
}
//______________________________________________________________________________
Bool_t BoolVector::AND() const {
// return OR of all elements
Bool_t res = fVector[0];
for(Int_t i=1; i<fSize; i++) {
res = (res && fVector[i]);
}
return res;
}
//______________________________________________________________________________
BoolVector& BoolVector::operator = (const BoolVector& other) {
// assignement operator
fSize = other.fSize;
for(int i=0; i<fSize; i++)
fVector[i] = other.fVector[i];
return *this;
}
//______________________________________________________________________________
BoolVector& BoolVector::operator &= (const BoolVector& other) {
// bitwise AND
if ( other.fSize != fSize ) {
cout << "vector.fSize = " << fSize << " other.fSize = " << other.fSize << endl;
throw std::runtime_error("Boolean algebra error in vector-vector AND");
}
BoolVector res(fSize);
for(int i=0; i<fSize; i++) {
res.fVector[i] = (fVector[i] && other.fVector[i]);
}
*this = res;
return *this;
}
////////////////////////
/// class BoolMatrix ///
////////////////////////
//______________________________________________________________________________
BoolMatrix::BoolMatrix(Int_t size) : fSize(size) {
// ctor
if ( size <= 0 )
throw runtime_error("Meaningless BoolMatrix");
if ( size >= 256 )
throw runtime_error("BoolMatrix too large");
Zero();
}
//______________________________________________________________________________
BoolMatrix::~BoolMatrix() {
// dtor
}
//______________________________________________________________________________
void BoolMatrix::Zero() {
// set all elements to false
for(Int_t i=0; i<fSize; i++) {
for(Int_t j=0; j<fSize; j++) {
fMatrix[i][j]=0;
}
}
}
//______________________________________________________________________________
void BoolMatrix::SetNeighborsMatrix() {
// set to a neighbors matrix
//Zero();
Int_t side = (Int_t)TMath::Sqrt((Double_t)fSize);
for( Int_t i(0); i<side; i++)
for( Int_t j(0); j<side; j++) {
Int_t col=j*side+i;
for( Int_t m(0); m<side; m++)
for( Int_t n(0); n<side; n++) {
Int_t row = n*side+m;
fMatrix[row][col] = ((m <= i+1) && (m >= i-1)) && ((n <= j+1) && (n >= j-1));
}
}
}
//______________________________________________________________________________
BoolMatrix& BoolMatrix::operator = (const BoolMatrix& other) {
// matrix assignement operator
fSize = other.fSize;
for(int i=0; i<fSize; i++)
for(int j=0; j<fSize; j++)
fMatrix[i][j] = other.fMatrix[i][j];
return *this;
}
//______________________________________________________________________________
BoolVector& BoolVector::operator *= (const BoolMatrix& A) {
// left matrix multiplication of the vector
if ( A.fSize != fSize)
throw std::runtime_error("Boolean algebra error in matrix-vector multiplication");
BoolVector res(A.fSize);
res.Zero();
for(Int_t i=0; i<A.fSize; i++) {
for(Int_t j=0; j<A.fSize; j++) {
res.fVector[i] |= fVector[j]*A(i,j);
}
}
*this = res;
return *this;
}
//______________________________________________________________________________
BoolMatrix& BoolMatrix::operator *= (const BoolMatrix& other) {
// matrix multiplication (must be squared and equal and always the same size)
if ( other.fSize != fSize )
throw std::runtime_error("Boolean algebra error in matrix-matrix multiplication");
BoolMatrix res(other.fSize);
res.Zero();
Bool_t tmp = kFALSE;
for(Int_t i=0; i<other.fSize; i++) {
for(Int_t j=0; j<other.fSize; j++) {
for(Int_t k=0; k<other.fSize; k++) {
tmp += fMatrix[i][k] && other(k,j);
}
res.fMatrix[i][j] = tmp;
}
}
*this = res;
return *this;
}