Skip to content

Commit

Permalink
Add accumulated index calculator with tests.
Browse files Browse the repository at this point in the history
 * 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
achimnol committed Mar 3, 2016
1 parent 584ead2 commit 1d78da1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
39 changes: 39 additions & 0 deletions include/nba/core/accumidx.hh
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
16 changes: 16 additions & 0 deletions include/nba/core/errors.hh
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
43 changes: 43 additions & 0 deletions tests/test_accumidx.cc
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

0 comments on commit 1d78da1

Please sign in to comment.