context_util.h 1.9 KB

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