00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef OR_TOOLS_BASE_MAP_UTIL_H_
00015 #define OR_TOOLS_BASE_MAP_UTIL_H_
00016
00017 #include <utility>
00018
00019 namespace operations_research {
00020
00021
00022
00023
00024 template <class Collection>
00025 const typename Collection::value_type::second_type&
00026 FindWithDefault(const Collection& collection,
00027 const typename Collection::value_type::first_type& key,
00028 const typename Collection::value_type::second_type& value) {
00029 typename Collection::const_iterator it = collection.find(key);
00030 if (it == collection.end()) {
00031 return value;
00032 }
00033 return it->second;
00034 }
00035
00036
00037
00038
00039 template <class Collection>
00040 const typename Collection::value_type::second_type*
00041 FindOrNull(const Collection& collection,
00042 const typename Collection::value_type::first_type& key) {
00043 typename Collection::const_iterator it = collection.find(key);
00044 if (it == collection.end()) {
00045 return 0;
00046 }
00047 return &it->second;
00048 }
00049
00050
00051
00052
00053 template <class Collection>
00054 typename Collection::value_type::second_type*
00055 FindOrNull(Collection& collection,
00056 const typename Collection::value_type::first_type& key) {
00057 typename Collection::iterator it = collection.find(key);
00058 if (it == collection.end()) {
00059 return 0;
00060 }
00061 return &it->second;
00062 }
00063
00064
00065
00066
00067
00068
00069 template <class Collection>
00070 const typename Collection::value_type::second_type
00071 FindPtrOrNull(const Collection& collection,
00072 const typename Collection::value_type::first_type& key) {
00073 typename Collection::const_iterator it = collection.find(key);
00074 if (it == collection.end()) {
00075 return 0;
00076 }
00077 return it->second;
00078 }
00079
00080
00081
00082
00083
00084 template <class Collection, class Key, class Value>
00085 bool InsertOrUpdate(Collection * const collection,
00086 const Key& key, const Value& value) {
00087 std::pair<typename Collection::iterator, bool> ret =
00088 collection->insert(typename Collection::value_type(key, value));
00089 if (!ret.second) {
00090
00091 ret.first->second = value;
00092 return false;
00093 }
00094 return true;
00095 }
00096
00097
00098
00099
00100
00101 template <class Collection, class Key, class Value>
00102 bool InsertIfNotPresent(Collection * const collection,
00103 const Key& key, const Value& value) {
00104 std::pair<typename Collection::iterator, bool> ret =
00105 collection->insert(typename Collection::value_type(key, value));
00106 return ret.second;
00107 }
00108
00109
00110
00111
00112 template<class Collection>
00113 void InsertOrDie(Collection* const collection,
00114 const typename Collection::value_type& value) {
00115 CHECK(collection->insert(value).second) << "duplicate value: " << value;
00116 }
00117
00118
00119
00120 template<class Collection>
00121 void InsertOrDie(Collection* const collection,
00122 const typename Collection::value_type::first_type& key,
00123 const typename Collection::value_type::second_type& data) {
00124 typedef typename Collection::value_type value_type;
00125 CHECK(collection->insert(value_type(key, data)).second)
00126 << "duplicate key: " << key;
00127 }
00128
00129
00130
00131
00132 template <class Collection, class Key, class Value>
00133 bool FindCopy(const Collection& collection,
00134 const Key& key,
00135 Value* const value) {
00136 typename Collection::const_iterator it = collection.find(key);
00137 if (it == collection.end()) {
00138 return false;
00139 }
00140 if (value) {
00141 *value = it->second;
00142 }
00143 return true;
00144 }
00145
00146
00147
00148 template <class Collection, class Key>
00149 bool ContainsKey(const Collection& collection, const Key& key) {
00150 typename Collection::const_iterator it = collection.find(key);
00151 return it != collection.end();
00152 }
00153
00154 template <class Collection>
00155 const typename Collection::value_type::second_type&
00156 FindOrDie(const Collection& collection,
00157 const typename Collection::value_type::first_type& key) {
00158 typename Collection::const_iterator it = collection.find(key);
00159 CHECK(it != collection.end()) << "Map key not found: " << key;
00160 return it->second;
00161 }
00162 }
00163
00164 #endif // OR_TOOLS_BASE_MAP_UTIL_H_