Austin Eng | d761d5a | 2020-07-08 20:27:30 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Dawn Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef COMMON_ITYP_STACK_VEC_H_ |
| 16 | #define COMMON_ITYP_STACK_VEC_H_ |
| 17 | |
| 18 | #include "common/Assert.h" |
| 19 | #include "common/StackContainer.h" |
| 20 | #include "common/UnderlyingType.h" |
| 21 | |
| 22 | namespace ityp { |
| 23 | |
Austin Eng | e700a6a | 2020-07-14 01:32:43 +0000 | [diff] [blame] | 24 | template <typename Index, typename Value, size_t StaticCapacity> |
Austin Eng | d761d5a | 2020-07-08 20:27:30 +0000 | [diff] [blame] | 25 | class stack_vec : private StackVector<Value, StaticCapacity> { |
| 26 | using I = UnderlyingType<Index>; |
| 27 | using Base = StackVector<Value, StaticCapacity>; |
Austin Eng | e700a6a | 2020-07-14 01:32:43 +0000 | [diff] [blame] | 28 | using VectorBase = std::vector<Value, StackAllocator<Value, StaticCapacity>>; |
Austin Eng | d761d5a | 2020-07-08 20:27:30 +0000 | [diff] [blame] | 29 | static_assert(StaticCapacity <= std::numeric_limits<I>::max(), ""); |
| 30 | |
| 31 | public: |
| 32 | stack_vec() : Base() { |
| 33 | } |
| 34 | stack_vec(Index size) : Base() { |
| 35 | this->container().resize(static_cast<I>(size)); |
| 36 | } |
| 37 | |
| 38 | Value& operator[](Index i) { |
| 39 | ASSERT(i < size()); |
| 40 | return Base::operator[](static_cast<I>(i)); |
| 41 | } |
| 42 | |
| 43 | constexpr const Value& operator[](Index i) const { |
| 44 | ASSERT(i < size()); |
| 45 | return Base::operator[](static_cast<I>(i)); |
| 46 | } |
| 47 | |
| 48 | void resize(Index size) { |
| 49 | this->container().resize(static_cast<I>(size)); |
| 50 | } |
| 51 | |
| 52 | void reserve(Index size) { |
| 53 | this->container().reserve(static_cast<I>(size)); |
| 54 | } |
| 55 | |
| 56 | Value* data() { |
| 57 | return this->container().data(); |
| 58 | } |
| 59 | |
| 60 | const Value* data() const { |
| 61 | return this->container().data(); |
| 62 | } |
| 63 | |
Austin Eng | e700a6a | 2020-07-14 01:32:43 +0000 | [diff] [blame] | 64 | typename VectorBase::iterator begin() noexcept { |
Austin Eng | d761d5a | 2020-07-08 20:27:30 +0000 | [diff] [blame] | 65 | return this->container().begin(); |
| 66 | } |
| 67 | |
Austin Eng | e700a6a | 2020-07-14 01:32:43 +0000 | [diff] [blame] | 68 | typename VectorBase::const_iterator begin() const noexcept { |
Austin Eng | d761d5a | 2020-07-08 20:27:30 +0000 | [diff] [blame] | 69 | return this->container().begin(); |
| 70 | } |
| 71 | |
Austin Eng | e700a6a | 2020-07-14 01:32:43 +0000 | [diff] [blame] | 72 | typename VectorBase::iterator end() noexcept { |
Austin Eng | d761d5a | 2020-07-08 20:27:30 +0000 | [diff] [blame] | 73 | return this->container().end(); |
| 74 | } |
| 75 | |
Austin Eng | e700a6a | 2020-07-14 01:32:43 +0000 | [diff] [blame] | 76 | typename VectorBase::const_iterator end() const noexcept { |
Austin Eng | d761d5a | 2020-07-08 20:27:30 +0000 | [diff] [blame] | 77 | return this->container().end(); |
| 78 | } |
| 79 | |
Austin Eng | e700a6a | 2020-07-14 01:32:43 +0000 | [diff] [blame] | 80 | typename VectorBase::reference front() { |
Austin Eng | d761d5a | 2020-07-08 20:27:30 +0000 | [diff] [blame] | 81 | return this->container().front(); |
| 82 | } |
| 83 | |
Austin Eng | e700a6a | 2020-07-14 01:32:43 +0000 | [diff] [blame] | 84 | typename VectorBase::const_reference front() const { |
Austin Eng | d761d5a | 2020-07-08 20:27:30 +0000 | [diff] [blame] | 85 | return this->container().front(); |
| 86 | } |
| 87 | |
Austin Eng | e700a6a | 2020-07-14 01:32:43 +0000 | [diff] [blame] | 88 | typename VectorBase::reference back() { |
Austin Eng | d761d5a | 2020-07-08 20:27:30 +0000 | [diff] [blame] | 89 | return this->container().back(); |
| 90 | } |
| 91 | |
Austin Eng | e700a6a | 2020-07-14 01:32:43 +0000 | [diff] [blame] | 92 | typename VectorBase::const_reference back() const { |
Austin Eng | d761d5a | 2020-07-08 20:27:30 +0000 | [diff] [blame] | 93 | return this->container().back(); |
| 94 | } |
| 95 | |
| 96 | Index size() const { |
| 97 | return Index(static_cast<I>(this->container().size())); |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | } // namespace ityp |
| 102 | |
| 103 | #endif // COMMON_ITYP_STACK_VEC_H_ |