Google OR-Tools: ortools/base/map_util.h Source File

1

2

3

4

5

6

7

8

9

10

11

12

13

14#ifndef ORTOOLS_BASE_MAP_UTIL_H_

15#define ORTOOLS_BASE_MAP_UTIL_H_

16

17#include <utility>

18

20

21namespace gtl {

22template <typename M>

24template <typename M>

26template <typename M>

28

29

30

31

32

33

34

35template <typename Collection, typename KeyType = MapUtilKeyT<Collection>>

37 const Collection& collection, const KeyType& key,

39 typename Collection::const_iterator it = collection.find(key);

40 if (it == collection.end()) {

41 return value;

42 }

43 return it->second;

44}

45

46

47

48

49template <class Collection, typename KeyType = MapUtilKeyT<Collection>>

51 const KeyType& key) {

54 typename Collection::const_iterator it = collection.find(key);

55 if (it == collection.end()) {

56 return *default_value;

57 }

58 return it->second;

59}

60

61

62

63

64template <class Collection>

65const typename Collection::value_type::second_type* FindOrNull(

66 const Collection& collection,

67 const typename Collection::value_type::first_type& key) {

68 typename Collection::const_iterator it = collection.find(key);

69 if (it == collection.end()) {

70 return 0;

71 }

72 return &it->second;

73}

74

75

76

77

78template <class Collection>

79typename Collection::value_type::second_type* FindOrNull(

80 Collection& collection,

81 const typename Collection::value_type::first_type& key) {

82 typename Collection::iterator it = collection.find(key);

83 if (it == collection.end()) {

84 return 0;

85 }

86 return &it->second;

87}

88

89

90

91

92

93template <class Collection>

94const typename Collection::value_type::second_type FindPtrOrNull(

95 const Collection& collection,

96 const typename Collection::value_type::first_type& key) {

97 typename Collection::const_iterator it = collection.find(key);

98 if (it == collection.end()) {

99 return 0;

100 }

101 return it->second;

102}

103

104

105

106

107

108

109template <class Collection, class Key, class Value>

111 const Value& value) {

112 std::pair<typename Collection::iterator, bool> ret =

113 collection->insert(typename Collection::value_type(key, value));

114 if (!ret.second) {

115

116 ret.first->second = value;

117 return false;

118 }

119 return true;

120}

121

122

123

124

125

126template <class Collection>

128 const typename Collection::value_type& value) {

129 std::pair<typename Collection::iterator, bool> ret =

130 collection->insert(value);

131 return ret.second;

132}

133

134

135

136

137

138template <class Collection, class Key, class Value>

140 const Value& value) {

141 std::pair<typename Collection::iterator, bool> ret =

142 collection->insert(typename Collection::value_type(key, value));

143 return ret.second;

144}

145

146

147

148

149template <class Collection>

151 const typename Collection::value_type& value) {

152 CHECK(collection->insert(value).second);

153}

154

155

156

157

158template <class Collection>

160 const typename Collection::value_type& value) {

161 CHECK(collection->insert(value).second) << "duplicate value: " << value;

162}

163

164

165

166template <class Collection>

168 const typename Collection::value_type::first_type& key,

169 const typename Collection::value_type::second_type& data) {

170 typedef typename Collection::value_type value_type;

171 CHECK(collection->insert(value_type(key, data)).second)

172 << "duplicate key: " << key;

173}

174

175

176

177template <typename Collection>

179 const typename Collection::value_type::first_type& key) {

180 auto [it, did_insert] = collection->insert(typename Collection::value_type(

181 key, typename Collection::value_type::second_type()));

182 CHECK(did_insert) << "duplicate key " << key;

183 return it->second;

184}

185

186

187

188

189template <class Collection, class Key, class Value>

190bool FindCopy(const Collection& collection, const Key& key,

191 Value* const value) {

192 typename Collection::const_iterator it = collection.find(key);

193 if (it == collection.end()) {

194 return false;

195 }

196 if (value) {

197 *value = it->second;

198 }

199 return true;

200}

201

202

203

204template <class Collection, class Key>

205bool ContainsKey(const Collection& collection, const Key& key) {

206 typename Collection::const_iterator it = collection.find(key);

207 return it != collection.end();

208}

209

210template <class Collection>

211const typename Collection::value_type::second_type& FindOrDie(

212 const Collection& collection,

213 const typename Collection::value_type::first_type& key) {

214 typename Collection::const_iterator it = collection.find(key);

215 CHECK(it != collection.end()) << "Map key not found: " << key;

216 return it->second;

217}

218

219

220template <class Collection>

222 const Collection& collection,

223 const typename Collection::value_type::first_type& key) {

224 typename Collection::const_iterator it = collection.find(key);

225 CHECK(it != collection.end()) << "Map key not found";

226 return it->second;

227}

228

229

230template <class Collection>

232 Collection& collection,

233 const typename Collection::value_type::first_type& key) {

234 typename Collection::iterator it = collection.find(key);

235 CHECK(it != collection.end()) << "Map key not found";

236 return it->second;

237}

238

239

240

241template <class Collection>

243 Collection* const collection,

244 const typename Collection::value_type::first_type& key,

245 const typename Collection::value_type::second_type& value) {

246 std::pair<typename Collection::iterator, bool> ret =

247 collection->insert(typename Collection::value_type(key, value));

248 return ret.first->second;

249}

250}

251#endif

typename MapUtilValueT< M >::second_type MapUtilMappedT

Definition map_util.h:27

const Collection::value_type::second_type FindPtrOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)

Definition map_util.h:94

bool InsertOrUpdate(Collection *const collection, const Key &key, const Value &value)

Definition map_util.h:110

void InsertOrDieNoPrint(Collection *const collection, const typename Collection::value_type &value)

Definition map_util.h:150

typename MapUtilValueT< M >::first_type MapUtilKeyT

Definition map_util.h:25

bool InsertIfNotPresent(Collection *const collection, const typename Collection::value_type &value)

Definition map_util.h:127

Collection::value_type::second_type & LookupOrInsert(Collection *const collection, const typename Collection::value_type::first_type &key, const typename Collection::value_type::second_type &value)

Definition map_util.h:242

typename M::value_type MapUtilValueT

Definition map_util.h:23

void InsertOrDie(Collection *const collection, const typename Collection::value_type &value)

Definition map_util.h:159

bool FindCopy(const Collection &collection, const Key &key, Value *const value)

Definition map_util.h:190

const Collection::value_type::second_type & FindOrDie(const Collection &collection, const typename Collection::value_type::first_type &key)

Definition map_util.h:211

const Collection::value_type::second_type & FindOrDieNoPrint(const Collection &collection, const typename Collection::value_type::first_type &key)

Definition map_util.h:221

const Collection::value_type::second_type * FindOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)

Definition map_util.h:65

bool ContainsKey(const Collection &collection, const Key &key)

Definition map_util.h:205

auto & InsertKeyOrDie(Collection *const collection, const typename Collection::value_type::first_type &key)

Definition map_util.h:178

const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)

Definition map_util.h:36