11 #include <unordered_map>
12 #include <unordered_set>
31 class TensorMap :
public std::unordered_map<std::string, core::Tensor> {
35 :
std::unordered_map<
std::string, core::Tensor>(),
36 primary_key_(primary_key) {
37 AssertPrimaryKeyInMapOrEmpty();
38 AssertNoReservedKeys();
48 template <
class InputIt>
49 TensorMap(
const std::string& primary_key, InputIt first, InputIt last)
50 :
std::unordered_map<
std::string, core::Tensor>(first, last),
51 primary_key_(primary_key) {
52 AssertPrimaryKeyInMapOrEmpty();
53 AssertNoReservedKeys();
57 const std::unordered_map<std::string, core::Tensor>& tensor_map)
58 :
TensorMap(primary_key, tensor_map.begin(), tensor_map.end()) {
59 AssertPrimaryKeyInMapOrEmpty();
60 AssertNoReservedKeys();
64 std::initializer_list<value_type> init)
65 :
std::unordered_map<
std::string, core::Tensor>(init),
66 primary_key_(primary_key) {
67 AssertPrimaryKeyInMapOrEmpty();
68 AssertNoReservedKeys();
73 :
std::unordered_map<
std::string, core::Tensor>(other),
74 primary_key_(other.primary_key_) {
75 AssertPrimaryKeyInMapOrEmpty();
76 AssertNoReservedKeys();
81 :
std::unordered_map<
std::string, core::Tensor>(other),
82 primary_key_(other.primary_key_) {
83 AssertPrimaryKeyInMapOrEmpty();
84 AssertNoReservedKeys();
92 std::size_t
Erase(
const std::string key) {
93 if (key == primary_key_) {
99 return this->erase(key);
102 std::pair<iterator, bool>
insert(
const value_type& value) {
106 return std::unordered_map<std::string, core::Tensor>::insert(value);
110 std::pair<iterator, bool>
insert(P&& value) {
114 return std::unordered_map<std::string, core::Tensor>::insert(
115 std::forward<P>(value));
118 iterator
insert(const_iterator hint,
const value_type& value) {
122 return std::unordered_map<std::string, core::Tensor>::insert(hint,
127 iterator
insert(const_iterator hint, P&& value) {
131 return std::unordered_map<std::string, core::Tensor>::insert(
132 hint, std::forward<P>(value));
135 template <
class InputIt>
136 void insert(InputIt first, InputIt last) {
137 for (
auto it = first; it != last; ++it) {
142 std::unordered_map<std::string, core::Tensor>::insert(first, last);
145 void insert(std::initializer_list<value_type> ilist) {
146 for (
auto it = ilist.begin(); it != ilist.end(); ++it) {
151 std::unordered_map<std::string, core::Tensor>::insert(ilist);
163 std::unordered_set<std::string> keys;
164 for (
const auto& item : *
this) {
165 keys.insert(item.first);
198 void AssertPrimaryKeyInMapOrEmpty()
const;
202 void AssertNoReservedKeys()
const;
205 int64_t GetPrimarySize()
const {
return at(primary_key_).GetLength(); }
209 return at(primary_key_).GetDevice();
213 std::string primary_key_;
TensorMap(const std::string &primary_key, std::initializer_list< value_type > init)
TensorMap & operator=(const TensorMap &)=default
bool IsSizeSynchronized() const
Returns true if all tensors in the map have the same size.
std::string ToString() const
Print the TensorMap to string.
void AssertSizeSynchronized() const
Assert IsSizeSynchronized().
TensorMap & operator=(TensorMap &&)=default
TensorMap(TensorMap &&other)
Move constructor performs a "shallow" copy of the Tensors.
void insert(std::initializer_list< value_type > ilist)
TensorMap(const std::string &primary_key, const std::unordered_map< std::string, core::Tensor > &tensor_map)
static std::unordered_set< std::string > GetReservedKeys()
Get reserved keys for the map. A map cannot contain any of these keys.
std::pair< iterator, bool > insert(P &&value)
std::string GetPrimaryKey() const
Returns the primary key of the TensorMap.
std::size_t Erase(const std::string key)
Erase elements for the TensorMap by key value, if the key exists. If the key does not exists,...
TensorMap(const std::string &primary_key)
Create empty TensorMap and set primary key.
std::pair< iterator, bool > insert(const value_type &value)
void insert(InputIt first, InputIt last)
TensorMap(const std::string &primary_key, InputIt first, InputIt last)
iterator insert(const_iterator hint, const value_type &value)
bool IsContiguous() const
TensorMap Contiguous() const
std::unordered_set< std::string > GetKeySet() const
Returns a set with all keys.
iterator insert(const_iterator hint, P &&value)
TensorMap(const TensorMap &other)
Copy constructor performs a "shallow" copy of the Tensors.
bool Contains(const std::string &key) const
Generic file read and write utility for python interface.