10#ifndef TEUCHOS_STRING_INDEXED_ORDERED_VALUE_OBJECT_CONTAINER_HPP
11#define TEUCHOS_STRING_INDEXED_ORDERED_VALUE_OBJECT_CONTAINER_HPP
15#include "Teuchos_FilteredIterator.hpp"
72 template<
class ObjType>
84 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, ObjType>>>
85 KeyObjectPair(
const std::string &key_in, U&& obj_in,
bool isActive_in =
true)
86 :
first(
key),
second(std::forward<U>(obj_in)),
key(key_in), isActive_(isActive_in) {}
98 isActive_ = kop.isActive_;
111 template<
class ObjType>
119 class InvalidOrdinalIndexError :
public ExceptionBase
120 {
public:InvalidOrdinalIndexError(
const std::string& what_arg) : ExceptionBase(what_arg) {}};
123 class InvalidKeyError :
public ExceptionBase
124 {
public:InvalidKeyError(
const std::string& what_arg) : ExceptionBase(what_arg) {}};
150template<
class ObjType>
159 typedef std::deque<key_and_obj_t> key_and_obj_array_t;
161 typedef std::map<std::string, OrdinalIndex> key_to_idx_map_t;
206 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, ObjType>>>
268 key_and_obj_array_t key_and_obj_array_;
270 key_to_idx_map_t key_to_idx_map_;
297 void assertOrdinalIndex(
const Ordinal idx)
const;
300 key_and_obj_t& getNonconstKeyAndObject(
const Ordinal idx);
303 const key_and_obj_t& getKeyAndObject(
const Ordinal idx)
const;
306 void throwInvalidKeyError(
const Ordinal idx,
const std::string &key)
const;
309 Ordinal assertKeyGetOrdinal(
const std::string &key)
const;
322template<
class ObjType>
327 return ptrFromRef(getNonconstKeyAndObject(idx).second);
331template<
class ObjType>
336 return ptrFromRef(getKeyAndObject(idx).second);
340template<
class ObjType>
349template<
class ObjType>
354 return getObjPtr(assertKeyGetOrdinal(key));
361template<
class ObjType>
366 return Iterator(key_and_obj_array_.begin(), key_and_obj_array_.begin(),
367 key_and_obj_array_.end());
371template<
class ObjType>
376 return Iterator(key_and_obj_array_.end(), key_and_obj_array_.begin(),
377 key_and_obj_array_.end());
381template<
class ObjType>
386 return ConstIterator(key_and_obj_array_.begin(), key_and_obj_array_.begin(),
387 key_and_obj_array_.end());
391template<
class ObjType>
396 return ConstIterator(key_and_obj_array_.end(), key_and_obj_array_.begin(),
397 key_and_obj_array_.end());
409template<
class ObjType>
414template<
class ObjType>
418 return key_to_idx_map_.size();
422template<
class ObjType>
426 return key_and_obj_array_.size();
433template<
class ObjType>
438 key_to_idx_map_t::const_iterator itr = key_to_idx_map_.find(key);
439 if (itr != key_to_idx_map_.end()) {
440 return itr->second.idx;
446template <
typename ObjType>
447template <
typename U,
typename>
452 typename key_to_idx_map_t::iterator obj_idx_itr = key_to_idx_map_.find(key);
453 if (obj_idx_itr != key_to_idx_map_.end()) {
455 const Ordinal obj_idx = obj_idx_itr->second.idx;
456 key_and_obj_array_[obj_idx].second = std::forward<U>(obj);
460 key_and_obj_array_.emplace_back(key, std::forward<U>(obj));
461 const Ordinal new_idx = key_and_obj_array_.size()-1;
462 key_to_idx_map_[key] = new_idx;
467template<
class ObjType>
470 key_and_obj_t &key_and_obj = getNonconstKeyAndObject(idx);
471 key_to_idx_map_.erase(key_and_obj.
first);
476template<
class ObjType>
479 typename key_to_idx_map_t::iterator itr = key_to_idx_map_.find(key);
480 if (itr == key_to_idx_map_.end()) {
483 const Ordinal idx = itr->second.idx;
484 key_to_idx_map_.erase(itr);
492template<
class ObjType>
493void StringIndexedOrderedValueObjectContainer<ObjType>::assertOrdinalIndex(
const Ordinal idx)
const
496 InvalidOrdinalIndexError,
497 "Error, the ordinal index " << idx <<
" is invalid"
498 <<
" because it falls outside of the range of valid objects"
499 <<
" [0,"<<numStorage()-1<<
"]!");
503template<
class ObjType>
504typename StringIndexedOrderedValueObjectContainer<ObjType>::key_and_obj_t&
505StringIndexedOrderedValueObjectContainer<ObjType>::getNonconstKeyAndObject(
const Ordinal idx)
507 assertOrdinalIndex(idx);
508 key_and_obj_t &key_and_obj = key_and_obj_array_[idx];
510 InvalidOrdinalIndexError,
511 "Error, the ordinal index " << idx <<
" is invalid"
512 <<
" because the object has been deleted!");
517template<
class ObjType>
518const typename StringIndexedOrderedValueObjectContainer<ObjType>::key_and_obj_t&
519StringIndexedOrderedValueObjectContainer<ObjType>::getKeyAndObject(
const Ordinal idx)
const
521 assertOrdinalIndex(idx);
522 const key_and_obj_t &key_and_obj = key_and_obj_array_[idx];
524 InvalidOrdinalIndexError,
525 "Error, the ordinal index " << idx <<
" is invalid"
526 <<
" because the object has been deleted!");
531template<
class ObjType>
533StringIndexedOrderedValueObjectContainer<ObjType>::throwInvalidKeyError(
534 const Ordinal idx,
const std::string &key)
const
537 "Error, the key '" << key <<
"' does not exist!");
541template<
class ObjType>
543StringIndexedOrderedValueObjectContainer<ObjType>::assertKeyGetOrdinal(
const std::string &key)
const
545 const Ordinal idx = getObjOrdinalIndex(key);
546 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,...