Skip to content

Commit

Permalink
Merge pull request #53 from serge-sans-paille/fix/various-warnings
Browse files Browse the repository at this point in the history
Fix/various warnings
  • Loading branch information
serge-sans-paille authored Aug 20, 2018
2 parents 325449a + b178735 commit e3c9933
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 28 deletions.
36 changes: 24 additions & 12 deletions benchmarks/bench_int_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ static auto const* volatile Some = &Keywords;

static void BM_IntInFzSet(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *Some)
volatile bool status = Keywords.count(kw);
for(auto kw : *Some) {
volatile bool status = Keywords.count(kw);
(void) status;
}
}
}
BENCHMARK(BM_IntInFzSet);
Expand All @@ -27,8 +29,10 @@ static const std::set<int> Keywords_(Keywords.begin(), Keywords.end());

static void BM_IntInStdSet(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *Some)
volatile bool status = Keywords_.count(kw);
for(auto kw : *Some) {
volatile bool status = Keywords_.count(kw);
(void)status;
}
}
}

Expand All @@ -42,8 +46,10 @@ static const std::array<int, 32> Keywords__{{
}};
static void BM_IntInStdArray(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *Some)
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end();
for(auto kw : *Some) {
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end();
(void)status;
}
}
}

Expand All @@ -59,24 +65,30 @@ static auto const * volatile SomeIntsPtr = &SomeInts;

static void BM_IntNotInFzSet(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *SomeIntsPtr)
volatile bool status = Keywords.count(kw);
for(auto kw : *SomeIntsPtr) {
volatile bool status = Keywords.count(kw);
(void)status;
}
}
}
BENCHMARK(BM_IntNotInFzSet);

static void BM_IntNotInStdSet(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *SomeIntsPtr)
volatile bool status = Keywords_.count(kw);
for(auto kw : *SomeIntsPtr) {
volatile bool status = Keywords_.count(kw);
(void)status;
}
}
}
BENCHMARK(BM_IntNotInStdSet);

static void BM_IntNotInStdArray(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *SomeIntsPtr)
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end();
for(auto kw : *SomeIntsPtr) {
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end();
(void)status;
}
}
}
BENCHMARK(BM_IntNotInStdArray);
2 changes: 1 addition & 1 deletion benchmarks/bench_main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include <benchmark/benchmark.h>
BENCHMARK_MAIN();
BENCHMARK_MAIN()

36 changes: 24 additions & 12 deletions benchmarks/bench_str_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ static auto const* volatile Some = &Keywords;

static void BM_StrInFzSet(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *Some)
volatile bool status = Keywords.count(kw);
for(auto kw : *Some) {
volatile bool status = Keywords.count(kw);
(void)status;
}
}
}
BENCHMARK(BM_StrInFzSet);
Expand All @@ -30,8 +32,10 @@ static const std::set<frozen::string> Keywords_(Keywords.begin(), Keywords.end()

static void BM_StrInStdSet(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *Some)
volatile bool status = Keywords_.count(kw);
for(auto kw : *Some) {
volatile bool status = Keywords_.count(kw);
(void)status;
}
}
}

Expand All @@ -47,8 +51,10 @@ static const std::array<frozen::string, 32> Keywords__{

static void BM_StrInStdArray(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *Some)
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end();
for(auto kw : *Some) {
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end();
(void)status;
}
}
}

Expand All @@ -66,24 +72,30 @@ static auto const * volatile SomeStringsPtr = &SomeStrings;

static void BM_StrNotInFzSet(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *SomeStringsPtr)
volatile bool status = Keywords.count(kw);
for(auto kw : *SomeStringsPtr) {
volatile bool status = Keywords.count(kw);
(void)status;
}
}
}
BENCHMARK(BM_StrNotInFzSet);

static void BM_StrNotInStdSet(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *SomeStringsPtr)
volatile bool status = Keywords_.count(kw);
for(auto kw : *SomeStringsPtr) {
volatile bool status = Keywords_.count(kw);
(void)status;
}
}
}
BENCHMARK(BM_StrNotInStdSet);

static void BM_StrNotInStdArray(benchmark::State& state) {
for (auto _ : state) {
for(auto kw : *SomeStringsPtr)
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end();
for(auto kw : *SomeStringsPtr) {
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end();
(void)status;
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions include/frozen/bits/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ namespace frozen {

namespace bits {

// used as a fake argument for frozen::make_set and frozen::make_map in the case of N=0
struct ignored_arg {};

template <class T, std::size_t N>
class cvector {
T data [N] = {}; // zero-initialization for scalar type T, default-initialized otherwise
Expand Down Expand Up @@ -168,6 +171,27 @@ class carray {
data_[i] = val;
}
};
template <class T>
class carray<T, 0> {

public:
// Container typdefs
using value_type = T;
using reference = value_type &;
using const_reference = const value_type &;
using pointer = value_type *;
using const_pointer = const value_type *;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;

// Constructors
constexpr carray(void) = default;

};

} // namespace bits

Expand Down
9 changes: 7 additions & 2 deletions include/frozen/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ template <class Comparator> class CompareKey {
return comparator_(self_key, other_key);
}
};

} // namespace impl

template <class Key, class Value, std::size_t N, class Compare = std::less<Key>>
Expand Down Expand Up @@ -177,8 +178,7 @@ class map {

template <class Key, class Value, class Compare>
class map<Key, Value, 0, Compare> {
using container_type =
bits::carray<std::pair<Key, Value>, 1>; // just for the type definitions
using container_type = bits::carray<std::pair<Key, Value>, 0>;
impl::CompareKey<Compare> compare_;

public:
Expand Down Expand Up @@ -246,6 +246,11 @@ class map<Key, Value, 0, Compare> {
constexpr key_compare value_comp() const { return compare_; }
};

template <typename T, typename U>
constexpr auto make_map(bits::ignored_arg = {}/* for consistency with the initializer below for N = 0*/) {
return map<T, U, 0>{};
}

template <typename T, typename U, std::size_t N>
constexpr auto make_map(std::pair<T, U> const (&items)[N]) {
return map<T, U, N>{items};
Expand Down
8 changes: 7 additions & 1 deletion include/frozen/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ template <class Key, std::size_t N, class Compare = std::less<Key>> class set {
};

template <class Key, class Compare> class set<Key, 0, Compare> {
using container_type = bits::carray<Key, 1>; // just for the type definitions
using container_type = bits::carray<Key, 0>; // just for the type definitions
Compare const compare_;

public:
Expand Down Expand Up @@ -194,11 +194,17 @@ template <class Key, class Compare> class set<Key, 0, Compare> {
constexpr const_reverse_iterator crend() const { return nullptr; }
};

template <typename T>
constexpr auto make_set(bits::ignored_arg = {}/* for consistency with the initializer below for N = 0*/) {
return set<T, 0>{};
}

template <typename T, std::size_t N>
constexpr auto make_set(const T (&args)[N]) {
return set<T, N>(args);
}


} // namespace frozen

#endif
19 changes: 19 additions & 0 deletions tests/test_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,27 @@ TEST_CASE("frozen::map <> frozen::make_map", "[map]") {
for (auto v : frozen_map)
REQUIRE(frozen_map2.count(std::get<0>(v)));
}

constexpr frozen::map<int, short, 0> frozen_empty_map = {};
constexpr auto frozen_empty_map2 = frozen::make_map<int, short>();
constexpr auto frozen_empty_map3 = frozen::make_map<int, short>({});

SECTION("checking empty map") {
REQUIRE(frozen_empty_map.empty());
REQUIRE(frozen_empty_map.size() == 0);
REQUIRE(frozen_empty_map.begin() == frozen_empty_map.end());

REQUIRE(frozen_empty_map2.empty());
REQUIRE(frozen_empty_map2.size() == 0);
REQUIRE(frozen_empty_map2.begin() == frozen_empty_map2.end());

REQUIRE(frozen_empty_map3.empty());
REQUIRE(frozen_empty_map3.size() == 0);
REQUIRE(frozen_empty_map3.begin() == frozen_empty_map3.end());
}
}


TEST_CASE("frozen::map constexpr", "[map]") {
constexpr frozen::map<unsigned, unsigned, 2> ce = {{3,4}, {11,12}};
static_assert(*ce.begin() == std::pair<unsigned, unsigned>{3,4}, "");
Expand Down
18 changes: 18 additions & 0 deletions tests/test_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,24 @@ TEST_CASE("frozen::set <> frozen::make_set", "[set]") {
for (auto v : frozen_set)
REQUIRE(frozen_set2.count(v));
}

constexpr frozen::set<short, 0> frozen_empty_set = {};
constexpr auto frozen_empty_set2 = frozen::make_set<short>();
constexpr auto frozen_empty_set3 = frozen::make_set<short>({});

SECTION("checking empty set") {
REQUIRE(frozen_empty_set.empty());
REQUIRE(frozen_empty_set.size() == 0);
REQUIRE(frozen_empty_set.begin() == frozen_empty_set.end());

REQUIRE(frozen_empty_set2.empty());
REQUIRE(frozen_empty_set2.size() == 0);
REQUIRE(frozen_empty_set2.begin() == frozen_empty_set2.end());

REQUIRE(frozen_empty_set3.empty());
REQUIRE(frozen_empty_set3.size() == 0);
REQUIRE(frozen_empty_set3.begin() == frozen_empty_set3.end());
}
}

TEST_CASE("frozen::set constexpr", "[set]") {
Expand Down

0 comments on commit e3c9933

Please sign in to comment.