1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
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 // This provides a few C++ helpers that are useful for manipulating C structures
16 // in C++.
17 #ifndef TENSORFLOW_LITE_CONTEXT_UTIL_H_
18 #define TENSORFLOW_LITE_CONTEXT_UTIL_H_
19 
20 #include <stddef.h>
21 
22 #include "tensorflow/lite/c/common.h"
23 
24 namespace tflite {
25 
26 // Provide a range iterable wrapper for TfLiteIntArray* (C lists that TfLite
27 // C api uses. Can't use the google array_view, since we can't depend on even
28 // absl for embedded device reasons.
29 class TfLiteIntArrayView {
30  public:
31   // Construct a view of a TfLiteIntArray*. Note, `int_array` should be non-null
32   // and this view does not take ownership of it.
TfLiteIntArrayView(const TfLiteIntArray * int_array)33   explicit TfLiteIntArrayView(const TfLiteIntArray* int_array)
34       : int_array_(int_array) {}
35 
36   TfLiteIntArrayView(const TfLiteIntArrayView&) = default;
37   TfLiteIntArrayView& operator=(const TfLiteIntArrayView& rhs) = default;
38 
39   typedef const int* const_iterator;
begin()40   const_iterator begin() const { return int_array_->data; }
end()41   const_iterator end() const { return &int_array_->data[int_array_->size]; }
size()42   size_t size() const { return end() - begin(); }
43   int operator[](size_t pos) const { return int_array_->data[pos]; }
44 
45  private:
46   const TfLiteIntArray* int_array_;
47 };
48 
49 }  // namespace tflite
50 
51 #endif  // TENSORFLOW_LITE_CONTEXT_UTIL_H_
52