From 1b0df76f04f25d43da6a33a517e19a9f0a16edfd Mon Sep 17 00:00:00 2001 From: lixinqi Date: Mon, 13 Jun 2022 17:37:18 +0800 Subject: [PATCH] AddAndReadVector::operator[] --- oneflow/core/common/add_and_read_vector.h | 7 ++----- oneflow/core/common/container_util.h | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/oneflow/core/common/add_and_read_vector.h b/oneflow/core/common/add_and_read_vector.h index 1ca1552328d..8099f20bb78 100644 --- a/oneflow/core/common/add_and_read_vector.h +++ b/oneflow/core/common/add_and_read_vector.h @@ -31,7 +31,7 @@ class AddAndReadVector { AddAndReadVector() : size_(0) {} ~AddAndReadVector() = default; - using value_type = T; + using value_type = const T; using size_type = size_t; // not thread safe. @@ -46,10 +46,7 @@ class AddAndReadVector { return granularity2vector_[gran].data()[index - start]; } - // lock free. - T& at(size_t index) { - CHECK_GE(index, 0); - CHECK_LT(index, size_); + const T& operator[](size_t index) const { int gran = GetGranularity(index); int start = (1 << gran) - 1; return granularity2vector_[gran].data()[index - start]; diff --git a/oneflow/core/common/container_util.h b/oneflow/core/common/container_util.h index 6eb4b84c7db..1c5902e45c8 100644 --- a/oneflow/core/common/container_util.h +++ b/oneflow/core/common/container_util.h @@ -57,7 +57,7 @@ Maybe VectorAt(VecT& vec, typename VecT::size_type i static_assert(!std::is_same::value, "VectorAt(vector&, size_t) is not supported."); CHECK_LT_OR_RETURN(index, vec.size()); - return vec.at(index); + return vec[index]; } template<>