Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion external/versions.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# for each dependency track both current and previous id (the variable for the latter must contain PREVIOUS)
# to be able to auto-update them

set(TA_TRACKED_VGCMAKEKIT_TAG d29dc6177884557d13fb87f364eb76350e8ddc9c)
set(TA_TRACKED_VGCMAKEKIT_TAG f946aa274745da09702a727492f1cabb843dc9e6)

# Boost explicitly downgraded to 1.59 from 1.68
set(TA_TRACKED_BOOST_VERSION 1.59)
Expand Down
146 changes: 73 additions & 73 deletions src/TiledArray/util/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#include "TiledArray/expressions/fwd.h"

#include <TiledArray/error.h>
#include <TiledArray/util/vector.h>
#include <TiledArray/permutation.h>
#include <TiledArray/util/vector.h>

#include <string>
#include <iosfwd>
#include <string>

namespace TiledArray::index {

template<typename T>
template <typename T>
using small_vector = container::svector<T>;

small_vector<std::string> tokenize(const std::string &s);
Expand All @@ -19,73 +19,71 @@ small_vector<std::string> validate(const small_vector<std::string> &v);

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>;
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
template<typename T>
template <typename T>
class Index {
public:
public:
using container_type = small_vector<T>;
using value_type = typename container_type::value_type;

Index() = default;
Index(container_type &&s) : data_(std::move(s)) {}

template<typename S, typename U = void>
template <typename S, typename U = void>
Index(const S &s) : data_(s.begin(), s.end()) {}

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

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

template<typename U = void>
operator std::string() const { return index::join(data_); }
template <typename U = void>
operator std::string() const {
return index::join(data_);
}

explicit operator bool() const { return !data_.empty(); }

bool operator==(const Index &other) const {
return (this->data_ == other.data_);
}

bool operator!=(const Index &other) const {
return !(*this == other);
}
bool operator!=(const Index &other) const { return !(*this == other); }

size_t size() const { return data_.size(); }

auto begin() const { return data_.begin(); }
auto end() const { return data_.end(); }

auto find(const T& v) const {
auto find(const T &v) const {
return std::find(this->begin(), this->end(), v);
}

const auto& operator[](size_t idx) const { return data_.at(idx); }
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 {
size_t indexof(const T &v) const {
for (size_t i = 0; i < this->size(); ++i) {
if ((*this)[i] == v) return i;
}
TA_ASSERT(false);
}

/// Returns true if argument exists in the Index object, else returns false
bool contains(const T& v) const {
return (this->find(v) != this->end());
}
bool contains(const T &v) const { return (this->find(v) != this->end()); }

private:
container_type data_;
};

template<typename T>
std::ostream& operator<<(std::ostream& os, const Index<T> &idx) {
template <typename T>
std::ostream &operator<<(std::ostream &os, const Index<T> &idx) {
os << std::string(idx);
return os;
}
Expand All @@ -94,7 +92,7 @@ std::ostream& operator<<(std::ostream& os, const Index<T> &idx) {
/// @param[in] a an Index object
/// @param[in] b an Index object
/// @pre a and b do not have duplicates
template<typename T>
template <typename T>
Index<T> operator&(const Index<T> &a, const Index<T> &b) {
typename Index<T>::container_type r;
for (const auto &s : a) {
Expand All @@ -107,7 +105,7 @@ Index<T> operator&(const Index<T> &a, const Index<T> &b) {
/// @param[in] a an Index object
/// @param[in] b an Index object
/// @pre a and b do not have duplicates
template<typename T>
template <typename T>
Index<T> operator|(const Index<T> &a, const Index<T> &b) {
typename Index<T>::container_type r;
r.assign(a.begin(), a.end());
Expand All @@ -122,7 +120,7 @@ Index<T> operator|(const Index<T> &a, const Index<T> &b) {
/// @param[in] a an Index object
/// @param[in] b an Index object
/// @note unline operator| @p a and @p b can have have duplicates
template<typename T>
template <typename T>
Index<T> operator+(const Index<T> &a, const Index<T> &b) {
typename Index<T>::container_type r;
r.assign(a.begin(), a.end());
Expand All @@ -134,7 +132,7 @@ Index<T> operator+(const Index<T> &a, const Index<T> &b) {
/// @param[in] a an Index object
/// @param[in] b an Index object
/// @note unline operator& @p a and @p b can have have duplicates
template<typename T>
template <typename T>
Index<T> operator-(const Index<T> &a, const Index<T> &b) {
typename Index<T>::container_type r;
for (const auto &s : a) {
Expand All @@ -148,82 +146,86 @@ Index<T> operator-(const Index<T> &a, const Index<T> &b) {
/// @param[in] a an Index object
/// @param[in] b an Index object
/// @pre a and b do not have duplicates
template<typename T>
template <typename T>
inline Index<T> operator^(const Index<T> &a, const Index<T> &b) {
return (a | b) - (a & b);
}

template<typename T>
size_t rank(const Index<T> &idx) { return idx.size(); }
template <typename T>
size_t rank(const Index<T> &idx) {
return idx.size();
}

template <typename T>
Index<T> sorted(const Index<T>& a) {
Index<T> sorted(const Index<T> &a) {
typename Index<T>::container_type r(a.begin(), a.end());
std::sort(r.begin(), r.end());
return Index<T>(r);
}

template<typename T>
Permutation permutation(const Index<T> &s, const Index<T> &p) {
assert(sorted(s) == sorted(p));
small_vector<size_t> m;
m.reserve(p.size());
for (size_t i = 0; i != p.size(); ++i) {
m.push_back(s.indexof(p[i]));
/// @param[in] from original (preimage) indices
/// @param[in] to target (image) indices
/// @return Permutation mapping @p from to @p to
template <typename T>
Permutation permutation(const Index<T> &from, const Index<T> &to) {
const auto order = from.size();
TA_ASSERT(order == to.size() && sorted(from) == sorted(to));
small_vector<size_t> p;
p.reserve(order);
for (auto &&f : from) {
p.emplace_back(to.indexof(f));
}
return Permutation(m);
return Permutation(std::move(p));
}

template<typename T, bool Inverse>
auto permute(const Permutation &p, const Index<T> &s, std::bool_constant<Inverse>) {
template <typename T, bool Inverse>
auto permute(const Permutation &p, const Index<T> &s,
std::bool_constant<Inverse>) {
if (!p) return s;
using R = typename Index<T>::container_type;
R r(p.size());
detail::permute_n(p.size(), p.begin(), s.begin(), r.begin(), std::bool_constant<Inverse>{});
detail::permute_n(p.size(), p.begin(), s.begin(), r.begin(),
std::bool_constant<Inverse>{});
return Index<T>{r};
}

/// @brief Index-annotated collection of objects
/// @tparam Value
/// This is a map using Index::element_type as key
template<typename K, typename V>
template <typename K, typename V>
struct IndexMap {

using key_type = K;
using value_type = V;

IndexMap(const Index<K> &keys, std::initializer_list<V> s)
: IndexMap(keys, s.begin(), s.end()) {}
: IndexMap(keys, s.begin(), s.end()) {}

template <typename S>
IndexMap(const Index<K> &keys, S &&s)
: IndexMap(keys, s.begin(), s.end()) {}
IndexMap(const Index<K> &keys, S &&s) : IndexMap(keys, s.begin(), s.end()) {}

template <typename It>
IndexMap(const Index<K> &keys, It begin, It end) {
auto it = begin;
data_.reserve(keys.size());
for (auto &&key : keys) {
assert(it != end);
data_.emplace_back(std::pair<K,V>{key, *it});
TA_ASSERT(it != end);
data_.emplace_back(std::pair<K, V>{key, *it});
++it;
}
assert(it == end);
TA_ASSERT(it == end);
}

IndexMap(const small_vector<std::pair<K, V> > &data) : data_(data) { }
IndexMap(const small_vector<std::pair<K, V> > &data) : data_(data) {}

/// @return const iterator pointing to the element associated with @p key
auto find(const key_type &key) const {
return std::find_if(
data_.begin(), data_.end(),
[&key](const auto &v) { return key == v.first; }
);
return std::find_if(data_.begin(), data_.end(),
[&key](const auto &v) { return key == v.first; });
}

/// @return reference to the element associated with @p key
/// @throw TA::Exception if @p key is not in this map
const auto& operator[](const key_type &key) const {
const auto &operator[](const key_type &key) const {
auto it = find(key);
if (it != data_.end()) return it->second;
throw TiledArray::Exception("IndexMap::at(key): key not found");
Expand All @@ -245,26 +247,25 @@ struct IndexMap {
auto end() const { return data_.end(); }

private:
small_vector< std::pair<key_type, value_type> > data_;

small_vector<std::pair<key_type, value_type> > data_;
};

template<typename K, typename V>
bool operator==(const IndexMap<K,V>& lhs, const IndexMap<K,V>& rhs) {
for (const auto& [k,v] : lhs) {
template <typename K, typename V>
bool operator==(const IndexMap<K, V> &lhs, const IndexMap<K, V> &rhs) {
for (const auto &[k, v] : lhs) {
if (rhs.find(k) == rhs.end() || v != rhs[k]) return false;
}
for (const auto& [k,v] : rhs) {
for (const auto &[k, v] : rhs) {
if (lhs.find(k) == lhs.end()) return false;
}
return true;
}

/// TODO to be filled by Sam
template <typename K, typename V>
IndexMap<K,V> operator|(const IndexMap<K,V> &a, const IndexMap<K,V> &b) {
small_vector< std::pair<K, V> > d(a.begin(), a.end());
for (const auto [k,v] : b) {
IndexMap<K, V> operator|(const IndexMap<K, V> &a, const IndexMap<K, V> &b) {
small_vector<std::pair<K, V> > d(a.begin(), a.end());
for (const auto [k, v] : b) {
if (a.find(k) != a.end()) {
TA_ASSERT(a[k] == b[k]);
continue;
Expand All @@ -282,22 +283,21 @@ using Index = TiledArray::index::Index<std::string>;
using TiledArray::index::IndexMap;

/// converts the annotation of an expression to an Index
template<typename Array>
template <typename Array>
auto idx(const std::string &s) {
if constexpr (detail::is_tensor_of_tensor_v<typename Array::value_type>) {
auto semi = std::find(s.begin(), s.end(), ';');
assert(semi != s.end());
TA_ASSERT(semi != s.end());
auto first = std::string(s.begin(), semi);
auto second = std::string(semi+1, s.end());
return std::tuple<Index,Index>{ first, second };
}
else {
return std::tuple<Index>{ s };
auto second = std::string(semi + 1, s.end());
return std::tuple<Index, Index>{first, second};
} else {
return std::tuple<Index>{s};
}
}

/// converts the annotation of an expression to an Index
template<typename A, bool Alias>
template <typename A, bool Alias>
auto idx(const expressions::TsrExpr<A, Alias> &e) {
return idx<A>(e.annotation());
}
Expand Down
2 changes: 1 addition & 1 deletion src/TiledArray/util/singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Singleton {
/// @param args a parameter pack
template <typename... Args>
static void set_instance(Args&&... args) {
assert(instance_accessor() == nullptr);
TA_ASSERT(instance_accessor() == nullptr);
instance_accessor() = std::move(
std::unique_ptr<Derived>(new Derived(std::forward<Args>(args)...)));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/diagonal_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "unit_test_config.h"

auto gen_trange1(size_t N, const std::vector<size_t>& TA_NBs) {
assert(TA_NBs.size() > 0);
TA_ASSERT(TA_NBs.size() > 0);
static int seed = 0;

std::default_random_engine gen(seed++);
Expand Down
2 changes: 1 addition & 1 deletion tests/linalg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ struct LinearAlgebraFixture : ReferenceFixture {
};

TA::TiledRange gen_trange(size_t N, const std::vector<size_t>& TA_NBs) {
assert(TA_NBs.size() > 0);
TA_ASSERT(TA_NBs.size() > 0);

std::default_random_engine gen(0);
std::uniform_int_distribution<> dist(0, TA_NBs.size() - 1);
Expand Down