Philote-Cpp
C++ bindings for the Philote MDO standard
Loading...
Searching...
No Matches
philote::PairDict< T > Class Template Reference

Dictionary for storing values with respect to two keys. More...

#include <variable.h>

Public Member Functions

 PairDict ()=default
 Default constructor.
 
 PairDict (const PairDict &)=default
 Copy constructor.
 
PairDictoperator= (const PairDict &)=default
 Copy assignment operator.
 
 PairDict (PairDict &&) noexcept=default
 Move constructor.
 
PairDictoperator= (PairDict &&) noexcept=default
 Move assignment operator.
 
 ~PairDict () noexcept=default
 Destructor.
 
T & operator() (const std::string &key1, const std::string &key2)
 Access/modify value using two keys.
 
const T & operator() (const std::string &key1, const std::string &key2) const
 Access value using two keys (const version)
 
bool contains (const std::string &key1, const std::string &key2) const noexcept
 Check if a key pair exists.
 
size_t size () const noexcept
 Get the size of the dictionary.
 
bool empty () const noexcept
 Check if the dictionary is empty.
 
void clear ()
 Clear all entries.
 
auto begin ()
 Get iterator to beginning.
 
auto begin () const
 
auto end ()
 Get iterator to end.
 
auto end () const
 

Detailed Description

template<typename T>
class philote::PairDict< T >

Dictionary for storing values with respect to two keys.

Provides a cleaner interface for accessing values using two string keys, similar to Philote-Python's PairDict class. This is particularly useful for storing Jacobian/partial derivative values.

Template Parameters
TThe value type to store
Example: Using PairDict for Gradients
// Create gradient variables
gradients("f", "x") = philote::Variable(philote::kOutput, {1});
gradients("f", "y") = philote::Variable(philote::kOutput, {1});
// Set gradient values
gradients("f", "x")(0) = 2.5;
gradients("f", "y")(0) = 3.7;
// Check if a gradient exists
if (gradients.contains("f", "x")) {
double df_dx = gradients("f", "x")(0);
}
Dictionary for storing values with respect to two keys.
Definition variable.h:292
bool contains(const std::string &key1, const std::string &key2) const noexcept
Check if a key pair exists.
Definition variable.h:355
A class for storing continuous and discrete variables.
Definition variable.h:85
Example: Traditional Partials vs PairDict
// Traditional approach with std::map
partials[std::make_pair("output", "input")](0) = 1.5;
// Cleaner PairDict approach
pair_partials("output", "input")(0) = 1.5;
PairDict< philote::Variable > PartialsPairDict
Definition variable.h:406
std::map< std::pair< std::string, std::string >, philote::Variable > Partials
Definition variable.h:405
Note
Thread Safety: This class is NOT thread-safe. The underlying std::map does not provide thread safety for concurrent modifications. If multiple threads need to access the same PairDict, external synchronization is required.

Constructor & Destructor Documentation

◆ PairDict() [1/3]

template<typename T >
philote::PairDict< T >::PairDict ( )
default

Default constructor.

◆ PairDict() [2/3]

template<typename T >
philote::PairDict< T >::PairDict ( const PairDict< T > &  )
default

Copy constructor.

◆ PairDict() [3/3]

template<typename T >
philote::PairDict< T >::PairDict ( PairDict< T > &&  )
defaultnoexcept

Move constructor.

◆ ~PairDict()

template<typename T >
philote::PairDict< T >::~PairDict ( )
defaultnoexcept

Destructor.

Member Function Documentation

◆ begin() [1/2]

template<typename T >
auto philote::PairDict< T >::begin ( )
inline

Get iterator to beginning.

◆ begin() [2/2]

template<typename T >
auto philote::PairDict< T >::begin ( ) const
inline

◆ clear()

template<typename T >
void philote::PairDict< T >::clear ( )
inline

Clear all entries.

◆ contains()

template<typename T >
bool philote::PairDict< T >::contains ( const std::string &  key1,
const std::string &  key2 
) const
inlinenoexcept

Check if a key pair exists.

Parameters
key1First key
key2Second key
Returns
true if the key pair exists

◆ empty()

template<typename T >
bool philote::PairDict< T >::empty ( ) const
inlinenoexcept

Check if the dictionary is empty.

Returns
true if empty

◆ end() [1/2]

template<typename T >
auto philote::PairDict< T >::end ( )
inline

Get iterator to end.

◆ end() [2/2]

template<typename T >
auto philote::PairDict< T >::end ( ) const
inline

◆ operator()() [1/2]

template<typename T >
T & philote::PairDict< T >::operator() ( const std::string &  key1,
const std::string &  key2 
)
inline

Access/modify value using two keys.

Parameters
key1First key
key2Second key
Returns
Reference to the value

◆ operator()() [2/2]

template<typename T >
const T & philote::PairDict< T >::operator() ( const std::string &  key1,
const std::string &  key2 
) const
inline

Access value using two keys (const version)

Parameters
key1First key
key2Second key
Returns
Const reference to the value

◆ operator=() [1/2]

template<typename T >
PairDict & philote::PairDict< T >::operator= ( const PairDict< T > &  )
default

Copy assignment operator.

◆ operator=() [2/2]

template<typename T >
PairDict & philote::PairDict< T >::operator= ( PairDict< T > &&  )
defaultnoexcept

Move assignment operator.

◆ size()

template<typename T >
size_t philote::PairDict< T >::size ( ) const
inlinenoexcept

Get the size of the dictionary.

Returns
Number of key-value pairs

The documentation for this class was generated from the following file: