| @@ -1,5 +1,5 @@ | |||||
| /** | /** | ||||
| * Copyright 2020 Huawei Technologies Co., Ltd | |||||
| * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. | |||||
| * | * | ||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| * you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
| @@ -32,27 +32,23 @@ class Tuple { | |||||
| delete[] data_heap_; | delete[] data_heap_; | ||||
| data_heap_ = nullptr; | data_heap_ = nullptr; | ||||
| } | } | ||||
| /// | |||||
| /// @brief copy constructor from another tuple | /// @brief copy constructor from another tuple | ||||
| /// @param s the source tuple | /// @param s the source tuple | ||||
| /// | |||||
| inline Tuple(const Tuple<ValueType> &s) { this->assign(s.begin(), s.end()); } | inline Tuple(const Tuple<ValueType> &s) { this->assign(s.begin(), s.end()); } | ||||
| /// | |||||
| /// @brief constructor from initializer list | /// @brief constructor from initializer list | ||||
| /// @param init the initializer_list | /// @param init the initializer_list | ||||
| /// | |||||
| explicit Tuple(const std::initializer_list<ValueType> &init) { this->assign(init.begin(), init.end()); } | explicit Tuple(const std::initializer_list<ValueType> &init) { this->assign(init.begin(), init.end()); } | ||||
| /// | |||||
| /// @brief constructor from vector | /// @brief constructor from vector | ||||
| /// @param init the vector | /// @param init the vector | ||||
| /// | |||||
| explicit Tuple(const std::vector<ValueType> &init) { // NOLINT(runtime/explicit) | explicit Tuple(const std::vector<ValueType> &init) { // NOLINT(runtime/explicit) | ||||
| this->assign(init.begin(), init.end()); | this->assign(init.begin(), init.end()); | ||||
| } | } | ||||
| /// | |||||
| /// @brief move constructor from Tuple | /// @brief move constructor from Tuple | ||||
| /// @param src the source shape | /// @param src the source shape | ||||
| /// | |||||
| inline Tuple(Tuple<ValueType> &&src) { // NOLINT(runtime/explicit) | inline Tuple(Tuple<ValueType> &&src) { // NOLINT(runtime/explicit) | ||||
| this->swap(src); | this->swap(src); | ||||
| } | } | ||||
| @@ -77,102 +73,88 @@ class Tuple { | |||||
| this->SetDim(end - begin); | this->SetDim(end - begin); | ||||
| (void)std::copy(begin, end, this->begin()); | (void)std::copy(begin, end, this->begin()); | ||||
| } | } | ||||
| /// | |||||
| /// @brief Swap current object with other | /// @brief Swap current object with other | ||||
| /// @param other another object to be swapped. | /// @param other another object to be swapped. | ||||
| /// | |||||
| inline void swap(Tuple<ValueType> &other) { // NOLINT(*) | inline void swap(Tuple<ValueType> &other) { // NOLINT(*) | ||||
| std::swap(ndim_, other.ndim_); | std::swap(ndim_, other.ndim_); | ||||
| std::swap(num_heap_allocated_, other.num_heap_allocated_); | std::swap(num_heap_allocated_, other.num_heap_allocated_); | ||||
| std::swap(data_stack_, other.data_stack_); | std::swap(data_stack_, other.data_stack_); | ||||
| std::swap(data_heap_, other.data_heap_); | std::swap(data_heap_, other.data_heap_); | ||||
| } | } | ||||
| /// | |||||
| /// @brief assignment from another tuple. | /// @brief assignment from another tuple. | ||||
| /// @param src source tuple | /// @param src source tuple | ||||
| /// @return reference of self | /// @return reference of self | ||||
| /// | |||||
| inline Tuple<ValueType> &operator=(const Tuple<ValueType> &src) { | inline Tuple<ValueType> &operator=(const Tuple<ValueType> &src) { | ||||
| if (&src != this) { | if (&src != this) { | ||||
| this->assign(src.begin(), src.end()); | this->assign(src.begin(), src.end()); | ||||
| } | } | ||||
| return *this; | return *this; | ||||
| } | } | ||||
| /// | |||||
| /// @brief assignment from rvalue of another tuple. | /// @brief assignment from rvalue of another tuple. | ||||
| /// @param src source tuple | /// @param src source tuple | ||||
| /// @return reference of self | /// @return reference of self | ||||
| /// | |||||
| inline Tuple<ValueType> &operator=(Tuple<ValueType> &&src) { | inline Tuple<ValueType> &operator=(Tuple<ValueType> &&src) { | ||||
| if (&src != this) { | if (&src != this) { | ||||
| Tuple<ValueType>(std::move(src)).swap(*this); | Tuple<ValueType>(std::move(src)).swap(*this); | ||||
| } | } | ||||
| return *this; | return *this; | ||||
| } | } | ||||
| /// | |||||
| /// @brief assignment from initializer list | /// @brief assignment from initializer list | ||||
| /// @param init the source initializer list | /// @param init the source initializer list | ||||
| /// @return reference of self | /// @return reference of self | ||||
| /// | |||||
| inline Tuple<ValueType> &operator=(std::initializer_list<ValueType> init) { | inline Tuple<ValueType> &operator=(std::initializer_list<ValueType> init) { | ||||
| this->assign(init.begin(), init.end()); | this->assign(init.begin(), init.end()); | ||||
| return *this; | return *this; | ||||
| } | } | ||||
| /// | |||||
| /// @return whether two tuple equals | /// @return whether two tuple equals | ||||
| /// @param s the tuple to compare against | /// @param s the tuple to compare against | ||||
| /// | |||||
| inline bool operator==(const Tuple<ValueType> &s) const { | inline bool operator==(const Tuple<ValueType> &s) const { | ||||
| if (ndim_ != s.ndim_) { | if (ndim_ != s.ndim_) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| return std::equal(begin(), end(), s.begin()); | return std::equal(begin(), end(), s.begin()); | ||||
| } | } | ||||
| /// | |||||
| /// @return whether two tuple not equal | /// @return whether two tuple not equal | ||||
| /// @param s the tuple to compare against | /// @param s the tuple to compare against | ||||
| /// | |||||
| inline bool operator!=(const Tuple<ValueType> &s) const { return !(*this == s); } | inline bool operator!=(const Tuple<ValueType> &s) const { return !(*this == s); } | ||||
| /// | |||||
| /// @return the begin data pointer to content of the tuple | /// @return the begin data pointer to content of the tuple | ||||
| /// | |||||
| inline const ValueType *begin() const { return ndim_ <= STACK_CACHE_NUM ? data_stack_ : data_heap_; } | inline const ValueType *begin() const { return ndim_ <= STACK_CACHE_NUM ? data_stack_ : data_heap_; } | ||||
| /// | |||||
| /// @return the begin data pointer to content of the tuple | /// @return the begin data pointer to content of the tuple | ||||
| /// | |||||
| inline ValueType *begin() { return ndim_ <= STACK_CACHE_NUM ? data_stack_ : data_heap_; } | inline ValueType *begin() { return ndim_ <= STACK_CACHE_NUM ? data_stack_ : data_heap_; } | ||||
| /// | |||||
| /// @return the data pointer to end of the tuple | /// @return the data pointer to end of the tuple | ||||
| /// | |||||
| inline const ValueType *end() const { | inline const ValueType *end() const { | ||||
| return ndim_ <= STACK_CACHE_NUM ? (data_stack_ + ndim_) : (data_heap_ + ndim_); | return ndim_ <= STACK_CACHE_NUM ? (data_stack_ + ndim_) : (data_heap_ + ndim_); | ||||
| } | } | ||||
| /// | |||||
| /// @return the data pointer to end the tuple | /// @return the data pointer to end the tuple | ||||
| /// | |||||
| inline ValueType *end() { return ndim_ <= STACK_CACHE_NUM ? (data_stack_ + ndim_) : (data_heap_ + ndim_); } | inline ValueType *end() { return ndim_ <= STACK_CACHE_NUM ? (data_stack_ + ndim_) : (data_heap_ + ndim_); } | ||||
| /// | |||||
| /// @return number of dimension of the tuple | /// @return number of dimension of the tuple | ||||
| /// | |||||
| inline uint32_t ndim() const { return ndim_; } | inline uint32_t ndim() const { return ndim_; } | ||||
| /// | |||||
| /// @brief get corresponding index | /// @brief get corresponding index | ||||
| /// @param i dimension index | /// @param i dimension index | ||||
| /// @return the corresponding dimension size | /// @return the corresponding dimension size | ||||
| /// | |||||
| inline ValueType &operator[](size_t i) { return begin()[i]; } | inline ValueType &operator[](size_t i) { return begin()[i]; } | ||||
| /// | |||||
| /// @brief get corresponding index | /// @brief get corresponding index | ||||
| /// @param i dimension index | /// @param i dimension index | ||||
| /// @return the corresponding dimension size | /// @return the corresponding dimension size | ||||
| /// | |||||
| inline const ValueType &operator[](size_t i) const { return begin()[i]; } | inline const ValueType &operator[](size_t i) const { return begin()[i]; } | ||||
| /// | |||||
| /// @brief allow output string of tuple to ostream | /// @brief allow output string of tuple to ostream | ||||
| /// @param os the output stream | /// @param os the output stream | ||||
| /// @param t the tuple | /// @param t the tuple | ||||
| /// @return the ostream | /// @return the ostream | ||||
| /// | |||||
| friend std::ostream &operator<<(std::ostream &os, const Tuple<ValueType> &t) { | friend std::ostream &operator<<(std::ostream &os, const Tuple<ValueType> &t) { | ||||
| os << '['; | os << '['; | ||||
| const ValueType *begin = t.begin(); | const ValueType *begin = t.begin(); | ||||
| @@ -186,12 +168,11 @@ class Tuple { | |||||
| os << ']'; | os << ']'; | ||||
| return os; | return os; | ||||
| } | } | ||||
| /// | |||||
| /// @brief read tuple from the istream | /// @brief read tuple from the istream | ||||
| /// @param is the input stream | /// @param is the input stream | ||||
| /// @param t The tuple | /// @param t The tuple | ||||
| /// @return the istream | /// @return the istream | ||||
| /// | |||||
| friend std::istream &operator>>(std::istream &is, Tuple<ValueType> &t) { | friend std::istream &operator>>(std::istream &is, Tuple<ValueType> &t) { | ||||
| // get ( | // get ( | ||||
| if (!HandleLeftBracket(is, t)) { | if (!HandleLeftBracket(is, t)) { | ||||