xref: /llvm-project/libcxx/test/support/test_constexpr_container.h (revision fb855eb941b6d740cc6560297d0b4d3201dcaf9f)
106e2b737SArthur O'Dwyer //===----------------------------------------------------------------------===//
206e2b737SArthur O'Dwyer //
306e2b737SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406e2b737SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
506e2b737SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606e2b737SArthur O'Dwyer //
706e2b737SArthur O'Dwyer //===----------------------------------------------------------------------===//
8480cd780SLouis Dionne 
906e2b737SArthur O'Dwyer #ifndef SUPPORT_TEST_CONSTEXPR_CONTAINER_H
1006e2b737SArthur O'Dwyer #define SUPPORT_TEST_CONSTEXPR_CONTAINER_H
1106e2b737SArthur O'Dwyer 
1206e2b737SArthur O'Dwyer // A dummy container with enough constexpr support to test the standard
1306e2b737SArthur O'Dwyer // insert iterators, such as `back_insert_iterator`.
1406e2b737SArthur O'Dwyer 
1506e2b737SArthur O'Dwyer #include <algorithm>
1606e2b737SArthur O'Dwyer #include <cassert>
17*fb855eb9SMark de Wever #include <cstddef>
1806e2b737SArthur O'Dwyer #include <utility>
1906e2b737SArthur O'Dwyer 
2006e2b737SArthur O'Dwyer #include "test_macros.h"
2106e2b737SArthur O'Dwyer 
2206e2b737SArthur O'Dwyer #if TEST_STD_VER >= 14
2306e2b737SArthur O'Dwyer 
2406e2b737SArthur O'Dwyer template<class T, int N>
2506e2b737SArthur O'Dwyer class ConstexprFixedCapacityDeque {
2606e2b737SArthur O'Dwyer     T data_[N];
2706e2b737SArthur O'Dwyer     int size_ = 0;
2806e2b737SArthur O'Dwyer public:
2906e2b737SArthur O'Dwyer     using value_type = T;
3006e2b737SArthur O'Dwyer     using iterator = T *;
3106e2b737SArthur O'Dwyer     using const_iterator = T const *;
3206e2b737SArthur O'Dwyer 
3306e2b737SArthur O'Dwyer     constexpr ConstexprFixedCapacityDeque() = default;
begin()3406e2b737SArthur O'Dwyer     constexpr iterator begin() { return data_; }
end()3506e2b737SArthur O'Dwyer     constexpr iterator end() { return data_ + size_; }
begin()3606e2b737SArthur O'Dwyer     constexpr const_iterator begin() const { return data_; }
end()3706e2b737SArthur O'Dwyer     constexpr const_iterator end() const { return data_ + size_; }
size()38*fb855eb9SMark de Wever     constexpr std::size_t size() const { return size_; }
front()3906e2b737SArthur O'Dwyer     constexpr const T& front() const { assert(size_ >= 1); return data_[0]; }
back()4006e2b737SArthur O'Dwyer     constexpr const T& back() const { assert(size_ >= 1); return data_[size_-1]; }
4106e2b737SArthur O'Dwyer 
insert(const_iterator pos,T t)4206e2b737SArthur O'Dwyer     constexpr iterator insert(const_iterator pos, T t) {
43cb71d77cSCasey Carter         int i = static_cast<int>(pos - data_);
4406e2b737SArthur O'Dwyer         if (i != size_) {
4506e2b737SArthur O'Dwyer             std::move_backward(data_ + i, data_ + size_, data_ + size_ + 1);
4606e2b737SArthur O'Dwyer         }
4706e2b737SArthur O'Dwyer         data_[i] = std::move(t);
4806e2b737SArthur O'Dwyer         size_ += 1;
4906e2b737SArthur O'Dwyer         return data_ + i;
5006e2b737SArthur O'Dwyer     }
5106e2b737SArthur O'Dwyer 
push_back(T t)5206e2b737SArthur O'Dwyer     constexpr void push_back(T t) { insert(end(), std::move(t)); }
push_front(T t)5306e2b737SArthur O'Dwyer     constexpr void push_front(T t) { insert(begin(), std::move(t)); }
5406e2b737SArthur O'Dwyer };
5506e2b737SArthur O'Dwyer 
5606e2b737SArthur O'Dwyer #endif // TEST_STD_VER >= 14
5706e2b737SArthur O'Dwyer 
5806e2b737SArthur O'Dwyer #endif // SUPPORT_TEST_CONSTEXPR_CONTAINER_H
59