Skip to content

Use size_t instead of unsigned in small_mapt #4540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/util/small_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,35 +190,35 @@ class small_mapt
static_assert(S_BITS <= N_BITS, "S_BITS should be no larger than N_BITS");

static_assert(
std::numeric_limits<unsigned>::digits >= BITS,
std::numeric_limits<std::size_t>::digits >= BITS,
"BITS must fit into an unsigned");

// Internal

unsigned get_field(std::size_t field) const
std::size_t get_field(std::size_t field) const
{
PRECONDITION(field < NUM);

unsigned shift = field * BITS;
std::size_t shift = field * BITS;
return (ind & (MASK << shift)) >> shift;
}

void set_field(std::size_t field, unsigned v)
void set_field(std::size_t field, std::size_t v)
{
PRECONDITION(field < NUM);
PRECONDITION((std::size_t)(v >> 1) < NUM);

unsigned shift = field * BITS;
std::size_t shift = field * BITS;

ind &= ~((index_fieldt)MASK << shift);
ind |= v << shift;
ind |= (index_fieldt)v << shift;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is guaranteed by the preconditions of the method and the static assertions that index_fieldt is large enough to hold v << shift.

}

void shift_indices(std::size_t ii)
{
for(std::size_t idx = 0; idx < S_BITS / BITS; idx++)
{
unsigned v = get_field(idx);
std::size_t v = get_field(idx);
if(v & 1)
{
v >>= 1;
Expand All @@ -234,7 +234,7 @@ class small_mapt
public:
// Standard const iterator

typedef std::pair<const unsigned, const T &> value_type;
typedef std::pair<const std::size_t, const T &> value_type;

/// Const iterator
///
Expand Down Expand Up @@ -299,7 +299,7 @@ class small_mapt
{
while(idx < NUM)
{
unsigned v = m.get_field(idx);
std::size_t v = m.get_field(idx);
if(v & 1)
{
ii = v >> 1;
Expand Down Expand Up @@ -394,7 +394,7 @@ class small_mapt
{
PRECONDITION(idx < NUM);

unsigned v = get_field(idx);
std::size_t v = get_field(idx);
if(v & 1)
{
std::size_t ii = v >> 1;
Expand All @@ -415,7 +415,7 @@ class small_mapt
{
PRECONDITION(idx < NUM);

unsigned v = get_field(idx);
std::size_t v = get_field(idx);
if(v & 1)
{
std::size_t ii = v >> 1;
Expand All @@ -429,7 +429,7 @@ class small_mapt
{
PRECONDITION(idx < NUM);

unsigned v = get_field(idx);
std::size_t v = get_field(idx);

if(v & 1)
{
Expand Down Expand Up @@ -466,7 +466,7 @@ class small_mapt
{
PRECONDITION(idx < NUM);

unsigned v = get_field(idx);
std::size_t v = get_field(idx);
INVARIANT(v & 1, "element must be in map");

std::size_t ii = v >> 1;
Expand Down Expand Up @@ -500,7 +500,7 @@ class small_mapt
{
PRECONDITION(idx < NUM);

unsigned v = get_field(idx);
std::size_t v = get_field(idx);
INVARIANT(!(v & 1), "element must not be in map");

std::size_t n = size();
Expand Down