tvm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-tvm] junrushao1994 commented on a change in pull request #5585: [Runtime] Introduce runtime::Array
Date Fri, 15 May 2020 18:03:46 GMT

junrushao1994 commented on a change in pull request #5585:
URL: https://github.com/apache/incubator-tvm/pull/5585#discussion_r425964009



##########
File path: include/tvm/runtime/container.h
##########
@@ -188,6 +188,709 @@ class InplaceArrayBase {
     return data_start + idx * sizeof(ElemType);
   }
 };
+}  // namespace runtime
+}  // namespace tvm
+
+namespace tvm {
+namespace runtime {
+
+/*!
+ * \brief iterator adapter that adapts TIter to return another type.
+ * \tparam Converter a struct that contains converting function
+ * \tparam TIter the content iterator type.
+ */
+template <typename Converter, typename TIter>
+class IterAdapter {
+ public:
+  using difference_type = typename std::iterator_traits<TIter>::difference_type;
+  using value_type = typename Converter::ResultType;
+  using pointer = typename Converter::ResultType*;
+  using reference = typename Converter::ResultType&;  // NOLINT(*)
+  using iterator_category = typename std::iterator_traits<TIter>::iterator_category;
+
+  explicit IterAdapter(TIter iter) : iter_(iter) {}
+  inline IterAdapter& operator++() {
+    ++iter_;
+    return *this;
+  }
+  inline IterAdapter& operator--() {
+    --iter_;
+    return *this;
+  }
+  inline IterAdapter& operator++(int) {
+    IterAdapter copy = *this;
+    ++iter_;
+    return copy;
+  }
+  inline IterAdapter& operator--(int) {
+    IterAdapter copy = *this;
+    --iter_;
+    return copy;
+  }
+
+  inline IterAdapter operator+(difference_type offset) const { return IterAdapter(iter_ +
offset); }
+
+  template <typename T = IterAdapter>
+  typename std::enable_if<std::is_same<iterator_category, std::random_access_iterator_tag>::value,
+                          typename T::difference_type>::type inline
+  operator-(const IterAdapter& rhs) const {
+    return iter_ - rhs.iter_;
+  }
+
+  inline bool operator==(IterAdapter other) const { return iter_ == other.iter_; }
+  inline bool operator!=(IterAdapter other) const { return !(*this == other); }
+  inline const value_type operator*() const { return Converter::convert(*iter_); }
+
+ private:
+  TIter iter_;
+};
+
+/*!
+ * \brief iterator adapter that adapts TIter to return another type.
+ * \tparam Converter a struct that contains converting function
+ * \tparam TIter the content iterator type.
+ */
+template <typename Converter, typename TIter>
+class ReverseIterAdapter {
+ public:
+  using difference_type = typename std::iterator_traits<TIter>::difference_type;
+  using value_type = typename Converter::ResultType;
+  using pointer = typename Converter::ResultType*;
+  using reference = typename Converter::ResultType&;  // NOLINT(*)
+  using iterator_category = typename std::iterator_traits<TIter>::iterator_category;
+
+  explicit ReverseIterAdapter(TIter iter) : iter_(iter) {}
+  inline ReverseIterAdapter& operator++() {
+    --iter_;
+    return *this;
+  }
+  inline ReverseIterAdapter& operator--() {
+    ++iter_;
+    return *this;
+  }
+  inline ReverseIterAdapter& operator++(int) {
+    ReverseIterAdapter copy = *this;
+    --iter_;
+    return copy;
+  }
+  inline ReverseIterAdapter& operator--(int) {
+    ReverseIterAdapter copy = *this;
+    ++iter_;
+    return copy;
+  }
+  inline ReverseIterAdapter operator+(difference_type offset) const {
+    return ReverseIterAdapter(iter_ - offset);
+  }
+
+  template <typename T = ReverseIterAdapter>
+  typename std::enable_if<std::is_same<iterator_category, std::random_access_iterator_tag>::value,
+                          typename T::difference_type>::type inline
+  operator-(const ReverseIterAdapter& rhs) const {
+    return rhs.iter_ - iter_;
+  }
+
+  inline bool operator==(ReverseIterAdapter other) const { return iter_ == other.iter_; }
+  inline bool operator!=(ReverseIterAdapter other) const { return !(*this == other); }
+  inline const value_type operator*() const { return Converter::convert(*iter_); }
+
+ private:
+  TIter iter_;
+};
+
+}  // namespace runtime
+}  // namespace tvm
+
+namespace tvm {
+
+// forward-declare a friend method of ArrayNode.
+TVM_DLL runtime::ObjectRef LoadJSON(std::string json_str);
+
+namespace runtime {
+
+/*! \brief array node content in array */
+class ArrayNode : public Object, public InplaceArrayBase<ArrayNode, ObjectRef> {
+ public:
+  /*! \return The size of the array */
+  inline size_t size() const { return this->size_; }
+
+  /*!
+   * \brief Read i-th element from array.
+   * \param i The index
+   * \return the i-th element.
+   */
+  inline const ObjectRef at(const int64_t i) const { return this->operator[](i); }
+
+  /*! \return begin constant iterator */
+  inline const ObjectRef* begin() const {
+    return static_cast<ObjectRef*>(InplaceArrayBase::AddressOf(0));
+  }
+
+  /*! \return end constant iterator */
+  inline const ObjectRef* end() const { return begin() + size_; }
+
+  /*!
+   * \brief Set i-th element of the array in-place
+   * \param i The index
+   * \param item The value to be set
+   */
+  inline void SetItem(const int64_t i, ObjectRef item) { this->operator[](i) = std::move(item);
}
+
+  static constexpr const uint32_t _type_index = TypeIndex::kRuntimeArray;
+  static constexpr const char* _type_key = "Array";
+  TVM_DECLARE_FINAL_OBJECT_INFO(ArrayNode, Object);
+
+ private:
+  /*! \return Size of initialized memory, used by InplaceArrayBase. */
+  size_t GetSize() const { return this->size_; }
+
+  /*!
+   * \brief Create an ArrayNode with the given capacity.
+   * \param cap Required capacity
+   * \return Ref-counted ArrayNode requested
+   */
+  static inline ObjectPtr<ArrayNode> make(const int64_t cap = INIT_SIZE) {

Review comment:
       Yeah I will rename it to `Empty`.
   
   Moving it to public couldn't completely help LoadJSON, because it has to manipulate `size_`
on the fly to ensure exceptional safety.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message