Skip to content

Commit

Permalink
rename iset to bitset and improve the type interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kamarya committed Jul 24, 2018
1 parent 39cf12a commit 71c3b38
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
28 changes: 13 additions & 15 deletions inc/susa/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ template <class T> matrix <unsigned int> select_most(const matrix <T> &mat_arg,
* @return The index of the elements that are equal to T_arg
* @ingroup Search
*/
template <class T> iset find(const matrix <T> &mat_arg, T &T_arg);
template <class T> susa::bitset find(const matrix <T> &mat_arg, T &T_arg);

/**
* @brief Dijkstra finds the shortest path
Expand Down Expand Up @@ -117,8 +117,7 @@ template <class T> matrix <unsigned int> select_least(const matrix <T> &mat_arg,
matrix <T> mat_ret = mat_arg;
matrix <unsigned int> mat_index(uint_num, 1);

SUSA_ASSERT_MESSAGE(uint_size > uint_num,
"The number of elements to be selected is larger than the matrix size.");
SUSA_ASSERT_MESSAGE(uint_size > uint_num, "The number of elements to be selected is larger than the matrix size.");

if (uint_size < uint_num)
{
Expand All @@ -128,7 +127,7 @@ template <class T> matrix <unsigned int> select_least(const matrix <T> &mat_arg,
unsigned int uint_min_index;
T T_min;

for( unsigned int uint_i = 0; uint_i < uint_num; uint_i++)
for (unsigned int uint_i = 0; uint_i < uint_num; uint_i++)
{
uint_min_index = uint_i;
T_min = mat_ret(uint_i);
Expand Down Expand Up @@ -161,8 +160,7 @@ template <class T> matrix <unsigned int> select_limited_least(const matrix <T> &
matrix <T> mat_ret = mat_arg;
matrix <unsigned int> mat_index(uint_num, 1);

SUSA_ASSERT_MESSAGE(uint_size > uint_num,
"The number of elements to be selected is larger than the matrix size.");
SUSA_ASSERT_MESSAGE(uint_size > uint_num, "The number of elements to be selected is larger than the matrix size.");
if (uint_size < uint_num)
{
return mat_index;
Expand Down Expand Up @@ -235,20 +233,20 @@ template <class T> matrix <unsigned int> select_most(const matrix <T> &mat_arg,
}


template <class T> iset find(const matrix <T> &mat_arg, T &T_arg)
template <class T> susa::bitset find(const matrix <T> &mat_arg, T &T_arg)
{

//TODO support matrices
SUSA_ASSERT_MESSAGE(mat_arg.is_vector(), "this method supports one dimensional matrices (vectors) only.");

susa::iset __iset(mat_arg.size() + 1); // lets have at least one index
susa::bitset indxset(mat_arg.size() + 1); // lets have at least one index

for (unsigned int uint_index = 0; uint_index < mat_arg.size(); uint_index++)
{
if (mat_arg(uint_index) == T_arg) __iset.add(uint_index);
if (mat_arg(uint_index) == T_arg) indxset.set(uint_index);
}

return __iset;
return indxset;
}


Expand All @@ -265,27 +263,27 @@ template <class T> matrix <unsigned int> dijkstra(const matrix <T> &mat_graph, u

dist(uint_src) = 0;

iset __iset(uint_num_nodes);
__iset.add_all();
susa::bitset nset(uint_num_nodes);
nset.set();


unsigned int uint_min = 0;
unsigned int uint_min_index = 0;
unsigned int uint_alt = 0;
while (__iset.is_not_empty())
while (nset.any())
{

uint_min = std::numeric_limits <unsigned int>::max();
for (unsigned int uint_index = 0; uint_index < uint_num_nodes; uint_index++)
{
if (__iset.exists(uint_index) && dist(uint_index) < uint_min)
if (nset.exists(uint_index) && dist(uint_index) < uint_min)
{
uint_min_index = uint_index;
uint_min = dist(uint_index);
}
}

__iset.remove(uint_min_index);
nset.reset(uint_min_index);


for (unsigned int uint_i = 0; uint_i < uint_num_nodes; uint_i++)
Expand Down
34 changes: 19 additions & 15 deletions inc/susa/sets.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,43 @@ namespace susa
{

/**
* @brief The <i>iset</i> class.
* <i>iset</i> is an integer set type aimed to store
* a set of integers in a quick (using single memory allocation)
* and memory efficient manner.
* @brief The <i>bitset</i> class.
* <i>bitset</i> is a set type aimed to store
* a set of bits in a quick (using single memory allocation)
* and memory efficient manner. You may alternatively use <i>std::bitset</i>
* where the set size is in hand at compile time (no runtime initialization).
* @ingroup TYPES
*
*/
class iset :
public memory <char>
class bitset :
public memory <unsigned char>
{

public :
iset(size_t maxsize);

void add(unsigned int index);
bitset(size_t maxsize);

void remove(unsigned int index);
void set(size_t index);

unsigned int pop();
void reset(size_t index);

void push(unsigned int index);
size_t pop();

bool exists(unsigned int index);
void push(size_t index);

void add_all();
bool exists(size_t index);

void remove_all();
void set();

bool is_not_empty();
void reset();

bool any();

private:

size_t nbytes;
size_t uint_set_size;

};

} // NAMESPACE SUSA
Expand Down
24 changes: 12 additions & 12 deletions src/sets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
namespace susa
{

iset::iset(size_t maxsize)
bitset::bitset(size_t maxsize)
{
uint_set_size = maxsize;

Expand All @@ -37,10 +37,10 @@ namespace susa

this->allocate(nbytes);

remove_all();
reset();
}

void iset::add(unsigned int index)
void bitset::set(size_t index)
{

if (index >= uint_set_size) return;
Expand All @@ -53,7 +53,7 @@ namespace susa
this->_matrix[byte] |= (0x01 << bit);
}

void iset::remove(unsigned int index)
void bitset::reset(size_t index)
{
if (exists(index))
{
Expand All @@ -64,26 +64,26 @@ namespace susa
}
}

unsigned int iset::pop()
size_t bitset::pop()
{
for (unsigned int uint_index = 0; uint_index < uint_set_size; uint_index++)
{
if (exists(uint_index))
{
remove(uint_index);
reset(uint_index);
return uint_index;
}
}

return uint_set_size;
}

void iset::push(unsigned int index)
void bitset::push(size_t index)
{
add(index);
set(index);
}

bool iset::exists(unsigned int index)
bool bitset::exists(size_t index)
{
if (index >= uint_set_size) return false;
unsigned int byte = index / 8;
Expand All @@ -93,7 +93,7 @@ namespace susa
return ((this->_matrix[byte] >> bit) & 0x01);
}

void iset::add_all()
void bitset::set()
{
for (unsigned int index = 0; index < nbytes; index++)
{
Expand All @@ -103,12 +103,12 @@ namespace susa
}
}

void iset::remove_all()
void bitset::reset()
{
for (size_t indx = 0; indx < nbytes; indx++) this->_matrix[indx] = 0x00;
}

bool iset::is_not_empty()
bool bitset::any()
{
unsigned char empty = 0x00;
for (size_t indx = 0; indx < nbytes; indx++) empty |= this->_matrix[indx];
Expand Down

0 comments on commit 71c3b38

Please sign in to comment.