Skip to content

Commit

Permalink
When specifying the size of a container, now consider the size as an …
Browse files Browse the repository at this point in the history
…upperbound

Fix #111
  • Loading branch information
serge-sans-paille committed Dec 20, 2020
1 parent eacf89f commit 334a0ed
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
9 changes: 9 additions & 0 deletions include/frozen/bits/algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ constexpr bits::carray<T, N> quicksort(bits::carray<T, N> const &array,
return res;
}

template <typename T, std::size_t N, class Compare>
constexpr bits::cvector<T, N> quicksort(bits::cvector<T, N> const &array,
Compare const &compare) {
bits::cvector<T, N> res = array;
if(!array.empty())
quicksort(res.begin(), res.end() - 1, compare);
return res;
}

template <class T, class Compare> struct LowerBound {
T const &value_;
Compare const &compare_;
Expand Down
23 changes: 23 additions & 0 deletions include/frozen/bits/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class cvector {
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;

Expand All @@ -59,13 +61,34 @@ class cvector {
for (std::size_t i = 0; i < N; ++i)
data[i] = value;
}
template<typename Iter>
constexpr cvector(Iter begin, Iter end) : dsize(0) {
while(begin != end)
data[dsize++] = *begin++;
}
constexpr cvector(std::initializer_list<T> values) : dsize(0) {
for(auto value : values)
data[dsize++] = value;
}

// Iterators
constexpr iterator begin() noexcept { return data; }
constexpr iterator end() noexcept { return data + dsize; }
constexpr const_iterator begin() const noexcept { return data; }
constexpr const_iterator end() const noexcept { return data + dsize; }
constexpr const_iterator cbegin() const noexcept { return data; }
constexpr const_iterator cend() const noexcept { return data + dsize; }

constexpr reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
constexpr reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }

// Capacity
constexpr size_type size() const { return dsize; }
constexpr bool empty() const { return dsize == 0; }

// Element access
constexpr reference operator[](std::size_t index) { return data[index]; }
Expand Down
6 changes: 3 additions & 3 deletions include/frozen/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
namespace frozen {

template <class Key, std::size_t N, class Compare = std::less<Key>> class set {
using container_type = bits::carray<Key, N>;
using container_type = bits::cvector<Key, N>;
Compare less_than_;
container_type keys_;

Expand Down Expand Up @@ -68,7 +68,7 @@ template <class Key, std::size_t N, class Compare = std::less<Key>> class set {

constexpr set(std::initializer_list<Key> keys, Compare const & comp)
: set{container_type{keys}, comp} {
constexpr_assert(keys.size() == N, "Inconsistent initializer_list size and type size argument");
constexpr_assert(keys.size() <= N, "Inconsistent initializer_list size and type size argument");
}

constexpr set(std::initializer_list<Key> keys)
Expand Down Expand Up @@ -211,7 +211,7 @@ constexpr auto make_set(bits::ignored_arg = {}/* for consistency with the initia

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


Expand Down
1 change: 1 addition & 0 deletions include/frozen/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class basic_string {
std::size_t size_;

public:
constexpr basic_string() = default;
template <std::size_t N>
constexpr basic_string(chr_t const (&data)[N])
: data_(data), size_(N - 1) {}
Expand Down
11 changes: 11 additions & 0 deletions tests/test_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,14 @@ TEST_CASE("frozen::set of frozen::set", "[set]") {
static_assert(!ce.count(s1({0})), "");
static_assert(ce.find(s1({0})) == ce.end(), "");
}

TEST_CASE("frozen::set of frozen::set with different sizes", "[set]") {
using s1 = frozen::set<unsigned, 2>;
constexpr frozen::set<s1, 2> ce = {{3}, {11, 4}};
static_assert(*ce.begin() == s1({3}), "");
static_assert(*(ce.begin() + 1) == s1({11, 4}), "");
static_assert(ce.size() == 2, "");
static_assert(ce.count(s1({3})), "");
static_assert(!ce.count(s1({0})), "");
static_assert(ce.find(s1({0})) == ce.end(), "");
}

0 comments on commit 334a0ed

Please sign in to comment.