Shortcuts

Template Class OrderedDict

Nested Relationships

Class Documentation

template<typename Key, typename Value>
class torch::OrderedDict

An ordered dictionary implementation, akin to Python’s OrderedDict.

Public Types

using Iterator = typename std::vector<Item>::iterator
using ConstIterator = typename std::vector<Item>::const_iterator

Public Functions

OrderedDict(std::string key_description = "Key")

Constructs the OrderedDict with a short description of the kinds of keys stored in the OrderedDict.

This description is used in error messages thrown by the OrderedDict.

OrderedDict(const OrderedDict &other)

Copy constructs this OrderedDict from other.

OrderedDict &operator=(const OrderedDict &other)

Assigns items from other to this OrderedDict.

OrderedDict(OrderedDict &&other) = default
OrderedDict &operator=(OrderedDict &&other) = default
~OrderedDict() = default
OrderedDict(std::initializer_list<Item> initializer_list)

Constructs a new OrderedDict and pre-populates it with the given Items.

const std::string &key_description() const noexcept

Returns the key description string the OrderedDict was constructed with.

Item &front()

Returns the very first item in the OrderedDict and throws an exception if it is empty.

const Item &front() const

Returns the very first item in the OrderedDict and throws an exception if it is empty.

Item &back()

Returns the very last item in the OrderedDict and throws an exception if it is empty.

const Item &back() const

Returns the very last item in the OrderedDict and throws an exception if it is empty.

Item &operator[](size_t index)

Returns the item at the index-th position in the OrderedDict.

Throws an exception if the index is out of bounds.

const Item &operator[](size_t index) const

Returns the item at the index-th position in the OrderedDict.

Throws an exception if the index is out of bounds.

Value &operator[](const Key &key)

Returns the value associated with the given key.

Throws an exception if no such key is stored in the OrderedDict. Use find() for a non-throwing way of accessing a value if it is present.

const Value &operator[](const Key &key) const

Returns the value associated with the given key.

Throws an exception if no such key is stored in the OrderedDict. Use find() for a non-throwing way of accessing a value if it is present.

Value *find(const Key &key) noexcept

Returns a pointer to the value associated with the given key, or a nullptr if no such key is stored in the OrderedDict.

const Value *find(const Key &key) const noexcept

Returns a pointer to the value associated with the given key, or a nullptr if no such key is stored in the OrderedDict.

bool contains(const Key &key) const noexcept

Returns true if the key is present in the OrderedDict.

Iterator begin()

Returns an iterator to the first item in the OrderedDict.

Iteration is ordered.

ConstIterator begin() const

Returns an iterator to the first item in the OrderedDict.

Iteration is ordered.

Iterator end()

Returns an iterator one past the last item in the OrderedDict.

ConstIterator end() const

Returns an iterator one past the last item in the OrderedDict.

size_t size() const noexcept

Returns the number of items currently stored in the OrderedDict.

bool is_empty() const noexcept

Returns true if the OrderedDict contains no elements.

void reserve(size_t requested_capacity)

Resizes internal storage to fit at least requested_capacity items without requiring reallocation.

template<typename K, typename V>
Value &insert(K &&key, V &&value)

Inserts a new (key, value) pair into the OrderedDict.

Throws an exception if the key is already present. If insertion is successful, immediately returns a reference to the inserted value.

Value &insert(Key key, Value &&value)

Inserts a new (key, value) pair into the OrderedDict.

Throws an exception if the key is already present. If insertion is successful, immediately returns a reference to the inserted value.

void update(OrderedDict &&other)

Inserts all items from other into this OrderedDict.

If any key from other is already present in this OrderedDict, an exception is thrown.

void update(const OrderedDict &other)

Inserts all items from other into this OrderedDict.

If any key from other is already present in this OrderedDict, an exception is thrown.

void erase(const Key &key)

Removes the item that has key from this OrderedDict if exists and if it doesn’t an exception is thrown.

void clear()

Removes all items from this OrderedDict.

const std::vector<Item> &items() const noexcept

Returns the items stored in the OrderedDict.

::std::vector<Key> keys() const

Returns a newly allocated vector and copies all keys from this OrderedDict into the vector.

::std::vector<Value> values() const

Returns a newly allocated vector and copies all values from this OrderedDict into the vector.

::std::vector<std::pair<Key, Value>> pairs() const

Returns a newly allocated vector and copies all keys and values from this OrderedDict into a vector of std::pair<Key, Value>.

Friends

template<typename K, typename V>
friend bool operator==(const OrderedDict<K, V> &a, const OrderedDict<K, V> &b)

Returns true if both dicts contain the same keys and values, in the same order.

class Item

Public Functions

Item(Key key, Value value)

Constructs a new item.

Value &operator*()

Returns a reference to the value.

const Value &operator*() const

Returns a reference to the value.

Value *operator->()

Allows access to the value using the arrow operator.

const Value *operator->() const

Allows access to the value using the arrow operator.

const Key &key() const noexcept

Returns a reference to the key.

Value &value() noexcept

Returns a reference to the value.

const Value &value() const noexcept

Returns a reference to the value.

const std::pair<Key, Value> &pair() const noexcept

Returns a (key, value) pair.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources