29 #ifndef CDPL_UTIL_MAP_HPP
30 #define CDPL_UTIL_MAP_HPP
48 template <
typename ValueType,
bool Allow = true>
52 static const ValueType&
get()
60 template <
typename ValueType,
bool Allow>
63 template <
typename ValueType>
67 static const ValueType&
get()
93 template <
typename Key,
typename Value,
bool AllowDefValues =
false,
94 typename KeyCompFunc = std::less<Key> >
98 typedef std::map<Key, Value, KeyCompFunc> StorageType;
122 typedef typename StorageType::value_type
Entry;
154 Map(
const KeyCompFunc& func):
162 template <
typename InputIter>
163 Map(
const InputIter& first,
const InputIter& last):
174 template <
typename InputIter>
175 Map(
const InputIter& first,
const InputIter& last,
const KeyCompFunc& func):
176 data(first, last, func)
318 const Value&
getValue(
const Key& key,
const Value& def_value)
const;
398 std::pair<EntryIterator, bool>
insertEntry(
const Key& key,
const Value& value);
470 template <
typename InputIter>
483 template <
typename InputIter>
484 void setEntries(
const InputIter& first,
const InputIter& last);
618 const Value& getDefaultValue()
const;
631 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
643 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
655 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
667 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
679 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
691 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
701 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
702 typename CDPL::Util::Map<Key, Value, AllowDefValues, KeyCompFunc>::StorageType&
708 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
709 const typename CDPL::Util::Map<Key, Value, AllowDefValues, KeyCompFunc>::StorageType&
715 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
721 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
727 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
733 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
739 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
742 return data.key_comp();
745 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
749 return data.find(key);
752 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
756 return data.find(key);
759 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
762 return (data.find(key) != data.end());
765 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
770 if (lb == data.end() || data.key_comp()(key, (*lb).first)) {
774 return (*data.insert(lb,
Entry(key, getDefaultValue()))).second;
780 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
785 if (it == data.end())
791 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
796 if (it == data.end()) {
800 return getDefaultValue();
806 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
808 const Value& def_value)
const
812 if (it == data.end())
818 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
821 return getValue(key);
824 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
827 return getValue(key);
830 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
836 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
839 return (data.erase(key) > 0);
842 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
846 data.erase(first, last);
849 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
850 std::pair<typename CDPL::Util::Map<Key, Value, AllowDefValues, KeyCompFunc>::EntryIterator,
bool>
853 return data.insert(item);
856 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
857 std::pair<typename CDPL::Util::Map<Key, Value, AllowDefValues, KeyCompFunc>::EntryIterator,
bool>
860 return data.insert(
Entry(key, value));
863 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
867 return data.insert(it, item);
870 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
875 return data.insert(it,
Entry(key, value));
878 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
884 if (lb == data.end() || data.key_comp()(item.first, (*lb).first))
885 return data.insert(lb, item);
887 (*lb).second = item.second;
891 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
895 return setEntry(
Entry(key, value));
898 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
899 template <
typename InputIter>
902 data.insert(first, last);
905 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
906 template <
typename InputIter>
909 for (InputIter it = first; it != last; ++it)
913 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
917 return data.lower_bound(key);
920 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
924 return data.lower_bound(key);
927 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
931 return data.upper_bound(key);
934 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
938 return data.upper_bound(key);
941 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
948 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
955 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
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.rbegin();
1004 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1008 return data.rbegin();
1011 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1018 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1025 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1031 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1039 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1041 const Map<Key, Value, AllowDefValues, KeyCompFunc>& map2)
1043 return (map1.getData() == map2.getData());
1046 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1048 const Map<Key, Value, AllowDefValues, KeyCompFunc>& map2)
1050 return (map1.getData() != map2.getData());
1053 template <
typename Key,
typename Value,
bool AllowDefValues,
typename KeyCompFunc>
1055 const Map<Key, Value, AllowDefValues, KeyCompFunc>& map2)
1057 return (map1.getData() <= map2.getData());
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());
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:96
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:893
const Value & operator[](const Key &key) const
Returns a const reference to the value associated with the specified key.
Definition: Map.hpp:825
EntryIterator getLowerBound(const Key &key)
Returns a mutable iterator pointing to the first entry whose key is not less than key.
Definition: Map.hpp:915
std::pair< EntryIterator, bool > insertEntry(const Entry &item)
Tries to insert the key/value pair item into the map.
Definition: Map.hpp:851
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:858
KeyCompFunc getKeyCompareFunction() const
Returns the key compare function used by the map.
Definition: Map.hpp:740
virtual const char * getClassName() const
Returns the name of the (derived) Map class.
Definition: Map.hpp:1026
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:872
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:865
EntryIterator getEntriesBegin()
Returns a mutable iterator pointing to the beginning of the map.
Definition: Map.hpp:950
std::size_t getSize() const
Returns the size (number of entries) of the map.
Definition: Map.hpp:716
void removeEntry(const EntryIterator &it)
Removes the entry pointed to by the iterator it from the map.
Definition: Map.hpp:831
Value & getValue(const Key &key)
Returns a non-const reference to the value associated with the specified key.
Definition: Map.hpp:766
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:163
ConstEntryIterator getEntriesBegin() const
Returns a constant iterator pointing to the beginning of the map.
Definition: Map.hpp:943
StorageType & getData()
Returns a non-const reference to the underlying map storage.
Definition: Map.hpp:703
bool isEmpty() const
Tells whether the map is empty (getSize() == 0).
Definition: Map.hpp:722
EntryIterator getUpperBound(const Key &key)
Returns a mutable iterator pointing to the first entry whose key is greater than key.
Definition: Map.hpp:929
void insertEntries(const InputIter &first, const InputIter &last)
Tries to insert the key/value pairs in the range [first, last).
Definition: Map.hpp:900
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:175
StorageType::const_reverse_iterator ConstReverseEntryIterator
A constant iterator used to iterate backwards over the entries of the map.
Definition: Map.hpp:132
ReverseEntryIterator getEntriesReverseBegin()
Returns a mutable iterator pointing to the beginning of the reversed map.
Definition: Map.hpp:1006
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:922
StorageType::const_iterator ConstEntryIterator
A constant iterator used to iterate over the entries of the map.
Definition: Map.hpp:127
EntryIterator getEntriesEnd()
Returns a mutable iterator pointing to the end of the map.
Definition: Map.hpp:964
void swap(Map &map)
Swaps the contents with map.
Definition: Map.hpp:734
bool removeEntry(const Key &key)
Removes the entry specified by key from the map.
Definition: Map.hpp:837
ConstEntryIterator end() const
Returns a constant iterator pointing to the end of the map.
Definition: Map.hpp:985
EntryIterator getEntry(const Key &key)
Returns a mutable iterator pointing to the entry specified by key.
Definition: Map.hpp:747
ReverseEntryIterator getEntriesReverseEnd()
Returns a mutable iterator pointing to the end of the reversed map.
Definition: Map.hpp:1020
const StorageType & getData() const
Returns a const reference to the underlying map storage.
Definition: Map.hpp:710
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:781
const Value & getValue(const Key &key) const
Returns a const reference to the value associated with the specified key.
Definition: Map.hpp:792
Map()
Creates an empty map.
Definition: Map.hpp:147
bool containsEntry(const Key &key) const
Tells whether the map contains an entry with the specified key.
Definition: Map.hpp:760
StorageType::reverse_iterator ReverseEntryIterator
A mutable iterator used to iterate backwards over the entries of the map.
Definition: Map.hpp:142
Map(const KeyCompFunc &func)
Creates an empty map and uses func as key compare function.
Definition: Map.hpp:154
EntryIterator end()
Returns a mutable iterator pointing to the end of the map.
Definition: Map.hpp:992
void clear()
Erases all entries.
Definition: Map.hpp:728
ConstReverseEntryIterator getEntriesReverseBegin() const
Returns a constant iterator pointing to the beginning of the reversed map.
Definition: Map.hpp:999
Value & operator[](const Key &key)
Returns a non-const reference to the value associated with the specified key.
Definition: Map.hpp:819
StorageType::iterator EntryIterator
A mutable iterator used to iterate over the entries of the map.
Definition: Map.hpp:137
ConstEntryIterator begin() const
Returns a constant iterator pointing to the beginning of the map.
Definition: Map.hpp:971
ConstEntryIterator getUpperBound(const Key &key) const
Returns a constant iterator pointing to the first entry whose key is greater than key.
Definition: Map.hpp:936
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:880
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:807
ConstEntryIterator getEntriesEnd() const
Returns a constant iterator pointing to the end of the map.
Definition: Map.hpp:957
ConstReverseEntryIterator getEntriesReverseEnd() const
Returns a constant iterator pointing to the end of the reversed map.
Definition: Map.hpp:1013
std::shared_ptr< Map > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated Map instances.
Definition: Map.hpp:104
virtual ~Map()
Virtual destructor.
Definition: Map.hpp:182
Key KeyType
The type of the map's keys.
Definition: Map.hpp:109
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:843
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:907
Value ValueType
The type of the mapped values.
Definition: Map.hpp:114
EntryIterator begin()
Returns a mutable iterator pointing to the beginning of the map.
Definition: Map.hpp:978
StorageType::value_type Entry
The type of the key/value pairs stored in the map.
Definition: Map.hpp:122
ConstEntryIterator getEntry(const Key &key) const
Returns a constant iterator pointing to the entry specified by key.
Definition: Map.hpp:754
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()
Definition: Map.hpp:67
static const ValueType defValue
Definition: Map.hpp:57
static const ValueType & get()
Definition: Map.hpp:52