From commits-return-13422-archive-asf-public=cust-asf.ponee.io@tvm.apache.org Fri May 15 18:03:47 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 12045180608 for ; Fri, 15 May 2020 20:03:46 +0200 (CEST) Received: (qmail 40658 invoked by uid 500); 15 May 2020 18:03:46 -0000 Mailing-List: contact commits-help@tvm.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tvm.apache.org Delivered-To: mailing list commits@tvm.apache.org Received: (qmail 40649 invoked by uid 99); 15 May 2020 18:03:46 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 15 May 2020 18:03:46 +0000 From: =?utf-8?q?GitBox?= To: commits@tvm.apache.org Subject: =?utf-8?q?=5BGitHub=5D_=5Bincubator-tvm=5D_junrushao1994_commented_on_a_chan?= =?utf-8?q?ge_in_pull_request_=235585=3A_=5BRuntime=5D_Introduce_runtime=3A?= =?utf-8?q?=3AArray?= Message-ID: <158956582627.19379.9050368447262120084.asfpy@gitbox.apache.org> Date: Fri, 15 May 2020 18:03:46 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit References: In-Reply-To: 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 +class IterAdapter { + public: + using difference_type = typename std::iterator_traits::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::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 std::enable_if::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 +class ReverseIterAdapter { + public: + using difference_type = typename std::iterator_traits::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::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 std::enable_if::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 { + 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(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 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