Browse Source
Added BitVector, a simple boolean array
Added BitVector, a simple boolean array
Edited AtomicProposition to make use of BitVector Edited mrmc-cpp.cpp, now comes with speed tests for some tasks Edited static_sparse_matrix.h, added a copy constructor, added template conversion functions, applied template parameters to all relevant functions/variables Removed eigen_sparse_additions.h from internal filestempestpy_adaptions
PBerger
12 years ago
6 changed files with 577 additions and 172 deletions
-
41src/dtmc/atomic_proposition.h
-
97src/mrmc-cpp.cpp
-
266src/sparse/static_sparse_matrix.h
-
152src/vector/bitvector.h
-
79test/eigen/sparse_matrix_test.cpp
-
114test/vector/bitvector_test.cpp
@ -0,0 +1,152 @@ |
|||
#ifndef MRMC_VECTOR_BITVECTOR_H_ |
|||
#define MRMC_VECTOR_BITVECTOR_H_ |
|||
|
|||
#include <exception> |
|||
#include <new> |
|||
#include <cmath> |
|||
#include "boost/integer/integer_mask.hpp" |
|||
|
|||
#include <pantheios/pantheios.hpp> |
|||
#include <pantheios/inserters/integer.hpp> |
|||
|
|||
#include "src/exceptions/invalid_state.h" |
|||
#include "src/exceptions/invalid_argument.h" |
|||
#include "src/exceptions/out_of_range.h" |
|||
|
|||
namespace mrmc { |
|||
|
|||
namespace vector { |
|||
|
|||
//! A Boolean Array |
|||
/*! |
|||
A bit vector for boolean fields or quick selection schemas on Matrix entries. |
|||
Does NOT perform index bound checks! |
|||
*/ |
|||
class BitVector { |
|||
public: |
|||
//! Constructor |
|||
/*! |
|||
\param initial_length The initial size of the boolean Array. Can be changed later on via @resize() |
|||
*/ |
|||
BitVector(uint_fast64_t initial_length) { |
|||
bucket_count = initial_length / 64; |
|||
if (initial_length % 64 != 0) { |
|||
++bucket_count; |
|||
} |
|||
bucket_array = new uint_fast64_t[bucket_count](); |
|||
|
|||
// init all 0 |
|||
for (uint_fast64_t i = 0; i < bucket_count; ++i) { |
|||
bucket_array[i] = 0; |
|||
} |
|||
} |
|||
|
|||
//! Copy Constructor |
|||
/*! |
|||
Copy Constructor. Creates an exact copy of the source sparse matrix ssm. Modification of either matrix does not affect the other. |
|||
@param ssm A reference to the matrix that should be copied from |
|||
*/ |
|||
BitVector(const BitVector &bv) : bucket_count(bv.bucket_count) |
|||
{ |
|||
pantheios::log_DEBUG("BitVector::CopyCTor: Using Copy() Ctor."); |
|||
bucket_array = new uint_fast64_t[bucket_count](); |
|||
memcpy(bucket_array, bv.bucket_array, sizeof(uint_fast64_t) * bucket_count); |
|||
} |
|||
|
|||
~BitVector() { |
|||
if (bucket_array != NULL) { |
|||
delete[] bucket_array; |
|||
} |
|||
} |
|||
|
|||
void resize(uint_fast64_t new_length) { |
|||
uint_fast64_t* tempArray = new uint_fast64_t[new_length](); |
|||
|
|||
// 64 bit/entries per uint_fast64_t |
|||
uint_fast64_t copySize = (new_length <= (bucket_count * 64)) ? (new_length/64) : (bucket_count); |
|||
memcpy(tempArray, bucket_array, sizeof(uint_fast64_t) * copySize); |
|||
|
|||
bucket_count = new_length / 64; |
|||
if (new_length % 64 != 0) { |
|||
++bucket_count; |
|||
} |
|||
|
|||
delete[] bucket_array; |
|||
bucket_array = tempArray; |
|||
} |
|||
|
|||
void set(const uint_fast64_t index, const bool value) { |
|||
uint_fast64_t bucket = index / 64; |
|||
// Taking the step with mask is crucial as we NEED a 64bit shift, not a 32bit one. |
|||
// MSVC: C4334, use 1i64 or cast to __int64. |
|||
// result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?) |
|||
uint_fast64_t mask = 1; |
|||
mask = mask << (index % 64); |
|||
if (value) { |
|||
bucket_array[bucket] |= mask; |
|||
} else { |
|||
bucket_array[bucket] &= ~mask; |
|||
} |
|||
} |
|||
|
|||
bool get(const uint_fast64_t index) { |
|||
uint_fast64_t bucket = index / 64; |
|||
// Taking the step with mask is crucial as we NEED a 64bit shift, not a 32bit one. |
|||
// MSVC: C4334, use 1i64 or cast to __int64. |
|||
// result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?) |
|||
uint_fast64_t mask = 1; |
|||
mask = mask << (index % 64); |
|||
return ((bucket_array[bucket] & mask) == mask); |
|||
} |
|||
|
|||
// Operators |
|||
BitVector operator &(BitVector const &bv) { |
|||
uint_fast64_t minSize = (bv.bucket_count < this->bucket_count) ? bv.bucket_count : this->bucket_count; |
|||
BitVector result(minSize * 64); |
|||
for (uint_fast64_t i = 0; i < minSize; ++i) { |
|||
result.bucket_array[i] = this->bucket_array[i] & bv.bucket_array[i]; |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
BitVector operator |(BitVector const &bv) { |
|||
uint_fast64_t minSize = (bv.bucket_count < this->bucket_count) ? bv.bucket_count : this->bucket_count; |
|||
BitVector result(minSize * 64); |
|||
for (uint_fast64_t i = 0; i < minSize; ++i) { |
|||
result.bucket_array[i] = this->bucket_array[i] | bv.bucket_array[i]; |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
BitVector operator ^(BitVector const &bv) { |
|||
uint_fast64_t minSize = (bv.bucket_count < this->bucket_count) ? bv.bucket_count : this->bucket_count; |
|||
BitVector result(minSize * 64); |
|||
for (uint_fast64_t i = 0; i < minSize; ++i) { |
|||
result.bucket_array[i] = this->bucket_array[i] ^ bv.bucket_array[i]; |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
BitVector operator ~() { |
|||
BitVector result(this->bucket_count * 64); |
|||
for (uint_fast64_t i = 0; i < this->bucket_count; ++i) { |
|||
result.bucket_array[i] = ~this->bucket_array[i]; |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
private: |
|||
uint_fast64_t bucket_count; |
|||
|
|||
/*! Array containing the boolean bits for each node, 64bits/64nodes per element */ |
|||
uint_fast64_t* bucket_array; |
|||
|
|||
}; |
|||
|
|||
} // namespace vector |
|||
|
|||
} // namespace mrmc |
|||
|
|||
#endif // MRMC_SPARSE_STATIC_SPARSE_MATRIX_H_ |
@ -0,0 +1,79 @@ |
|||
#include "gtest/gtest.h"
|
|||
|
|||
#include "Eigen/Sparse"
|
|||
#include "src/exceptions/invalid_argument.h"
|
|||
#include "boost/integer/integer_mask.hpp"
|
|||
|
|||
TEST(EigenSparseMatrixTest, BasicReadWriteTest) { |
|||
// 25 rows, 50 non zero entries
|
|||
Eigen::SparseMatrix<int, 1> esm(25, 25); |
|||
|
|||
int values[50] = { |
|||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
|||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
|||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, |
|||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, |
|||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49 |
|||
}; |
|||
int position_row[50] = { |
|||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
|||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
|||
2, 2, 2, 2, /* first row empty, one full row � 25 minus the diagonal entry */ |
|||
4, 4, /* one empty row, then first and last column */ |
|||
13, 13, 13, 13, /* a few empty rows, middle columns */ |
|||
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
|||
24, 24, 24, 24, 24, 24, 24, 24, 24, 24 /* second to last row */ |
|||
}; |
|||
int position_col[50] = { |
|||
1, 3, 4, 5, 6, 7, 8, 9, 10, |
|||
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
|||
21, 22, 23, 24, 25, /* first row empty, one full row a 25 */ |
|||
1, 25, /* one empty row, then first and last column */ |
|||
16, 17, 18, 19, /* a few empty rows, middle columns */ |
|||
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, |
|||
14, 15, 16, 17, 18, 19, 20, 21, 22, 23 /* second to last row */ |
|||
}; |
|||
|
|||
typedef Eigen::Triplet<double> ETd; |
|||
std::vector<ETd> tripletList; |
|||
tripletList.reserve(50); |
|||
|
|||
for (int i = 0; i < 50; ++i) { |
|||
ASSERT_NO_THROW(tripletList.push_back(ETd(position_row[i] - 1, position_col[i] - 1, values[i]))); |
|||
} |
|||
|
|||
ASSERT_NO_THROW(esm.setFromTriplets(tripletList.begin(), tripletList.end())); |
|||
|
|||
for (int i = 0; i < 50; ++i) { |
|||
Eigen::SparseMatrix<int, 1>::InnerIterator it(esm, position_row[i] - 1); |
|||
ASSERT_EQ(values[i], esm.coeff(position_row[i] - 1, position_col[i] - 1)); |
|||
} |
|||
|
|||
// test for a few of the empty rows
|
|||
for (int row = 15; row < 24; ++row) { |
|||
for (int col = 1; col <= 25; ++col) { |
|||
ASSERT_EQ(0, esm.coeff(row - 1, col - 1)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
TEST(EigenSparseMatrixTest, BoundaryTest) { |
|||
Eigen::SparseMatrix<int, 1> esm(10, 10); |
|||
esm.reserve(100); |
|||
int values[100]; |
|||
for (uint_fast32_t i = 0; i < 100; ++i) { |
|||
values[i] = i; |
|||
} |
|||
|
|||
for (uint_fast32_t row = 0; row < 10; ++row) { |
|||
for (uint_fast32_t col = 0; col < 10; ++col) { |
|||
ASSERT_NO_THROW(esm.insert(row, col) = values[row * 10 + col]); |
|||
} |
|||
} |
|||
|
|||
for (uint_fast32_t row = 0; row < 10; ++row) { |
|||
for (uint_fast32_t col = 0; col < 10; ++col) { |
|||
ASSERT_EQ(values[row * 10 + col], esm.coeff(row, col)); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,114 @@ |
|||
#include "gtest/gtest.h"
|
|||
#include "src/vector/bitvector.h"
|
|||
#include "src/exceptions/invalid_argument.h"
|
|||
|
|||
TEST(BitVectorTest, GetSetTest) { |
|||
mrmc::vector::BitVector *bv = NULL; |
|||
ASSERT_NO_THROW(bv = new mrmc::vector::BitVector(32)); |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
bv->set(i, i % 2 == 0); |
|||
} |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
ASSERT_EQ(bv->get(i), i % 2 == 0); |
|||
} |
|||
delete bv; |
|||
} |
|||
|
|||
TEST(BitVectorTest, InitialZeroTest) { |
|||
mrmc::vector::BitVector bvA(32); |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
ASSERT_FALSE(bvA.get(i)); |
|||
} |
|||
} |
|||
|
|||
TEST(BitVectorTest, ResizeTest) { |
|||
mrmc::vector::BitVector bvA(32); |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
bvA.set(i, true); |
|||
} |
|||
|
|||
bvA.resize(70); |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
ASSERT_TRUE(bvA.get(i)); |
|||
} |
|||
|
|||
bool result; |
|||
for (int i = 32; i < 70; ++i) { |
|||
result = true; |
|||
ASSERT_NO_THROW(result = bvA.get(i)); |
|||
ASSERT_FALSE(result); |
|||
} |
|||
} |
|||
|
|||
TEST(BitVectorTest, OperatorNotTest) { |
|||
mrmc::vector::BitVector bvA(32); |
|||
mrmc::vector::BitVector bvB(32); |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
bvA.set(i, i % 2 == 0); |
|||
bvB.set(i, i % 2 == 1); |
|||
} |
|||
|
|||
mrmc::vector::BitVector bvN = ~bvB; |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
ASSERT_EQ(bvA.get(i), bvN.get(i)); |
|||
} |
|||
} |
|||
|
|||
TEST(BitVectorTest, OperatorAndTest) { |
|||
mrmc::vector::BitVector bvA(32); |
|||
mrmc::vector::BitVector bvB(32); |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
bvA.set(i, i % 2 == 0); |
|||
bvB.set(i, i % 2 == 1); |
|||
} |
|||
|
|||
mrmc::vector::BitVector bvN = bvA & bvB; |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
ASSERT_FALSE(bvN.get(i)); |
|||
} |
|||
} |
|||
|
|||
TEST(BitVectorTest, OperatorOrTest) { |
|||
mrmc::vector::BitVector bvA(32); |
|||
mrmc::vector::BitVector bvB(32); |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
bvA.set(i, i % 2 == 0); |
|||
bvB.set(i, i % 2 == 1); |
|||
} |
|||
|
|||
mrmc::vector::BitVector bvN = bvA | bvB; |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
ASSERT_TRUE(bvN.get(i)); |
|||
} |
|||
} |
|||
|
|||
TEST(BitVectorTest, OperatorXorTest) { |
|||
mrmc::vector::BitVector bvA(32); |
|||
mrmc::vector::BitVector bvB(32); |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
bvA.set(i, true); |
|||
bvB.set(i, i % 2 == 1); |
|||
} |
|||
|
|||
mrmc::vector::BitVector bvN = bvA ^ bvB; |
|||
mrmc::vector::BitVector bvO = ~bvB; |
|||
mrmc::vector::BitVector bvP = bvA ^ bvA; |
|||
|
|||
for (int i = 0; i < 32; ++i) { |
|||
ASSERT_EQ(bvN.get(i), bvO.get(i)); |
|||
// A XOR A = 0
|
|||
ASSERT_FALSE(bvP.get(i)); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue