29 #ifndef CDPL_UTIL_MAP_HPP
30 #define CDPL_UTIL_MAP_HPP
53 template <
typename ValueType,
bool Allow = true>
61 static const ValueType&
get()
72 template <
typename ValueType,
bool Allow>
79 template <
typename ValueType>
88 static const ValueType&
get()
114 template <
typename Key,
typename Value,
bool AllowDefValues =
false,
115 typename KeyCompFunc = std::less<Key> >
119 typedef std::map<Key, Value, KeyCompFunc> StorageType;
143 typedef typename StorageType::value_type
Entry;
175 Map(
const KeyCompFunc& func):
183 template <
typename InputIter>
184 Map(
const InputIter& first,
const InputIter& last):
195 template <
typename InputIter>
196 Map(
const InputIter& first,
const InputIter& last,
const KeyCompFunc& func):
197 data(first, last, func)
339 const Value&
getValue(
const Key& key,
const Value& def_value)
const;
419 std::pair<EntryIterator, bool>
insertEntry(
const Key& key,
const Value& value);
491 template <
typename InputIter>
504 template <
typename InputIter>
505 void setEntries(
const InputIter& first,
const InputIter& last);
639 const Value& getDefaultValue()
const;
652 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
664 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
676 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
688 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
700 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
712 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
722 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
723 typename CDPL::Util::Map<Key, Value, AllowDefValues, KeyCompFunc>::StorageType&
729 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
730 const typename CDPL::Util::Map<Key, Value, AllowDefValues, KeyCompFunc>::StorageType&
736 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
742 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
748 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
754 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
760 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
763 return data.key_comp();
766 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
770 return data.find(key);
773 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
777 return data.find(key);
780 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
783 return (data.find(key) != data.end());
786 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
791 if (lb == data.end() || data.key_comp()(key, (*lb).first)) {
795 return (*data.insert(lb,
Entry(key, getDefaultValue()))).second;
801 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
806 if (it == data.end())
812 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
817 if (it == data.end()) {
821 return getDefaultValue();
827 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
829 const Value& def_value)
const
833 if (it == data.end())
839 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
842 return getValue(key);
845 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
848 return getValue(key);
851 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
857 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
860 return (data.erase(key) > 0);
863 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
867 data.erase(first, last);
870 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
871 std::pair<typename CDPL::Util::Map<Key, Value, AllowDefValues, KeyCompFunc>::EntryIterator,
bool>
874 return data.insert(item);
877 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
878 std::pair<typename CDPL::Util::Map<Key, Value, AllowDefValues, KeyCompFunc>::EntryIterator,
bool>
881 return data.insert(
Entry(key, value));
884 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
888 return data.insert(it, item);
891 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
896 return data.insert(it,
Entry(key, value));
899 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
905 if (lb == data.end() || data.key_comp()(item.first, (*lb).first))
906 return data.insert(lb, item);
908 (*lb).second = item.second;
912 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
916 return setEntry(
Entry(key, value));
919 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
920 template <
typename InputIter>
923 data.insert(first, last);
926 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
927 template <
typename InputIter>
930 for (InputIter it = first; it != last; ++it)
934 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
938 return data.lower_bound(key);
941 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
945 return data.lower_bound(key);
948 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
952 return data.upper_bound(key);
955 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
959 return data.upper_bound(key);
962 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
969 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
976 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
983 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
990 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
997 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1001 return data.begin();
1004 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1011 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1018 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1022 return data.rbegin();
1025 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1029 return data.rbegin();
1032 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1039 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1046 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1052 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1060 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1062 const Map<Key, Value, AllowDefValues, KeyCompFunc>& map2)
1064 return (map1.getData() == map2.getData());
1067 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1069 const Map<Key, Value, AllowDefValues, KeyCompFunc>& map2)
1071 return (map1.getData() != map2.getData());
1074 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1076 const Map<Key, Value, AllowDefValues, KeyCompFunc>& map2)
1078 return (map1.getData() <= map2.getData());
1081 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1083 const Map<Key, Value, AllowDefValues, KeyCompFunc>& map2)
1085 return (map1.getData() >= map2.getData());
1088 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1090 const Map<Key, Value, AllowDefValues, KeyCompFunc>& map2)
1092 return (map1.getData() < map2.getData());
1095 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1097 const Map<Key, Value, AllowDefValues, KeyCompFunc>& map2)
1099 return (map1.getData() > map2.getData());
Definition of exception classes.
Thrown to indicate that some requested data item could not be found.
Definition: Base/Exceptions.hpp:171
Thrown to indicate that some requested operation has failed (e.g. due to unfulfilled preconditions or...
Definition: Base/Exceptions.hpp:211
A unique sorted associative container that maps keys to values.
Definition: Map.hpp:117
EntryIterator setEntry(const Key &key, const Value &value)
Inserts a new entry or updates the value of an existing entry with the specified key and value.
Definition: Map.hpp:914
const Value & operator[](const Key &key) const
Returns a const reference to the value associated with the specified key.
Definition: Map.hpp:846
EntryIterator getLowerBound(const Key &key)
Returns a mutable iterator pointing to the first entry whose key is not less than key.
Definition: Map.hpp:936
std::pair< EntryIterator, bool > insertEntry(const Entry &item)
Tries to insert the key/value pair item into the map.
Definition: Map.hpp:872
std::pair< EntryIterator, bool > insertEntry(const Key &key, const Value &value)
Tries to insert a new entry with specified key and value into the map.
Definition: Map.hpp:879
KeyCompFunc getKeyCompareFunction() const
Returns the key compare function used by the map.
Definition: Map.hpp:761
virtual const char * getClassName() const
Returns the name of the (derived) Map class.
Definition: Map.hpp:1047
EntryIterator insertEntry(const EntryIterator &it, const Key &key, const Value &value)
Tries to insert a new entry with the specified key and value into the map and uses the iterator it as...
Definition: Map.hpp:893
EntryIterator insertEntry(const EntryIterator &it, const Entry &item)
Tries to insert the key/value pair item into the map and uses the iterator it as a hint for the inser...
Definition: Map.hpp:886
EntryIterator getEntriesBegin()
Returns a mutable iterator pointing to the beginning of the map.
Definition: Map.hpp:971
std::size_t getSize() const
Returns the size (number of entries) of the map.
Definition: Map.hpp:737
void removeEntry(const EntryIterator &it)
Removes the entry pointed to by the iterator it from the map.
Definition: Map.hpp:852
Value & getValue(const Key &key)
Returns a non-const reference to the value associated with the specified key.
Definition: Map.hpp:787
Map(const InputIter &first, const InputIter &last)
Creates and initializes the map with copies of the key value pairs in the range [first,...
Definition: Map.hpp:184
ConstEntryIterator getEntriesBegin() const
Returns a constant iterator pointing to the beginning of the map.
Definition: Map.hpp:964
StorageType & getData()
Returns a non-const reference to the underlying map storage.
Definition: Map.hpp:724
bool isEmpty() const
Tells whether the map is empty (getSize() == 0).
Definition: Map.hpp:743
EntryIterator getUpperBound(const Key &key)
Returns a mutable iterator pointing to the first entry whose key is greater than key.
Definition: Map.hpp:950
void insertEntries(const InputIter &first, const InputIter &last)
Tries to insert the key/value pairs in the range [first, last).
Definition: Map.hpp:921
Map(const InputIter &first, const InputIter &last, const KeyCompFunc &func)
Creates and initializes the map with copies of the key value pairs in the range [first,...
Definition: Map.hpp:196
StorageType::const_reverse_iterator ConstReverseEntryIterator
A constant iterator used to iterate backwards over the entries of the map.
Definition: Map.hpp:153
ReverseEntryIterator getEntriesReverseBegin()
Returns a mutable iterator pointing to the beginning of the reversed map.
Definition: Map.hpp:1027
ConstEntryIterator getLowerBound(const Key &key) const
Returns a constant iterator pointing to the first entry whose key is not less than key.
Definition: Map.hpp:943
StorageType::const_iterator ConstEntryIterator
A constant iterator used to iterate over the entries of the map.
Definition: Map.hpp:148
EntryIterator getEntriesEnd()
Returns a mutable iterator pointing to the end of the map.
Definition: Map.hpp:985
void swap(Map &map)
Swaps the contents with map.
Definition: Map.hpp:755
bool removeEntry(const Key &key)
Removes the entry specified by key from the map.
Definition: Map.hpp:858
ConstEntryIterator end() const
Returns a constant iterator pointing to the end of the map.
Definition: Map.hpp:1006
EntryIterator getEntry(const Key &key)
Returns a mutable iterator pointing to the entry specified by key.
Definition: Map.hpp:768
ReverseEntryIterator getEntriesReverseEnd()
Returns a mutable iterator pointing to the end of the reversed map.
Definition: Map.hpp:1041
const StorageType & getData() const
Returns a const reference to the underlying map storage.
Definition: Map.hpp:731
Value & getValue(const Key &key, Value &def_value)
Returns a non-const reference to the value associated with the specified key, or the value given by t...
Definition: Map.hpp:802
const Value & getValue(const Key &key) const
Returns a const reference to the value associated with the specified key.
Definition: Map.hpp:813
Map()
Creates an empty map.
Definition: Map.hpp:168
bool containsEntry(const Key &key) const
Tells whether the map contains an entry with the specified key.
Definition: Map.hpp:781
StorageType::reverse_iterator ReverseEntryIterator
A mutable iterator used to iterate backwards over the entries of the map.
Definition: Map.hpp:163
Map(const KeyCompFunc &func)
Creates an empty map and uses func as key compare function.
Definition: Map.hpp:175
EntryIterator end()
Returns a mutable iterator pointing to the end of the map.
Definition: Map.hpp:1013
void clear()
Erases all entries.
Definition: Map.hpp:749
ConstReverseEntryIterator getEntriesReverseBegin() const
Returns a constant iterator pointing to the beginning of the reversed map.
Definition: Map.hpp:1020
Value & operator[](const Key &key)
Returns a non-const reference to the value associated with the specified key.
Definition: Map.hpp:840
StorageType::iterator EntryIterator
A mutable iterator used to iterate over the entries of the map.
Definition: Map.hpp:158
ConstEntryIterator begin() const
Returns a constant iterator pointing to the beginning of the map.
Definition: Map.hpp:992
ConstEntryIterator getUpperBound(const Key &key) const
Returns a constant iterator pointing to the first entry whose key is greater than key.
Definition: Map.hpp:957
EntryIterator setEntry(const Entry &item)
Inserts a new entry or updates the value of an existing entry with the key and value given by item.
Definition: Map.hpp:901
const Value & getValue(const Key &key, const Value &def_value) const
Returns a const reference to the value associated with the specified key, or the value given by the s...
Definition: Map.hpp:828
ConstEntryIterator getEntriesEnd() const
Returns a constant iterator pointing to the end of the map.
Definition: Map.hpp:978
ConstReverseEntryIterator getEntriesReverseEnd() const
Returns a constant iterator pointing to the end of the reversed map.
Definition: Map.hpp:1034
std::shared_ptr< Map > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated Map instances.
Definition: Map.hpp:125
virtual ~Map()
Virtual destructor.
Definition: Map.hpp:203
Key KeyType
The type of the map's keys.
Definition: Map.hpp:130
void removeEntries(const EntryIterator &first, const EntryIterator &last)
Removes all entries pointed to by the iterators in the range [first, last) from the map.
Definition: Map.hpp:864
void setEntries(const InputIter &first, const InputIter &last)
Inserts new entries or updates the value of existing entries using the key/value pairs in the range [...
Definition: Map.hpp:928
Value ValueType
The type of the mapped values.
Definition: Map.hpp:135
EntryIterator begin()
Returns a mutable iterator pointing to the beginning of the map.
Definition: Map.hpp:999
StorageType::value_type Entry
The type of the key/value pairs stored in the map.
Definition: Map.hpp:143
ConstEntryIterator getEntry(const Key &key) const
Returns a constant iterator pointing to the entry specified by key.
Definition: Map.hpp:775
bool operator<(const Array< ValueType > &array1, const Array< ValueType > &array2)
Less than comparison operator.
bool operator>=(const Array< ValueType > &array1, const Array< ValueType > &array2)
Greater or equal comparison operator.
bool operator>(const Array< ValueType > &array1, const Array< ValueType > &array2)
Greater than comparison operator.
bool operator!=(const Array< ValueType > &array1, const Array< ValueType > &array2)
Inequality comparison operator.
bool operator<=(const Array< ValueType > &array1, const Array< ValueType > &array2)
Less or equal comparison operator.
bool operator==(const Array< ValueType > &array1, const Array< ValueType > &array2)
Equality comparison operator.
The namespace of the Chemical Data Processing Library.
static const ValueType & get()
Always throws, since default values are not supported for this map configuration.
Definition: Map.hpp:88
Helper supplying the default value returned by Util::Map when a queried key is absent.
Definition: Map.hpp:55
static const ValueType defValue
The default-constructed value returned for an absent key.
Definition: Map.hpp:69
static const ValueType & get()
Returns a const reference to the shared default-constructed value.
Definition: Map.hpp:61