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 24 template <typename Index, typename Value, size_t StaticCapacity> 25 class stack_vec : private StackVector<Value, StaticCapacity> { 26 using I = UnderlyingType<Index>; 27 using Base = StackVector<Value, StaticCapacity>; 28 using VectorBase = std::vector<Value, StackAllocator<Value, StaticCapacity>>; 29 static_assert(StaticCapacity <= std::numeric_limits<I>::max(), ""); 30 31 public: stack_vec()32 stack_vec() : Base() { 33 } stack_vec(Index size)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 resize(Index size)48 void resize(Index size) { 49 this->container().resize(static_cast<I>(size)); 50 } 51 reserve(Index size)52 void reserve(Index size) { 53 this->container().reserve(static_cast<I>(size)); 54 } 55 data()56 Value* data() { 57 return this->container().data(); 58 } 59 data()60 const Value* data() const { 61 return this->container().data(); 62 } 63 begin()64 typename VectorBase::iterator begin() noexcept { 65 return this->container().begin(); 66 } 67 begin()68 typename VectorBase::const_iterator begin() const noexcept { 69 return this->container().begin(); 70 } 71 end()72 typename VectorBase::iterator end() noexcept { 73 return this->container().end(); 74 } 75 end()76 typename VectorBase::const_iterator end() const noexcept { 77 return this->container().end(); 78 } 79 front()80 typename VectorBase::reference front() { 81 return this->container().front(); 82 } 83 front()84 typename VectorBase::const_reference front() const { 85 return this->container().front(); 86 } 87 back()88 typename VectorBase::reference back() { 89 return this->container().back(); 90 } 91 back()92 typename VectorBase::const_reference back() const { 93 return this->container().back(); 94 } 95 size()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_ 104