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 KeyObjectPair(
const std::string &key_in,
const ObjType &obj_in,
bool isActive_in =
true)
127 isActive_ = kop.isActive_;
140 template<
class ObjType>
179 template<
class ObjType>
188 typedef std::deque<key_and_obj_t> key_and_obj_array_t;
190 typedef std::map<std::string, OrdinalIndex> key_to_idx_map_t;
296 key_and_obj_array_t key_and_obj_array_;
298 key_to_idx_map_t key_to_idx_map_;
325 void assertOrdinalIndex(
const Ordinal idx)
const;
334 void throwInvalidKeyError(
const Ordinal idx,
const std::string &key)
const;
337 Ordinal assertKeyGetOrdinal(
const std::string &key)
const;
350 template<
class ObjType>
355 return ptrFromRef(getNonconstKeyAndObject(idx).second);
359 template<
class ObjType>
364 return ptrFromRef(getKeyAndObject(idx).second);
368 template<
class ObjType>
373 return getNonconstObjPtr(assertKeyGetOrdinal(key));
377 template<
class ObjType>
382 return getObjPtr(assertKeyGetOrdinal(key));
389 template<
class ObjType>
394 return Iterator(key_and_obj_array_.begin(), key_and_obj_array_.begin(),
395 key_and_obj_array_.end());
399 template<
class ObjType>
404 return Iterator(key_and_obj_array_.end(), key_and_obj_array_.begin(),
405 key_and_obj_array_.end());
409 template<
class ObjType>
414 return ConstIterator(key_and_obj_array_.begin(), key_and_obj_array_.begin(),
415 key_and_obj_array_.end());
419 template<
class ObjType>
424 return ConstIterator(key_and_obj_array_.end(), key_and_obj_array_.begin(),
425 key_and_obj_array_.end());
437 template<
class ObjType>
442 template<
class ObjType>
446 return key_to_idx_map_.size();
450 template<
class ObjType>
454 return key_and_obj_array_.size();
461 template<
class ObjType>
466 key_to_idx_map_t::const_iterator itr = key_to_idx_map_.find(key);
467 if (itr != key_to_idx_map_.end()) {
468 return itr->second.idx;
470 return getInvalidOrdinal();
474 template<
class ObjType>
479 typename key_to_idx_map_t::iterator obj_idx_itr = key_to_idx_map_.find(key);
480 if (obj_idx_itr != key_to_idx_map_.end()) {
482 const Ordinal obj_idx = obj_idx_itr->second.idx;
483 key_and_obj_array_[obj_idx].second = obj;
488 const Ordinal new_idx = key_and_obj_array_.size()-1;
489 key_to_idx_map_[key] = new_idx;
494 template<
class ObjType>
498 key_to_idx_map_.erase(key_and_obj.
first);
499 key_and_obj = key_and_obj_t::makeInvalid();
503 template<
class ObjType>
506 typename key_to_idx_map_t::iterator itr = key_to_idx_map_.find(key);
507 if (itr == key_to_idx_map_.end()) {
508 throwInvalidKeyError(getInvalidOrdinal(), key);
510 const Ordinal idx = itr->second.idx;
511 key_to_idx_map_.erase(itr);
512 key_and_obj_array_[idx] = key_and_obj_t::makeInvalid();
519 template<
class ObjType>
523 InvalidOrdinalIndexError,
524 "Error, the ordinal index " << idx <<
" is invalid"
525 <<
" because it falls outside of the range of valid objects"
526 <<
" [0,"<<numStorage()-1<<
"]!");
530 template<
class ObjType>
531 typename StringIndexedOrderedValueObjectContainer<ObjType>::key_and_obj_t&
532 StringIndexedOrderedValueObjectContainer<ObjType>::getNonconstKeyAndObject(
const Ordinal idx)
534 assertOrdinalIndex(idx);
535 key_and_obj_t &key_and_obj = key_and_obj_array_[idx];
537 InvalidOrdinalIndexError,
538 "Error, the ordinal index " << idx <<
" is invalid"
539 <<
" because the object has been deleted!");
544 template<
class ObjType>
545 const typename StringIndexedOrderedValueObjectContainer<ObjType>::key_and_obj_t&
546 StringIndexedOrderedValueObjectContainer<ObjType>::getKeyAndObject(
const Ordinal idx)
const
548 assertOrdinalIndex(idx);
549 const key_and_obj_t &key_and_obj = key_and_obj_array_[idx];
551 InvalidOrdinalIndexError,
552 "Error, the ordinal index " << idx <<
" is invalid"
553 <<
" because the object has been deleted!");
558 template<
class ObjType>
560 StringIndexedOrderedValueObjectContainer<ObjType>::throwInvalidKeyError(
561 const Ordinal idx,
const std::string &key)
const
564 "Error, the key '" << key <<
"' does not exist!");
568 template<
class ObjType>
569 typename StringIndexedOrderedValueObjectContainer<ObjType>::Ordinal
570 StringIndexedOrderedValueObjectContainer<ObjType>::assertKeyGetOrdinal(
const std::string &key)
const
572 const Ordinal idx = getObjOrdinalIndex(key);
573 throwInvalidKeyError(idx, key);
599 #endif // TEUCHOS_STRING_INDEXED_ORDERED_VALUE_OBJECT_CONTAINER_HPP
C++ Standard Library compatable filtered iterator.
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.
String indexed ordered value-type object container class.
A simple aggregate type to bind a key string and and objects value.
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.
StringIndexedOrderedValueObjectContainerBase::Ordinal Ordinal
Ordinal used for the index.
KeyObjectPair(const KeyObjectPair< ObjType > &kop)
ConstIterator end() const
Thrown if an invalid string is passed in.
KeyObjectPair(const std::string &key_in, const ObjType &obj_in, bool isActive_in=true)
FilteredIterator< typename key_and_obj_array_t::const_iterator, SelectActive< ObjType > > ConstIterator
The const iterator type.
static KeyObjectPair< ObjType > makeInvalid()
StringIndexedOrderedValueObjectContainer()
Ptr< const ObjType > getObjPtr(const Ordinal &idx) const
Get a const semi-persisting association with the stored object indexed by ordinal.
StringIndexedOrderedValueObjectContainerBase::Ordinal Ordinal
KeyObjectPair< ObjType > & operator=(const KeyObjectPair< ObjType > &kop)
Templated array class derived from the STL std::vector.
const std::string & first
Ordinal setObj(const std::string &key, const ObjType &obj)
Set (or reset) object by value and return its ordinal index.
virtual ~StringIndexedOrderedValueObjectContainerBase()
Destructor.
Ordinal numStorage() const
ConstIterator begin() const
Predicate for selecting active object entries in filtered iterator.
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.
static Ordinal getInvalidOrdinal()
Return the value for invalid ordinal.
Base types for StringIndexedOrderedValueObjectContainer.
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)