Added size() method
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <bit>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
@@ -51,6 +52,11 @@ namespace lms::core
|
|||||||
assign(begin, end);
|
assign(begin, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr std::size_t size() const
|
||||||
|
{
|
||||||
|
return std::popcount(_bitfield);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename It>
|
template<typename It>
|
||||||
constexpr void assign(It begin, It end)
|
constexpr void assign(It begin, It end)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -31,9 +31,19 @@ namespace lms::core
|
|||||||
Two,
|
Two,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr EnumSet<Foo> test;
|
||||||
|
|
||||||
|
static_assert(test.size() == 0);
|
||||||
|
static_assert(test.empty());
|
||||||
|
static_assert(!test.contains(Foo::One));
|
||||||
|
static_assert(!test.contains(Foo::Two));
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
constexpr EnumSet<Foo> test{ Foo::One };
|
constexpr EnumSet<Foo> test{ Foo::One };
|
||||||
|
|
||||||
|
static_assert(test.size() == 1);
|
||||||
static_assert(!test.empty());
|
static_assert(!test.empty());
|
||||||
static_assert(test.contains(Foo::One));
|
static_assert(test.contains(Foo::One));
|
||||||
static_assert(!test.contains(Foo::Two));
|
static_assert(!test.contains(Foo::Two));
|
||||||
@@ -49,6 +59,8 @@ namespace lms::core
|
|||||||
constexpr EnumSet<Foo> test{ Foo::One, Foo::Two };
|
constexpr EnumSet<Foo> test{ Foo::One, Foo::Two };
|
||||||
constexpr auto bitfield{ test.getBitfield() };
|
constexpr auto bitfield{ test.getBitfield() };
|
||||||
|
|
||||||
|
static_assert(test.size() == 2);
|
||||||
|
|
||||||
EnumSet<Foo> test2;
|
EnumSet<Foo> test2;
|
||||||
EXPECT_FALSE(test2.contains(Foo::One));
|
EXPECT_FALSE(test2.contains(Foo::One));
|
||||||
EXPECT_FALSE(test2.contains(Foo::Two));
|
EXPECT_FALSE(test2.contains(Foo::Two));
|
||||||
|
|||||||
Reference in New Issue
Block a user