context_util.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. // This provides a few C++ helpers that are useful for manipulating C structures
  13. // in C++.
  14. #ifndef TENSORFLOW_LITE_CONTEXT_UTIL_H_
  15. #define TENSORFLOW_LITE_CONTEXT_UTIL_H_
  16. #include <stddef.h>
  17. #include "tensorflow/lite/c/common.h"
  18. namespace tflite {
  19. // Provide a range iterable wrapper for TfLiteIntArray* (C lists that TfLite
  20. // C api uses. Can't use the google array_view, since we can't depend on even
  21. // absl for embedded device reasons.
  22. class TfLiteIntArrayView {
  23. public:
  24. // Construct a view of a TfLiteIntArray*. Note, `int_array` should be non-null
  25. // and this view does not take ownership of it.
  26. explicit TfLiteIntArrayView(const TfLiteIntArray* int_array)
  27. : int_array_(int_array) {}
  28. TfLiteIntArrayView(const TfLiteIntArrayView&) = default;
  29. TfLiteIntArrayView& operator=(const TfLiteIntArrayView& rhs) = default;
  30. typedef const int* const_iterator;
  31. const_iterator begin() const { return int_array_->data; }
  32. const_iterator end() const { return &int_array_->data[int_array_->size]; }
  33. size_t size() const { return end() - begin(); }
  34. int operator[](size_t pos) const { return int_array_->data[pos]; }
  35. private:
  36. const TfLiteIntArray* int_array_;
  37. };
  38. } // namespace tflite
  39. #endif // TENSORFLOW_LITE_CONTEXT_UTIL_H_