43 #ifndef TEUCHOS_STRING_INDEXED_ORDERED_VALUE_OBJECT_CONTAINER_HPP
44 #define TEUCHOS_STRING_INDEXED_ORDERED_VALUE_OBJECT_CONTAINER_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)
144 template<
class ObjType>
183 template<
class ObjType>
239 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, ObjType>>>
355 template<
class ObjType>
360 return ptrFromRef(getNonconstKeyAndObject(idx).second);
364 template<
class ObjType>
369 return ptrFromRef(getKeyAndObject(idx).second);
373 template<
class ObjType>
378 return getNonconstObjPtr(assertKeyGetOrdinal(key));
382 template<
class ObjType>
387 return getObjPtr(assertKeyGetOrdinal(key));
394 template<
class ObjType>
399 return Iterator(key_and_obj_array_.begin(), key_and_obj_array_.begin(),
400 key_and_obj_array_.end());
404 template<
class ObjType>
409 return Iterator(key_and_obj_array_.end(), key_and_obj_array_.begin(),
410 key_and_obj_array_.end());
414 template<
class ObjType>
419 return ConstIterator(key_and_obj_array_.begin(), key_and_obj_array_.begin(),
420 key_and_obj_array_.end());
424 template<
class ObjType>
429 return ConstIterator(key_and_obj_array_.end(), key_and_obj_array_.begin(),
430 key_and_obj_array_.end());
442 template<
class ObjType>
447 template<
class ObjType>
451 return key_to_idx_map_.size();
455 template<
class ObjType>
459 return key_and_obj_array_.size();
466 template<
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;
475 return getInvalidOrdinal();
479 template <
typename ObjType>
480 template <
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;
500 template<
class ObjType>
504 key_to_idx_map_.erase(key_and_obj.
first);
505 key_and_obj = key_and_obj_t::makeInvalid();
509 template<
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()) {
514 throwInvalidKeyError(getInvalidOrdinal(), key);
516 const Ordinal idx = itr->second.idx;
517 key_to_idx_map_.erase(itr);
518 key_and_obj_array_[idx] = key_and_obj_t::makeInvalid();
525 template<
class ObjType>
530 "Error, the ordinal index " << idx <<
" is invalid"
531 <<
" because it falls outside of the range of valid objects"
532 <<
" [0,"<<numStorage()-1<<
"]!");
536 template<
class ObjType>
540 assertOrdinalIndex(idx);
544 "Error, the ordinal index " << idx <<
" is invalid"
545 <<
" because the object has been deleted!");
550 template<
class ObjType>
554 assertOrdinalIndex(idx);
558 "Error, the ordinal index " << idx <<
" is invalid"
559 <<
" because the object has been deleted!");
564 template<
class ObjType>
567 const Ordinal idx,
const std::string &key)
const
570 "Error, the key '" << key <<
"' does not exist!");
574 template<
class ObjType>
578 const Ordinal idx = getObjOrdinalIndex(key);
579 throwInvalidKeyError(idx, key);
605 #endif // TEUCHOS_STRING_INDEXED_ORDERED_VALUE_OBJECT_CONTAINER_HPP
std::map< std::string, OrdinalIndex > key_to_idx_map_t
C++ Standard Library compatable filtered iterator.
KeyObjectPair(const std::string &key_in, U &&obj_in, bool isActive_in=true)
Ordinal getObjOrdinalIndex(const std::string &key) const
Get the ordinal index given the string key.
FilteredIterator< typename key_and_obj_array_t::iterator, SelectActive< ObjType > > Iterator
The non-const iterator type.
key_and_obj_array_t key_and_obj_array_
Stories objects contiguously along with key strings.
String indexed ordered value-type object container class.
A simple aggregate type to bind a key string and and objects value.
std::deque< key_and_obj_t > key_and_obj_array_t
A safe ordinal index type that default initializes to a special value.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
InvalidOrdinalIndexError(const std::string &what_arg)
Ordinal setObj(const std::string &key, U &&obj)
Set (or reset) object by value and return its ordinal index.
StringIndexedOrderedValueObjectContainerBase::Ordinal Ordinal
Ordinal used for the index.
Ordinal assertKeyGetOrdinal(const std::string &key) const
InvalidKeyError(const std::string &what_arg)
void assertOrdinalIndex(const Ordinal idx) const
ConstIterator end() const
Thrown if an invalid string is passed in.
TEUCHOS_ORDINAL_TYPE Teuchos_Ordinal
KeyObjectPair(KeyObjectPair &&kop)
Move-constructor.
ExceptionBase(const std::string &what_arg)
FilteredIterator< typename key_and_obj_array_t::const_iterator, SelectActive< ObjType > > ConstIterator
The const iterator type.
static KeyObjectPair< ObjType > makeInvalid()
KeyObjectPair & operator=(const KeyObjectPair &kop)
Copy-assignment operator.
bool operator()(const KeyObjectPair< ObjType > &key_and_obj) const
StringIndexedOrderedValueObjectContainer()
KeyObjectPair(const KeyObjectPair &kop)
Copy-constructor.
Ptr< const ObjType > getObjPtr(const Ordinal &idx) const
Get a const semi-persisting association with the stored object indexed by ordinal.
StringIndexedOrderedValueObjectContainerBase::Ordinal Ordinal
Templated array class derived from the STL std::vector.
const std::string & first
virtual ~StringIndexedOrderedValueObjectContainerBase()
Destructor.
Ordinal numStorage() const
ConstIterator begin() const
key_to_idx_map_t key_to_idx_map_
Provides lookups of key -> ordinal index into above array.
Predicate for selecting active object entries in filtered iterator.
const key_and_obj_t & getKeyAndObject(const Ordinal idx) const
key_and_obj_t & getNonconstKeyAndObject(const Ordinal idx)
void removeObj(const Ordinal &idx)
Remove an object given its ordinal index.
Base exception class for Teuchos.
Ordinal numObjects() const
Ptr< ObjType > getNonconstObjPtr(const Ordinal &idx)
Get a nonconst semi-persisting association with the stored object indexed by ordinal.
Teuchos_Ordinal Ordinal
Ordinal used for the index.
void throwInvalidKeyError(const Ordinal idx, const std::string &key) const
static Ordinal getInvalidOrdinal()
Return the value for invalid ordinal.
Base types for StringIndexedOrderedValueObjectContainer.
KeyObjectPair< ObjType > key_and_obj_t
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
Thrown if an invalid ordinal index is passed in.
OrdinalIndex(const Ordinal idx_in)