Skip to content

wrong operator[] used in Index::indexof() #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/TiledArray/util/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ std::string join(const small_vector<std::string> &v);
template<typename T, typename U>
using enable_if_string = std::enable_if_t< std::is_same_v<T,std::string>, U>;

/// an n-index, with n a runtime parameter
/// an `n`-index, with `n` a runtime parameter
template<typename T>
class Index {
public:
Expand All @@ -36,10 +36,10 @@ class Index {
Index(const S &s) : data_(s.begin(), s.end()) {}

template<typename U = void>
Index(const std::string &s) : Index(index::tokenize(s)) {}
explicit Index(const std::string &s) : Index(index::tokenize(s)) {}

template<typename U = void>
Index(const char *s) : Index(std::string(s)) {}
explicit Index(const char *s) : Index(std::string(s)) {}

template<typename U = void>
operator std::string() const { return index::join(data_); }
Expand All @@ -65,11 +65,14 @@ class Index {

const auto& operator[](size_t idx) const { return data_.at(idx); }

/// @param[in] v element to seek
/// @pre `this->contains(v)`
/// @return the location of @p v in `*this`
size_t indexof(const T& v) const {
for (size_t i = 0; i < this->size(); ++i) {
if (this[i] == v) return i;
if ((*this)[i] == v) return i;
}
return -1;
TA_ASSERT(false);
}

/// Returns true if argument exists in the Index object, else returns false
Expand Down