1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef TVM_RUNTIME_MICRO_STANDALONE_MINIMAL_VECTOR_H_
21 #define TVM_RUNTIME_MICRO_STANDALONE_MINIMAL_VECTOR_H_
22 
23 #include <algorithm>
24 #include <cassert>
25 #include <memory>
26 
27 namespace tvm {
28 namespace micro {
29 
30 
31 // A minimal wrapper, derived from https://github.com/Robbepop/dynarray/, that
32 // supports a minimal subset of the std::vector API with a minimized code size.
33 template <typename T>
34 struct DynArray {
35   using value_type = T;
36   using size_type = size_t;
37   using difference_type = std::ptrdiff_t;
38   using reference = value_type&;
39   using const_reference = value_type const&;
40   using pointer = value_type*;
41   using const_pointer = value_type const*;
42   using iterator = pointer;
43   using const_iterator = const_pointer;
44   using reverse_iterator = std::reverse_iterator<iterator>;
45   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
46 
47   explicit DynArray(size_type size = 0) { resize(size); }
48 
DynArrayDynArray49   DynArray(const DynArray& other) {
50     resize(other.size());
51     std::copy(other.begin(), other.end(), begin());
52   }
53 
54   DynArray& operator=(const DynArray& other) {
55     resize(other.size());
56     std::copy(other.begin(), other.end(), begin());
57     return *this;
58   }
59 
resizeDynArray60   void resize(size_type size) {
61     if (size > 0) {
62       data_.reset(new T[size]);
63     } else {
64       data_.reset();
65     }
66     size_ = size;
67   }
68 
sizeDynArray69   size_type size() const { return size_; }
70 
71   reference operator[](size_type pos) { return data_[pos]; }
72 
73   const_reference operator[](size_type pos) const { return data_[pos]; }
74 
dataDynArray75   pointer data() { return data_.get(); }
76 
dataDynArray77   const_pointer data() const { return data_.get(); }
78 
beginDynArray79   iterator begin() { return data_.get(); }
80 
beginDynArray81   const_iterator begin() const { return data_.get(); }
82 
cbeginDynArray83   const_iterator cbegin() const { return data_.get(); }
84 
endDynArray85   iterator end() { return data_.get() + size_; }
86 
endDynArray87   const_iterator end() const { return data_.get() + size_; }
88 
cendDynArray89   const_iterator cend() const { return data_.get() + size_; }
90 
frontDynArray91   reference front() { return data_[0]; }
92 
frontDynArray93   const_reference front() const { return data_[0]; }
94 
backDynArray95   reference back() { return data_[size_ - 1]; }
96 
backDynArray97   const_reference back() const { return data_[size_ - 1]; }
98 
99  private:
100   std::unique_ptr<T[]> data_;
101   size_type size_;
102 };
103 
104 }  // namespace micro
105 }  // namespace tvm
106 
107 #endif  // TVM_RUNTIME_MICRO_STANDALONE_MINIMAL_VECTOR_H_
108