-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add accumulated index calculator with tests.
* This will replace batch_ids/item_ids arrays for offloading, greatly saving the memory footprint. * Adds a standardized return (error) codes for NBA internal APIs.
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef __NBA_CORE_ACCUMIDX_HH__ | ||
#define __NBA_CORE_ACCUMIDX_HH__ | ||
|
||
#include <nba/core/errors.hh> | ||
#include <type_traits> | ||
|
||
namespace nba { | ||
|
||
template<typename T> | ||
static inline nba::error_t get_accum_idx(const T *group_counts, | ||
const T num_groups, | ||
const T global_idx, | ||
T &group_idx, | ||
T &item_idx) | ||
{ | ||
static_assert(std::is_integral<T>::value, "Integer type required."); | ||
T sum = 0; | ||
T i; | ||
bool found = false; | ||
for (i = 0; i < num_groups; i++) { | ||
if (global_idx >= sum && global_idx < sum + group_counts[i]) { | ||
item_idx = global_idx - sum; | ||
found = true; | ||
break; | ||
} | ||
sum += group_counts[i]; | ||
} | ||
if (found) | ||
group_idx = i; | ||
else | ||
return NBA_NOT_FOUND; | ||
return NBA_SUCCESS; | ||
} | ||
|
||
} // endns(nba) | ||
|
||
#endif | ||
|
||
// vim: ts=8 sts=4 sw=4 et |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef __NBA_CORE_ERRORS_HH__ | ||
#define __NBA_CORE_ERRORS_HH__ | ||
|
||
namespace nba { | ||
|
||
typedef enum : int { | ||
NBA_SUCCESS = 0, | ||
NBA_FAILURE = 1, | ||
NBA_NOT_FOUND = 2, | ||
} error_t; | ||
|
||
} // endns(nba) | ||
|
||
#endif | ||
|
||
// vim: ts=8 sts=4 sw=4 et |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <nba/core/errors.hh> | ||
#include <nba/core/accumidx.hh> | ||
#include <gtest/gtest.h> | ||
|
||
using namespace std; | ||
using namespace nba; | ||
|
||
TEST(AccumIdxTest, Count) { | ||
const unsigned num_groups = 4; | ||
const unsigned groups[num_groups] = { 35, 1, 0, 21 }; | ||
unsigned group_idx = 0, item_idx = 0; | ||
EXPECT_EQ(NBA_SUCCESS, get_accum_idx(groups, num_groups, 0u, group_idx, item_idx)); | ||
EXPECT_EQ(0, group_idx); | ||
EXPECT_EQ(0, item_idx); | ||
EXPECT_EQ(NBA_SUCCESS, get_accum_idx(groups, num_groups, 1u, group_idx, item_idx)); | ||
EXPECT_EQ(0, group_idx); | ||
EXPECT_EQ(1, item_idx); | ||
EXPECT_EQ(NBA_SUCCESS, get_accum_idx(groups, num_groups, 17u, group_idx, item_idx)); | ||
EXPECT_EQ(0, group_idx); | ||
EXPECT_EQ(17, item_idx); | ||
EXPECT_EQ(NBA_SUCCESS, get_accum_idx(groups, num_groups, 34u, group_idx, item_idx)); | ||
EXPECT_EQ(0, group_idx); | ||
EXPECT_EQ(34, item_idx); | ||
EXPECT_EQ(NBA_SUCCESS, get_accum_idx(groups, num_groups, 35u, group_idx, item_idx)); | ||
EXPECT_EQ(1, group_idx); | ||
EXPECT_EQ(0, item_idx); | ||
EXPECT_EQ(NBA_SUCCESS, get_accum_idx(groups, num_groups, 36u, group_idx, item_idx)); | ||
EXPECT_EQ(3, group_idx); | ||
EXPECT_EQ(0, item_idx); | ||
EXPECT_EQ(NBA_SUCCESS, get_accum_idx(groups, num_groups, 37u, group_idx, item_idx)); | ||
EXPECT_EQ(3, group_idx); | ||
EXPECT_EQ(1, item_idx); | ||
EXPECT_EQ(NBA_SUCCESS, get_accum_idx(groups, num_groups, 56u, group_idx, item_idx)); | ||
EXPECT_EQ(3, group_idx); | ||
EXPECT_EQ(20, item_idx); | ||
group_idx = 7; | ||
item_idx = 7; | ||
EXPECT_EQ(NBA_NOT_FOUND, get_accum_idx(groups, num_groups, 57u, group_idx, item_idx)); | ||
EXPECT_EQ(7, group_idx); | ||
EXPECT_EQ(7, item_idx); | ||
} | ||
|
||
// vim: ts=8 sts=4 sw=4 et |