43#ifndef TEUCHOS_STRING_INDEXED_ORDERED_VALUE_OBJECT_CONTAINER_HPP
44#define TEUCHOS_STRING_INDEXED_ORDERED_VALUE_OBJECT_CONTAINER_HPP
48#include "Teuchos_FilteredIterator.hpp"
105 template<
class ObjType>
117 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, ObjType>>>
118 KeyObjectPair(
const std::string &key_in, U&& obj_in,
bool isActive_in =
true)
119 :
first(
key),
second(std::forward<U>(obj_in)),
key(key_in), isActive_(isActive_in) {}
131 isActive_ = kop.isActive_;
144 template<
class ObjType>
152 class InvalidOrdinalIndexError :
public ExceptionBase
153 {
public:InvalidOrdinalIndexError(
const std::string& what_arg) : ExceptionBase(what_arg) {}};
156 class InvalidKeyError :
public ExceptionBase
157 {
public:InvalidKeyError(
const std::string& what_arg) : ExceptionBase(what_arg) {}};
183template<
class ObjType>
192 typedef std::deque<key_and_obj_t> key_and_obj_array_t;
194 typedef std::map<std::string, OrdinalIndex> key_to_idx_map_t;
239 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, ObjType>>>
301 key_and_obj_array_t key_and_obj_array_;
303 key_to_idx_map_t key_to_idx_map_;
330 void assertOrdinalIndex(
const Ordinal idx)
const;
333 key_and_obj_t& getNonconstKeyAndObject(
const Ordinal idx);
336 const key_and_obj_t& getKeyAndObject(
const Ordinal idx)
const;
339 void throwInvalidKeyError(
const Ordinal idx,
const std::string &key)
const;
342 Ordinal assertKeyGetOrdinal(
const std::string &key)
const;
355template<
class ObjType>
360 return ptrFromRef(getNonconstKeyAndObject(idx).second);
364template<
class ObjType>
369 return ptrFromRef(getKeyAndObject(idx).second);
373template<
class ObjType>
382template<
class ObjType>
387 return getObjPtr(assertKeyGetOrdinal(key));
394template<
class ObjType>
399 return Iterator(key_and_obj_array_.begin(), key_and_obj_array_.begin(),
400 key_and_obj_array_.end());
404template<
class ObjType>
409 return Iterator(key_and_obj_array_.end(), key_and_obj_array_.begin(),
410 key_and_obj_array_.end());
414template<
class ObjType>
419 return ConstIterator(key_and_obj_array_.begin(), key_and_obj_array_.begin(),
420 key_and_obj_array_.end());
424template<
class ObjType>
429 return ConstIterator(key_and_obj_array_.end(), key_and_obj_array_.begin(),
430 key_and_obj_array_.end());
442template<
class ObjType>
447template<
class ObjType>
451 return key_to_idx_map_.size();
455template<
class ObjType>
459 return key_and_obj_array_.size();
466template<
class ObjType>
471 key_to_idx_map_t::const_iterator itr = key_to_idx_map_.find(key);
472 if (itr != key_to_idx_map_.end()) {
473 return itr->second.idx;
479template <
typename ObjType>
480template <
typename U,
typename>
485 typename key_to_idx_map_t::iterator obj_idx_itr = key_to_idx_map_.find(key);
486 if (obj_idx_itr != key_to_idx_map_.end()) {
488 const Ordinal obj_idx = obj_idx_itr->second.idx;
489 key_and_obj_array_[obj_idx].second = std::forward<U>(obj);
493 key_and_obj_array_.emplace_back(key, std::forward<U>(obj));
494 const Ordinal new_idx = key_and_obj_array_.size()-1;
495 key_to_idx_map_[key] = new_idx;
500template<
class ObjType>
503 key_and_obj_t &key_and_obj = getNonconstKeyAndObject(idx);
504 key_to_idx_map_.erase(key_and_obj.
first);
509template<
class ObjType>
512 typename key_to_idx_map_t::iterator itr = key_to_idx_map_.find(key);
513 if (itr == key_to_idx_map_.end()) {
516 const Ordinal idx = itr->second.idx;
517 key_to_idx_map_.erase(itr);
525template<
class ObjType>
526void StringIndexedOrderedValueObjectContainer<ObjType>::assertOrdinalIndex(
const Ordinal idx)
const
529 InvalidOrdinalIndexError,
530 "Error, the ordinal index " << idx <<
" is invalid"
531 <<
" because it falls outside of the range of valid objects"
532 <<
" [0,"<<numStorage()-1<<
"]!");
536template<
class ObjType>
537typename StringIndexedOrderedValueObjectContainer<ObjType>::key_and_obj_t&
538StringIndexedOrderedValueObjectContainer<ObjType>::getNonconstKeyAndObject(
const Ordinal idx)
540 assertOrdinalIndex(idx);
541 key_and_obj_t &key_and_obj = key_and_obj_array_[idx];
543 InvalidOrdinalIndexError,
544 "Error, the ordinal index " << idx <<
" is invalid"
545 <<
" because the object has been deleted!");
550template<
class ObjType>
551const typename StringIndexedOrderedValueObjectContainer<ObjType>::key_and_obj_t&
552StringIndexedOrderedValueObjectContainer<ObjType>::getKeyAndObject(
const Ordinal idx)
const
554 assertOrdinalIndex(idx);
555 const key_and_obj_t &key_and_obj = key_and_obj_array_[idx];
557 InvalidOrdinalIndexError,
558 "Error, the ordinal index " << idx <<
" is invalid"
559 <<
" because the object has been deleted!");
564template<
class ObjType>
566StringIndexedOrderedValueObjectContainer<ObjType>::throwInvalidKeyError(
567 const Ordinal idx,
const std::string &key)
const
570 "Error, the key '" << key <<
"' does not exist!");
574template<
class ObjType>
576StringIndexedOrderedValueObjectContainer<ObjType>::assertKeyGetOrdinal(
const std::string &key)
const
578 const Ordinal idx = getObjOrdinalIndex(key);
579 throwInvalidKeyError(idx, key);
Templated array class derived from the STL std::vector.
C++ Standard Library compatable filtered iterator.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists.
Ptr< T > ptrFromRef(T &arg)
Create a pointer to a object from an object reference.
A simple aggregate type to bind a key string and and objects value.
KeyObjectPair(const KeyObjectPair &kop)
Copy-constructor.
const std::string & first
KeyObjectPair & operator=(const KeyObjectPair &kop)
Copy-assignment operator.
KeyObjectPair(KeyObjectPair &&kop)
Move-constructor.
KeyObjectPair(const std::string &key_in, U &&obj_in, bool isActive_in=true)
static KeyObjectPair< ObjType > makeInvalid()
StringIndexedOrderedValueObjectContainerBase::Ordinal Ordinal
OrdinalIndex(const Ordinal idx_in)
Predicate for selecting active object entries in filtered iterator.
Base types for StringIndexedOrderedValueObjectContainer.
virtual ~StringIndexedOrderedValueObjectContainerBase()
Destructor.
static Ordinal getInvalidOrdinal()
Return the value for invalid ordinal.
Teuchos_Ordinal Ordinal
Ordinal used for the index.
ConstIterator end() const
Ptr< const ObjType > getObjPtr(const std::string &key) const
Get a const semi-persisting association with the stored object indexed by string key.
Ordinal setObj(const std::string &key, U &&obj)
Set (or reset) object by value and return its ordinal index.
Ordinal getObjOrdinalIndex(const std::string &key) const
Get the ordinal index given the string key.
FilteredIterator< typename key_and_obj_array_t::const_iterator, SelectActive< ObjType > > ConstIterator
The const iterator type.
Ordinal numObjects() const
FilteredIterator< typename key_and_obj_array_t::iterator, SelectActive< ObjType > > Iterator
The non-const iterator type.
void removeObj(const Ordinal &idx)
Remove an object given its ordinal index.
Ordinal numStorage() const
Ptr< ObjType > getNonconstObjPtr(const std::string &key)
Get a nonconst semi-persisting association with the stored object indexed by string key.
ConstIterator begin() const
Ptr< ObjType > getNonconstObjPtr(const Ordinal &idx)
Get a nonconst semi-persisting association with the stored object indexed by ordinal.
StringIndexedOrderedValueObjectContainerBase::Ordinal Ordinal
Ordinal used for the index.
Ptr< const ObjType > getObjPtr(const Ordinal &idx) const
Get a const semi-persisting association with the stored object indexed by ordinal.
void removeObj(const std::string &key)
Remove an object given its string key.
StringIndexedOrderedValueObjectContainer()
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...