1 /** 2 * Copyright (c) 2021-2024 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 ark::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 explicit ShiftedVector(typename Base::size_type size) : Base(size) {} 30 typename Base::reference operator[](int idx) 31 { 32 // NOLINTNEXTLINE(bugprone-misplaced-widening-cast) 33 return Base::operator[](static_cast<typename Base::size_type>(idx + SHIFT)); 34 } 35 typename Base::const_reference &operator[](int idx) const 36 { 37 // NOLINTNEXTLINE(bugprone-misplaced-widening-cast) 38 return Base::operator[](static_cast<typename Base::size_type>(idx + SHIFT)); 39 } At(int idx)40 typename Base::reference At(int idx) 41 { 42 // NOLINTNEXTLINE(bugprone-misplaced-widening-cast) 43 return Base::at(static_cast<typename Base::size_type>(idx + SHIFT)); 44 } At(int idx)45 typename Base::const_reference &At(int idx) const 46 { 47 // NOLINTNEXTLINE(bugprone-misplaced-widening-cast) 48 return Base::at(static_cast<typename Base::size_type>(idx + SHIFT)); 49 } InValidRange(int idx)50 bool InValidRange(int idx) const 51 { 52 // NOLINTNEXTLINE(bugprone-misplaced-widening-cast) 53 return idx + SHIFT >= 0 && static_cast<typename Base::size_type>(idx + SHIFT) < Base::size(); 54 } BeginIndex()55 int BeginIndex() const 56 { 57 return -SHIFT; 58 } EndIndex()59 int EndIndex() const 60 { 61 return static_cast<int>(Base::size()) - SHIFT; 62 } ExtendToInclude(int idx)63 void ExtendToInclude(int idx) 64 { 65 if (idx >= EndIndex()) { 66 Base::resize(Base::size() + static_cast<size_t>(idx - EndIndex() + 1)); 67 } 68 } 69 }; 70 71 } // namespace ark::verifier 72 73 #endif // !PANDA_VERIFIER_UTIL_SHIFTED_VECTOR_H_ 74