Skip to content

Commit 71c3b38

Browse files
committed
rename iset to bitset and improve the type interfaces
1 parent 39cf12a commit 71c3b38

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

inc/susa/search.h

+13-15
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ template <class T> matrix <unsigned int> select_most(const matrix <T> &mat_arg,
7878
* @return The index of the elements that are equal to T_arg
7979
* @ingroup Search
8080
*/
81-
template <class T> iset find(const matrix <T> &mat_arg, T &T_arg);
81+
template <class T> susa::bitset find(const matrix <T> &mat_arg, T &T_arg);
8282

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

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

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

131-
for( unsigned int uint_i = 0; uint_i < uint_num; uint_i++)
130+
for (unsigned int uint_i = 0; uint_i < uint_num; uint_i++)
132131
{
133132
uint_min_index = uint_i;
134133
T_min = mat_ret(uint_i);
@@ -161,8 +160,7 @@ template <class T> matrix <unsigned int> select_limited_least(const matrix <T> &
161160
matrix <T> mat_ret = mat_arg;
162161
matrix <unsigned int> mat_index(uint_num, 1);
163162

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

237235

238-
template <class T> iset find(const matrix <T> &mat_arg, T &T_arg)
236+
template <class T> susa::bitset find(const matrix <T> &mat_arg, T &T_arg)
239237
{
240238

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

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

246244
for (unsigned int uint_index = 0; uint_index < mat_arg.size(); uint_index++)
247245
{
248-
if (mat_arg(uint_index) == T_arg) __iset.add(uint_index);
246+
if (mat_arg(uint_index) == T_arg) indxset.set(uint_index);
249247
}
250248

251-
return __iset;
249+
return indxset;
252250
}
253251

254252

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

266264
dist(uint_src) = 0;
267265

268-
iset __iset(uint_num_nodes);
269-
__iset.add_all();
266+
susa::bitset nset(uint_num_nodes);
267+
nset.set();
270268

271269

272270
unsigned int uint_min = 0;
273271
unsigned int uint_min_index = 0;
274272
unsigned int uint_alt = 0;
275-
while (__iset.is_not_empty())
273+
while (nset.any())
276274
{
277275

278276
uint_min = std::numeric_limits <unsigned int>::max();
279277
for (unsigned int uint_index = 0; uint_index < uint_num_nodes; uint_index++)
280278
{
281-
if (__iset.exists(uint_index) && dist(uint_index) < uint_min)
279+
if (nset.exists(uint_index) && dist(uint_index) < uint_min)
282280
{
283281
uint_min_index = uint_index;
284282
uint_min = dist(uint_index);
285283
}
286284
}
287285

288-
__iset.remove(uint_min_index);
286+
nset.reset(uint_min_index);
289287

290288

291289
for (unsigned int uint_i = 0; uint_i < uint_num_nodes; uint_i++)

inc/susa/sets.h

+19-15
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,43 @@ namespace susa
3333
{
3434

3535
/**
36-
* @brief The <i>iset</i> class.
37-
* <i>iset</i> is an integer set type aimed to store
38-
* a set of integers in a quick (using single memory allocation)
39-
* and memory efficient manner.
36+
* @brief The <i>bitset</i> class.
37+
* <i>bitset</i> is a set type aimed to store
38+
* a set of bits in a quick (using single memory allocation)
39+
* and memory efficient manner. You may alternatively use <i>std::bitset</i>
40+
* where the set size is in hand at compile time (no runtime initialization).
4041
* @ingroup TYPES
4142
*
4243
*/
43-
class iset :
44-
public memory <char>
44+
class bitset :
45+
public memory <unsigned char>
4546
{
4647

4748
public :
48-
iset(size_t maxsize);
4949

50-
void add(unsigned int index);
50+
bitset(size_t maxsize);
5151

52-
void remove(unsigned int index);
52+
void set(size_t index);
5353

54-
unsigned int pop();
54+
void reset(size_t index);
5555

56-
void push(unsigned int index);
56+
size_t pop();
5757

58-
bool exists(unsigned int index);
58+
void push(size_t index);
5959

60-
void add_all();
60+
bool exists(size_t index);
6161

62-
void remove_all();
62+
void set();
6363

64-
bool is_not_empty();
64+
void reset();
65+
66+
bool any();
6567

6668
private:
69+
6770
size_t nbytes;
6871
size_t uint_set_size;
72+
6973
};
7074

7175
} // NAMESPACE SUSA

src/sets.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
namespace susa
2929
{
3030

31-
iset::iset(size_t maxsize)
31+
bitset::bitset(size_t maxsize)
3232
{
3333
uint_set_size = maxsize;
3434

@@ -37,10 +37,10 @@ namespace susa
3737

3838
this->allocate(nbytes);
3939

40-
remove_all();
40+
reset();
4141
}
4242

43-
void iset::add(unsigned int index)
43+
void bitset::set(size_t index)
4444
{
4545

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

56-
void iset::remove(unsigned int index)
56+
void bitset::reset(size_t index)
5757
{
5858
if (exists(index))
5959
{
@@ -64,26 +64,26 @@ namespace susa
6464
}
6565
}
6666

67-
unsigned int iset::pop()
67+
size_t bitset::pop()
6868
{
6969
for (unsigned int uint_index = 0; uint_index < uint_set_size; uint_index++)
7070
{
7171
if (exists(uint_index))
7272
{
73-
remove(uint_index);
73+
reset(uint_index);
7474
return uint_index;
7575
}
7676
}
7777

7878
return uint_set_size;
7979
}
8080

81-
void iset::push(unsigned int index)
81+
void bitset::push(size_t index)
8282
{
83-
add(index);
83+
set(index);
8484
}
8585

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

96-
void iset::add_all()
96+
void bitset::set()
9797
{
9898
for (unsigned int index = 0; index < nbytes; index++)
9999
{
@@ -103,12 +103,12 @@ namespace susa
103103
}
104104
}
105105

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

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

0 commit comments

Comments
 (0)