1 /** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 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 16 #ifndef PANDA_VERIFIER_UTIL_SHIFTED_VECTOR_H__ 17 #define PANDA_VERIFIER_UTIL_SHIFTED_VECTOR_H__ 18 19 #include "runtime/include/mem/panda_containers.h" 20 21 namespace panda::verifier { 22 23 template <const int SHIFT, typename T, template <typename...> class VECTOR = PandaVector> 24 class ShiftedVector : public VECTOR<T> { 25 using Base = VECTOR<T>; 26 27 public: 28 ShiftedVector() = default; ShiftedVector(typename Base::size_type size)29 ShiftedVector(typename Base::size_type size) : Base(size) {} 30 typename Base::reference operator[](int idx) 31 { 32 return Base::operator[](static_cast<typename Base::size_type>(idx + SHIFT)); 33 } 34 typename Base::const_reference &operator[](int idx) const 35 { 36 return Base::operator[](static_cast<typename Base::size_type>(idx + SHIFT)); 37 } at(int idx)38 typename Base::reference at(int idx) 39 { 40 return Base::at(static_cast<typename Base::size_type>(idx + SHIFT)); 41 } at(int idx)42 typename Base::const_reference &at(int idx) const 43 { 44 return Base::at(static_cast<typename Base::size_type>(idx + SHIFT)); 45 } InValidRange(int idx)46 bool InValidRange(int idx) const 47 { 48 return idx + SHIFT >= 0 && static_cast<typename Base::size_type>(idx + SHIFT) < Base::size(); 49 } begin_index()50 int begin_index() const 51 { 52 return -SHIFT; 53 } end_index()54 int end_index() const 55 { 56 return static_cast<int>(Base::size()) - SHIFT; 57 } ExtendToInclude(int idx)58 void ExtendToInclude(int idx) 59 { 60 if (idx >= end_index()) { 61 Base::resize(Base::size() + static_cast<size_t>(idx - end_index() + 1)); 62 } 63 } 64 }; 65 66 } // namespace panda::verifier 67 68 #endif // !PANDA_VERIFIER_UTIL_SHIFTED_VECTOR_H__ 69