blob: b88888b789722616b7dcc730e9a33204d3d939f8 [file] [log] [blame]
Austin Engd761d5a2020-07-08 20:27:30 +00001// 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
22namespace ityp {
23
Austin Enge700a6a2020-07-14 01:32:43 +000024 template <typename Index, typename Value, size_t StaticCapacity>
Austin Engd761d5a2020-07-08 20:27:30 +000025 class stack_vec : private StackVector<Value, StaticCapacity> {
26 using I = UnderlyingType<Index>;
27 using Base = StackVector<Value, StaticCapacity>;
Austin Enge700a6a2020-07-14 01:32:43 +000028 using VectorBase = std::vector<Value, StackAllocator<Value, StaticCapacity>>;
Austin Engd761d5a2020-07-08 20:27:30 +000029 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 Enge700a6a2020-07-14 01:32:43 +000064 typename VectorBase::iterator begin() noexcept {
Austin Engd761d5a2020-07-08 20:27:30 +000065 return this->container().begin();
66 }
67
Austin Enge700a6a2020-07-14 01:32:43 +000068 typename VectorBase::const_iterator begin() const noexcept {
Austin Engd761d5a2020-07-08 20:27:30 +000069 return this->container().begin();
70 }
71
Austin Enge700a6a2020-07-14 01:32:43 +000072 typename VectorBase::iterator end() noexcept {
Austin Engd761d5a2020-07-08 20:27:30 +000073 return this->container().end();
74 }
75
Austin Enge700a6a2020-07-14 01:32:43 +000076 typename VectorBase::const_iterator end() const noexcept {
Austin Engd761d5a2020-07-08 20:27:30 +000077 return this->container().end();
78 }
79
Austin Enge700a6a2020-07-14 01:32:43 +000080 typename VectorBase::reference front() {
Austin Engd761d5a2020-07-08 20:27:30 +000081 return this->container().front();
82 }
83
Austin Enge700a6a2020-07-14 01:32:43 +000084 typename VectorBase::const_reference front() const {
Austin Engd761d5a2020-07-08 20:27:30 +000085 return this->container().front();
86 }
87
Austin Enge700a6a2020-07-14 01:32:43 +000088 typename VectorBase::reference back() {
Austin Engd761d5a2020-07-08 20:27:30 +000089 return this->container().back();
90 }
91
Austin Enge700a6a2020-07-14 01:32:43 +000092 typename VectorBase::const_reference back() const {
Austin Engd761d5a2020-07-08 20:27:30 +000093 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_