junrushao commented on code in PR #443: URL: https://github.com/apache/tvm-ffi/pull/443#discussion_r2805960312
########## include/tvm/ffi/container/list.h: ########## @@ -0,0 +1,523 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file tvm/ffi/container/list.h + * \brief Mutable list type. + * + * tvm::ffi::List<Any> is an erased mutable sequence container. + */ +#ifndef TVM_FFI_CONTAINER_LIST_H_ +#define TVM_FFI_CONTAINER_LIST_H_ + +#include <tvm/ffi/any.h> +#include <tvm/ffi/container/array.h> +#include <tvm/ffi/container/seq_base.h> +#include <tvm/ffi/object.h> + +#include <algorithm> +#include <initializer_list> +#include <sstream> +#include <string> +#include <type_traits> +#include <utility> +#include <vector> + +namespace tvm { +namespace ffi { + +/*! \brief List node content in list */ +class ListObj : public SeqBaseObj { + public: + /*! + * \brief Constructs a container with n elements. Each element is a copy of val + * \param n The size of the container + * \param val The init value + * \return Ref-counted ListObj requested + */ + static ObjectPtr<ListObj> CreateRepeated(int64_t n, const Any& val) { + ObjectPtr<ListObj> p = ListObj::Empty(n); + Any* itr = p->MutableBegin(); + for (int64_t& i = p->TVMFFISeqCell::size = 0; i < n; ++i) { + new (itr++) Any(val); + } + return p; + } + + /// \cond Doxygen_Suppress + static constexpr const int32_t _type_index = TypeIndex::kTVMFFIList; + static const constexpr bool _type_final = true; + TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFIList, ListObj, Object); + /// \endcond + + private: + /*! + * \brief Ensure the list has at least n slots. + * \param n The lower bound of required capacity. + */ + void Reserve(int64_t n) { + if (n <= TVMFFISeqCell::capacity) { + return; + } + Any* old_data = MutableBegin(); + Any* new_data = static_cast<Any*>(::operator new(sizeof(Any) * static_cast<size_t>(n))); + for (int64_t i = 0; i < TVMFFISeqCell::size; ++i) { Review Comment: updated -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
