Skip to content

Commit

Permalink
Merge pull request #329 from ValeevGroup/evaleev/fix/index-indexof
Browse files Browse the repository at this point in the history
wrong operator[] used in Index::indexof()
  • Loading branch information
evaleev authored Mar 14, 2022
2 parents 6786a55 + c5e7731 commit b50f1a8
Showing 1 changed file with 8 additions and 5 deletions.
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

0 comments on commit b50f1a8

Please sign in to comment.