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" 103 template<
class ObjType>
113 KeyObjectPair() : first(key), second(ObjType()), key(
""), isActive_(true) {}
115 KeyObjectPair(
const std::string &key_in,
const ObjType &obj_in,
bool isActive_in =
true)
116 : first(key), second(obj_in), key(key_in), isActive_(isActive_in) {}
119 : first(key), second(kop.second), key(kop.key), isActive_(kop.isActive_) {}
125 isActive_ = kop.isActive_;
138 template<
class ObjType>
177 template<
class ObjType>
186 typedef std::deque<key_and_obj_t> key_and_obj_array_t;
188 typedef std::map<std::string, OrdinalIndex> key_to_idx_map_t;
215 Ordinal numObjects()
const;
218 Ordinal numStorage()
const;
233 Ordinal setObj(
const std::string &key,
const ObjType &obj);
239 inline Ordinal getObjOrdinalIndex(
const std::string &key)
const;
254 inline Ptr<ObjType> getNonconstObjPtr(
const std::string &key);
267 void removeObj(
const Ordinal &
idx);
270 void removeObj(
const std::string &key);
294 key_and_obj_array_t key_and_obj_array_;
296 key_to_idx_map_t key_to_idx_map_;
323 void assertOrdinalIndex(
const Ordinal
idx)
const;
326 key_and_obj_t& getNonconstKeyAndObject(
const Ordinal
idx);
329 const key_and_obj_t& getKeyAndObject(
const Ordinal
idx)
const;
332 void throwInvalidKeyError(
const Ordinal
idx,
const std::string &key)
const;
335 Ordinal assertKeyGetOrdinal(
const std::string &key)
const;
348 template<
class ObjType>
353 return ptrFromRef(getNonconstKeyAndObject(idx).second);
357 template<
class ObjType>
362 return ptrFromRef(getKeyAndObject(idx).second);
366 template<
class ObjType>
371 return getNonconstObjPtr(assertKeyGetOrdinal(key));
375 template<
class ObjType>
380 return getObjPtr(assertKeyGetOrdinal(key));
387 template<
class ObjType>
392 return Iterator(key_and_obj_array_.begin(), key_and_obj_array_.begin(),
393 key_and_obj_array_.end());
397 template<
class ObjType>
402 return Iterator(key_and_obj_array_.end(), key_and_obj_array_.begin(),
403 key_and_obj_array_.end());
407 template<
class ObjType>
412 return ConstIterator(key_and_obj_array_.begin(), key_and_obj_array_.begin(),
413 key_and_obj_array_.end());
417 template<
class ObjType>
422 return ConstIterator(key_and_obj_array_.end(), key_and_obj_array_.begin(),
423 key_and_obj_array_.end());
435 template<
class ObjType>
440 template<
class ObjType>
444 return key_to_idx_map_.size();
448 template<
class ObjType>
452 return key_and_obj_array_.size();
459 template<
class ObjType>
464 key_to_idx_map_t::const_iterator itr = key_to_idx_map_.find(key);
465 if (itr != key_to_idx_map_.end()) {
466 return itr->second.idx;
472 template<
class ObjType>
477 typename key_to_idx_map_t::iterator obj_idx_itr = key_to_idx_map_.find(key);
478 if (obj_idx_itr != key_to_idx_map_.end()) {
480 const Ordinal obj_idx = obj_idx_itr->second.idx;
481 key_and_obj_array_[obj_idx].second = obj;
486 const Ordinal new_idx = key_and_obj_array_.size()-1;
487 key_to_idx_map_[key] = new_idx;
492 template<
class ObjType>
496 key_to_idx_map_.erase(key_and_obj.
first);
497 key_and_obj = key_and_obj_t::makeInvalid();
501 template<
class ObjType>
504 typename key_to_idx_map_t::iterator itr = key_to_idx_map_.find(key);
505 if (itr == key_to_idx_map_.end()) {
509 key_to_idx_map_.erase(itr);
510 key_and_obj_array_[
idx] = key_and_obj_t::makeInvalid();
517 template<
class ObjType>
522 "Error, the ordinal index " << idx <<
" is invalid" 523 <<
" because it falls outside of the range of valid objects" 524 <<
" [0,"<<numStorage()-1<<
"]!");
528 template<
class ObjType>
532 assertOrdinalIndex(idx);
536 "Error, the ordinal index " << idx <<
" is invalid" 537 <<
" because the object has been deleted!");
542 template<
class ObjType>
546 assertOrdinalIndex(idx);
550 "Error, the ordinal index " << idx <<
" is invalid" 551 <<
" because the object has been deleted!");
556 template<
class ObjType>
559 const Ordinal idx,
const std::string &key)
const 562 "Error, the key '" << key <<
"' does not exist!");
566 template<
class ObjType>
570 const Ordinal idx = getObjOrdinalIndex(key);
571 throwInvalidKeyError(idx, key);
597 #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
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
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)